feat(config): Expose binary in config file

This commit is contained in:
Ed Page 2019-08-07 17:14:29 -05:00
parent 77603daab5
commit 29ff040fd1
2 changed files with 44 additions and 27 deletions

View file

@ -5,6 +5,11 @@ pub trait ConfigSource {
} }
pub trait WalkSource { pub trait WalkSource {
/// Search binary files.
fn binary(&self) -> Option<bool> {
None
}
/// Skip hidden files and directories. /// Skip hidden files and directories.
fn ignore_hidden(&self) -> Option<bool> { fn ignore_hidden(&self) -> Option<bool> {
None None
@ -81,6 +86,7 @@ impl ConfigSource for Config {
#[serde(deny_unknown_fields, default)] #[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct Walk { pub struct Walk {
pub binary: Option<bool>,
pub ignore_hidden: Option<bool>, pub ignore_hidden: Option<bool>,
pub ignore_files: Option<bool>, pub ignore_files: Option<bool>,
pub ignore_dot: Option<bool>, pub ignore_dot: Option<bool>,
@ -91,6 +97,9 @@ pub struct Walk {
impl Walk { impl Walk {
pub fn update(&mut self, source: &dyn WalkSource) { 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() { if let Some(source) = source.ignore_hidden() {
self.ignore_hidden = Some(source); 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 { pub fn ignore_hidden(&self) -> bool {
self.ignore_hidden.unwrap_or(true) self.ignore_hidden.unwrap_or(true)
} }
@ -147,6 +160,10 @@ impl Walk {
} }
impl WalkSource for Walk { impl WalkSource for Walk {
fn binary(&self) -> Option<bool> {
self.binary
}
fn ignore_hidden(&self) -> Option<bool> { fn ignore_hidden(&self) -> Option<bool> {
self.ignore_hidden self.ignore_hidden
} }

View file

@ -83,12 +83,6 @@ struct Args {
)] )]
pub format: Format, 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)] #[structopt(flatten)]
config: ConfigArgs, config: ConfigArgs,
@ -127,15 +121,6 @@ impl Args {
(_, _) => unreachable!("StructOpt should make this impossible"), (_, _) => unreachable!("StructOpt should make this impossible"),
} }
} }
pub fn binary(&self) -> Option<bool> {
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)] #[derive(Debug, StructOpt)]
@ -154,6 +139,12 @@ impl config::ConfigSource for ConfigArgs {
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt(rename_all = "kebab-case")] #[structopt(rename_all = "kebab-case")]
struct WalkArgs { 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""#))] #[structopt(long, raw(overrides_with = r#""no-hidden""#))]
/// Search hidden files and directories. /// Search hidden files and directories.
hidden: bool, hidden: bool,
@ -200,6 +191,15 @@ struct WalkArgs {
} }
impl config::WalkSource for WalkArgs { impl config::WalkSource for WalkArgs {
fn binary(&self) -> Option<bool> {
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<bool> { fn ignore_hidden(&self) -> Option<bool> {
match (self.hidden, self.no_hidden) { match (self.hidden, self.no_hidden) {
(true, false) => Some(false), (true, false) => Some(false),
@ -282,18 +282,6 @@ fn run() -> Result<i32, failure::Error> {
let mut builder = get_logging(args.verbose.log_level()); let mut builder = get_logging(args.verbose.log_level());
builder.init(); 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(); let mut config = config::Config::default();
if let Some(path) = args.custom_config.as_ref() { if let Some(path) = args.custom_config.as_ref() {
let custom = config::Config::from_file(path)?; let custom = config::Config::from_file(path)?;
@ -318,6 +306,18 @@ fn run() -> Result<i32, failure::Error> {
config.update(&args.config); config.update(&args.config);
let config = 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); let mut walk = ignore::WalkBuilder::new(path);
walk.hidden(config.files.ignore_hidden()) walk.hidden(config.files.ignore_hidden())
.ignore(config.files.ignore_dot()) .ignore(config.files.ignore_dot())