Skip to content

Commit

Permalink
Enable excluding rules
Browse files Browse the repository at this point in the history
Closes #53
  • Loading branch information
Notgnoshi committed Oct 28, 2024
1 parent fa67bc9 commit ba91394
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ focus on the user impact** rather than the actual changes made.
length_threshold = 72
```

* You can now configure a list of rules to exclude from checking. You can do this like so:

```toml
[rules]
exclude = ["H4-non-unicode"]
```

## Changed
## Deprecated
## Removed
Expand Down
4 changes: 4 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub struct Config {

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct RulesConfig {
/// Rules to exclude. Maybe the rule ID (2), human ID (shortest-subject-line), or pretty ID
/// (H2-shortest-subject-line).
pub exclude: Option<Vec<String>>,

// TODO: There's bound to be some kind of serde voodoo to reduce the copy-pasta and effort it
// takes to add a configuration for a new rule. Or maybe this is better because it's simple?
pub h2_shortest_subject_line: Option<H002Config>,
Expand Down
13 changes: 13 additions & 0 deletions src/config/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ fn generates_default_config_if_missing() {
assert_eq!(config, default_config);
}

#[test]
fn config_exclude_rules() {
let config_toml = "[repositories.herostratus]\n\
path = \"git/Notgnoshi/herostratus\"\n\
url = \"[email protected]:Notgnoshi/herostratus.git\"\n\
[rules]\n\
exclude = [\"H4-non-unicode\"]\n\
";

let config = deserialize_config(config_toml).unwrap();
assert_eq!(config.rules.unwrap().exclude.unwrap(), ["H4-non-unicode"]);
}

#[test]
fn rule_specific_config() {
let config_toml = "[repositories.herostratus]\n\
Expand Down
50 changes: 46 additions & 4 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,31 @@ pub fn builtin_rules(config: Option<&Config>) -> Vec<Box<dyn Rule>> {
}) => r,
_ => &default_rules_config,
};
let excludes = rules_config.exclude.clone().unwrap_or_default();

let mut rules = Vec::new();
// Each Rule uses inventory::submit! to register a factory to build themselves with.
inventory::iter::<RuleFactory>
.into_iter()
.map(|factory| factory.build(rules_config))
.collect()
'outer: for factory in inventory::iter::<RuleFactory>.into_iter() {
let rule = factory.build(rules_config);
let string_id = rule.id().to_string();
let short_pretty_id = format!("H{}", rule.id());
let pretty_id = rule.pretty_id(); // each call does an allocation

for exclude in &excludes {
if &string_id == exclude
|| &short_pretty_id == exclude
|| rule.human_id() == exclude
|| &pretty_id == exclude
{
tracing::info!("Excluding rule: {pretty_id}");
continue 'outer;
}
}

rules.push(rule);
}

rules
}

pub fn builtin_rules_all() -> Vec<Box<dyn Rule>> {
Expand Down Expand Up @@ -106,4 +125,27 @@ mod tests {
assert_ne!(rule.description().to_lowercase(), "todo");
}
}

#[test]
fn exclude_rules() {
let config = RulesConfig {
exclude: Some(vec![
"1".to_string(),
"H2".to_string(),
"longest-subject-line".to_string(),
"H4-non-unicode".to_string(),
]),
..Default::default()
};
let config = Config {
rules: Some(config),
..Default::default()
};
let rules = builtin_rules(Some(&config));
let ids: Vec<_> = rules.iter().map(|r| r.id()).collect();
assert!(!ids.contains(&1));
assert!(!ids.contains(&2));
assert!(!ids.contains(&3));
assert!(!ids.contains(&4));
}
}

0 comments on commit ba91394

Please sign in to comment.