Interfaces and traits impl for _Keyword

This commit is contained in:
Andrew Golovashevich 2025-11-09 12:12:17 +03:00
commit dd86157e7e
4 changed files with 117 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea
/target

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "untitled"
version = "0.1.0"
edition = "2024"
[dependencies]
[lib]

63
src/_keyword_impls.rs Normal file
View File

@ -0,0 +1,63 @@
use std::slice::Iter;
use std::str::Chars;
struct _StringIteratorImpl<'a> {
orig: Chars<'a>,
}
impl crate::_KeywordComparatorIterator<char> for _StringIteratorImpl<'_> {
fn consume(&mut self, e: &char) -> bool {
return self.orig.next().is_some_and(|a| a.eq(e));
}
}
impl crate::_Keyword<char> for &str {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<char>> {
if (self.len() != expectedLen) {
return Option::None;
}
return Option::Some(_StringIteratorImpl { orig: self.chars() });
}
}
impl crate::_Keyword<char> for &String {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<char>> {
if (self.len() != expectedLen) {
return Option::None;
}
return Option::Some(_StringIteratorImpl { orig: self.chars() });
}
}
struct _ArrayIteratorImpl<'a, T> {
orig: Iter<'a, T>,
}
impl<T: PartialEq> crate::_KeywordComparatorIterator<T> for _ArrayIteratorImpl<'_, T> {
fn consume(&mut self, e: &T) -> bool {
return self.orig.next().is_some_and(|a| a == e);
}
}
impl<T: PartialEq> crate::_Keyword<T> for &[T] {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<T>> {
if (self.len() != expectedLen) {
return Option::None;
}
return Option::Some(_ArrayIteratorImpl { orig: self.iter() });
}
}
impl<T: PartialEq, const SZ: usize> crate::_Keyword<T> for &[T; SZ] {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<T>> {
if (self.len() != expectedLen) {
return Option::None;
}
return Option::Some(_ArrayIteratorImpl { orig: self.iter() });
}
}

44
src/lib.rs Normal file
View File

@ -0,0 +1,44 @@
mod _keyword_impls;
pub trait Predicate<C> {
fn check(&mut self, chr: &C) -> bool;
}
impl<C> Predicate<C> for fn(&C) -> bool {
fn check(&mut self, chr: &C) -> bool {
return self(chr);
}
}
pub trait Pos {}
pub trait _KeywordComparatorIterator<C> {
fn consume(&mut self, c: &C) -> bool;
}
pub trait _Keyword<C> {
fn startComparation(&self, expectedLen: usize) -> Option<impl _KeywordComparatorIterator<C>>;
}
pub trait CollectedSubstring<C> {
fn compareKeyword(&self, kw: impl _Keyword<C>);
}
pub trait SourceStream<C, P: Pos, CS: CollectedSubstring<C>> {
fn skip(&mut self, predicate: impl Predicate<C>);
fn collect(&mut self, predicate: impl Predicate<C>) -> CS;
fn pos(&mut self) -> P;
fn currentChar(&mut self) -> C;
}
//
// fn sandbox<P: Pos, CS: CollectedSubstring<char>>(ctx: &mut impl SourceStream<char, P, CS>) {
// let x = "1324";
// let y = ['a'];
// ctx.collect(|c: &char| -> bool {
// return true;
// }).compareKeyword(&y);
//
// return;
// }