serde-xml-fast 1.0.0

A fast, 100% Serde-compatible XML serialization and deserialization library
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Low-level XML writer.
//!
//! This module provides a fast XML writer that produces well-formed XML output.

use crate::escape::escape_to;
use std::io::{self, Write};

/// An XML writer that produces well-formed XML output.
pub struct XmlWriter<W: Write> {
    writer: W,
    /// Stack of open element names.
    element_stack: Vec<String>,
    /// Whether we're currently in an element tag (before the closing >).
    in_tag: bool,
    /// Indentation settings.
    indent: Option<IndentConfig>,
    /// Current indentation level.
    level: usize,
    /// Whether the last write was a start element (for formatting).
    last_was_start: bool,
}

/// Indentation configuration.
#[derive(Clone)]
pub struct IndentConfig {
    /// Characters to use for each level of indentation.
    pub indent_str: String,
    /// Whether to add a newline before each element.
    pub newlines: bool,
}

impl Default for IndentConfig {
    fn default() -> Self {
        Self {
            indent_str: "  ".to_string(),
            newlines: true,
        }
    }
}

impl<W: Write> XmlWriter<W> {
    /// Creates a new XML writer.
    #[inline]
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            element_stack: Vec::new(),
            in_tag: false,
            indent: None,
            level: 0,
            last_was_start: false,
        }
    }

    /// Creates a new XML writer with indentation.
    #[inline]
    pub fn with_indent(writer: W, indent: IndentConfig) -> Self {
        Self {
            writer,
            element_stack: Vec::new(),
            in_tag: false,
            indent: Some(indent),
            level: 0,
            last_was_start: false,
        }
    }

    /// Returns the inner writer.
    #[inline]
    pub fn into_inner(self) -> W {
        self.writer
    }

    /// Returns the current nesting depth.
    #[inline]
    pub fn depth(&self) -> usize {
        self.element_stack.len()
    }

    /// Writes the XML declaration.
    pub fn write_declaration(&mut self, version: &str, encoding: Option<&str>) -> io::Result<()> {
        self.close_tag_if_open()?;
        write!(self.writer, "<?xml version=\"{}\"", version)?;
        if let Some(enc) = encoding {
            write!(self.writer, " encoding=\"{}\"", enc)?;
        }
        self.writer.write_all(b"?>")
    }

    /// Starts an element.
    pub fn start_element(&mut self, name: &str) -> io::Result<()> {
        self.close_tag_if_open()?;
        self.write_indent()?;
        write!(self.writer, "<{}", name)?;
        self.element_stack.push(name.to_string());
        self.in_tag = true;
        self.last_was_start = true;
        self.level += 1;
        Ok(())
    }

    /// Writes an attribute for the current element.
    pub fn write_attribute(&mut self, name: &str, value: &str) -> io::Result<()> {
        if !self.in_tag {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "cannot write attribute outside of element tag",
            ));
        }
        write!(self.writer, " {}=\"", name)?;
        self.write_escaped(value)?;
        self.writer.write_all(b"\"")
    }

    /// Ends the current element.
    pub fn end_element(&mut self) -> io::Result<()> {
        self.level = self.level.saturating_sub(1);

        if let Some(name) = self.element_stack.pop() {
            if self.in_tag {
                // Self-closing tag
                self.writer.write_all(b"/>")?;
                self.in_tag = false;
            } else {
                if !self.last_was_start {
                    self.write_indent()?;
                }
                write!(self.writer, "</{}>", name)?;
            }
            self.last_was_start = false;
            Ok(())
        } else {
            Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "no element to close",
            ))
        }
    }

    /// Writes text content.
    pub fn write_text(&mut self, text: &str) -> io::Result<()> {
        self.close_tag_if_open()?;
        self.write_escaped(text)?;
        self.last_was_start = false;
        Ok(())
    }

    /// Writes a CDATA section.
    pub fn write_cdata(&mut self, data: &str) -> io::Result<()> {
        self.close_tag_if_open()?;
        write!(self.writer, "<![CDATA[{}]]>", data)
    }

    /// Writes a comment.
    pub fn write_comment(&mut self, comment: &str) -> io::Result<()> {
        self.close_tag_if_open()?;
        self.write_indent()?;
        write!(self.writer, "<!-- {} -->", comment)
    }

    /// Writes a processing instruction.
    pub fn write_pi(&mut self, target: &str, data: Option<&str>) -> io::Result<()> {
        self.close_tag_if_open()?;
        self.write_indent()?;
        write!(self.writer, "<?{}", target)?;
        if let Some(d) = data {
            write!(self.writer, " {}", d)?;
        }
        self.writer.write_all(b"?>")
    }

    /// Writes a complete element with text content.
    pub fn write_element(&mut self, name: &str, content: &str) -> io::Result<()> {
        self.start_element(name)?;
        self.write_text(content)?;
        self.end_element()
    }

    /// Writes an empty element.
    pub fn write_empty_element(&mut self, name: &str) -> io::Result<()> {
        self.close_tag_if_open()?;
        self.write_indent()?;
        write!(self.writer, "<{}/>", name)?;
        self.last_was_start = false;
        Ok(())
    }

    /// Closes the opening tag if one is open.
    fn close_tag_if_open(&mut self) -> io::Result<()> {
        if self.in_tag {
            self.writer.write_all(b">")?;
            self.in_tag = false;
        }
        Ok(())
    }

    /// Writes indentation if configured.
    fn write_indent(&mut self) -> io::Result<()> {
        if let Some(ref indent) = self.indent {
            if indent.newlines && self.level > 0 {
                self.writer.write_all(b"\n")?;
            }
            for _ in 0..self.level.saturating_sub(1) {
                self.writer.write_all(indent.indent_str.as_bytes())?;
            }
        }
        Ok(())
    }

    /// Writes escaped text.
    fn write_escaped(&mut self, s: &str) -> io::Result<()> {
        let mut escaped = String::with_capacity(s.len());
        escape_to(s, &mut escaped);
        self.writer.write_all(escaped.as_bytes())
    }

    /// Flushes the writer.
    pub fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

