xsd_parser/quick_xml/
serialize.rs

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::borrow::Cow;
use std::fmt::Debug;
use std::io::Write;
use std::mem::replace;

use quick_xml::{
    escape::escape,
    events::{BytesEnd, BytesStart, BytesText, Event},
    Writer,
};

use super::{Error, ErrorKind, RawByteStr};

/// Trait that defines the [`Serializer`] for a type.
pub trait WithSerializer: Sized {
    /// The serializer to use for this type.
    type Serializer<'x>: Serializer<'x, Self>
    where
        Self: 'x;
}

impl<X> WithSerializer for X
where
    X: SerializeBytes + Debug,
{
    type Serializer<'x>
        = ContentSerializer<'x, X>
    where
        Self: 'x;
}

/// Trait that defines a serializer that can be used to destruct a type to
/// suitable XML [`Event`]s.
pub trait Serializer<'ser, T>: Iterator<Item = Result<Event<'ser>, Error>> + Debug + Sized {
    /// Initializes a new serializer from the passed `value`.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] is the serializer could not be initialized.
    fn init(value: &'ser T, name: Option<&'ser str>, is_root: bool) -> Result<Self, Error>;
}

/// Trait that could be implemented by types to support serialization to XML
/// using the [`quick_xml`] crate.
pub trait SerializeSync: Sized {
    /// Error returned by the `serialize` method.
    type Error;

    /// Serializes the type to XML using the provided `writer`.
    ///
    /// # Errors
    ///
    /// Returns a suitable error if the operation was not successful.
    fn serialize<W: Write>(&self, root: &str, writer: &mut Writer<W>) -> Result<(), Self::Error>;
}

impl<X> SerializeSync for X
where
    X: WithSerializer,
{
    type Error = Error;

    fn serialize<W: Write>(&self, root: &str, writer: &mut Writer<W>) -> Result<(), Self::Error> {
        SerializeHelper::new(self, Some(root), writer)?.serialize_sync()
    }
}

/// Trait that could be implemented by types to support asynchronous serialization
/// to XML using the [`quick_xml`] crate.
#[cfg(feature = "async")]
pub trait SerializeAsync: Sized {
    /// Future that is returned by the `serialize_async` method.
    type Future<'x>: std::future::Future<Output = Result<(), Self::Error>> + 'x
    where
        Self: 'x;

    /// Error returned by the `serialize_async` method.
    type Error;

    /// Asynchronously serializes the type to XML using the provided `writer`.
    fn serialize_async<'a, W: tokio::io::AsyncWrite + Unpin>(
        &'a self,
        root: &'a str,
        writer: &'a mut Writer<W>,
    ) -> Self::Future<'a>;
}

#[cfg(feature = "async")]
impl<X> SerializeAsync for X
where
    X: WithSerializer,
{
    type Future<'x>
        = std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), Self::Error>> + 'x>>
    where
        X: 'x;

    type Error = Error;

    fn serialize_async<'a, W: tokio::io::AsyncWrite + Unpin>(
        &'a self,
        root: &'a str,
        writer: &'a mut Writer<W>,
    ) -> Self::Future<'a> {
        Box::pin(async move {
            SerializeHelper::new(self, Some(root), writer)?
                .serialize_async()
                .await
        })
    }
}

/// Trait that could be implemented by types to support serialization to
/// XML byte streams.
///
/// This is usually implemented for simple types like numbers, strings or enums.
pub trait SerializeBytes: Sized {
    /// Try to serialize the type to bytes.
    ///
    /// This is used to serialize the type to attributes or raw element
    /// content.
    ///
    /// # Errors
    ///
    /// Returns a suitable [`Error`] if the serialization was not successful.
    fn serialize_bytes(&self) -> Result<Option<Cow<'_, str>>, Error>;
}

impl<X> SerializeBytes for X
where
    X: ToString,
{
    fn serialize_bytes(&self) -> Result<Option<Cow<'_, str>>, Error> {
        Ok(Some(Cow::Owned(self.to_string())))
    }
}

/// Implements a [`Serializer`] for any type that implements [`SerializeBytes`].

#[derive(Debug)]
#[allow(missing_docs)]
pub enum ContentSerializer<'ser, T> {
    Begin {
        name: &'ser str,
        value: &'ser T,
    },
    Data {
        name: &'ser str,
        data: Cow<'ser, str>,
    },
    End {
        name: &'ser str,
    },
    Done,
}

impl<'ser, T> Serializer<'ser, T> for ContentSerializer<'ser, T>
where
    T: SerializeBytes + Debug,
{
    fn init(value: &'ser T, name: Option<&'ser str>, is_root: bool) -> Result<Self, Error> {
        let _is_root = is_root;

        Ok(Self::Begin {
            name: name.ok_or(ErrorKind::MissingName)?,
            value,
        })
    }
}

