diff --git a/src/config.rs b/src/config.rs index d93977c..431500a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,11 @@ pub trait ConfigSource { } pub trait WalkSource { + /// Search binary files. + fn binary(&self) -> Option { + None + } + /// Skip hidden files and directories. fn ignore_hidden(&self) -> Option { None @@ -81,6 +86,7 @@ impl ConfigSource for Config { #[serde(deny_unknown_fields, default)] #[serde(rename_all = "kebab-case")] pub struct Walk { + pub binary: Option, pub ignore_hidden: Option, pub ignore_files: Option, pub ignore_dot: Option, @@ -91,6 +97,9 @@ pub struct Walk { impl Walk { pub fn update(&mut self, source: &dyn WalkSource) { + if let Some(source) = source.binary() { + self.binary = Some(source); + } if let Some(source) = source.ignore_hidden() { self.ignore_hidden = Some(source); } @@ -116,6 +125,10 @@ impl Walk { } } + pub fn binary(&self) -> bool { + self.binary.unwrap_or(false) + } + pub fn ignore_hidden(&self) -> bool { self.ignore_hidden.unwrap_or(true) } @@ -147,6 +160,10 @@ impl Walk { } impl WalkSource for Walk { + fn binary(&self) -> Option { + self.binary + } + fn ignore_hidden(&self) -> Option { self.ignore_hidden } diff --git a/src/main.rs b/src/main.rs index 684ceb2..43f3218 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,12 +83,6 @@ struct Args { )] pub format: Format, - #[structopt(long, raw(overrides_with = r#""no-binary""#))] - /// Search binary files. - binary: bool, - #[structopt(long, raw(overrides_with = r#""binary""#), raw(hidden = "true"))] - no_binary: bool, - #[structopt(flatten)] config: ConfigArgs, @@ -127,15 +121,6 @@ impl Args { (_, _) => unreachable!("StructOpt should make this impossible"), } } - - pub fn binary(&self) -> Option { - match (self.binary, self.no_binary) { - (true, false) => Some(true), - (false, true) => Some(false), - (false, false) => None, - (_, _) => unreachable!("StructOpt should make this impossible"), - } - } } #[derive(Debug, StructOpt)] @@ -154,6 +139,12 @@ impl config::ConfigSource for ConfigArgs { #[derive(Debug, StructOpt)] #[structopt(rename_all = "kebab-case")] struct WalkArgs { + #[structopt(long, raw(overrides_with = r#""no-binary""#))] + /// Search binary files. + binary: bool, + #[structopt(long, raw(overrides_with = r#""binary""#), raw(hidden = "true"))] + no_binary: bool, + #[structopt(long, raw(overrides_with = r#""no-hidden""#))] /// Search hidden files and directories. hidden: bool, @@ -200,6 +191,15 @@ struct WalkArgs { } impl config::WalkSource for WalkArgs { + fn binary(&self) -> Option { + match (self.binary, self.no_binary) { + (true, false) => Some(true), + (false, true) => Some(false), + (false, false) => None, + (_, _) => unreachable!("StructOpt should make this impossible"), + } + } + fn ignore_hidden(&self) -> Option { match (self.hidden, self.no_hidden) { (true, false) => Some(false), @@ -282,18 +282,6 @@ fn run() -> Result { let mut builder = get_logging(args.verbose.log_level()); builder.init(); - let dictionary = typos::BuiltIn::new(); - - let parser = typos::tokens::ParserBuilder::new() - .ignore_hex(args.ignore_hex().unwrap_or(true)) - .build(); - - let checks = typos::checks::CheckSettings::new() - .check_filenames(args.check_filenames().unwrap_or(true)) - .check_files(args.check_files().unwrap_or(true)) - .binary(args.binary().unwrap_or(false)) - .build(&dictionary, &parser); - let mut config = config::Config::default(); if let Some(path) = args.custom_config.as_ref() { let custom = config::Config::from_file(path)?; @@ -318,6 +306,18 @@ fn run() -> Result { config.update(&args.config); let config = config; + let dictionary = typos::BuiltIn::new(); + + let parser = typos::tokens::ParserBuilder::new() + .ignore_hex(args.ignore_hex().unwrap_or(true)) + .build(); + + let checks = typos::checks::CheckSettings::new() + .check_filenames(args.check_filenames().unwrap_or(true)) + .check_files(args.check_files().unwrap_or(true)) + .binary(config.files.binary()) + .build(&dictionary, &parser); + let mut walk = ignore::WalkBuilder::new(path); walk.hidden(config.files.ignore_hidden()) .ignore(config.files.ignore_dot())