Skip to content

Commit d818d87

Browse files
committed
Support selecting rules
1 parent 887331e commit d818d87

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/cli.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ pub fn main() -> IoResult<()> {
2323
.long("output-format")
2424
.value_parser(["full", "json", "jsonl", "short"])
2525
.default_value("short"),
26-
),
26+
)
27+
.arg(Arg::new("select").long("select").value_delimiter(',')),
2728
)
2829
.subcommand(
2930
Command::new("rule")

src/cli/commands.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ pub fn check(matches: &ArgMatches) -> Result<()> {
2424
"full" => OutputFormat::Full,
2525
_ => unreachable!(),
2626
};
27-
let profile = Profile::default();
27+
let profile = match matches.get_many::<&str>("select") {
28+
Some(values) => {
29+
let codes: Vec<String> = values.into_iter().map(|s| s.to_uppercase()).collect();
30+
Profile::try_from(codes).expect("invalid code")
31+
}
32+
None => Profile::default(),
33+
};
2834
let (_, diagnostics) = parse_and_lint_file(&path, &profile).unwrap();
2935
if diagnostics.is_empty() {
3036
Ok(())

src/profile.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
use std::collections::HashSet;
1+
use std::collections::HashMap;
22

33
use crate::rule::Rule;
44

55
pub struct Profile {
6-
rules: HashSet<Rule>,
6+
pub rules: HashMap<String, Rule>,
77
}
88

99
impl Profile {
1010
pub fn new(rules: &[Rule]) -> Self {
11+
let mut rules_by_code = HashMap::new();
12+
for rule in rules {
13+
rules_by_code.insert(rule.code().to_string(), *rule);
14+
}
1115
Self {
12-
rules: rules.iter().copied().collect(),
16+
rules: rules_by_code,
1317
}
1418
}
1519

1620
pub fn is_enabled(&self, rule: Rule) -> bool {
17-
self.rules.contains(&rule)
21+
self.rules.contains_key(rule.code())
1822
}
1923
}
2024

@@ -23,3 +27,20 @@ impl Default for Profile {
2327
Self::new(&Rule::ALL)
2428
}
2529
}
30+
31+
impl TryFrom<Vec<String>> for Profile {
32+
type Error = String;
33+
34+
fn try_from(codes: Vec<String>) -> Result<Self, Self::Error> {
35+
let default = Profile::default();
36+
let mut rules = Vec::new();
37+
for code in codes.iter() {
38+
if let Some(rule) = default.rules.get(code) {
39+
rules.push(*rule)
40+
} else {
41+
return Err(format!("Invalid rule code '{}'", &code));
42+
}
43+
}
44+
Ok(Self::new(rules.as_slice()))
45+
}
46+
}

0 commit comments

Comments
 (0)