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
use mockall::mock;
use treaty::{
    any::{any_trait, TypeName},
    effect::{blocking::Blocking, Effect, Effective, ErasedEffective},
    protocol::{
        visitor::{
            visit_recoverable, visit_sequence, DynRecoverableScope, DynSequenceScope, Recoverable,
            RecoverableProto, RecoverableScope, RequestHint, RequestHintProto, Sequence,
            SequenceProto, SequenceScope, Value, ValueProto, VisitResult,
        },
        AsVisitor, DynVisitor, DynWalker,
    },
    Flow, Status,
};

mock! {
    pub RecoverableVisitor<E> {
        pub fn visit<'a, 'ctx>(&mut self, scope: DynRecoverableScope<'a, 'ctx, E>) -> VisitResult;
    }
}

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

impl<'ctx, E: Effect> Recoverable<'ctx, E> for MockRecoverableVisitor<E> {
    fn visit<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, E>,
    ) -> ErasedEffective<'a, VisitResult, E> {
        E::ready(self.visit(scope))
    }
}

mock! {
    pub RecoverableScopeVisitor<E> {
        pub fn new_walk<'a, 'b, 'ctx>(&'a mut self, visitor: DynVisitor<'b, 'ctx>) -> Status;
    }
}

impl<'ctx, E: Effect> RecoverableScope<'ctx, E> for MockRecoverableScopeVisitor<E> {
    fn new_walk<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        visitor: DynVisitor<'b, 'ctx>,
    ) -> ErasedEffective<'c, Status, E> {
        E::ready(self.new_walk(visitor))
    }
}

pub trait RecoverableVisitorExt<'ctx> {
    fn visit_recoverable_and_done<'a>(&'a mut self, scope: DynRecoverableScope<'a, 'ctx, Blocking>);
}

impl<'ctx, T> RecoverableVisitorExt<'ctx> for T
where
    T: AsVisitor<'ctx>,
{
    fn visit_recoverable_and_done<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, Blocking>,
    ) {
        let result = visit_recoverable(self.as_visitor(), scope).value();

        assert_eq!(result, VisitResult::Control(Flow::Done));
    }
}