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