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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use core::marker::PhantomData;

use ::alloc::string::String;

use crate::{error::{BasicError, UniError}, WalkerHints, protocol::{ProtocolId, AnyHint, Hint, lookup_visit, lookup_hint, AnyVisit, Protocol}, protocols::{str::Str, self}, HintGiven, walk::{WalkOnce, WalkMut, Walk}, build::{Builder, Build}};

macro_rules! impl_walker {
    ($walker:ident) => {
        impl<'value, 'ctx: 'value, VisitorErr: 'ctx> crate::Walker<'value, 'ctx, VisitorErr>
            for $walker<'value, 'ctx>
        {
            type Error = BasicError;

            fn hints(&mut self) -> &mut dyn WalkerHints<'value, 'ctx, VisitorErr, Error = Self::Error> {
                self
            }
        }
    }
}

macro_rules! impl_hints {
    ($walker:ident, [$($protocol:ty),* $(,)?]) => {
        impl<'value, 'ctx: 'value, VisitorErr: 'ctx> WalkerHints<'value, 'ctx, VisitorErr>
            for $walker<'value, 'ctx>
        {
            type Error = BasicError;

            fn protocol(
                &mut self,
                id: ProtocolId,
            ) -> Option<AnyHint<'_, 'value, 'ctx, Self::Error, VisitorErr>> {
                match id {
                    $(id if id == ProtocolId::of::<$protocol>() => Some(AnyHint::new(self)),)?
                    _ => None,
                }
            }
        }
    }
}

pub struct WalkerOnce<'value, 'ctx: 'value>(String, PhantomData<&'value ()>);

impl_walker!(WalkerOnce);

impl<'value, 'ctx: 'value, VisitorErr: 'ctx> WalkerHints<'value, 'ctx, VisitorErr>
    for WalkerOnce<'value, 'ctx>
{
    type Error = BasicError;

    fn protocol(
        &mut self,
        id: ProtocolId,
    ) -> Option<AnyHint<'_, 'value, 'ctx, Self::Error, VisitorErr>> {
        match id {
            id if id == ProtocolId::of::<Str>() => Some(AnyHint::new(self)),
            _ => None,
        }
    }
}

impl<'value, 'ctx: 'value, VisitorErr: 'ctx> Hint<'value, 'ctx, Str, VisitorErr>
    for Walker<'value, 'ctx>
{
    type Error = BasicError;

    fn hint(
        &mut self,
        visitor: &mut dyn crate::Visitor<'value, 'ctx, Self::Error, Error = VisitorErr>,
        _hint: <Str as crate::protocol::Protocol<'value, 'ctx>>::Hint,
    ) -> Result<HintGiven, UniError<Self::Error, VisitorErr>> {
        lookup_visit::<Str, _, _>(visitor)
            .map_err(UniError::Walker)?
            .ok_or_else(|| UniError::Walker(BasicError("visitor is missing the str protocol")))?
            .visit(protocols::str::Data::Value(self.0))?;

        Ok(HintGiven)
    }

    fn known(
        &mut self,
        _hint: &<Str as crate::protocol::Protocol<'value, 'ctx>>::Hint,
    ) -> Result<<Str as crate::protocol::Protocol<'value, 'ctx>>::Known, BasicError> {
        Ok(protocols::str::Known {
            len: Some(self.0.len()),
            kind: Some(protocols::str::Kind::Value),
        })
    }
}

pub struct WalkerMut<'value, 'ctx: 'value>(&'value mut String, PhantomData<&'ctx ()>);
pub struct WalkerRef<'value, 'ctx: 'value>(&'value String, PhantomData<&'ctx ()>);

impl<'value, 'ctx: 'value, VisitorErr: 'ctx> WalkOnce<'value, 'ctx, VisitorErr> for &'ctx str {
    type Error = BasicError;
    type Walker = Walker<'value, 'ctx>;

    fn into_walker(self) -> Self::Walker {
        Walker(self, PhantomData)
    }
}

impl<'value, 'ctx: 'value, VisitorErr: 'ctx> WalkMut<'value, 'ctx, VisitorErr> for &'ctx str {
    type ErrorMut = BasicError;
    type WalkerMut = Walker<'value, 'ctx>;

    fn walker_mut(&'value mut self) -> Self::Walker {
        Walker(self, PhantomData)
    }
}

impl<'value, 'ctx: 'value, VisitorErr: 'ctx> Walk<'value, 'ctx, VisitorErr> for &'ctx str {
    type ErrorRef = BasicError;
    type WalkerRef = Walker<'value, 'ctx>;

    fn walker_ref(&'value self) -> Self::Walker {
        Walker(self, PhantomData)
    }
}

pub struct Visitor<'value>(Option<&'value str>);

impl<'value, 'ctx: 'value, WalkerErr: 'value> crate::Visitor<'value, 'ctx, WalkerErr>
    for Visitor<'value>
{
    type Error = BasicError;

    fn request_hint(
        &mut self,
        hints: &mut dyn WalkerHints<'value, 'ctx, BasicError, Error = WalkerErr>,
    ) -> Result<Option<HintGiven>, UniError<WalkerErr, BasicError>> {
        if let Some(hint) = lookup_hint::<Str, _, _>(hints).map_err(UniError::Visitor)? {
            Ok(Some(hint.hint(
                self,
                protocols::str::Hint {
                    kind: Some(protocols::str::Kind::Value),
                    min_len: None,
                    max_len: None,
                },
            )?))
        } else {
            Ok(None)
        }
    }

    fn protocol(
        &mut self,
        id: ProtocolId,
    ) -> Option<AnyVisit<'_, 'value, 'ctx, WalkerErr, BasicError>> {
        match id {
            id if id == ProtocolId::of::<Str>() => Some(AnyVisit::new(self)),
            _ => None,
        }
    }
}

impl<'value, 'ctx: 'value, WalkerErr: 'value> crate::Visit<'value, 'ctx, Str, WalkerErr>
    for Visitor<'value>
{
    type Error = BasicError;

    fn visit<'walking>(
        &'walking mut self,
        accessor: <Str as Protocol<'value, 'ctx>>::Accessor<'walking, WalkerErr, BasicError>,
    ) -> Result<(), UniError<WalkerErr, BasicError>> {
        match accessor {
            protocols::str::Data::Value(str)
            | protocols::str::Data::Context(str)
            | protocols::str::Data::Static(str) => self.0 = Some(str),
            protocols::str::Data::Walking(_) => {
                return Err(UniError::Visitor(BasicError(
                    "str doesn't live long enough",
                )))
            }
        }
        Ok(())
    }
}

impl<'value, 'ctx: 'value, WalkerErr: 'value> Builder<'value, 'ctx, WalkerErr> for Visitor<'value> {
    type Error = BasicError;

    type Value = &'value str;

    fn init() -> Self
    where
        Self: Sized,
    {
        Visitor(None)
    }

    fn as_visitor(
        &mut self,
    ) -> &mut dyn crate::Visitor<'value, 'ctx, WalkerErr, Error = Self::Error> {
        self
    }

    fn finish(self) -> Result<Self::Value, UniError<WalkerErr, Self::Error>>
    where
        Self: Sized,
    {
        if let Some(str) = self.0 {
            Ok(str)
        } else {
            Err(UniError::Visitor(BasicError("missing str")))
        }
    }
}

impl<'value, 'ctx: 'value, WalkerErr: 'value> Build<'value, 'ctx, WalkerErr> for &'value str {
    type Error = BasicError;

    type Builder = Visitor<'value>;
}