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 = vec!["a", "b", "c"];
let mut w = IterWalker::new(x);
let mut b = VecBuilder::init();
w.walk(b.as_visitor()).unwrap();
dbg!(b.finish().unwrap());
todo!();
}
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)
}
}
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(())
}
}