/// A string-based XML writer for convenience.
pub struct StringXmlWriter {
    writer: XmlWriter<Vec<u8>>,
}

impl StringXmlWriter {
    /// Creates a new string-based XML writer.
    pub fn new() -> Self {
        Self {
            writer: XmlWriter::new(Vec::new()),
        }
    }

    /// Creates a new string-based XML writer with indentation.
    pub fn with_indent(indent: IndentConfig) -> Self {
        Self {
            writer: XmlWriter::with_indent(Vec::new(), indent),
        }
    }

    /// Consumes the writer and returns the XML string.
    pub fn into_string(self) -> String {
        String::from_utf8(self.writer.into_inner()).unwrap_or_default()
    }
}

impl Default for StringXmlWriter {
    fn default() -> Self {
        Self::new()
    }
}

impl std::ops::Deref for StringXmlWriter {
    type Target = XmlWriter<Vec<u8>>;

    fn deref(&self) -> &Self::Target {
        &self.writer
    }
}

impl std::ops::DerefMut for StringXmlWriter {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.writer
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write_to_string<F>(f: F) -> String
    where
        F: FnOnce(&mut XmlWriter<Vec<u8>>) -> io::Result<()>,
    {
        let mut writer = XmlWriter::new(Vec::new());
        f(&mut writer).unwrap();
        String::from_utf8(writer.into_inner()).unwrap()
    }

    #[test]
    fn test_simple_element() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.end_element()
        });
        assert_eq!(result, "<root/>");
    }

    #[test]
    fn test_element_with_text() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_text("Hello")?;
            w.end_element()
        });
        assert_eq!(result, "<root>Hello</root>");
    }

    #[test]
    fn test_element_with_attributes() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_attribute("id", "1")?;
            w.write_attribute("name", "test")?;
            w.end_element()
        });
        assert_eq!(result, r#"<root id="1" name="test"/>"#);
    }

    #[test]
    fn test_nested_elements() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.start_element("child")?;
            w.write_text("content")?;
            w.end_element()?;
            w.end_element()
        });
        assert_eq!(result, "<root><child>content</child></root>");
    }

    #[test]
    fn test_escaped_content() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_text("<>&\"\'")?;
            w.end_element()
        });
        assert_eq!(result, "<root>&lt;&gt;&amp;&quot;&apos;</root>");
    }

    #[test]
    fn test_escaped_attribute() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_attribute("attr", "value with \"quotes\"")?;
            w.end_element()
        });
        assert_eq!(result, r#"<root attr="value with &quot;quotes&quot;"/>"#);
    }

    #[test]
    fn test_xml_declaration() {
        let result = write_to_string(|w| {
            w.write_declaration("1.0", Some("UTF-8"))?;
            w.start_element("root")?;
            w.end_element()
        });
        assert_eq!(result, r#"<?xml version="1.0" encoding="UTF-8"?><root/>"#);
    }

    #[test]
    fn test_comment() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_comment("This is a comment")?;
            w.end_element()
        });
        assert!(result.contains("<!-- This is a comment -->"));
    }

    #[test]
    fn test_cdata() {
        let result = write_to_string(|w| {
            w.start_element("root")?;
            w.write_cdata("<special>content</special>")?;
            w.end_element()
        });
        assert_eq!(result, "<root><![CDATA[<special>content</special>]]></root>");
    }

    #[test]
    fn test_empty_element() {
        let result = write_to_string(|w| {
            w.write_empty_element("br")
        });
        assert_eq!(result, "<br/>");
    }

    #[test]
    fn test_write_element_shorthand() {
        let result = write_to_string(|w| {
            w.write_element("name", "John")
        });
        assert_eq!(result, "<name>John</name>");
    }

    #[test]
    fn test_depth() {
        let mut writer = XmlWriter::new(Vec::new());
        assert_eq!(writer.depth(), 0);

        writer.start_element("a").unwrap();
        assert_eq!(writer.depth(), 1);

        writer.start_element("b").unwrap();
        assert_eq!(writer.depth(), 2);

        writer.end_element().unwrap();
        assert_eq!(writer.depth(), 1);

        writer.end_element().unwrap();
        assert_eq!(writer.depth(), 0);
    }

    #[test]
    fn test_processing_instruction() {
        let result = write_to_string(|w| {
            w.write_pi("xml-stylesheet", Some("type=\"text/xsl\" href=\"style.xsl\""))
        });
        assert_eq!(result, r#"<?xml-stylesheet type="text/xsl" href="style.xsl"?>"#);
    }

    #[test]
    fn test_indented_output() {
        let mut writer = XmlWriter::with_indent(Vec::new(), IndentConfig::default());
        writer.start_element("root").unwrap();
        writer.start_element("child").unwrap();
        writer.write_text("text").unwrap();
        writer.end_element().unwrap();
        writer.end_element().unwrap();

        let result = String::from_utf8(writer.into_inner()).unwrap();
        assert!(result.contains("\n"));
    }
}