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
use crate::{
    effect::{Effect, Future},
    never::Never,
    protocol::{
        visitor::{visit_tag, TagConst, TagError, TagKind, VisitResult},
        Visitor,
    },
    walkers::core::noop::NoopWalker,
    Flow, WalkerTypes, TAG_KEY, TAG_KEY_VALUE, TAG_VALUE,
};

pub struct KeyValueWalker<T, K, V> {
    key_walker: K,
    value_walker: V,
    tag: T,
}

impl<T, K, V> KeyValueWalker<T, K, V> {
    pub fn new(tag: T, key_walker: K, value_walker: V) -> Self {
        Self {
            key_walker,
            value_walker,
            tag,
        }
    }
}

#[derive(Debug)]
#[allow(unused)]
enum KeyValueErrorKind<K, V> {
    Tag(TagError<Never>),
    Key(K),
    Value(V),
}

#[derive(Debug)]
pub struct KeyValueError<K, V>(KeyValueErrorKind<K, V>);

impl<T, K, V> WalkerTypes for KeyValueWalker<T, K, V>
where
    K: WalkerTypes,
    V: WalkerTypes,
{
    type Error = KeyValueError<K::Error, V::Error>;

    type Output = ();
}

impl<'ctx, T, K, V, E> crate::Walker<'ctx, E> for KeyValueWalker<T, K, V>
where
    E: Effect,
    T: TagKind,
    K: crate::Walker<'ctx, E>,
    V: crate::Walker<'ctx, E>,
{
    fn walk<'a>(
        self,
        visitor: Visitor<'a, 'ctx>,
    ) -> Future<'a, Result<Self::Output, Self::Error>, E>
    where
        Self: 'a,
    {
        E::wrap(async move {
            match visit_tag::<T, E, _>(self.tag, visitor, NoopWalker::new()).await {
                Ok(VisitResult::Skipped(_)) => {
                    match visit_tag::<TagConst<{ TAG_KEY_VALUE.to_int() }>, E, _>(
                        TagConst,
                        visitor,
                        NoopWalker::new(),
                    )
                    .await
                    {
                        Ok(VisitResult::Skipped(_) | VisitResult::Control(Flow::Continue)) => {}
                        Ok(VisitResult::Control(_flow)) => return Ok(()),
                        Err(_) => todo!(),
                    }
                }
                Ok(VisitResult::Control(Flow::Continue)) => {}
                Ok(VisitResult::Control(_flow)) => todo!(),
                Err(_) => todo!(),
            }

            match visit_tag::<TagConst<{ TAG_KEY.to_int() }>, E, _>(
                TagConst,
                visitor,
                self.key_walker,
            )
            .await
            {
                Ok(VisitResult::Skipped(_) | VisitResult::Control(Flow::Continue)) => {}
                Ok(VisitResult::Control(_flow)) => return Ok(()),
                Err(_) => todo!(),
            }

            match visit_tag::<TagConst<{ TAG_VALUE.to_int() }>, E, _>(
                TagConst,
                visitor,
                self.value_walker,
            )
            .await
            {
                Ok(VisitResult::Control(Flow::Continue)) => {}
                Ok(VisitResult::Skipped(value_walker)) => {
                    // Fallback to just walking the value.
                    match value_walker.walk(visitor).await {
                        Ok(_) => {}
                        Err(_err) => todo!(),
                    }
                }
                Ok(VisitResult::Control(_flow)) => todo!(),
                Err(_) => todo!(),
            }

            Ok(())
        })
    }
}