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
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>(
        &'a mut self,
        scope: DynSequenceScope<'a, 'ctx, E>,
    ) -> ErasedEffective<'a, VisitResult<DynSequenceScope<'a, 'ctx, E>>, E> {
        E::ready(self.visit()(&(), scope)).into_erased()
    }
}

mock! {
    pub SequenceScope<E> {
        pub fn size_hint(&mut self) -> (usize, Option<usize>);
        pub fn next<'a, 'ctx>(&'a mut self, visitor: DynVisitor<'a, '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()).into_erased()
    }

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