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

use crate::{
    any::static_wrapper::OwnedStatic,
    any_trait,
    effect::{AsyncEffect, AsyncSendEffect, EffectAnyTrait, SyncEffect, Yield},
    protocol::visitor::{Tagged, TaggedScope, Value},
    AsVisitor,
};

pub struct VariantInfo<'ctx> {
    pub
}

pub struct Builder<'ctx, E: EffectAnyTrait<'ctx>> {
    value: Option<Option<B::Value>>,
    ignore_missing: bool,
    _marker: PhantomData<fn() -> (&'ctx (), E)>,
}

#[derive(Default)]
pub enum IgnoreMissing {
    #[default]
    Yes,
    No,
}

#[derive(Debug)]
pub enum Error<T> {
    Missing,
    VariantNone,
    VariantSome(T),
}

impl<'ctx, B, E> crate::Builder<'ctx, E> for Builder<'ctx, B, E>
where
    E: EffectAnyTrait<'ctx>,
    Self: AsVisitor<'ctx, E>,
    B: crate::Builder<'ctx, E>,
{
    type Error = Error<B::Error>;

    type Value = Option<B::Value>;

    #[inline]
    fn build<'a>(self) -> Result<Self::Value, Self::Error>
    where
        Self: 'a,
    {
        if self.ignore_missing {
            Ok(self.value.flatten())
        } else {
            self.value.ok_or(Error::Missing)
        }
    }

    type Seed = IgnoreMissing;

    fn from_seed(seed: Self::Seed) -> Self {
        Self {
            value: None,
            ignore_missing: match seed {
                IgnoreMissing::Yes => true,
                IgnoreMissing::No => false,
            },
            _marker: PhantomData,
        }
    }

    type Effect = SyncEffect;
}

impl<'ctx, B: crate::Builder<'ctx, SyncEffect>> AsVisitor<'ctx, SyncEffect>
    for Builder<'ctx, B, SyncEffect>
{
    fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, SyncEffect> {
        self
    }
}

impl<'ctx, B: crate::Builder<'ctx, AsyncEffect>> AsVisitor<'ctx, AsyncEffect>
    for Builder<'ctx, B, AsyncEffect>
{
    fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, AsyncEffect> {
        self
    }
}

impl<'ctx, B> AsVisitor<'ctx, AsyncSendEffect> for Builder<'ctx, B, AsyncSendEffect>
where
    B: crate::Builder<'ctx, AsyncSendEffect>,
    <B as crate::Builder<'ctx, AsyncSendEffect>>::Value: Send,
{
    fn as_visitor(&mut self) -> crate::protocol::Visitor<'_, 'ctx, AsyncSendEffect> {
        self
    }
}

any_trait! {
    impl['a, 'ctx, B] Builder<'ctx, B, SyncEffect> = [
    ] where B: crate::Builder<'ctx, SyncEffect>
}

any_trait! {
    impl['a, 'ctx, B] Builder<'ctx, B, AsyncEffect> = [
    ] where B: crate::Builder<'ctx, AsyncEffect>
}

any_trait! {
    impl['a, 'ctx, B] Builder<'ctx, B, AsyncSendEffect> = [
    ] where B: crate::Builder<'ctx, AsyncSendEffect>
}

pub mod symbol {
    use crate::symbol::Symbol;

    pub const TYPE: Symbol = Symbol::new("Type");
    pub const VARIANT: Symbol = Symbol::new("Variant");
}

impl<'ctx, B> Tagged<'ctx, SyncEffect> for Builder<'ctx, B, SyncEffect>
where
    B: crate::Builder<'ctx, SyncEffect>,
{
    fn visit<'a>(
        &'a mut self,
        scope: &'a mut dyn TaggedScope<'ctx, SyncEffect>,
    ) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect>
    where
        'ctx: 'a,
    {
        match scope.kind() {
            symbol::TYPE => {
                // The type info doesn't matter, just move on to the value.
                scope.value(self)?;
            },
            symbol::VARIANT => {
                // We need to check the variant id.
                // let mut variant = VariantBuilder::from_seed(());
            },
            _ => {}
        }

        todo!();
    }
}