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
use treaty::{
    any::{OwnedStatic, TempBorrowedStatic},
    builders::{self, core::r#struct::StructBuilder},
    effect::{Blocking, Effect, Future, ReadyValue},
    protocol::{
        visitor::{tags, visit_sequence, visit_tag, visit_value, TagConst, VisitResult},
        DynVisitor,
    },
    transform,
    walkers::{
        self,
        core::{
            noop::NoopWalker,
            r#struct::{StaticType, StructWalker},
        },
    },
    Build, Builder, DefaultMode, Flow, Walk, Walker,
};

use crate::common::{
    protocol::sequence::MockSequenceScope,
    walker::MockWalker,
};

mod common;

#[derive(Debug, PartialEq)]
struct X {
    a: bool,
    b: bool,
}

struct Info;

impl<'ctx, M> walkers::core::r#struct::StructTypeInfo<'ctx, M> for Info {
    const NAME: &'static str = "X";

    const FIELDS: &'static [&'static str] = &["a", "b"];

    type FieldError = ();

    type S = StaticType;

    type T = X;

    fn walk_field<'a, E: Effect>(
        index: usize,
        value: &'ctx Self::T,
        visitor: DynVisitor<'a, 'ctx>,
    ) -> Future<'a, Result<Flow, Self::FieldError>, E> {
        E::wrap(async move {
            match index {
                0 => {
                    let walker = <&bool as Walk<M, E>>::into_walker(&value.a);

                    assert_eq!(Walker::<E>::walk(walker, visitor).await, Ok(()));

                    Ok(Flow::Continue)
                }
                1 => {
                    let walker = <&bool as Walk<M, E>>::into_walker(&value.b);

                    assert_eq!(Walker::<E>::walk(walker, visitor).await, Ok(()));

                    Ok(Flow::Continue)
                }
                _ => Ok(Flow::Done),
            }
        })
    }
}

struct Fields<'ctx, M, E: Effect> {
    a: <bool as Build<'ctx, M, E>>::Builder,
    b: <bool as Build<'ctx, M, E>>::Builder,
}

#[derive(Copy, Clone)]
enum FieldMarker {
    A,
    B,
}

impl<'ctx, M, E: Effect> builders::core::r#struct::StructTypeInfo<'ctx, M, E> for Info {
    type Builders = Fields<'ctx, M, E>;

    type FieldMarker = FieldMarker;

    type T = X;

    fn marker_from_index(index: usize) -> Option<Self::FieldMarker> {
        match index {
            0 => Some(FieldMarker::A),
            1 => Some(FieldMarker::B),
            _ => None,
        }
    }

    fn marker_from_name(name: &str) -> Option<Self::FieldMarker> {
        match name {
            "a" => Some(FieldMarker::A),
            "b" => Some(FieldMarker::B),
            _ => None,
        }
    }

    type Error = ();

    fn from_builders<'a>(builders: Self::Builders) -> Future<'a, Result<Self::T, Self::Error>, E> {
        E::wrap(async {
            Ok(X {
                a: builders.a.build().await.unwrap(),
                b: builders.b.build().await.unwrap(),
            })
        })
    }

    fn as_visitor<'a>(
        marker: Self::FieldMarker,
        builders: &'a mut Self::Builders,
    ) -> DynVisitor<'a, 'ctx> {
        match marker {
            FieldMarker::A => builders.a.as_visitor(),
            FieldMarker::B => builders.b.as_visitor(),
        }
    }

    type Seed = ();

    fn new_builders<'a>(_seed: Self::Seed) -> Future<'a, Self::Builders, E> {
        E::wrap(async {
            Fields {
                a: Builder::<E>::from_seed(()).await,
                b: Builder::<E>::from_seed(()).await,
            }
        })
    }
}

#[test]
#[ignore]
fn demo() {
    let value = X { a: true, b: false };

    let (other, _) = transform::<StructBuilder<Info, DefaultMode, _>, _, Blocking>(
        (),
        StructWalker::<Info, _, DefaultMode, _>::new(&value),
    )
    .value();

    assert_eq!(other.unwrap(), value);
}

