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
pub mod builders;

use crate::{
    effect::{Effect, Future},
    protocol::Visitor,
};

/// A buildable type.
pub trait Build<'ctx, M, E: Effect<'ctx>>: BuilderTypes<Value = Self> + Send {
    /// The builder that can be used to build a value of `Self`.
    type Builder: Builder<'ctx, E, Seed = Self::Seed, Error = Self::Error, Value = Self>;
}

pub trait BuilderTypes {
    type Seed: Send;

    /// Error that can happen during filling the builder with data.
    type Error: Send;

    /// Type to be built.
    type Value: Send;
}

/// Builder for a type.
///
/// The `'ctx` lifetime is some lifetime that is longer than the walker.
/// As such, the built value can borrow from other data with a `'ctx` lifetimes.
///
/// A builder allows creating a value of a type [`Self::Value`].
/// The way to use a builder is as follows.
/// - Call [`Default::default()`] to create an instance of the builder.
/// - Call [`Self::as_visitor()`] and give it to a walker's
///     [`walk()`][crate::walk::Walker::walk]. The walker will then fill
///     the builder with data from it's walk.
/// - Call [`Self::build()`] to finish building the value and get any errors
///     that happened during filling it with data.
pub trait Builder<'ctx, E: Effect<'ctx>>: BuilderTypes + Sized + Send {
    fn from_seed<'a>(seed: Self::Seed) -> Future<'a, 'ctx, Self, E>
    where
        Self: 'a;

    /// Finish the value.
    ///
    /// If an error happened with the builder during the walk
    /// it will be reported here.
    fn build<'a>(self) -> Future<'a, 'ctx, Result<Self::Value, Self::Error>, E>
    where
        Self: 'a;

    /// Get the builder as a visitor that a walker can use.
    ///
    /// This is expected to just be `self`.
    fn as_visitor(&mut self) -> Visitor<'_, 'ctx>;
}

#[cfg(test)]
pub mod test {
    use std::{
        collections::HashMap,
        sync::{Mutex, MutexGuard, PoisonError},
    };

    use crate::{
        any::{
            static_wrapper::{DynOwnedStatic, OwnedStatic},
            AnyTrait, Boxed, Indirect, IndirectLtAny, LtTypeId, MaybeSized, Mut, Ref, TypeName,
        },
        effect::{BlockOn, Blocking, Spin},
        protocol::visitor::value::{DynValue, Value},
        Flow,
    };

    use super::*;

    use mockall::mock;
    use mockall::predicate::eq;

    pub mod mock {
        use std::collections::HashMap;

        use super::*;

