Diffstat (limited to 'tests/demo.rs')
-rw-r--r--tests/demo.rs116
1 files changed, 41 insertions, 75 deletions
diff --git a/tests/demo.rs b/tests/demo.rs
index 263a766..81b4875 100644
--- a/tests/demo.rs
+++ b/tests/demo.rs
@@ -1,104 +1,70 @@
-use std::marker::PhantomData;
-
-use uniserde::{
- build::{Build, Builder},
- error::BasicError,
- impls::core::{
- iterator::IterWalker,
- reference::{ContextLifetime, RefBuilder, RefWalker, StaticLifetime, ValueLifetime},
- },
- protocol::{AnyVisit, ProtocolId, Visit},
- protocols::{reference, sequence},
- transform::{build_from, build_from_ref},
- Visitor, Walker,
-};
+use uniserde::{transform::from, build::{Builder, Build}, Visitor, ControlFlow, protocol::{ProtocolId, AnyVisit, Visit}, protocols::sequence, impls::core::iterator::IterWalker, Walker};
#[test]
fn demo() {
- let x = vec!["a", "b", "c"];
+ let x = ["1", "2", "3"];
- let mut w = IterWalker::new(x);
- let mut b = VecBuilder::init();
- w.walk(b.as_visitor()).unwrap();
- dbg!(b.finish().unwrap());
+ let mut builder = VecBuilder::default();
+ IterWalker::new(x.iter()).walk(builder.as_visitor());
+ dbg!(builder.build());
todo!();
}
+#[no_mangle]
+pub fn example<'a>(x: &'a str) -> &'a str {
+ from(x).unwrap()
+}
+
+#[derive(Default)]
pub struct VecBuilder(Vec<&'static str>);
-impl<'value, 'ctx> Builder<'value, 'ctx> for VecBuilder {
- type Error = BasicError;
+impl Builder<'static> for VecBuilder {
+ type Error = ();
type Value = Vec<&'static str>;
- fn init() -> Self
- where
- Self: Sized,
- {
- Self(Vec::new())
- }
-
- fn as_visitor<WalkerErr: 'value>(
- &mut self,
- ) -> &mut dyn uniserde::Visitor<'value, 'ctx, WalkerErr, Error = Self::Error> {
+ fn as_visitor(&mut self) -> &mut dyn uniserde::Visitor<'static> {
self
}
- fn finish(self) -> Result<Self::Value, Self::Error>
- where
- Self: Sized,
- {
+ fn build(self) -> Result<Self::Value, Self::Error> {
Ok(self.0)
}
}
-impl<'value, 'ctx, WalkerErr> Visitor<'value, 'ctx, WalkerErr> for VecBuilder {
- type Error = BasicError;
-
- fn protocol(
+impl Visitor<'static> for VecBuilder {
+ fn request_hint(
&mut self,
- id: uniserde::protocol::ProtocolId,
- ) -> Option<uniserde::protocol::AnyProtocolVisit<'_, 'value, 'ctx, WalkerErr, Self::Error>> {
- match id {
- id if id == ProtocolId::of::<sequence::Sequence>() => Some(AnyVisit::new(self).erase()),
- _ => None,
- }
+ _hints: &mut dyn uniserde::WalkerHints<'static>,
+ _need_hint: bool,
+ ) -> uniserde::ControlFlow {
+ ControlFlow::Continue
}
-}
-
-pub struct MapError<V, F, E>(V, F, PhantomData<fn() -> E>);
-
-impl<'value, 'ctx, V, F, E, WalkerErr> Visitor<'value, 'ctx, WalkerErr> for MapError<V, F, E>
-where
- V: Visitor<'value, 'ctx, WalkerErr>,
- F: Fn(V::Error) -> E,
-{
- type Error = E;
- fn protocol(
- &mut self,
- id: uniserde::protocol::ProtocolId,
- ) -> Option<uniserde::protocol::AnyProtocolVisit<'_, 'value, 'ctx, WalkerErr, Self::Error>> {
- match self.0.protocol(id) {
- Some(visit) => Some(AnyVisit::new_with_wrapper(visit))),
- None => todo!(),
+ fn protocol(&mut self, id: uniserde::protocol::ProtocolId) -> Option<uniserde::protocol::AnyVisit<'_, 'static>> {
+ if id == ProtocolId::of::<sequence::Sequence>() {
+ Some(AnyVisit::new(self))
+ } else {
+ None
}
}
}
-impl<'value, 'ctx: 'value, WalkerErr> Visit<'value, 'ctx, sequence::Sequence, WalkerErr>
- for VecBuilder
-{
- type Error = BasicError;
-
- fn visit<'walking>(
- &'walking mut self,
- accessor: &'walking mut dyn sequence::Accessor<'value, 'ctx, WalkerErr, Self::Error>,
- ) -> Result<(), uniserde::error::UniError<WalkerErr, Self::Error>> {
- let mut b = <&str as Build>::Builder::init();
- accessor.next(&mut b)?;
- self.0.push(b.finish().unwrap());
- Ok(())
+impl Visit<'static, sequence::Sequence> for VecBuilder {
+ fn visit(&mut self, accessor: <sequence::Sequence as uniserde::protocol::Protocol>::Accessor<'_, 'static>) -> ControlFlow {
+ loop {
+ let mut builder = <&'static str as Build<'static>>::Builder::default();
+ match accessor.next(builder.as_visitor()) {
+ ControlFlow::Continue => {},
+ ControlFlow::Done => {},
+ ControlFlow::Error => break ControlFlow::Error,
+ }
+
+ match builder.build() {
+ Ok(value) => self.0.push(value),
+ Err(err) => break ControlFlow::Error,
+ }
+ }
}
}