#[test]
fn from_basic_tuple_like() {
    // A tuple like is just a sequence.
    let mut scope = MockSequenceScope::<Blocking>::new();

    // First field.
    scope.expect_next().once().returning(|visitor| {
        // Visit a bool value.
        //
        // The bool visitor should report that it is done.
        assert_eq!(
            visit_value::<_, Blocking>(visitor, OwnedStatic(true)).value(),
            VisitResult::Control(Flow::Done)
        );

        // We have another field.
        Flow::Continue
    });

    // Second field.
    scope.expect_next().once().returning(|visitor| {
        // Visit a bool value.
        //
        // The bool visitor should report that it is done.
        assert_eq!(
            visit_value::<_, Blocking>(visitor, OwnedStatic(false)).value(),
            VisitResult::Control(Flow::Done)
        );

        // No more fields.
        Flow::Done
    });

    let mut builder = StructBuilder::<Info, DefaultMode, Blocking>::from_seed(()).value();
    let visitor = builder.as_visitor();

    // Visit the sequence of field values.
    assert!(matches!(
        visit_sequence(visitor, &mut scope).value(),
        VisitResult::Control(Flow::Done)
    ));

    assert_eq!(builder.build().value().unwrap(), X { a: true, b: false });
}

#[test]
fn from_basic_map_like() {
    // A map is built from a sequence.
    let mut scope = MockSequenceScope::<Blocking>::new();

    // Here we do the b field first to show a map-like doesn't care about order.
    scope.expect_next().once().returning(|mut visitor| {
        let mut walker = MockWalker::<(), ()>::new();

        // We need to give the b field name in the key tag.
        walker.expect_walk().once().returning(|visitor| {
            assert_eq!(
                visit_value::<_, Blocking>(visitor, TempBorrowedStatic("b")).value(),
                VisitResult::Control(Flow::Done)
            );

            Ok(())
        });

        // Tag the value with a key as the field name.
        assert_eq!(
            visit_tag::<tags::Key, Blocking, _>(TagConst, visitor.cast(), walker).value(),
            Ok(VisitResult::Control(Flow::Continue)),
        );

        // Visit the value as normal.
        assert_eq!(
            visit_value::<_, Blocking>(visitor, OwnedStatic(true)).value(),
            VisitResult::Control(Flow::Done)
        );

        // There is another field.
        Flow::Continue
    });

    // The other field.
    scope.expect_next().once().returning(|mut visitor| {
        let mut walker = MockWalker::<(), ()>::new();

        // Here we do field a.
        walker.expect_walk().once().returning(|visitor| {
            assert_eq!(
                visit_value::<_, Blocking>(visitor, TempBorrowedStatic("a")).value(),
                VisitResult::Control(Flow::Done)
            );

            Ok(())
        });

        // Tag the value with a key.
        assert_eq!(
            visit_tag::<tags::Key, Blocking, _>(TagConst, visitor.cast(), walker).value(),
            Ok(VisitResult::Control(Flow::Continue)),
        );

        // The field value.
        assert_eq!(
            visit_value::<_, Blocking>(visitor, OwnedStatic(false)).value(),
            VisitResult::Control(Flow::Done)
        );

        // The sequence protocol allows for us to wait to decide if there is another item.
        Flow::Continue
    });

    // There are no more fields.
    scope.expect_next().once().returning(|_visitor| Flow::Done);

    let mut builder = StructBuilder::<Info, DefaultMode, Blocking>::from_seed(()).value();
    let mut visitor = builder.as_visitor();

    // We need to provide the map tag to the struct before getting into the sequence.
    // This tag notifies the struct builder to expect the sequence as a map.
    assert_eq!(
        visit_tag::<tags::Map, Blocking, _>(TagConst, visitor.cast(), NoopWalker::new()).value(),
        Ok(VisitResult::Control(Flow::Continue))
    );

    // Visit the sequence of fields.
    assert_eq!(
        visit_sequence(visitor, &mut scope).value(),
        VisitResult::Control(Flow::Done)
    );

    // The struct is built as the mock walker above makes it.
    assert_eq!(builder.build().value().unwrap(), X { a: false, b: true });
}