Skip to content

Commit

Permalink
Merge pull request #217 from epage/refactor
Browse files Browse the repository at this point in the history
Lay groundwork for new policy engine
  • Loading branch information
epage authored Mar 1, 2021
2 parents c6a5cc0 + e9ff977 commit 60dbf0a
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 458 deletions.
176 changes: 82 additions & 94 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions benches/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod data;

use assert_fs::prelude::*;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use typos_cli::checks::FileChecker;
use typos_cli::file::FileChecker;

fn bench_checks(c: &mut Criterion) {
let mut group = c.benchmark_group("checks");
Expand All @@ -15,11 +15,12 @@ fn bench_checks(c: &mut Criterion) {

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let checks = typos_cli::checks::TyposSettings::new().build_files();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
checks.check_file(
typos_cli::file::FoundFiles.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&typos_cli::report::PrintSilent,
Expand All @@ -35,11 +36,12 @@ fn bench_checks(c: &mut Criterion) {

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let checks = typos_cli::checks::TyposSettings::new().build_identifier_parser();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
checks.check_file(
typos_cli::file::Identifiers.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&typos_cli::report::PrintSilent,
Expand All @@ -55,11 +57,12 @@ fn bench_checks(c: &mut Criterion) {

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let checks = typos_cli::checks::TyposSettings::new().build_word_parser();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
checks.check_file(
typos_cli::file::Words.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&typos_cli::report::PrintSilent,
Expand All @@ -75,11 +78,12 @@ fn bench_checks(c: &mut Criterion) {

let corrections = typos_cli::dict::BuiltIn::new(Default::default());
let parser = typos::tokens::Tokenizer::new();
let checks = typos_cli::checks::TyposSettings::new().build_typos();
let settings = typos_cli::file::CheckSettings::new();
b.iter(|| {
checks.check_file(
typos_cli::file::Typos.check_file(
sample_path.path(),
true,
&settings,
&parser,
&corrections,
&typos_cli::report::PrintSilent,
Expand Down
100 changes: 100 additions & 0 deletions crates/typos/src/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use crate::tokens;
use crate::Dictionary;
use std::borrow::Cow;

pub fn check_str<'b, 's: 'b>(
buffer: &'b str,
tokenizer: &'s tokens::Tokenizer,
dictionary: &'s dyn Dictionary,
) -> impl Iterator<Item = Typo<'b>> {
tokenizer
.parse_str(buffer)
.flat_map(move |ident| process_ident(ident, dictionary))
}

pub fn check_bytes<'b, 's: 'b>(
buffer: &'b [u8],
tokenizer: &'s tokens::Tokenizer,
dictionary: &'s dyn Dictionary,
) -> impl Iterator<Item = Typo<'b>> {
tokenizer
.parse_bytes(buffer)
.flat_map(move |ident| process_ident(ident, dictionary))
}

fn process_ident<'i, 's: 'i>(
ident: tokens::Identifier<'i>,
dictionary: &'s dyn Dictionary,
) -> impl Iterator<Item = Typo<'i>> {
match dictionary.correct_ident(ident) {
Some(crate::Status::Valid) => itertools::Either::Left(None.into_iter()),
Some(corrections) => {
let typo = Typo {
byte_offset: ident.offset(),
typo: ident.token().into(),
corrections,
};
itertools::Either::Left(Some(typo).into_iter())
}
None => itertools::Either::Right(
ident
.split()
.filter_map(move |word| process_word(word, dictionary)),
),
}
}

fn process_word<'w, 's: 'w>(
word: tokens::Word<'w>,
dictionary: &'s dyn Dictionary,
) -> Option<Typo<'w>> {
match dictionary.correct_word(word) {
Some(crate::Status::Valid) => None,
Some(corrections) => {
let typo = Typo {
byte_offset: word.offset(),
typo: word.token().into(),
corrections,
};
Some(typo)
}
None => None,
}
}

/// An invalid term found in the buffer.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Typo<'m> {
pub byte_offset: usize,
pub typo: Cow<'m, str>,
pub corrections: crate::Status<'m>,
}

impl<'m> Typo<'m> {
pub fn into_owned(self) -> Typo<'static> {
Typo {
byte_offset: self.byte_offset,
typo: Cow::Owned(self.typo.into_owned()),
corrections: self.corrections.into_owned(),
}
}

pub fn borrow(&self) -> Typo<'_> {
Typo {
byte_offset: self.byte_offset,
typo: Cow::Borrowed(self.typo.as_ref()),
corrections: self.corrections.borrow(),
}
}
}

impl<'m> Default for Typo<'m> {
fn default() -> Self {
Self {
byte_offset: 0,
typo: "".into(),
corrections: crate::Status::Invalid,
}
}
}
15 changes: 0 additions & 15 deletions crates/typos/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ pub trait Dictionary: Send + Sync {
fn correct_word<'s, 'w>(&'s self, word: crate::tokens::Word<'w>) -> Option<Status<'s>>;
}

pub(crate) struct NullDictionary;

impl Dictionary for NullDictionary {
fn correct_ident<'s, 'w>(
&'s self,
_ident: crate::tokens::Identifier<'w>,
) -> Option<Status<'s>> {
None
}

fn correct_word<'s, 'w>(&'s self, _word: crate::tokens::Word<'w>) -> Option<Status<'s>> {
None
}
}

/// Validity of a term in a Dictionary.
#[derive(Clone, PartialEq, Eq, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
Expand Down
4 changes: 2 additions & 2 deletions crates/typos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod check;
mod dict;
mod parser;

pub mod tokens;

pub use check::*;
pub use dict::*;
pub use parser::*;
147 changes: 0 additions & 147 deletions crates/typos/src/parser.rs

This file was deleted.

8 changes: 2 additions & 6 deletions crates/typos/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'t> Identifier<'t> {

/// Split into individual Words.
pub fn split(&self) -> impl Iterator<Item = Word<'t>> {
split_ident(self.token, self.offset)
SplitIdent::new(self.token, self.offset)
}
}

Expand All @@ -195,7 +195,7 @@ pub struct Word<'t> {

impl<'t> Word<'t> {
pub fn new(token: &'t str, offset: usize) -> Result<Self, std::io::Error> {
let mut itr = split_ident(token, 0);
let mut itr = SplitIdent::new(token, 0);
let mut item = itr.next().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
Expand Down Expand Up @@ -239,10 +239,6 @@ impl<'t> Word<'t> {
}
}

fn split_ident(ident: &str, offset: usize) -> impl Iterator<Item = Word<'_>> {
SplitIdent::new(ident, offset)
}

struct SplitIdent<'s> {
ident: &'s str,
offset: usize,
Expand Down
2 changes: 1 addition & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Loading

0 comments on commit 60dbf0a

Please sign in to comment.