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
169
170
171
172
use std::{collections::VecDeque, ops::ControlFlow};
use treaty::{
    any::{any_trait, static_wrapper::OwnedStatic},
    build, build_with,
    builders::core::array,
    into_walker,
    protocol::{
        visitor::{Sequence, SequenceScope, Value},
        ControlFlowFor, SyncEffect, Visitor,
    },
    Builder, Walk, Walker,
};

#[test]
fn demo() {
    let a = Data::Sequence(vec![
        Data::Bool(true),
        Data::Sequence(vec![Data::Bool(false), Data::Bool(true)]),
        Data::Bool(false),
    ]);

    dbg!(array_to_array2([true, false]));
    dbg!(array_to_array([true, true]));

    let s = build_with::<array::Builder<'_, JsonLike, 3>, _>(into_walker(&a)).unwrap();
    dbg!(s);

    use treaty::builders::serde::deserialize::Builder as SerdeBuilder;

    let x = build_with::<SerdeBuilder<serde_json::Value>, _>(into_walker(&a)).unwrap();
    dbg!(x);

    // let s = build_with::<JsonLike, _>(into_walker(a)).unwrap();

    todo!();
    // assert_eq!(s, "[true,[false,true,],false,]");
}

#[inline(never)]
pub fn array_to_array(x: [bool; 2]) -> [bool; 2] {
    build(into_walker(x)).unwrap()
}

#[inline(never)]
pub fn array_to_array2(x: [bool; 2]) -> [bool; 2] {
    array_to_array(x)
}

#[derive(Debug)]
enum Data {
    Bool(bool),
    Sequence(Vec<Data>),
}

const _: () = {
    struct Impl<'ctx>(&'ctx Data);

    impl<'ctx> From<&'ctx Data> for Impl<'ctx> {
        fn from(value: &'ctx Data) -> Self {
            Self(value)
        }
    }

    impl<'ctx> Walk<'ctx> for &'ctx Data {
        type Walker = Impl<'ctx>;
    }

    impl<'ctx> Walker<'ctx> for Impl<'ctx> {
        type Effect = SyncEffect;

        type Error = ();

        type Output = ();

        fn walk<'a>(
            self,
            visitor: &'a mut Visitor<'a, 'ctx>,
        ) -> ControlFlowFor<'a, SyncEffect, Self::Error, Self::Output> {
            match self.0 {
                Data::Bool(value) => walk_bool(*value, visitor),
                Data::Sequence(value) => walk_vec(value, visitor),
            }
            core::ops::ControlFlow::Continue(())
        }
    }
};

fn walk_bool(value: bool, visitor: &mut Visitor<'_, '_>) {
    visitor
        .upcast_mut::<dyn Value<OwnedStatic<bool>>>()
        .unwrap()
        .visit(OwnedStatic(value));
}

fn walk_vec<'a, 'ctx>(value: &'ctx [Data], visitor: &'a mut Visitor<'a, 'ctx>) {
    struct Scope<'ctx>(VecDeque<&'ctx Data>);

    impl<'ctx> SequenceScope<'ctx> for Scope<'ctx> {
        fn next<'a>(
            &'a mut self,
            visitor: &'a mut Visitor<'a, 'ctx>,
        ) -> ControlFlowFor<'a, SyncEffect, treaty::protocol::visitor::Status> {
            if let Some(value) = self.0.pop_front() {
                into_walker(value).walk(visitor);

                ControlFlow::Continue(treaty::protocol::visitor::Status::Continue)
            } else {
                ControlFlow::Continue(treaty::protocol::visitor::Status::Done)
            }
        }
    }

    let mut scope = Scope(value.into_iter().collect());

    visitor
        .upcast_mut::<dyn Sequence<'_>>()
        .unwrap()
        .visit(&mut scope);
}

#[derive(Default)]
struct JsonLike(String);

impl<'ctx> Builder<'ctx> for JsonLike {
    type Error = ();

    type Value = String;

    fn as_visitor(&mut self) -> &mut Visitor<'_, 'ctx> {
        self
    }

    fn build(self) -> Result<Self::Value, Self::Error> {
        Ok(self.0)
    }

    type Seed = ();

    fn from_seed(seed: Self::Seed) -> Self {
        Self::default()
    }
}

any_trait! {
    impl['a, 'ctx] JsonLike = [
        dyn Value<'a, OwnedStatic<bool>> + 'a,
        dyn Sequence<'ctx> + 'a,
    ]
}

impl Value<'_, OwnedStatic<bool>> for JsonLike {
    fn visit(&mut self, value: OwnedStatic<bool>) -> ControlFlowFor<'_> {
        self.0.push_str(&format!("{}", value.0));
        ControlFlow::Continue(())
    }
}

impl<'ctx> Sequence<'ctx> for JsonLike {
    fn visit<'a>(
        &'a mut self,
        scope: &'a mut dyn SequenceScope<'ctx>,
    ) -> ControlFlowFor<'a, SyncEffect> {
        self.0.push_str("[");
        while let ControlFlow::Continue(treaty::protocol::visitor::Status::Continue) =
            scope.next(self)
        {
            self.0.push_str(",");
        }
        self.0.push_str("]");
        ControlFlow::Continue(())
    }
}