use std::slice::Iter; use std::str::Chars; pub trait KeywordComparatorIterator { fn consume(&mut self, c: C) -> bool; } pub trait Keyword { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, ) -> Option + use<'keyword, Self, C>>; } pub trait CollectedSubstring { type C: Copy; fn compareKeyword(&self, kw: impl Keyword) -> bool; } impl<'keyword> KeywordComparatorIterator for Chars<'keyword> { fn consume(&mut self, e: char) -> bool { return self.next().is_some_and(|a| a == e); } } impl Keyword for &str { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, ) -> Option> { if self.len() != expectedLen { return Option::None; } return Option::Some(self.chars()); } } impl Keyword for &String { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, ) -> Option> { if self.len() != expectedLen { return Option::None; } return Option::Some(self.chars()); } } impl<'keyword, C: Copy + PartialEq> KeywordComparatorIterator for Iter<'keyword, C> { fn consume(&mut self, e: C) -> bool { return self.next().is_some_and(|a| a.eq(&e)); } } impl Keyword for &[C] { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, ) -> Option> { if self.len() != expectedLen { return Option::None; } return Option::Some(self.iter()); } } impl Keyword for &[C; SZ] { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, ) -> Option> { if self.len() != expectedLen { return Option::None; } return Option::Some(self.iter()); } }