Skip to content

Commit

Permalink
Merge pull request #218 from epage/types
Browse files Browse the repository at this point in the history
refactor(cli): Prepare for more advanced config
  • Loading branch information
epage authored Mar 31, 2021
2 parents 60dbf0a + d51725b commit a148054
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 301 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ toml = "0.5"
log = "0.4"
env_logger = "0.8"
bstr = "0.2"
once_cell = "1.2.0"
ahash = "0.7"
difflib = "0.4"
proc-exit = "1.0"
Expand All @@ -57,6 +58,8 @@ derive_setters = "0.1"
itertools = "0.10"
serde_json = "1.0"
encoding = "0.2"
kstring = "1.0"
typed-arena = "2.0.1"

[dev-dependencies]
assert_fs = "1.0"
Expand Down
34 changes: 10 additions & 24 deletions benches/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use typos_cli::file::FileChecker;

fn bench_checks(c: &mut Criterion) {
let dict = typos_cli::dict::BuiltIn::new(Default::default());
let tokenizer = typos::tokens::Tokenizer::new();
let policy = typos_cli::policy::Policy::new()
.dict(&dict)
.tokenizer(&tokenizer);

let mut group = c.benchmark_group("checks");
for (name, sample) in data::DATA {
let len = sample.len();
Expand All @@ -13,16 +19,11 @@ fn bench_checks(c: &mut Criterion) {
let sample_path = temp.child("sample");
sample_path.write_str(sample).unwrap();

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
typos_cli::file::FoundFiles.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&policy,
&typos_cli::report::PrintSilent,
)
});
Expand All @@ -34,16 +35,11 @@ fn bench_checks(c: &mut Criterion) {
let sample_path = temp.child("sample");
sample_path.write_str(sample).unwrap();

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
typos_cli::file::Identifiers.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&policy,
&typos_cli::report::PrintSilent,
)
});
Expand All @@ -55,16 +51,11 @@ fn bench_checks(c: &mut Criterion) {
let sample_path = temp.child("sample");
sample_path.write_str(sample).unwrap();

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
typos_cli::file::Words.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&policy,
&typos_cli::report::PrintSilent,
)
});
Expand All @@ -76,16 +67,11 @@ fn bench_checks(c: &mut Criterion) {
let sample_path = temp.child("sample");
sample_path.write_str(sample).unwrap();

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
typos_cli::file::Typos.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&policy,
&typos_cli::report::PrintSilent,
)
});
Expand Down
2 changes: 1 addition & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ Configuration is read from the following (in precedence order)
| default.identifier-include-digits | \- | bool | Allow identifiers to include digits, in addition to letters. |
| default.identifier-leading-chars | \- | string | Allow identifiers to start with one of these characters. |
| default.identifier-include-chars | \- | string | Allow identifiers to include these characters. |
| default.locale | \- | en, en-us, en-gb, en-ca, en-au | English dialect to correct to. |
| default.locale | --locale | en, en-us, en-gb, en-ca, en-au | English dialect to correct to. |
| default.extend-identifiers | \- | table of strings | Corrections for identifiers. When the correction is blank, the word is never valid. When the correction is the key, the word is always valid. |
| default.extend-words | \- | table of strings | Corrections for identifiers. When the correction is blank, the word is never valid. When the correction is the key, the word is always valid. |
21 changes: 6 additions & 15 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub(crate) struct Args {
pub(crate) verbose: clap_verbosity_flag::Verbosity,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Clone, StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub(crate) struct FileArgs {
#[structopt(long, overrides_with("no-binary"))]
Expand All @@ -122,20 +122,14 @@ pub(crate) struct FileArgs {
#[structopt(long, overrides_with("no-check-files"), hidden(true))]
check_files: bool,

#[structopt(long, overrides_with("hex"))]
/// Don't try to detect that an identifier looks like hex
no_hex: bool,
#[structopt(long, overrides_with("no-hex"), hidden(true))]
hex: bool,

#[structopt(
long,
possible_values(&config::Locale::variants()),
)]
pub(crate) locale: Option<config::Locale>,
}

impl config::FileSource for FileArgs {
impl config::EngineSource for FileArgs {
fn binary(&self) -> Option<bool> {
match (self.binary, self.no_binary) {
(true, false) => Some(true),
Expand Down Expand Up @@ -163,15 +157,12 @@ impl config::FileSource for FileArgs {
}
}

fn ignore_hex(&self) -> Option<bool> {
match (self.hex, self.no_hex) {
(true, false) => Some(true),
(false, true) => Some(false),
(false, false) => None,
(_, _) => unreachable!("StructOpt should make this impossible"),
}
fn dict(&self) -> Option<&dyn config::DictSource> {
Some(self)
}
}

impl config::DictSource for FileArgs {
fn locale(&self) -> Option<config::Locale> {
self.locale
}
Expand Down
Loading

0 comments on commit a148054

Please sign in to comment.