impl<'ser, T> Iterator for ContentSerializer<'ser, T>
where
    T: SerializeBytes + Debug,
{
    type Item = Result<Event<'ser>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match replace(self, Self::Done) {
            Self::Begin { name, value } => match value.serialize_bytes() {
                Ok(None) => Some(Ok(Event::Empty(BytesStart::new(name)))),
                Ok(Some(data)) => {
                    if data.contains("]]>") {
                        return Some(Err(ErrorKind::InvalidData(RawByteStr::from_slice(
                            data.as_bytes(),
                        ))
                        .into()));
                    }

                    *self = Self::Data { name, data };

                    Some(Ok(Event::Start(BytesStart::new(name))))
                }
                Err(error) => Some(Err(error)),
            },
            Self::Data { name, data } => {
                *self = Self::End { name };

                Some(Ok(Event::Text(BytesText::from_escaped(escape(data)))))
            }
            Self::End { name } => Some(Ok(Event::End(BytesEnd::new(name)))),
            Self::Done => None,
        }
    }
}

/// Implements a [`Serializer`] for any type that implements an [`Iterator`]
/// that emits references to a type that implements [`WithSerializer`].
#[derive(Debug)]
#[allow(missing_docs)]
pub enum IterSerializer<'ser, T, TItem>
where
    &'ser T: IntoIterator<Item = &'ser TItem>,
    <&'ser T as IntoIterator>::IntoIter: Debug,
    TItem: WithSerializer + 'ser,
{
    Pending {
        name: Option<&'ser str>,
        iter: <&'ser T as IntoIterator>::IntoIter,
    },
    Emitting {
        name: Option<&'ser str>,
        iter: <&'ser T as IntoIterator>::IntoIter,
        serializer: TItem::Serializer<'ser>,
    },
    Done,
}

impl<'ser, T, TItem> Iterator for IterSerializer<'ser, T, TItem>
where
    T: 'ser,
    &'ser T: IntoIterator<Item = &'ser TItem>,
    <&'ser T as IntoIterator>::IntoIter: Debug,
    TItem: WithSerializer + 'ser,
{
    type Item = Result<Event<'ser>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match replace(self, Self::Done) {
                Self::Pending { name, mut iter } => {
                    let item = iter.next()?;

                    match Serializer::init(item, name, false) {
                        Ok(serializer) => {
                            *self = Self::Emitting {
                                name,
                                iter,
                                serializer,
                            }
                        }
                        Err(error) => return Some(Err(error)),
                    }
                }
                Self::Emitting {
                    name,
                    iter,
                    mut serializer,
                } => {
                    if let Some(ret) = serializer.next() {
                        *self = Self::Emitting {
                            name,
                            iter,
                            serializer,
                        };

                        return Some(ret);
                    }

                    *self = Self::Pending { name, iter };
                }
                Self::Done => return None,
            }
        }
    }
}

impl<'ser, T, TItem> Serializer<'ser, T> for IterSerializer<'ser, T, TItem>
where
    T: Debug + 'ser,
    &'ser T: IntoIterator<Item = &'ser TItem>,
    <&'ser T as IntoIterator>::IntoIter: Debug,
    TItem: WithSerializer + Debug + 'ser,
{
    fn init(value: &'ser T, name: Option<&'ser str>, is_root: bool) -> Result<Self, Error> {
        let _is_root = is_root;

        Ok(Self::Pending {
            name,
            iter: value.into_iter(),
        })
    }
}

/* SerializeHelper */

struct SerializeHelper<'a, T, W>
where
    T: WithSerializer + 'a,
{
    writer: &'a mut Writer<W>,
    serializer: T::Serializer<'a>,
}

impl<'a, T, W> SerializeHelper<'a, T, W>
where
    T: WithSerializer,
{
    fn new(value: &'a T, name: Option<&'a str>, writer: &'a mut Writer<W>) -> Result<Self, Error> {
        let serializer = Serializer::init(value, name, true)?;

        Ok(Self { writer, serializer })
    }
}

impl<T, W> SerializeHelper<'_, T, W>
where
    T: WithSerializer,
    W: Write,
{
    fn serialize_sync(&mut self) -> Result<(), Error> {
        for event in self.serializer.by_ref() {
            self.writer
                .write_event(event?)
                .map_err(|error| ErrorKind::XmlError(error.into()))?;
        }

        Ok(())
    }
}

#[cfg(feature = "async")]
impl<T, W> SerializeHelper<'_, T, W>
where
    T: WithSerializer,
    W: tokio::io::AsyncWrite + Unpin,
{
    async fn serialize_async(&mut self) -> Result<(), Error> {
        for event in self.serializer.by_ref() {
            self.writer
                .write_event_async(event?)
                .await
                .map_err(ErrorKind::XmlError)?;
        }

        Ok(())
    }
}