refactor(parser): Split out parser creation

This commit is contained in:
Ed Page 2019-07-23 20:42:20 -06:00
parent 8e4708dfdf
commit d0b9979c36

View file

@ -6,6 +6,25 @@ pub enum Case {
None, 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)] #[derive(Debug, Clone)]
pub struct Parser { pub struct Parser {
words_str: regex::Regex, words_str: regex::Regex,
@ -14,13 +33,7 @@ pub struct Parser {
impl Parser { impl Parser {
pub fn new() -> Self { pub fn new() -> Self {
let pattern = r#"\b(\p{Alphabetic}|\d|_|')+\b"#; ParserBuilder::default().build()
let words_str = regex::Regex::new(pattern).unwrap();
let words_bytes = regex::bytes::Regex::new(pattern).unwrap();
Self {
words_str,
words_bytes,
}
} }
pub fn parse<'c>(&'c self, content: &'c str) -> impl Iterator<Item = Identifier<'c>> { 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Identifier<'t> { pub struct Identifier<'t> {
token: &'t str, token: &'t str,