From 1c3acd747a32533ec7dec15c5de9d1ae285ca7c7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 5 Jan 2021 10:35:43 -0600 Subject: [PATCH] fix(config)!: Move binary to file Seems like it would make sense to allow varying this by directory (when supporting layering) and by file type. --- docs/reference.md | 2 +- src/args.rs | 30 +++++++++++++++--------------- src/config.rs | 36 ++++++++++++++++++------------------ src/main.rs | 2 +- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 3c068c1..7720c4c 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -14,13 +14,13 @@ Configuration is read from the following (in precedence order) | Field | Argument | Format | Description | |------------------------|-------------------|--------|-------------| -| files.binary | --binary | bool | Check binary files as text | | files.ignore-hidden | --hidden | bool | Skip hidden files and directories. | | files.ignore-files | --ignore | bool | Respect ignore files. | | files.ignore-dot | --ignore-dot | bool | Respect .ignore files. | | files.ignore-vcs | --ignore-vcs | bool | Respect ignore files in vcs directories. | | files.ignore-global | --ignore-global | bool | Respect global ignore files. | | files.ignore-parent | --ignore-parent | bool | Respect ignore files in parent directories. | +| default.binary | --binary | bool | Check binary files as text | | default.check-filename | \- | bool | Verifying spelling in file names. | | default.check-file | \- | bool | Verifying spelling in files. | | default.ignore-hex | \- | bool | Do not check identifiers that appear to be hexadecimal values. | diff --git a/src/args.rs b/src/args.rs index 503e37c..53ae795 100644 --- a/src/args.rs +++ b/src/args.rs @@ -104,6 +104,12 @@ pub(crate) struct Args { #[derive(Debug, StructOpt)] #[structopt(rename_all = "kebab-case")] pub(crate) struct FileArgs { + #[structopt(long, overrides_with("no-binary"))] + /// Search binary files. + binary: bool, + #[structopt(long, overrides_with("binary"), hidden(true))] + no_binary: bool, + #[structopt(long, overrides_with("check-filenames"))] /// Skip verifying spelling in file names. no_check_filenames: bool, @@ -130,6 +136,15 @@ pub(crate) struct FileArgs { } impl config::FileSource for FileArgs { + 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 check_filename(&self) -> Option { match (self.check_filenames, self.no_check_filenames) { (true, false) => Some(true), @@ -178,12 +193,6 @@ impl config::ConfigSource for ConfigArgs { #[derive(Debug, StructOpt)] #[structopt(rename_all = "kebab-case")] pub(crate) struct WalkArgs { - #[structopt(long, overrides_with("no-binary"))] - /// Search binary files. - binary: bool, - #[structopt(long, overrides_with("binary"), hidden(true))] - no_binary: bool, - #[structopt(long, overrides_with("no-hidden"))] /// Search hidden files and directories. hidden: bool, @@ -222,15 +231,6 @@ pub(crate) 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), diff --git a/src/config.rs b/src/config.rs index a9b8cbc..685ee91 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,11 +12,6 @@ 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 @@ -49,6 +44,11 @@ pub trait WalkSource { } pub trait FileSource { + /// Check binary files. + fn binary(&self) -> Option { + None + } + /// Verifying spelling in file names. fn check_filename(&self) -> Option { None @@ -157,7 +157,6 @@ 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, @@ -170,7 +169,6 @@ impl Walk { pub fn from_defaults() -> Self { let empty = Self::default(); Self { - binary: Some(empty.binary()), ignore_hidden: Some(empty.ignore_hidden()), ignore_files: Some(true), ignore_dot: Some(empty.ignore_dot()), @@ -181,9 +179,6 @@ 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); } @@ -209,10 +204,6 @@ impl Walk { } } - pub fn binary(&self) -> bool { - self.binary.unwrap_or(false) - } - pub fn ignore_hidden(&self) -> bool { self.ignore_hidden.unwrap_or(true) } @@ -238,10 +229,6 @@ impl Walk { } impl WalkSource for Walk { - fn binary(&self) -> Option { - self.binary - } - fn ignore_hidden(&self) -> Option { self.ignore_hidden } @@ -271,6 +258,7 @@ impl WalkSource for Walk { #[serde(deny_unknown_fields, default)] #[serde(rename_all = "kebab-case")] pub struct FileConfig { + pub binary: Option, pub check_filename: Option, pub check_file: Option, pub ignore_hex: Option, @@ -287,6 +275,7 @@ impl FileConfig { pub fn from_defaults() -> Self { let empty = Self::default(); FileConfig { + binary: Some(empty.binary()), check_filename: Some(empty.check_filename()), check_file: Some(empty.check_file()), ignore_hex: Some(empty.ignore_hex()), @@ -301,6 +290,9 @@ impl FileConfig { } pub fn update(&mut self, source: &dyn FileSource) { + if let Some(source) = source.binary() { + self.binary = Some(source); + } if let Some(source) = source.check_filename() { self.check_filename = Some(source); } @@ -337,6 +329,10 @@ impl FileConfig { ); } + pub fn binary(&self) -> bool { + self.binary.unwrap_or(false) + } + pub fn check_filename(&self) -> bool { self.check_filename.unwrap_or(true) } @@ -387,6 +383,10 @@ impl FileConfig { } impl FileSource for FileConfig { + fn binary(&self) -> Option { + self.binary + } + fn check_filename(&self) -> Option { self.check_filename } diff --git a/src/main.rs b/src/main.rs index b8095b5..30eb5ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,7 +109,7 @@ fn run_checks(args: &args::Args) -> proc_exit::ExitResult { settings .check_filenames(config.default.check_filename()) .check_files(config.default.check_file()) - .binary(config.files.binary()); + .binary(config.default.binary()); let threads = if path.is_file() { 1 } else { args.threads }; let single_threaded = threads == 1;