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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
pub mod hint {
    use crate::protocol::{Implementer, Protocol};

    /// Protocol for requesting a visitor give a hint.
    ///
    /// This protocol is implemented by visitors.
    ///
    /// Some walkers don't know what the data they are walking actually represents.
    /// This protocol allows such walkers to request the visitor to give a hint of what
    /// it is expecting.
    ///
    /// This can also be paired with the [`Recoverable`][super::recoverable::Recoverable]
    /// protocol to place the walker in a recoverable mode for the visitor to try multiple
    /// protocols.
    pub enum RequestHint {}

    /// Object implementing the [`RequestHint`] protocol.
    pub trait RequestHintObject<'ctx> {
        /// Call this to request a hint.
        ///
        /// `hints` is expected to be the walker. This is what the visitor (`Self`)
        /// will call to give a hint using the [`Hint`] protocol.
        ///
        /// `needs_hints` can be `false` if the walker can continue without a hint.
        fn request_hint(&mut self, hints: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    impl Protocol for RequestHint {
        type Object<'a, 'ctx: 'a> = &'a mut dyn RequestHintObject<'ctx>;
    }
}

pub mod recoverable {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    /// Protocol for providing a recoverable walker.
    ///
    /// This protocol is implemented by visitors.
    pub enum Recoverable {}

    /// Implemented by walkers that can be reset.
    pub trait RecoverableWalker<'ctx> {
        /// Start a new walk with the walker reset to it's starting state.
        fn new_walk(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    /// Object implementing the [`Recoverable`] protocol.
    pub trait Object<'ctx> {
        /// Visit with a recoverable walker.
        ///
        /// The visitor can then use the `walker` to try multiple different ways
        /// of walking.
        fn visit(&mut self, walker: &mut dyn RecoverableWalker<'ctx>) -> Result<(), ()>;
    }

    impl Protocol for Recoverable {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx>;
    }

    impl Meta for Recoverable {
        type Known<'a, 'ctx: 'a> = ();

        type Hint<'a, 'ctx: 'a> = ();
    }
}

pub mod owned {
    use core::marker::PhantomData;

    use crate::{protocol::Protocol, walk::protocols::hint::Meta};

    pub struct Owned<T: 'static>(PhantomData<fn() -> T>);

    pub trait Object<'ctx, T: 'static> {
        fn visit(&mut self, value: T) -> Result<(), ()>;
    }

    impl<T: 'static> Protocol for Owned<T> {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx, T>;
    }

    impl<T: 'static> Meta for Owned<T> {
        type Known<'a, 'ctx: 'a> = ();

        type Hint<'a, 'ctx: 'a> = ();
    }
}

pub mod borrowed {
    use core::marker::PhantomData;

    use crate::{protocol::Protocol, walk::protocols::hint::Meta};

    pub struct Borrowed<T: 'static>(PhantomData<fn() -> T>);

    pub enum Value<'a, 'ctx, T: 'static> {
        Temp(&'a T),
        Context(&'ctx T),
        Static(&'static T),
    }

    pub trait Object<'ctx, T: 'static> {
        fn visit(&mut self, value: Value<'_, 'ctx, T>) -> Result<(), ()>;
    }

    impl<T: 'static> Protocol for Borrowed<T> {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx, T>;
    }

    pub enum Kind {
        Temp,
        Context,
        Static,
    }

    pub struct Known {
        pub kind: Option<Kind>,
    }

    pub struct Hint {
        pub kind: Option<Kind>,
    }

    impl<T: 'static> Meta for Borrowed<T> {
        type Known<'a, 'ctx: 'a> = Known;

        type Hint<'a, 'ctx: 'a> = Hint;
    }
}

pub mod borrowed_mut {
    use core::marker::PhantomData;

    use crate::{protocol::Protocol, walk::protocols::hint::Meta};

    pub struct BorrowedMut<T: 'static>(PhantomData<fn() -> T>);

    pub enum Value<'a, 'ctx, T: 'static> {
        Temp(&'a mut T),
        Context(&'ctx mut T),
        Static(&'static mut T),
    }

    pub trait Object<'ctx, T: 'static> {
        fn visit(&mut self, value: Value<'_, 'ctx, T>) -> Result<(), ()>;
    }

    impl<T: 'static> Protocol for BorrowedMut<T> {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx, T>;
    }

    pub enum Kind {
        Temp,
        Context,
        Static,
    }

    pub struct Known {
        pub kind: Option<Kind>,
    }

    pub struct Hint {
        pub kind: Option<Kind>,
    }

    impl<T: 'static> Meta for BorrowedMut<T> {
        type Known<'a, 'ctx: 'a> = Known;

        type Hint<'a, 'ctx: 'a> = Hint;
    }
}

pub mod tagged {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    pub enum Tagged {}

    pub trait TaggedWalker<'ctx> {
        fn walk_tag(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;

        fn walk_value(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    pub trait Object<'ctx> {
        fn visit(&mut self, walker: &mut dyn TaggedWalker<'ctx>) -> Result<(), ()>;
    }

    impl Protocol for Tagged {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx>;
    }

    impl Meta for Tagged {
        type Known<'a, 'ctx: 'a> = ();

        type Hint<'a, 'ctx: 'a> = ();
    }
}

pub mod sequence {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    pub enum Sequence {}

    #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
    pub enum Status {
        Done,
        Continue,
    }

    pub trait SequenceWalker<'ctx> {
        fn next(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<Status, ()>;
    }

    pub trait Object<'ctx> {
        fn visit(&mut self, walker: &mut dyn SequenceWalker<'ctx>) -> Result<(), ()>;
    }

    impl Protocol for Sequence {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx>;
    }

    #[derive(Default)]
    pub struct Known {
        pub len: (usize, Option<usize>),
    }

    pub struct Hint {
        pub len: (usize, Option<usize>),
    }

    impl Meta for Sequence {
        type Known<'a, 'ctx: 'a> = Known;

        type Hint<'a, 'ctx: 'a> = Hint;
    }
}

pub mod map {
    use crate::{
        protocol::{Implementer, Protocol},
        walk::protocols::hint::Meta,
    };

    pub enum Map {}

    #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
    pub enum Status {
        Done,
        Continue,
    }

    pub trait MapWalker<'ctx> {
        fn next_key(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<Status, ()>;

        fn value(&mut self, visitor: &mut dyn Implementer<'ctx>) -> Result<(), ()>;
    }

    pub trait Object<'ctx> {
        fn visit(&mut self, walker: &mut dyn MapWalker<'ctx>) -> Result<(), ()>;
    }

    impl Protocol for Map {
        type Object<'a, 'ctx: 'a> = &'a mut dyn Object<'ctx>;
    }

    #[derive(Default)]
    pub struct Known {
        pub len: (usize, Option<usize>),
    }

    pub struct Hint {
        pub len: (usize, Option<usize>),
    }

    impl Meta for Map {
        type Known<'a, 'ctx: 'a> = Known;

        type Hint<'a, 'ctx: 'a> = Hint;
    }
}