        mock! {
            pub Builder<Seed: 'static, Value: 'static, Error: 'static> {
                pub fn private_from_seed(seed: Seed) -> Self;
                pub fn private_build(self) -> Result<Value, Error>;

                // pub fn private_upcast_to_id(&self, id: LtTypeId<'static>) -> Option<Box<dyn for<'a> MockIndirect>>;
                // pub fn private_upcast_to_id_mut(&mut self, id: LtTypeId<'static>) -> Option<Box<dyn for<'a> MockIndirect<'a, Mut>>>;

                pub fn lookup(&self) -> &HashMap<LtTypeId<'static>, Box<dyn MockIndirect>>;
                pub fn lookup_mut(&mut self) -> &mut HashMap<LtTypeId<'static>, Box<dyn MockIndirect>>;
            }
        }
    }

    pub type MockBuilder<Seed = (), Value = (), Error = ()> = mock::MockBuilder<Seed, Value, Error>;

    pub trait MockIndirect: Send {
        fn indirect(&self) -> IndirectLtAny<'_, 'static, Ref>;
        fn indirect_mut<'a: 'b, 'b>(&'a mut self) -> IndirectLtAny<'b, 'static, Mut>;
    }

    impl MockIndirect for MockValueVisitor {
        fn indirect(&self) -> IndirectLtAny<'_, 'static, Ref> {
            IndirectLtAny::new::<DynValue<'static, DynOwnedStatic<'static, i32>, Blocking>>(
                self as _,
            )
        }

        fn indirect_mut<'a: 'b, 'b>(&'a mut self) -> IndirectLtAny<'b, 'static, Mut> {
            IndirectLtAny::<'b, 'static, Mut>::new::<
                DynValue<'static, DynOwnedStatic<'static, i32>, Blocking>,
            >(self as _)
        }
    }

    // pub struct IndirectBox<'a, 'ctx>(IndirectLtAny<'a, 'ctx, Boxed>);
    //
    // impl MockIndirect for IndirectBox
    // {
    //     fn indirect<'a>(&'a self) -> IndirectLtAny<'a, 'static, Ref> {
    //         // let x: &MaybeSized::T<'a, 'static, T> = &*self.0;
    //         // IndirectLtAny::<'a, 'static, Ref>::new::<T>(x)
    //         todo!()
    //     }
    //
    //     fn indirect_mut<'a>(&'a mut self) -> IndirectLtAny<'a, 'static, Mut> {
    //         let x: &'a mut MaybeSized::T<'static, 'static, T> = &mut *self.0;
    //         IndirectLtAny::<'static, 'static, Mut>::new::<T>(x)
    //     }
    // }

    impl<Seed: Send, Value: Send, Error: Send> BuilderTypes for MockBuilder<Seed, Value, Error> {
        type Seed = Seed;

        type Error = Error;

        type Value = Value;
    }

    impl<Seed, Value, Error> MockBuilder<Seed, Value, Error> {
        pub fn lock_context<'a>() -> MutexGuard<'a, ()> {
            static LOCK: Mutex<()> = Mutex::new(());
            LOCK.lock().unwrap_or_else(PoisonError::into_inner)
        }
    }

    impl<Seed: Send, Value: Send, Error: Send, E: Effect<'static>> Builder<'static, E>
        for MockBuilder<Seed, Value, Error>
    {
        fn from_seed<'a>(seed: Self::Seed) -> Future<'a, 'static, Self, E>
        where
            Self: 'a,
        {
            E::ready(Self::private_from_seed(seed))
        }

        fn build<'a>(self) -> Future<'a, 'static, Result<Self::Value, Self::Error>, E>
        where
            Self: 'a,
        {
            E::ready(self.private_build())
        }

        fn as_visitor(&mut self) -> Visitor<'_, 'static> {
            self
        }
    }

    impl<Seed, Value, Error> AnyTrait<'static> for MockBuilder<Seed, Value, Error> {
        fn upcast_to_id<'a>(
            &'a self,
            id: LtTypeId<'static>,
        ) -> Option<IndirectLtAny<'a, 'static, Ref>>
        where
            'static: 'a,
        {
            self.lookup().get(&id).map(|x| x.indirect())
        }

        fn upcast_to_id_mut<'a: 'b, 'b>(
            &'a mut self,
            id: LtTypeId<'static>,
        ) -> Option<IndirectLtAny<'b, 'static, Mut>>
        where
            'static: 'a,
        {
            self.lookup_mut().get_mut(&id).map(|x| x.indirect_mut())
        }
    }

    mock! {
        ValueVisitor {
            fn private_visit(&mut self, value: OwnedStatic<i32>) -> Flow;
        }
    }

    impl<'ctx, E: Effect<'ctx>> Value<'ctx, DynOwnedStatic<'ctx, i32>, E> for MockValueVisitor {
        fn visit<'a>(&'a mut self, value: OwnedStatic<i32>) -> Future<'a, 'ctx, Flow, E> {
            E::ready(self.private_visit(value))
        }
    }

    #[test]
    fn demo2() {
        let _lock = MockBuilder::<(), (), ()>::lock_context();
        let ctx = MockBuilder::<(), (), ()>::private_from_seed_context();

        // Expect one mock builder to be made from the from_seed static method;
        ctx.expect().once().returning(|_| {
            let mut mock = MockBuilder::new();

            // Expect that we will lookup one trait on the visitor.
            mock.expect_lookup_mut().once().returning(|| {
                let mut map = HashMap::<LtTypeId<'static>, Box<dyn MockIndirect>>::new();

                let mut mock = MockValueVisitor::new();

                // Expect that we will pass the value 42 to the visitor.
                mock.expect_private_visit()
                    .once()
                    .with(eq(OwnedStatic(42)))
                    .return_const(Flow::Done);

                // This mock will be for the value protocol.
                map.insert(
                    LtTypeId::of::<DynValue<'static, DynOwnedStatic<'static, i32>, Blocking>>(),
                    Box::new(mock),
                );

                map
            });

            mock.expect_private_build().once().returning(|| Ok(()));

            mock
        });

        // Create a mock builder using the static from_seed method.
        let mut builder = Spin::block_on(<MockBuilder as Builder<Blocking>>::from_seed(()));

        {
            // Get the mock builder as a visitor.
            let visitor = <MockBuilder as Builder<Blocking>>::as_visitor(&mut builder);

            // Upcast the mock visitor into a trait object for the value protocol.
            // This resolves to one of the values in the hashmap above and not the builder itself.
            let x = visitor
                .upcast_to_id_mut(LtTypeId::of::<
                    DynValue<'_, DynOwnedStatic<'_, i32>, Blocking>,
                >())
                .unwrap();

            // Finish the upcast by downcasting to the trait object.
            let Ok(x) = x.downcast::<DynValue<'_, DynOwnedStatic<'_, i32>, Blocking>>() else {
                panic!();
            };

            // Use the visit method on the mock visitor.
            assert_eq!(Spin::block_on(x.visit(OwnedStatic(42))), Flow::Done);
        }

        // Finish building the mock builder.
        assert_eq!(
            Spin::block_on(<MockBuilder as Builder<Blocking>>::build(builder)).unwrap(),
            ()
        );
    }
}