From dd86157e7e955f4a7b24b0f33098c13dbe412010 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 9 Nov 2025 12:12:17 +0300 Subject: [PATCH] Interfaces and traits impl for _Keyword --- .gitignore | 2 ++ Cargo.toml | 8 ++++++ src/_keyword_impls.rs | 63 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 44 ++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/_keyword_impls.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f419dc7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d9fc5b4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "untitled" +version = "0.1.0" +edition = "2024" + +[dependencies] + +[lib] \ No newline at end of file diff --git a/src/_keyword_impls.rs b/src/_keyword_impls.rs new file mode 100644 index 0000000..7d57578 --- /dev/null +++ b/src/_keyword_impls.rs @@ -0,0 +1,63 @@ +use std::slice::Iter; +use std::str::Chars; + +struct _StringIteratorImpl<'a> { + orig: Chars<'a>, +} + +impl crate::_KeywordComparatorIterator for _StringIteratorImpl<'_> { + fn consume(&mut self, e: &char) -> bool { + return self.orig.next().is_some_and(|a| a.eq(e)); + } +} + +impl crate::_Keyword for &str { + fn startComparation(&self, expectedLen: usize) -> Option> { + if (self.len() != expectedLen) { + return Option::None; + } + + return Option::Some(_StringIteratorImpl { orig: self.chars() }); + } +} + +impl crate::_Keyword for &String { + fn startComparation(&self, expectedLen: usize) -> Option> { + if (self.len() != expectedLen) { + return Option::None; + } + + return Option::Some(_StringIteratorImpl { orig: self.chars() }); + } +} + +struct _ArrayIteratorImpl<'a, T> { + orig: Iter<'a, T>, +} + +impl crate::_KeywordComparatorIterator for _ArrayIteratorImpl<'_, T> { + fn consume(&mut self, e: &T) -> bool { + return self.orig.next().is_some_and(|a| a == e); + } +} + +impl crate::_Keyword for &[T] { + fn startComparation(&self, expectedLen: usize) -> Option> { + if (self.len() != expectedLen) { + return Option::None; + } + + return Option::Some(_ArrayIteratorImpl { orig: self.iter() }); + } +} + + +impl crate::_Keyword for &[T; SZ] { + fn startComparation(&self, expectedLen: usize) -> Option> { + if (self.len() != expectedLen) { + return Option::None; + } + + return Option::Some(_ArrayIteratorImpl { orig: self.iter() }); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..44abaaa --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,44 @@ +mod _keyword_impls; + +pub trait Predicate { + fn check(&mut self, chr: &C) -> bool; +} + +impl Predicate for fn(&C) -> bool { + fn check(&mut self, chr: &C) -> bool { + return self(chr); + } +} + +pub trait Pos {} + +pub trait _KeywordComparatorIterator { + fn consume(&mut self, c: &C) -> bool; +} + +pub trait _Keyword { + fn startComparation(&self, expectedLen: usize) -> Option>; +} + +pub trait CollectedSubstring { + fn compareKeyword(&self, kw: impl _Keyword); +} + +pub trait SourceStream> { + fn skip(&mut self, predicate: impl Predicate); + fn collect(&mut self, predicate: impl Predicate) -> CS; + + fn pos(&mut self) -> P; + fn currentChar(&mut self) -> C; +} + +// +// fn sandbox>(ctx: &mut impl SourceStream) { +// let x = "1324"; +// let y = ['a']; +// ctx.collect(|c: &char| -> bool { +// return true; +// }).compareKeyword(&y); +// +// return; +// }