|
| 1 | +use colored::Colorize; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use crate::config::load_config; |
| 5 | +use crate::types::{CustomRuleType, RuleSeverity}; |
| 6 | + |
| 7 | +pub fn cmd_rules(root: &Path) { |
| 8 | + let config = load_config(root); |
| 9 | + |
| 10 | + println!("{}", "Built-in rules:".bold()); |
| 11 | + println!(); |
| 12 | + print_builtin( |
| 13 | + "max_changelog_entries", |
| 14 | + "Warn if Change Log exceeds N entries", |
| 15 | + config.rules.max_changelog_entries.map(|n| n.to_string()), |
| 16 | + ); |
| 17 | + print_builtin( |
| 18 | + "require_behavioral_examples", |
| 19 | + "Require at least one ### Scenario", |
| 20 | + config |
| 21 | + .rules |
| 22 | + .require_behavioral_examples |
| 23 | + .map(|b| b.to_string()), |
| 24 | + ); |
| 25 | + print_builtin( |
| 26 | + "min_invariants", |
| 27 | + "Require at least N numbered invariants", |
| 28 | + config.rules.min_invariants.map(|n| n.to_string()), |
| 29 | + ); |
| 30 | + print_builtin( |
| 31 | + "max_spec_size_kb", |
| 32 | + "Warn if spec file exceeds N KB", |
| 33 | + config.rules.max_spec_size_kb.map(|n| n.to_string()), |
| 34 | + ); |
| 35 | + print_builtin( |
| 36 | + "require_depends_on", |
| 37 | + "Require non-empty depends_on", |
| 38 | + config.rules.require_depends_on.map(|b| b.to_string()), |
| 39 | + ); |
| 40 | + println!(); |
| 41 | + |
| 42 | + if config.custom_rules.is_empty() { |
| 43 | + println!("{}", "No custom rules defined.".dimmed()); |
| 44 | + println!( |
| 45 | + "{}", |
| 46 | + "Add \"customRules\" to specsync.json to define declarative rules.".dimmed() |
| 47 | + ); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + println!( |
| 52 | + "{} ({} rule{}):", |
| 53 | + "Custom rules".bold(), |
| 54 | + config.custom_rules.len(), |
| 55 | + if config.custom_rules.len() == 1 { |
| 56 | + "" |
| 57 | + } else { |
| 58 | + "s" |
| 59 | + } |
| 60 | + ); |
| 61 | + println!(); |
| 62 | + |
| 63 | + for rule in &config.custom_rules { |
| 64 | + let severity_str = match rule.severity { |
| 65 | + RuleSeverity::Error => "error".red().to_string(), |
| 66 | + RuleSeverity::Warning => "warning".yellow().to_string(), |
| 67 | + RuleSeverity::Info => "info".blue().to_string(), |
| 68 | + }; |
| 69 | + |
| 70 | + let type_str = match rule.rule_type { |
| 71 | + CustomRuleType::RequireSection => "require_section", |
| 72 | + CustomRuleType::MinWordCount => "min_word_count", |
| 73 | + CustomRuleType::RequirePattern => "require_pattern", |
| 74 | + CustomRuleType::ForbidPattern => "forbid_pattern", |
| 75 | + }; |
| 76 | + |
| 77 | + println!(" {} [{}] ({})", rule.name.bold(), severity_str, type_str); |
| 78 | + |
| 79 | + if let Some(ref section) = rule.section { |
| 80 | + println!(" section: {section}"); |
| 81 | + } |
| 82 | + if let Some(ref pattern) = rule.pattern { |
| 83 | + println!(" pattern: {pattern}"); |
| 84 | + } |
| 85 | + if let Some(min) = rule.min_words { |
| 86 | + println!(" min_words: {min}"); |
| 87 | + } |
| 88 | + if let Some(ref filter) = rule.applies_to { |
| 89 | + let mut parts = Vec::new(); |
| 90 | + if let Some(ref s) = filter.status { |
| 91 | + parts.push(format!("status={s}")); |
| 92 | + } |
| 93 | + if let Some(ref m) = filter.module { |
| 94 | + parts.push(format!("module=/{m}/")); |
| 95 | + } |
| 96 | + if !parts.is_empty() { |
| 97 | + println!(" applies_to: {}", parts.join(", ")); |
| 98 | + } |
| 99 | + } |
| 100 | + if let Some(ref msg) = rule.message { |
| 101 | + println!(" message: {msg}"); |
| 102 | + } |
| 103 | + println!(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +fn print_builtin(name: &str, description: &str, value: Option<String>) { |
| 108 | + let status = match &value { |
| 109 | + Some(v) => format!("{} = {v}", "active".green()), |
| 110 | + None => "off".dimmed().to_string(), |
| 111 | + }; |
| 112 | + println!(" {name:.<40} {status}"); |
| 113 | + println!(" {}", description.dimmed()); |
| 114 | +} |
0 commit comments