1use std::borrow::Cow;
3
4use deser_core::State;
5use deser_core::de::{Deserialize, OwnedSink, Sink, SinkHandle};
6use deser_core::ser::{Chunk, Describe, Serialize};
7use deser_core::{Atom, ContainerShape, Error};
8
9#[derive(Debug, Default)]
18pub(crate) struct RootData {
19 pub(crate) name: Option<String>,
20 pub(crate) namespaces: Vec<(String, String)>,
21}
22
23impl Clone for RootData {
26 fn clone(&self) -> RootData {
27 RootData {
28 name: self.name.clone(),
29 namespaces: self.namespaces.clone(),
30 }
31 }
32
33 fn clone_from(&mut self, source: &RootData) {
34 self.name.clone_from(&source.name);
35 self.namespaces.clone_from(&source.namespaces);
36 }
37}
38
39#[derive(Debug, Default)]
46pub(crate) struct Declarations(pub(crate) Vec<(String, String)>);
47
48impl Clone for Declarations {
49 fn clone(&self) -> Declarations {
50 Declarations(self.0.clone())
51 }
52
53 fn clone_from(&mut self, source: &Declarations) {
54 self.0.clone_from(&source.0);
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
96pub struct Root<T> {
97 pub name: Option<String>,
99 pub namespaces: Vec<(String, String)>,
102 pub value: T,
104}
105
106impl<T> Root<T> {
107 pub fn new<S: Into<String>>(name: S, value: T) -> Root<T> {
109 Root {
110 name: Some(name.into()),
111 namespaces: Vec::new(),
112 value,
113 }
114 }
115
116 pub fn with_namespace<P: Into<String>, U: Into<String>>(
128 mut self,
129 prefix: P,
130 uri: U,
131 ) -> Root<T> {
132 self.namespaces.push((prefix.into(), uri.into()));
133 self
134 }
135
136 pub fn into_inner(self) -> T {
138 self.value
139 }
140}
141
142impl<T: Serialize> Serialize for Root<T> {
143 fn serialize(&self, state: &mut State) -> Result<Chunk<'_>, Error> {
144 let chunk = self.value.serialize(state)?;
147 if self.name.is_some() || !self.namespaces.is_empty() {
148 let data = state.event_mut::<RootData>();
149 data.name.clone_from(&self.name);
150 data.namespaces.clone_from(&self.namespaces);
151 }
152 Ok(chunk)
153 }
154
155 fn finish(&self, state: &mut State) -> Result<(), Error> {
156 self.value.finish(state)
157 }
158
159 fn is_optional(&self) -> bool {
160 self.value.is_optional()
161 }
162
163 fn container_shape(&self) -> ContainerShape {
164 self.value.container_shape()
165 }
166
167 fn describe(&self, d: &mut dyn Describe) {
168 self.value.describe(d)
169 }
170}
171
172impl<'de, T: Deserialize<'de>> Deserialize<'de> for Root<T> {
173 fn deserialize_into<'out>(
174 out: &'out mut Option<Self>,
175 state: &mut State,
176 ) -> SinkHandle<'out, 'de> {
177 SinkHandle::arena(
178 RootSink {
179 out,
180 slot: None,
181 compound: None,
182 data: RootData::default(),
183 },
184 state,
185 )
186 }
187}
188
189struct RootSink<'a, 'de, T> {
190 out: &'a mut Option<Root<T>>,
191 slot: Option<T>,
194 compound: Option<OwnedSink<'de, T>>,
195 data: RootData,
196}
197
198impl<'a, 'de, T: Deserialize<'de>> RootSink<'a, 'de, T> {
199 fn take_data(&mut self, state: &mut State) {
203 if let Some(data) = state.take_event::<RootData>() {
204 self.data = data;
205 }
206 }
207
208 fn compound(&mut self, state: &mut State) -> &mut dyn Sink<'de> {
209 self.compound
210 .get_or_insert_with(|| OwnedSink::deserialize(state))
211 .borrow_mut()
212 }
213}
214
215impl<'a, 'de, T: Deserialize<'de>> Sink<'de> for RootSink<'a, 'de, T> {
216 fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
217 self.take_data(state);
218 let mut sink = T::deserialize_into(&mut self.slot, state);
219 sink.atom(atom, state)?;
220 sink.finish(state)
221 }
222
223 fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
224 self.take_data(state);
225 let mut sink = T::deserialize_into(&mut self.slot, state);
226 sink.borrowed_atom(atom, state)?;
227 sink.finish(state)
228 }
229
230 fn map(&mut self, state: &mut State) -> Result<(), Error> {
231 self.take_data(state);
232 self.compound(state).map(state)
233 }
234
235 fn seq(&mut self, state: &mut State) -> Result<(), Error> {
236 self.take_data(state);
237 self.compound(state).seq(state)
238 }
239
240 fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
241 self.compound(state).next_key(state)
242 }
243
244 fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
245 self.compound(state).next_value(state)
246 }
247
248 fn value_for_key(
249 &mut self,
250 key: &str,
251 state: &mut State,
252 ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
253 self.compound(state).value_for_key(key, state)
254 }
255
256 fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
257 match self.compound {
258 Some(ref mut compound) => compound.borrow_mut().recover(err, state),
259 None => Err(err),
260 }
261 }
262
263 fn finish(&mut self, state: &mut State) -> Result<(), Error> {
264 let value = match self.compound {
265 Some(ref mut compound) => {
266 compound.borrow_mut().finish(state)?;
267 compound.take()
268 }
269 None => self.slot.take(),
270 };
271 let RootData { name, namespaces } = std::mem::take(&mut self.data);
272 *self.out = value.map(|value| Root {
273 name,
274 namespaces,
275 value,
276 });
277 Ok(())
278 }
279
280 fn expecting(&self) -> Cow<'_, str> {
281 if let Some(ref compound) = self.compound {
282 return compound.borrow().expecting();
283 }
284 let mut slot = None;
285 let mut state = State::new();
286 Cow::Owned(
287 T::deserialize_into(&mut slot, &mut state)
288 .expecting()
289 .into_owned(),
290 )
291 }
292}