mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 02:20:58 -05:00
refactor(parser): Split out parser creation
This commit is contained in:
parent
8e4708dfdf
commit
d0b9979c36
1 changed files with 26 additions and 7 deletions
|
@ -6,6 +6,25 @@ pub enum Case {
|
|||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ParserBuilder {}
|
||||
|
||||
impl ParserBuilder {
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn build(self) -> Parser {
|
||||
let pattern = r#"\b(\p{Alphabetic}|\d|_|')+\b"#;
|
||||
let words_str = regex::Regex::new(pattern).unwrap();
|
||||
let words_bytes = regex::bytes::Regex::new(pattern).unwrap();
|
||||
Parser {
|
||||
words_str,
|
||||
words_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Parser {
|
||||
words_str: regex::Regex,
|
||||
|
@ -14,13 +33,7 @@ pub struct Parser {
|
|||
|
||||
impl Parser {
|
||||
pub fn new() -> Self {
|
||||
let pattern = r#"\b(\p{Alphabetic}|\d|_|')+\b"#;
|
||||
let words_str = regex::Regex::new(pattern).unwrap();
|
||||
let words_bytes = regex::bytes::Regex::new(pattern).unwrap();
|
||||
Self {
|
||||
words_str,
|
||||
words_bytes,
|
||||
}
|
||||
ParserBuilder::default().build()
|
||||
}
|
||||
|
||||
pub fn parse<'c>(&'c self, content: &'c str) -> impl Iterator<Item = Identifier<'c>> {
|
||||
|
@ -37,6 +50,12 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Parser {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Identifier<'t> {
|
||||
token: &'t str,
|
||||
|
|
Loading…
Reference in a new issue