aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-11-28 07:51:54 +0000
committerAkshay <[email protected]>2021-11-28 07:51:54 +0000
commite1c0035cea51f6f2ceac2c920b0964d14ba54842 (patch)
treecd31973c32431a5cbf8c12ce574f799d065852be
parentf27372061a0effe3b00d400f4e577b9d9e0ad4c0 (diff)
add ability to disable checks with config fileconfig-file
-rw-r--r--bin/src/config.rs70
-rw-r--r--flake.nix2
2 files changed, 44 insertions, 28 deletions
diff --git a/bin/src/config.rs b/bin/src/config.rs
index 98b41da..b6310e6 100644
--- a/bin/src/config.rs
+++ b/bin/src/config.rs
@@ -51,8 +51,8 @@ pub struct Check {
51 pub format: OutFormat, 51 pub format: OutFormat,
52 52
53 /// Path to statix.toml 53 /// Path to statix.toml
54 #[clap(short = 'c', long = "config")] 54 #[clap(short = 'c', long = "config", default_value = ".")]
55 pub conf_path: Option<PathBuf>, 55 pub conf_path: PathBuf,
56 56
57 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout 57 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout
58 #[clap(short, long = "stdin")] 58 #[clap(short, long = "stdin")]
@@ -78,7 +78,7 @@ impl Check {
78 } 78 }
79 79
80 pub fn lints(&self) -> Result<LintMap, ConfigErr> { 80 pub fn lints(&self) -> Result<LintMap, ConfigErr> {
81 lints(self.conf_path.as_ref()) 81 lints(&self.conf_path)
82 } 82 }
83} 83}
84 84
@@ -101,8 +101,8 @@ pub struct Fix {
101 pub diff_only: bool, 101 pub diff_only: bool,
102 102
103 /// Path to statix.toml 103 /// Path to statix.toml
104 #[clap(short = 'c', long = "config")] 104 #[clap(short = 'c', long = "config", default_value = ".")]
105 pub conf_path: Option<PathBuf>, 105 pub conf_path: PathBuf,
106 106
107 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout 107 /// Enable "streaming" mode, accept file on stdin, output diagnostics on stdout
108 #[clap(short, long = "stdin")] 108 #[clap(short, long = "stdin")]
@@ -146,7 +146,7 @@ impl Fix {
146 } 146 }
147 147
148 pub fn lints(&self) -> Result<LintMap, ConfigErr> { 148 pub fn lints(&self) -> Result<LintMap, ConfigErr> {
149 lints(self.conf_path.as_ref()) 149 lints(&self.conf_path)
150 } 150 }
151} 151}
152 152
@@ -251,7 +251,14 @@ impl FromStr for OutFormat {
251 251
252#[derive(Serialize, Deserialize, Debug)] 252#[derive(Serialize, Deserialize, Debug)]
253pub struct ConfFile { 253pub struct ConfFile {
254 checks: Vec<String>, 254 disabled: Vec<String>,
255}
256
257impl Default for ConfFile {
258 fn default() -> Self {
259 let disabled = vec![];
260 Self { disabled }
261 }
255} 262}
256 263
257impl ConfFile { 264impl ConfFile {
@@ -268,18 +275,31 @@ impl ConfFile {
268 ConfigErr::ConfFileParse(msg) 275 ConfigErr::ConfFileParse(msg)
269 }) 276 })
270 } 277 }
278 pub fn discover<P: AsRef<Path>>(path: P) -> Result<Self, ConfigErr> {
279 let cannonical_path = fs::canonicalize(path.as_ref()).map_err(ConfigErr::InvalidPath)?;
280 for p in cannonical_path.ancestors() {
281 let statix_toml_path = p.with_file_name("statix.toml");
282 if statix_toml_path.exists() {
283 return Self::from_path(statix_toml_path);
284 };
285 }
286 Ok(Self::default())
287 }
288 pub fn dump(&self) -> String {
289 toml::ser::to_string_pretty(&self).unwrap()
290 }
271} 291}
272 292
273fn parse_line_col(src: &str) -> Result<(usize, usize), ConfigErr> { 293fn parse_line_col(src: &str) -> Result<(usize, usize), ConfigErr> {
274 let parts = src.split(','); 294 let parts = src.split(',');
275 match parts.collect::<Vec<_>>().as_slice() { 295 match parts.collect::<Vec<_>>().as_slice() {
276 [line, col] => { 296 [line, col] => {
277 let l = line 297 let do_parse = |val: &str| {
278 .parse::<usize>() 298 val.parse::<usize>()
279 .map_err(|_| ConfigErr::InvalidPosition(src.to_owned()))?; 299 .map_err(|_| ConfigErr::InvalidPosition(src.to_owned()))
280 let c = col 300 };
281 .parse::<usize>() 301 let l = do_parse(line)?;
282 .map_err(|_| ConfigErr::InvalidPosition(src.to_owned()))?; 302 let c = do_parse(col)?;
283 Ok((l, c)) 303 Ok((l, c))
284 } 304 }
285 _ => Err(ConfigErr::InvalidPosition(src.to_owned())), 305 _ => Err(ConfigErr::InvalidPosition(src.to_owned())),
@@ -314,18 +334,14 @@ fn vfs(files: Vec<PathBuf>) -> Result<ReadOnlyVfs, ConfigErr> {
314 Ok(vfs) 334 Ok(vfs)
315} 335}
316 336
317fn lints(conf_path: Option<&PathBuf>) -> Result<LintMap, ConfigErr> { 337fn lints(conf_path: &PathBuf) -> Result<LintMap, ConfigErr> {
318 if let Some(conf_path) = conf_path { 338 let config_file = ConfFile::discover(conf_path)?;
319 let config_file = ConfFile::from_path(conf_path)?; 339 Ok(utils::lint_map_of(
320 Ok(utils::lint_map_of( 340 (&*LINTS)
321 (&*LINTS) 341 .into_iter()
322 .into_iter() 342 .filter(|l| !config_file.disabled.iter().any(|check| check == l.name()))
323 .filter(|l| config_file.checks.iter().any(|check| check == l.name())) 343 .cloned()
324 .cloned() 344 .collect::<Vec<_>>()
325 .collect::<Vec<_>>() 345 .as_slice(),
326 .as_slice(), 346 ))
327 ))
328 } else {
329 Ok(utils::lint_map())
330 }
331} 347}
diff --git a/flake.nix b/flake.nix
index b166817..241eb2b 100644
--- a/flake.nix
+++ b/flake.nix
@@ -97,7 +97,7 @@
97 in 97 in
98 pkgs.mkShell { 98 pkgs.mkShell {
99 nativeBuildInputs = [ 99 nativeBuildInputs = [
100 pkgs.cargo-watch 100 pkgs.bacon
101 pkgs.cargo-insta 101 pkgs.cargo-insta
102 rust-analyzer 102 rust-analyzer
103 toolchain 103 toolchain