-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(typos): Simplify the top-level API
- Loading branch information
Ed Page
committed
Mar 1, 2021
1 parent
e1e4ce8
commit b5f606f
Showing
5 changed files
with
108 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters