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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use core::fmt::{Debug, Display};

use crate::any::{OwnedStatic, TempBorrowedStatic, TempBorrowedStaticHrt};
use crate::effect::{EffectExt, Effective, EffectiveExt};
use crate::protocol::visitor::{DynRecoverableScope, Recoverable, RecoverableProto};
use crate::{
    any::{AnyTraitObject, TypeName, TypeNameId},
    any_trait,
    effect::{Effect, ErasedEffective, Ss},
    hkt::Invariant,
    protocol::{
        visitor::{tags, Tag, TagProto, Value, ValueProto, VisitResult},
        DynVisitor,
    },
    Builder, BuilderTypes, DynWalkerObjSafe, Flow,
};

pub struct EnumBuilder<'ctx, Info, Mode, E: Effect>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    inner: Inner<'ctx, Info, Mode, E>,
}

enum Inner<'ctx, Info, Mode, E: Effect>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    Temp,
    Seed(Info::Seed),
    Builder { builder: Info::Builders },
    Value(Result<Info::T, Info::Error>),
}

pub trait EnumBuildInfo<'ctx, Mode, E: Effect> {
    type Builders: Ss;

    type Seed: Ss;

    type Error: Ss + Debug + Display;

    type ValueT: TypeName::MemberType;

    type T: Ss;

    type VariantMarker: Ss + Copy + Display;

    fn new_builder<'a>(
        seed: Self::Seed,
        variant: Self::VariantMarker,
    ) -> ErasedEffective<'a, Self::Builders, E>;

    fn finish_builder<'a>(
        builder: Self::Builders,
    ) -> ErasedEffective<'a, Result<Self::T, Self::Error>, E>;

    fn from_value<'a>(value: TypeName::T<'a, 'ctx, Self::ValueT>) -> Self::T;

    fn as_visitor<'a>(builder: &'a mut Self::Builders) -> DynVisitor<'a, 'ctx>;

    fn marker_from_name(name: &str) -> Option<Self::VariantMarker>;

    fn marker_from_discriminant(discriminant: u32) -> Option<Self::VariantMarker>;

    fn guess_variant<'a>(
        seed: Self::Seed,
        scope: DynRecoverableScope<'a, 'ctx, E>,
    ) -> ErasedEffective<'a, Result<Self::T, Self::Error>, E>;
}

impl<'ctx, Info, Mode, E: Effect> BuilderTypes for EnumBuilder<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    type Seed = Info::Seed;

    type Error = Info::Error;

    type Value = Info::T;
}

impl<'ctx, Info, Mode, E: Effect> Builder<'ctx, E> for EnumBuilder<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn from_seed<'a>(seed: Self::Seed) -> ErasedEffective<'a, Self, E>
    where
        Self: 'a,
    {
        E::ready(Self {
            inner: Inner::Seed(seed),
        })
    }

    fn build<'a>(self) -> ErasedEffective<'a, Result<Self::Value, Self::Error>, E>
    where
        Self: 'a,
    {
        match self.inner {
            Inner::Temp => unreachable!(),
            Inner::Seed(seed) => {
                // what to do...
                todo!()
            }
            Inner::Builder { builder } => Info::finish_builder(builder),
            Inner::Value(value) => E::ready(value),
        }
    }

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

any_trait! {
    impl['ctx, Info, Mode, E] EnumBuilder<'ctx, Info, Mode, E> = [
        ValueProto<Info::ValueT, E>,
        TagProto<tags::Variant, E>,
        RecoverableProto<E>
    ] ref {
        let (_this, _id);
    } else ref {
        None
    } mut {
        let (this, id);

        // If a variant has been chosen, then forward everything to it's builder.
        if matches!(this.inner, Inner::Builder { .. }) {
            match &mut this.inner {
                Inner::Builder { builder } => {
                    return Info::as_visitor(builder).0.upcast_to_id_mut(id)
                }
                _ => unreachable!(),
            }
        }
    } else mut {
        None
    } where
        E: Effect,
        Info: EnumBuildInfo<'ctx, Mode, E>,
}

impl<'ctx, Info, Mode, E: Effect> Recoverable<'ctx, E> for EnumBuilder<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn visit<'a>(
        &'a mut self,
        scope: DynRecoverableScope<'a, 'ctx, E>,
    ) -> ErasedEffective<'a, VisitResult<DynRecoverableScope<'a, 'ctx, E>>, E> {
        match core::mem::replace(&mut self.inner, Inner::Temp) {
            Inner::Seed(seed) => Info::guess_variant(seed, scope).map(|result| {
                self.inner = Inner::Value(result);
                Flow::Done.into()
            }),
            inner => {
                self.inner = inner;
                E::ready(Flow::Continue.into())
            }
        }
    }
}

impl<'ctx, Info, Mode, E: Effect> Value<'ctx, Info::ValueT, E> for EnumBuilder<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn visit<'a>(
        &'a mut self,
        value: TypeName::T<'a, 'ctx, Info::ValueT>,
    ) -> ErasedEffective<'a, VisitResult<TypeName::T<'a, 'ctx, Info::ValueT>>, E>
    where
        TypeName::T<'a, 'ctx, Info::ValueT>: Send + Sync + Sized,
        'ctx: 'a,
    {
        self.inner = Inner::Value(Ok(Info::from_value(value)));
        E::ready(Flow::Done.into())
    }
}

impl<'ctx, Info, Mode, E: Effect> Tag<'ctx, tags::Variant, E> for EnumBuilder<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn visit<'a: 'c, 'b: 'c, 'c>(
        &'a mut self,
        _kind: tags::Variant,
        walker: DynWalkerObjSafe<'b, 'ctx, E>,
    ) -> ErasedEffective<'c, VisitResult<DynWalkerObjSafe<'b, 'ctx, E>>, E> {
        let visitor = VariantVisitor::<Info, Mode, E> { marker: None };

        E::as_ctx((visitor, walker), |(visitor, walker)| {
            walker.walk(DynVisitor(visitor)).cast()
        })
        .then(|((visitor, _), result)| {
            if let Some(variant) = visitor.marker {
                match core::mem::replace(&mut self.inner, Inner::Temp) {
                    // A variant was given so we need to make the builder for
                    // it.
                    Inner::Seed(seed) => Info::new_builder(seed, variant).map(move |builder| {
                        self.inner = Inner::Builder { builder };
                        result.to_done().into()
                    }),
                    inner => {
                        self.inner = inner;
                        E::ready(result.to_done().into())
                    }
                }
            } else {
                E::ready(result.to_done().into())
            }
        })
    }
}

struct VariantVisitor<'ctx, Info, Mode, E: Effect>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    marker: Option<Info::VariantMarker>,
}

any_trait! {
    impl['ctx, Info, Mode, E] VariantVisitor<'ctx, Info, Mode, E> = [
        ValueProto<TempBorrowedStaticHrt<str>, E>,
        ValueProto<OwnedStatic<u32>, E>,
    ]
    where
        E: Effect,
        Info: EnumBuildInfo<'ctx, Mode, E>,
}

impl<'ctx, Info, Mode, E: Effect> Value<'ctx, TempBorrowedStaticHrt<str>, E>
    for VariantVisitor<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn visit<'a>(
        &'a mut self,
        TempBorrowedStatic(value): TypeName::T<'a, 'ctx, TempBorrowedStaticHrt<str>>,
    ) -> ErasedEffective<'a, VisitResult<TypeName::T<'a, 'ctx, TempBorrowedStaticHrt<str>>>, E>
    where
        TypeName::T<'a, 'ctx, TempBorrowedStaticHrt<str>>: Send + Sync + Sized,
        'ctx: 'a,
    {
        if let Some(variant) = Info::marker_from_name(value) {
            self.marker = Some(variant);

            E::ready(Flow::Done.into())
        } else {
            E::ready(Flow::Continue.into())
        }
    }
}

impl<'ctx, Info, Mode, E: Effect> Value<'ctx, OwnedStatic<u32>, E>
    for VariantVisitor<'ctx, Info, Mode, E>
where
    Info: EnumBuildInfo<'ctx, Mode, E>,
{
    fn visit<'a>(
        &'a mut self,
        OwnedStatic(value): TypeName::T<'a, 'ctx, OwnedStatic<u32>>,
    ) -> ErasedEffective<'a, VisitResult<TypeName::T<'a, 'ctx, OwnedStatic<u32>>>, E>
    where
        TypeName::T<'a, 'ctx, OwnedStatic<u32>>: Send + Sync + Sized,
        'ctx: 'a,
    {
        if let Some(variant) = Info::marker_from_discriminant(value) {
            self.marker = Some(variant);

            E::ready(Flow::Done.into())
        } else {
            E::ready(Flow::Continue.into())
        }
    }
}