From 4ffd175c433e470c85a376ea60db227bbc630aa0 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 19 Nov 2025 02:10:40 +0300 Subject: [PATCH] Splitted main traits to several files --- ...eyword_impls.rs => collected_substring.rs} | 38 ++++++--- src/default_streams/empty.rs | 4 +- src/lib.rs | 79 +++---------------- src/stream.rs | 44 +++++++++++ 4 files changed, 87 insertions(+), 78 deletions(-) rename src/{_keyword_impls.rs => collected_substring.rs} (53%) create mode 100644 src/stream.rs diff --git a/src/_keyword_impls.rs b/src/collected_substring.rs similarity index 53% rename from src/_keyword_impls.rs rename to src/collected_substring.rs index b422fdf..a58f651 100644 --- a/src/_keyword_impls.rs +++ b/src/collected_substring.rs @@ -1,18 +1,36 @@ use std::slice::Iter; use std::str::Chars; -impl<'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keyword> { +pub trait KeywordComparatorIterator<'keyword, C> { + fn consume(&mut self, c: C) -> bool; +} + +pub trait Keyword { + fn startComparation<'keyword>( + &'keyword self, + expectedLen: usize, + ) -> Option>; +} + +pub trait CollectedSubstring<'source> { + type C; + fn compareKeyword<'keyword>(&self, kw: impl Keyword) -> bool; +} + + + +impl<'keyword> KeywordComparatorIterator<'keyword, char> for Chars<'keyword> { fn consume(&mut self, e: char) -> bool { return self.next().is_some_and(|a| a == e); } } -impl crate::Keyword for &str { +impl Keyword for &str { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if self.len() != expectedLen { return Option::None; } @@ -21,11 +39,11 @@ impl crate::Keyword for &str { } } -impl crate::Keyword for &String { +impl Keyword for &String { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if self.len() != expectedLen { return Option::None; } @@ -33,17 +51,17 @@ impl crate::Keyword for &String { return Option::Some(self.chars()); } } -impl<'keyword, C: PartialEq> crate::KeywordComparatorIterator<'keyword, C> for Iter<'keyword, C> { +impl<'keyword, C: PartialEq> KeywordComparatorIterator<'keyword, C> for Iter<'keyword, C> { fn consume(&mut self, e: C) -> bool { return self.next().is_some_and(|a| a.eq(&e)); } } -impl crate::Keyword for &[C] { +impl Keyword for &[C] { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if self.len() != expectedLen { return Option::None; } @@ -52,11 +70,11 @@ impl crate::Keyword for &[C] { } } -impl crate::Keyword for &[C; SZ] { +impl Keyword for &[C; SZ] { fn startComparation<'keyword>( &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if self.len() != expectedLen { return Option::None; } diff --git a/src/default_streams/empty.rs b/src/default_streams/empty.rs index cb33138..f7863e2 100644 --- a/src/default_streams/empty.rs +++ b/src/default_streams/empty.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use crate::{CollectedSubstring, Pos, Predicate, SourceStream}; -use crate::pos::PosCounter; +use crate::{CollectedSubstring, Predicate, SourceStream}; +use crate::pos::{Pos, PosCounter}; pub struct EmptySourceStream< 'source, diff --git a/src/lib.rs b/src/lib.rs index f78da8d..65155fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,68 +1,15 @@ -#![allow(non_snake_case)] - -mod _keyword_impls; +mod collected_substring; mod macros; -mod default_streams; -mod ascii; -pub mod pos; +mod stream; + +pub use collected_substring::CollectedSubstring; +pub use collected_substring::Keyword; +pub use collected_substring::KeywordComparatorIterator; + +pub use stream::Predicate; +pub use stream::SourceStream; + +pub mod ascii; pub mod converter; - -use pos::Pos; - -pub trait Predicate { - fn check(&mut self, chr: C) -> bool; -} - -pub trait KeywordComparatorIterator<'keyword, C> { - fn consume(&mut self, c: C) -> bool; -} - -pub trait Keyword { - fn startComparation<'keyword>( - &'keyword self, - expectedLen: usize, - ) -> Option>; -} - -pub trait CollectedSubstring<'source> { - type C; - fn compareKeyword<'keyword>(&self, kw: impl Keyword) -> bool; -} - -pub trait SourceStream<'source, 'pos> { - type C: Copy; - type P: Pos<'pos>; - type CS: CollectedSubstring<'source, C = Self::C>; - - fn skipNext(&mut self, predicate: &mut impl Predicate) -> Option; - - fn skipCurrent( - &mut self, - current: Self::C, - predicate: &mut impl Predicate, - ) -> Option { - if !predicate.check(current) { - return Some(current); - } - - return self.skipNext(predicate); - } - - /** - * Returns reference to a slice of the source which can be used for comparations - * and exporting (not standardized). - * Also returns char that failed predicate check for further - * interpretation or 'None' if the stream is ended. - * - * Predicate applied to char starting with next after current, assuming that current char - * already checked (you don't call this method in the blind, aren't you). - * - * Calling this method on ended stream (previous `nextChar` returned `None`) - * is undefined behavior. - */ - fn collect(&mut self, predicate: &mut impl Predicate) -> (Self::CS, Option); - - fn pos(&self) -> Self::P; - - fn nextChar(&mut self) -> Option; -} \ No newline at end of file +pub mod default_streams; +pub mod pos; diff --git a/src/stream.rs b/src/stream.rs new file mode 100644 index 0000000..57ccf3e --- /dev/null +++ b/src/stream.rs @@ -0,0 +1,44 @@ +use crate::CollectedSubstring; +use crate::pos::Pos; + +pub trait Predicate { + fn check(&mut self, chr: C) -> bool; +} + +pub trait SourceStream<'source, 'pos> { + type C: Copy; + type P: Pos<'pos>; + type CS: CollectedSubstring<'source, C = Self::C>; + + fn skipNext(&mut self, predicate: &mut impl Predicate) -> Option; + + fn skipCurrent( + &mut self, + current: Self::C, + predicate: &mut impl Predicate, + ) -> Option { + if !predicate.check(current) { + return Some(current); + } + + return self.skipNext(predicate); + } + + /** + * Returns reference to a slice of the source which can be used for comparations + * and exporting (not standardized). + * Also returns char that failed predicate check for further + * interpretation or 'None' if the stream is ended. + * + * Predicate applied to char starting with next after current, assuming that current char + * already checked (you don't call this method in the blind, aren't you). + * + * Calling this method on ended stream (previous `nextChar` returned `None`) + * is undefined behavior. + */ + fn collect(&mut self, predicate: &mut impl Predicate) -> (Self::CS, Option); + + fn pos(&self) -> Self::P; + + fn nextChar(&mut self) -> Option; +}