Diffstat (limited to 'tests/demo.rs')
-rw-r--r--tests/demo.rs108
1 files changed, 94 insertions, 14 deletions
diff --git a/tests/demo.rs b/tests/demo.rs
index c0fe77e..263a766 100644
--- a/tests/demo.rs
+++ b/tests/demo.rs
@@ -1,24 +1,104 @@
-use uniserde::{impls::core::reference::{BuilderRefValue, BuilderRefContext}, transform::build_from_ref};
+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,
+};
#[test]
fn demo() {
- let x = String::from("a");
- // let y: &str = build_from_ref::<'_, 'static, BuilderRefValue<str>, _, _>(&*x).unwrap();
- let y: &str = uniserde::transform::from_ref(&*x).unwrap();
- dbg!(y);
- let y: &str = example(&&&&&&&*x);
- dbg!(y);
+ let x = vec!["a", "b", "c"];
- // let y: &String = uniserde::transform::from(&*x).unwrap();
- // dbg!(y);
+ let mut w = IterWalker::new(x);
+ let mut b = VecBuilder::init();
+ w.walk(b.as_visitor()).unwrap();
+ dbg!(b.finish().unwrap());
todo!();
}
-#[no_mangle]
-pub fn example<'a>(a: &&&&&&'a str) -> &'a str {
- // uniserde::transform::from(a).unwrap()
- build_from_ref::<BuilderRefContext<str>, _, _>(a).unwrap()
+pub struct VecBuilder(Vec<&'static str>);
+
+impl<'value, 'ctx> Builder<'value, 'ctx> for VecBuilder {
+ type Error = BasicError;
+
+ 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> {
+ self
+ }
+
+ fn finish(self) -> Result<Self::Value, Self::Error>
+ where
+ Self: Sized,
+ {
+ Ok(self.0)
+ }
}
-// default everything to value, have wrappers for context and static borrows
+impl<'value, 'ctx, WalkerErr> Visitor<'value, 'ctx, WalkerErr> for VecBuilder {
+ type Error = BasicError;
+
+ fn protocol(
+ &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,
+ }
+ }
+}
+
+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!(),
+ }
+ }
+}
+
+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(())
+ }
+}