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
use core::{any::TypeId, marker::PhantomData, ops::ControlFlow};

use crate::{
    any::{static_wrapper::OwnedStatic, LtTypeId},
    any_trait,
    effect::{Effect, Future},
    never::Never,
    protocol::{
        self,
        visitor::{
            request_hint::RequestHint,
            tag::{Dyn, Tag, DynTag},
            value::Value, sequence::{DynSequence, Sequence, SequenceFlow},
        },
    },
    DynWalker, Flow,
};

pub struct Visitor<E>(usize, PhantomData<fn() -> E>);

any_trait! {
    impl['a, 'ctx, E] Visitor<E> = [
        dyn RequestHint<'ctx, Effect = E> + 'a,
        DynTag<'a, 'ctx, Dyn, E>,
        dyn Value<'a, 'ctx, OwnedStatic<&'static str>, Effect = E> + 'a,
        dyn Value<'a, 'ctx, OwnedStatic<TypeId>, Effect = E> + 'a,
        dyn Value<'a, 'ctx, OwnedStatic<usize>, Effect = E> + 'a,
        DynSequence<'a, 'ctx, E>,
    ] else fallback where E: Effect<'ctx>
}

fn fallback(id: LtTypeId<'_>) {
    println!("Unknown trait: {}", id);
}

impl<'ctx, E: Effect<'ctx>> Visitor<E> {
    pub fn new() -> Self {
        Self(0, PhantomData)
    }

    fn tab(&self) {
        for _ in 0..self.0 {
            print!("  ");
        }
    }
}

impl<'ctx, E: Effect<'ctx>> RequestHint<'ctx> for Visitor<E> {
    type Effect = E;

    fn request_hint<'a>(
        &'a mut self,
        _walker: crate::protocol::Walker<'a, 'ctx>,
    ) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
    where
        Self: 'a,
    {
        self.tab();
        println!("Visit request hint (no hint given)");
        E::wrap(async { ControlFlow::Continue(()) })
    }
}

impl<'ctx, E: Effect<'ctx>> Tag<'ctx, Dyn> for Visitor<E> {
    type Effect = E;

    fn visit<'a>(
        &'a mut self,
        kind: Dyn,
        walker: DynWalker<'a, 'ctx, Self::Effect>,
    ) -> Future<'a, 'ctx, Flow<Never>, Self::Effect>
    where
        Self: 'a,
    {
        self.tab();
        println!("Visit tag: {}", kind.0);

        E::wrap(async {
            self.0 += 1;
            let result = walker.walk(self).await;
            self.0 -= 1;
            match result {
                ControlFlow::Continue(()) => Flow::Continue,
                ControlFlow::Break(()) => Flow::Break,
            }
        })
    }
}

impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<&'static str>> for Visitor<E> {
    type Effect = E;

    fn visit(
        &'a mut self,
        OwnedStatic(value): OwnedStatic<&'static str>,
    ) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
    where
        Self: 'a,
    {
        self.tab();
        println!("Visit static str: {:?}", value);
        E::wrap(async { ControlFlow::Continue(()) })
    }
}

impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<usize>> for Visitor<E> {
    type Effect = E;

    fn visit(
        &'a mut self,
        OwnedStatic(value): OwnedStatic<usize>,
    ) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
    where
        Self: 'a,
    {
        self.tab();
        println!("Visit usize: {}", value);
        E::wrap(async { ControlFlow::Continue(()) })
    }
}

impl<'a, 'ctx: 'a, E: Effect<'ctx>> Value<'a, 'ctx, OwnedStatic<TypeId>> for Visitor<E> {
    type Effect = E;

    fn visit(
        &'a mut self,
        OwnedStatic(value): OwnedStatic<TypeId>,
    ) -> Future<'a, 'ctx, ControlFlow<(), ()>, Self::Effect>
    where
        Self: 'a,
    {
        self.tab();
        println!("Visit type ID: {:?}", value);
        E::wrap(async { ControlFlow::Continue(()) })
    }
}

impl<'ctx, E: Effect<'ctx>> Sequence<'ctx> for Visitor<E> {
    type Effect = E;

    fn visit<'a>(
        &'a mut self,
        scope: protocol::visitor::sequence::DynSequenceScope<'a, 'ctx, Self::Effect>,
    ) -> Future<'a, 'ctx, Flow<Never>, Self::Effect> {
        self.tab();
        E::wrap(async {
            println!("Visit sequence, size hint: {:?}", scope.size_hint().await);
            self.0 += 1;
            let flow = loop {
                self.tab();
                println!("Calling next");
                self.0 += 1;
                match scope.next(self).await {
                    SequenceFlow::Done => {
                        self.tab();
                        println!("Sequence done");
                        break Flow::Continue
                    },
                    SequenceFlow::Continue => {},
                    SequenceFlow::Break => break Flow::Break,
                }
                self.0 -= 1;
            };
            self.0 -= 2;
            flow
        })
    }
}