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
//! Adapter for serde's Deserialize trait.

use core::{marker::PhantomData, ops::ControlFlow};

use serde::{de::SeqAccess, forward_to_deserialize_any, Deserialize, Deserializer};

use crate::{
    any::static_wrapper::OwnedStatic,
    any_trait,
    protocol::{
        visitor::{Sequence, SequenceScope, Status, Value},
        AsObj, AsyncEffect, Yield, Effect, SyncEffect,
    },
    AsVisitor, Builder as _,
};

#[derive(Debug)]
pub enum Error {
    Incomplete,
    Custom,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

pub struct Builder<T, E>(Result<T, Error>, PhantomData<fn() -> E>);

impl<'ctx, T: Deserialize<'ctx>, E: Effect<'ctx, ControlFlow<(), ()>>> crate::Builder<'ctx, E> for Builder<T, E>
where
    Self: AsVisitor<'ctx, E>,
{
    type Error = Error;

    type Value = T;

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

    type Seed = ();

    fn from_seed(seed: Self::Seed) -> Self {
        Self(Err(Error::Incomplete), PhantomData)
    }

    type Effect = SyncEffect;
}

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

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

any_trait! {
    impl['a, 'ctx, T: Deserialize<'ctx>, E] Builder<T, E> = [
        dyn Value<'ctx, OwnedStatic<bool>, SyncEffect> + 'a,
        dyn Sequence<'ctx, SyncEffect> + 'a,
    ]
}

enum InjectedValue<'a, 'ctx> {
    Bool(bool),
    Sequence(&'a mut dyn SequenceScope<'ctx, SyncEffect>),
}

impl<'ctx, T: Deserialize<'ctx>, E> Value<'ctx, OwnedStatic<bool>, SyncEffect> for Builder<T, E> {
    #[inline]
    fn visit<'a>(&'a mut self, OwnedStatic(value): OwnedStatic<bool>) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect>
    where
        'ctx: 'a,
    {
        self.0 = T::deserialize(InjectedValue::Bool(value));

        if self.0.is_err() {
            ControlFlow::Break(())
        } else {
            ControlFlow::Continue(())
        }
    }
}

impl<'ctx, T: Deserialize<'ctx>, E> Sequence<'ctx, SyncEffect> for Builder<T, E> {
    #[inline]
    fn visit<'a>(&'a mut self, scope: &'a mut dyn SequenceScope<'ctx, SyncEffect>) -> Yield<'a, 'ctx, ControlFlow<(), ()>, SyncEffect>
    where
        'ctx: 'a,
    {
        self.0 = T::deserialize(InjectedValue::Sequence(scope));

        if self.0.is_err() {
            ControlFlow::Break(())
        } else {
            ControlFlow::Continue(())
        }
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl serde::de::Error for Error {
    fn custom<T>(_msg: T) -> Self
    where
        T: core::fmt::Display,
    {
        Error::Custom
    }
}

impl<'a, 'ctx> Deserializer<'ctx> for InjectedValue<'a, 'ctx> {
    type Error = Error;

    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
    where
        V: serde::de::Visitor<'ctx>,
    {
        match self {
            InjectedValue::Bool(value) => visitor.visit_bool(value),
            InjectedValue::Sequence(scope) => visitor.visit_seq(SequenceAccess(scope)),
        }
    }

    forward_to_deserialize_any! {
        <W: Visitor<'ctx>>
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf option unit unit_struct newtype_struct seq tuple
        tuple_struct map struct enum identifier ignored_any
    }
}

struct SequenceAccess<'a, 'ctx>(&'a mut dyn SequenceScope<'ctx, SyncEffect>);

impl<'a, 'ctx> SeqAccess<'ctx> for SequenceAccess<'a, 'ctx> {
    type Error = Error;

    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
    where
        T: serde::de::DeserializeSeed<'ctx>,
    {
        let mut builder = super::deserialize_seed::Builder::<_, SyncEffect>::from_seed(seed);
        match self.0.next(builder.as_visitor()) {
            ControlFlow::Continue(Status::Continue) => match builder.build() {
                Ok(value) => Ok(Some(value)),
                Err(error) => Err(Error::Incomplete),
            },
            ControlFlow::Continue(Status::Done) => Ok(None),
            ControlFlow::Break(()) => Err(Error::Incomplete), // use the one from builder
                                                              // if any.
        }
    }
}