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
use mockall::mock;
use treaty::{
    any::{any_trait, OwnedStatic, TypeName},
    effect::{blocking::Blocking, Effect, Effective, ErasedEffective, Ss},
    protocol::{
        visitor::{visit_value, Value, ValueProto, VisitResult},
        AsVisitor, DynVisitor,
    },
    Flow,
};

mock! {
    pub ValueVisitor<T: TypeName::MemberType, E>
    where
        for<'a, 'ctx> TypeName::T<'a, 'ctx, T>: Sized
    {
        pub fn visit<'a, 'ctx>(&'a mut self, value: &TypeName::T<'a, 'ctx, T>) -> VisitResult<()>;
    }
}

any_trait! {
    impl['ctx, T, E] MockValueVisitor<T, E> = [
        ValueProto<T, E>
    ] where
        T: TypeName::MemberType + Ss,
        for<'a, 'b> TypeName::T<'a, 'b, T>: Sized,
        E: Effect,
}

impl<'ctx, T: TypeName::MemberType, E: Effect> Value<'ctx, T, E> for MockValueVisitor<T, E>
where
    for<'a, 'lt> TypeName::T<'a, 'lt, T>: Sized,
{
    fn visit<'a>(
        &'a mut self,
        value: TypeName::T<'a, 'ctx, T>,
    ) -> ErasedEffective<'a, VisitResult<TypeName::T<'a, 'ctx, T>>, E>
    where
        TypeName::T<'a, 'ctx, T>: Send,
        'ctx: 'a,
    {
        E::ready(match self.visit(&value) {
            VisitResult::Skipped(_) => VisitResult::Skipped(value),
            VisitResult::Control(flow) => VisitResult::Control(flow),
        })
    }
}

pub trait ValueVisitorExt<'ctx> {
    fn visit_value_and_done<'a, T>(&'a mut self, value: T)
    where
        T: TypeName::LowerType<'a, 'ctx>,
        'ctx: 'a;

    fn visit_value_and_skipped<'a, T>(&'a mut self, value: T)
    where
        T: TypeName::LowerType<'a, 'ctx>,
        'ctx: 'a;
}

impl<'ctx, U> ValueVisitorExt<'ctx> for U
where
    U: AsVisitor<'ctx>,
{
    fn visit_value_and_done<'a, T>(&'a mut self, value: T)
    where
        T: TypeName::LowerType<'a, 'ctx>,
        'ctx: 'a,
    {
        let result = visit_value::<_, Blocking>(self.as_visitor(), value).value();

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

    fn visit_value_and_skipped<'a, T>(&'a mut self, value: T)
    where
        T: TypeName::LowerType<'a, 'ctx>,
        'ctx: 'a,
    {
        let result = visit_value::<_, Blocking>(self.as_visitor(), value).value();

        assert_eq!(result.unit_skipped(), VisitResult::Skipped(()));
    }
}