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
use mockall::mock;
use treaty::{
    any::{any_trait, TypeName},
    effect::{Effect, Effective, ErasedEffective},
    protocol::DynWalker,
    protocol::{
        visitor::{
            DynSequenceScope, RequestHint, RequestHintProto, Sequence, SequenceProto,
            SequenceScope, Value, ValueProto, VisitResult,
        },
        DynVisitor,
    },
    Flow,
};

pub type SequenceScopeFactory<E> = for<'a, 'ctx> fn(
    &'ctx (),
    DynSequenceScope<'a, 'ctx, E>,
) -> VisitResult<DynSequenceScope<'a, 'ctx, E>>;

mock! {
    pub SequenceVisitor<E> {
        pub fn visit(&mut self) -> SequenceScopeFactory<E>;
    }
}

any_trait! {
    impl['ctx, E] MockSequenceVisitor<E> = [
        SequenceProto<E>
    ] where
        E: Effect,
}

impl<'ctx, E: Effect> Sequence<'ctx, E> for MockSequenceVisitor<E> {
    fn visit<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        scope: DynSequenceScope<'b, 'ctx, E>,
    ) -> ErasedEffective<'c, VisitResult<DynSequenceScope<'b, 'ctx, E>>, E> {
        E::ready(self.visit()(&(), scope))
    }
}

mock! {
    pub SequenceScope<E> {
        pub fn size_hint(&mut self) -> (usize, Option<usize>);
        pub fn next<'a, 'b, 'ctx>(&'a mut self, visitor: DynVisitor<'b, 'ctx>) -> Flow;
    }
}

impl<'ctx, E: Effect> SequenceScope<'ctx, E> for MockSequenceScope<E> {
    fn size_hint(&mut self) -> ErasedEffective<'_, (usize, Option<usize>), E> {
        E::ready(self.size_hint())
    }

    fn next<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        visitor: DynVisitor<'b, 'ctx>,
    ) -> ErasedEffective<'c, Flow, E> {
        E::ready(self.next(visitor))
    }
}