From 287c4cbfe9c492ff40e15c3c24dc73296e199d83 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 27 Apr 2021 21:26:05 -0500 Subject: [PATCH] refactor(parser): Give more impl flexibility --- crates/typos/src/tokens.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/crates/typos/src/tokens.rs b/crates/typos/src/tokens.rs index ae78716..8eb9fef 100644 --- a/crates/typos/src/tokens.rs +++ b/crates/typos/src/tokens.rs @@ -57,13 +57,8 @@ impl Tokenizer { pub fn parse_str<'c>(&'c self, content: &'c str) -> impl Iterator> { parser::iter_literals(content).filter_map(move |identifier| { - let case = Case::None; let offset = offset(content.as_bytes(), identifier.as_bytes()); - if self.accept(identifier) { - Some(Identifier::new_unchecked(identifier, case, offset)) - } else { - None - } + self.transform(identifier, offset) }) } @@ -76,21 +71,22 @@ impl Tokenizer { }) } - fn accept(&self, contents: &str) -> bool { - debug_assert!(!contents.is_empty()); + fn transform<'i>(&self, identifier: &'i str, offset: usize) -> Option> { + debug_assert!(!identifier.is_empty()); if self.leading_digits { - if is_number(contents.as_bytes()) { - return false; + if is_number(identifier.as_bytes()) { + return None; } - if self.ignore_hex && is_hex(contents.as_bytes()) { - return false; + if self.ignore_hex && is_hex(identifier.as_bytes()) { + return None; } - } else if is_digit(contents.as_bytes()[0]) { - return false; + } else if is_digit(identifier.as_bytes()[0]) { + return None; } - true + let case = Case::None; + Some(Identifier::new_unchecked(identifier, case, offset)) } }