From 4cd77ba975ee26835b841270117b5394a77a0f80 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Mon, 17 Nov 2025 20:13:03 +0300 Subject: [PATCH] Wrapper stream stub --- wrapper/Cargo.toml | 8 ++++ wrapper/src/lib.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 wrapper/Cargo.toml create mode 100644 wrapper/src/lib.rs diff --git a/wrapper/Cargo.toml b/wrapper/Cargo.toml new file mode 100644 index 0000000..2e04524 --- /dev/null +++ b/wrapper/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "source-stream-0-wrapper-0" +edition = "2024" + +[lib] + +[dependencies] +source-stream-0 = { path = ".." } \ No newline at end of file diff --git a/wrapper/src/lib.rs b/wrapper/src/lib.rs new file mode 100644 index 0000000..cbd6a96 --- /dev/null +++ b/wrapper/src/lib.rs @@ -0,0 +1,99 @@ +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream}; + +pub trait StreamConversions_Char { + type WC; + fn wrapChar(c: C) -> Self::WC; +} + +pub trait StreamConversions_Pos<'pos, P: Pos<'pos>> { + type WP: Pos<'pos>; + fn wrapPos(p: P) -> Self::WP; +} + +pub trait StreamConversions_Substring<'source, C, CS: CollectedSubstring<'source, C = C>>: + StreamConversions_Char +{ + type WCS: CollectedSubstring<'source, C = Self::WC>; + fn wrapSubstring(wcs: CS) -> Self::WCS; +} + +pub trait StreamConversions<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C = C>>: + StreamConversions_Char + StreamConversions_Pos<'pos, P> + StreamConversions_Substring<'source, C, CS> +{ +} + +struct PredicateWrapper<'predicate, I: Predicate> { + _orig: &'predicate mut I, +} + +impl<'predicate, C, W: StreamConversions_Char, I: Predicate> + PredicateWrapper<'predicate, I> +{ + fn wrap(pred: &'predicate mut I) -> Self { + return PredicateWrapper { _orig: pred }; + } +} + +impl<'predicate, C, W: StreamConversions_Char, I: Predicate> Predicate + for PredicateWrapper<'predicate, I> +{ + type C = C; + + fn check(&mut self, chr: C) -> bool { + return self._orig.check(W::wrapChar(chr)); + } +} + +pub struct SourceStreamWrapper< + 'source, + 'pos, + C, + P: Pos<'pos>, + CS: CollectedSubstring<'source, C = C>, + W: StreamConversions<'source, 'pos, C, P, CS>, + I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>, +> { + _src: I, +} + +impl< + 'source, + 'pos, + C, + P: Pos<'pos>, + CS: CollectedSubstring<'source>, + W: StreamConversions<'source, 'pos, C, P, CS>, + I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>, +> SourceStream<'source, 'pos> for SourceStreamWrapper<'source, 'pos, C, P, CS, W, I> +{ + type C = W::WC; + type P = W::WP; + type CS = W::WCS; + + fn skip(&mut self, predicate: &mut impl Predicate) -> bool { + self._src.skip(&mut PredicateWrapper::wrap(predicate)) + } + + fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult { + match self._src.collect(&mut PredicateWrapper::wrap(predicate)) { + CollectResult::EOF => return CollectResult::EOF, + CollectResult::NotMatches => return CollectResult::NotMatches, + CollectResult::Matches(cs) => return CollectResult::Matches(W::wrapSubstring(cs)), + } + } + + fn pos(&self) -> W::WP { + return W::wrapPos(self._src.pos()); + } + + fn currentChar(&self) -> Option { + return self._src.currentChar().map(W::wrapChar); + } + + fn nextChar(&mut self) -> Option { + return self._src.nextChar().map(W::wrapChar); + } +}