Skip to content

Commit

Permalink
feat(config): Expose binary in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 8, 2019
1 parent 77603da commit 29ff040
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub trait ConfigSource {
}

pub trait WalkSource {
/// Search binary files.
fn binary(&self) -> Option<bool> {
None
}

/// Skip hidden files and directories.
fn ignore_hidden(&self) -> Option<bool> {
None
Expand Down Expand Up @@ -81,6 +86,7 @@ impl ConfigSource for Config {
#[serde(deny_unknown_fields, default)]
#[serde(rename_all = "kebab-case")]
pub struct Walk {
pub binary: Option<bool>,
pub ignore_hidden: Option<bool>,
pub ignore_files: Option<bool>,
pub ignore_dot: Option<bool>,
Expand All @@ -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);
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -147,6 +160,10 @@ impl Walk {
}

impl WalkSource for Walk {
fn binary(&self) -> Option<bool> {
self.binary
}

fn ignore_hidden(&self) -> Option<bool> {
self.ignore_hidden
}
Expand Down
54 changes: 27 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -127,15 +121,6 @@ impl Args {
(_, _) => 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)]
Expand All @@ -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,
Expand Down Expand Up @@ -200,6 +191,15 @@ struct 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> {
match (self.hidden, self.no_hidden) {
(true, false) => Some(false),
Expand Down Expand Up @@ -282,18 +282,6 @@ fn run() -> Result<i32, failure::Error> {
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)?;
Expand All @@ -318,6 +306,18 @@ fn run() -> Result<i32, failure::Error> {
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())
Expand Down

0 comments on commit 29ff040

Please sign in to comment.