1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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(())
    }
}