Splitted main traits to several files
This commit is contained in:
parent
7e582faa3b
commit
4ffd175c43
@ -1,18 +1,36 @@
|
|||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
use std::str::Chars;
|
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<C> {
|
||||||
|
fn startComparation<'keyword>(
|
||||||
|
&'keyword self,
|
||||||
|
expectedLen: usize,
|
||||||
|
) -> Option<impl KeywordComparatorIterator<'keyword, C>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait CollectedSubstring<'source> {
|
||||||
|
type C;
|
||||||
|
fn compareKeyword<'keyword>(&self, kw: impl Keyword<Self::C>) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
impl<'keyword> KeywordComparatorIterator<'keyword, char> for Chars<'keyword> {
|
||||||
fn consume(&mut self, e: char) -> bool {
|
fn consume(&mut self, e: char) -> bool {
|
||||||
return self.next().is_some_and(|a| a == e);
|
return self.next().is_some_and(|a| a == e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Keyword<char> for &str {
|
impl Keyword<char> for &str {
|
||||||
|
|
||||||
fn startComparation<'keyword>(
|
fn startComparation<'keyword>(
|
||||||
&'keyword self,
|
&'keyword self,
|
||||||
expectedLen: usize,
|
expectedLen: usize,
|
||||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
) -> Option<impl KeywordComparatorIterator<'keyword, char>> {
|
||||||
if self.len() != expectedLen {
|
if self.len() != expectedLen {
|
||||||
return Option::None;
|
return Option::None;
|
||||||
}
|
}
|
||||||
@ -21,11 +39,11 @@ impl crate::Keyword<char> for &str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Keyword<char> for &String {
|
impl Keyword<char> for &String {
|
||||||
fn startComparation<'keyword>(
|
fn startComparation<'keyword>(
|
||||||
&'keyword self,
|
&'keyword self,
|
||||||
expectedLen: usize,
|
expectedLen: usize,
|
||||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
) -> Option<impl KeywordComparatorIterator<'keyword, char>> {
|
||||||
if self.len() != expectedLen {
|
if self.len() != expectedLen {
|
||||||
return Option::None;
|
return Option::None;
|
||||||
}
|
}
|
||||||
@ -33,17 +51,17 @@ impl crate::Keyword<char> for &String {
|
|||||||
return Option::Some(self.chars());
|
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 {
|
fn consume(&mut self, e: C) -> bool {
|
||||||
return self.next().is_some_and(|a| a.eq(&e));
|
return self.next().is_some_and(|a| a.eq(&e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C: PartialEq> crate::Keyword<C> for &[C] {
|
impl<C: PartialEq> Keyword<C> for &[C] {
|
||||||
fn startComparation<'keyword>(
|
fn startComparation<'keyword>(
|
||||||
&'keyword self,
|
&'keyword self,
|
||||||
expectedLen: usize,
|
expectedLen: usize,
|
||||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C>> {
|
) -> Option<impl KeywordComparatorIterator<'keyword, C>> {
|
||||||
if self.len() != expectedLen {
|
if self.len() != expectedLen {
|
||||||
return Option::None;
|
return Option::None;
|
||||||
}
|
}
|
||||||
@ -52,11 +70,11 @@ impl<C: PartialEq> crate::Keyword<C> for &[C] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C: PartialEq, const SZ: usize> crate::Keyword<C> for &[C; SZ] {
|
impl<C: PartialEq, const SZ: usize> Keyword<C> for &[C; SZ] {
|
||||||
fn startComparation<'keyword>(
|
fn startComparation<'keyword>(
|
||||||
&'keyword self,
|
&'keyword self,
|
||||||
expectedLen: usize,
|
expectedLen: usize,
|
||||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C>> {
|
) -> Option<impl KeywordComparatorIterator<'keyword, C>> {
|
||||||
if self.len() != expectedLen {
|
if self.len() != expectedLen {
|
||||||
return Option::None;
|
return Option::None;
|
||||||
}
|
}
|
||||||
@ -1,6 +1,6 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use crate::{CollectedSubstring, Pos, Predicate, SourceStream};
|
use crate::{CollectedSubstring, Predicate, SourceStream};
|
||||||
use crate::pos::PosCounter;
|
use crate::pos::{Pos, PosCounter};
|
||||||
|
|
||||||
pub struct EmptySourceStream<
|
pub struct EmptySourceStream<
|
||||||
'source,
|
'source,
|
||||||
|
|||||||
79
src/lib.rs
79
src/lib.rs
@ -1,68 +1,15 @@
|
|||||||
#![allow(non_snake_case)]
|
mod collected_substring;
|
||||||
|
|
||||||
mod _keyword_impls;
|
|
||||||
mod macros;
|
mod macros;
|
||||||
mod default_streams;
|
mod stream;
|
||||||
mod ascii;
|
|
||||||
pub mod pos;
|
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;
|
pub mod converter;
|
||||||
|
pub mod default_streams;
|
||||||
use pos::Pos;
|
pub mod pos;
|
||||||
|
|
||||||
pub trait Predicate<C: Copy> {
|
|
||||||
fn check(&mut self, chr: C) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait KeywordComparatorIterator<'keyword, C> {
|
|
||||||
fn consume(&mut self, c: C) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Keyword<C> {
|
|
||||||
fn startComparation<'keyword>(
|
|
||||||
&'keyword self,
|
|
||||||
expectedLen: usize,
|
|
||||||
) -> Option<impl KeywordComparatorIterator<'keyword, C>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait CollectedSubstring<'source> {
|
|
||||||
type C;
|
|
||||||
fn compareKeyword<'keyword>(&self, kw: impl Keyword<Self::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<Self::C>) -> Option<Self::C>;
|
|
||||||
|
|
||||||
fn skipCurrent(
|
|
||||||
&mut self,
|
|
||||||
current: Self::C,
|
|
||||||
predicate: &mut impl Predicate<Self::C>,
|
|
||||||
) -> Option<Self::C> {
|
|
||||||
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::C>) -> (Self::CS, Option<Self::C>);
|
|
||||||
|
|
||||||
fn pos(&self) -> Self::P;
|
|
||||||
|
|
||||||
fn nextChar(&mut self) -> Option<Self::C>;
|
|
||||||
}
|
|
||||||
|
|||||||
44
src/stream.rs
Normal file
44
src/stream.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
use crate::CollectedSubstring;
|
||||||
|
use crate::pos::Pos;
|
||||||
|
|
||||||
|
pub trait Predicate<C: Copy> {
|
||||||
|
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<Self::C>) -> Option<Self::C>;
|
||||||
|
|
||||||
|
fn skipCurrent(
|
||||||
|
&mut self,
|
||||||
|
current: Self::C,
|
||||||
|
predicate: &mut impl Predicate<Self::C>,
|
||||||
|
) -> Option<Self::C> {
|
||||||
|
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::C>) -> (Self::CS, Option<Self::C>);
|
||||||
|
|
||||||
|
fn pos(&self) -> Self::P;
|
||||||
|
|
||||||
|
fn nextChar(&mut self) -> Option<Self::C>;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user