tinyxml2 0.1.0

A ground-up Rust implementation of the TinyXML2 API — behavioral compatibility, idiomatic internals
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! XML error types compatible with TinyXML2's error codes.
//!
//! This module provides [`XmlError`], a comprehensive error enum covering all
//! error conditions that TinyXML2 reports. Parse errors carry line numbers for
//! diagnostic reporting.
//!
//! # Compatibility with TinyXML2
//!
//! Every `XMLError` variant from TinyXML2 has a corresponding [`XmlError`] variant.
//! The Rust API uses `Result<T, XmlError>` instead of TinyXML2's pattern of returning
//! error codes and polling `Document::Error()`.

use std::fmt;

/// Result type alias using [`XmlError`] as the error type.
pub type Result<T> = std::result::Result<T, XmlError>;

/// Comprehensive XML error type compatible with TinyXML2's error codes.
///
/// Parse errors include the line number where the error was detected, enabling
/// precise diagnostic reporting. File I/O errors wrap the underlying
/// [`std::io::Error`].
///
/// # TinyXML2 Mapping
///
/// | TinyXML2 | tinyxml2-rs |
/// |----------|-------------|
/// | `XML_SUCCESS` | `Ok(())` |
/// | `XML_NO_ATTRIBUTE` | `XmlError::NoAttribute` |
/// | `XML_WRONG_ATTRIBUTE_TYPE` | `XmlError::WrongAttributeType` |
/// | `XML_ERROR_FILE_*` | `XmlError::Io` |
/// | `XML_ERROR_PARSING_*` | `XmlError::Parse` with specific `kind` |
/// | `XML_ERROR_EMPTY_DOCUMENT` | `XmlError::EmptyDocument` |
/// | `XML_ERROR_MISMATCHED_ELEMENT` | `XmlError::MismatchedElement` |
/// | `XML_CAN_NOT_CONVERT_TEXT` | `XmlError::CanNotConvertText` |
/// | `XML_NO_TEXT_NODE` | `XmlError::NoTextNode` |
/// | `XML_ELEMENT_DEPTH_EXCEEDED` | `XmlError::ElementDepthExceeded` |
#[derive(Debug)]
pub enum XmlError {
    // --- Attribute errors ---
    /// The requested attribute does not exist on the element.
    ///
    /// Equivalent to `XML_NO_ATTRIBUTE`.
    NoAttribute,

    /// The attribute value could not be converted to the requested type.
    ///
    /// Equivalent to `XML_WRONG_ATTRIBUTE_TYPE`.
    WrongAttributeType,

    // --- File I/O errors ---
    /// An I/O error occurred during file loading or saving.
    ///
    /// Wraps [`std::io::Error`] and covers TinyXML2's `XML_ERROR_FILE_NOT_FOUND`,
    /// `XML_ERROR_FILE_COULD_NOT_BE_OPENED`, and `XML_ERROR_FILE_READ_ERROR`.
    Io(std::io::Error),

    // --- Parse errors ---
    /// A parse error occurred at the specified line.
    ///
    /// The `kind` field specifies what was being parsed when the error occurred.
    Parse {
        /// What XML construct was being parsed.
        kind: ParseErrorKind,
        /// The 1-based line number where the error was detected.
        line: u32,
        /// Optional human-readable detail message.
        message: Option<String>,
    },

    /// The document was empty or contained no XML content.
    ///
    /// Equivalent to `XML_ERROR_EMPTY_DOCUMENT`.
    EmptyDocument,

    /// An element's closing tag did not match its opening tag.
    ///
    /// Equivalent to `XML_ERROR_MISMATCHED_ELEMENT`.
    MismatchedElement {
        /// The 1-based line number where the mismatch was detected.
        line: u32,
        /// The expected element name.
        expected: String,
        /// The actual element name found.
        found: String,
    },

    /// Text content could not be converted to the requested type.
    ///
    /// Equivalent to `XML_CAN_NOT_CONVERT_TEXT`.
    CanNotConvertText,

    /// No text node was found where one was expected.
    ///
    /// Equivalent to `XML_NO_TEXT_NODE`.
    NoTextNode,

    /// The maximum element nesting depth was exceeded.
    ///
    /// This is a security limit to prevent stack overflow on deeply nested
    /// (potentially malicious) XML input.
    ///
    /// Equivalent to `XML_ELEMENT_DEPTH_EXCEEDED`.
    ElementDepthExceeded {
        /// The 1-based line number where the limit was exceeded.
        line: u32,
        /// The configured maximum depth.
        max_depth: u32,
    },

    /// A node ID was invalid (the node was deleted or never existed).
    ///
    /// This error has no TinyXML2 equivalent — it arises from the arena-based
    /// memory model where stale `NodeId` values are detected at runtime.
    InvalidNodeId,
}

/// Specifies what XML construct was being parsed when an error occurred.
///
/// Each variant maps to a TinyXML2 `XML_ERROR_PARSING_*` error code.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorKind {
    /// Error parsing an element tag.
    Element,
    /// Error parsing an attribute.
    Attribute,
    /// Error parsing text content.
    Text,
    /// Error parsing a CDATA section.
    Cdata,
    /// Error parsing a comment.
    Comment,
    /// Error parsing an XML declaration.
    Declaration,
    /// Error parsing an unknown construct.
    Unknown,
    /// General parse error not specific to any construct.
    General,
}

impl XmlError {
    /// Returns the TinyXML2-compatible error name string.
    ///
    /// # Examples
    ///
    /// ```
    /// use tinyxml2::XmlError;
    ///
    /// let err = XmlError::NoAttribute;
    /// assert_eq!(err.name(), "XML_NO_ATTRIBUTE");
    /// ```
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::NoAttribute => "XML_NO_ATTRIBUTE",
            Self::WrongAttributeType => "XML_WRONG_ATTRIBUTE_TYPE",
            Self::Io(_) => "XML_ERROR_FILE_READ_ERROR",
            Self::Parse { kind, .. } => kind.error_name(),
            Self::EmptyDocument => "XML_ERROR_EMPTY_DOCUMENT",
            Self::MismatchedElement { .. } => "XML_ERROR_MISMATCHED_ELEMENT",
            Self::CanNotConvertText => "XML_CAN_NOT_CONVERT_TEXT",
            Self::NoTextNode => "XML_NO_TEXT_NODE",
            Self::ElementDepthExceeded { .. } => "XML_ELEMENT_DEPTH_EXCEEDED",
            Self::InvalidNodeId => "XML_INVALID_NODE_ID",
        }
    }

    /// Returns the line number associated with this error, if any.
    ///
    /// Parse errors and mismatched element errors include line numbers.
    /// Other error types return `None`.
    #[must_use]
    pub fn line(&self) -> Option<u32> {
        match self {
            Self::Parse { line, .. }
            | Self::MismatchedElement { line, .. }
            | Self::ElementDepthExceeded { line, .. } => Some(*line),
            _ => None,
        }
    }
}

impl ParseErrorKind {
    /// Returns the TinyXML2-compatible error name for this parse error kind.
    #[must_use]
    pub const fn error_name(self) -> &'static str {
        match self {
            Self::Element => "XML_ERROR_PARSING_ELEMENT",
            Self::Attribute => "XML_ERROR_PARSING_ATTRIBUTE",
            Self::Text => "XML_ERROR_PARSING_TEXT",
            Self::Cdata => "XML_ERROR_PARSING_CDATA",
            Self::Comment => "XML_ERROR_PARSING_COMMENT",
            Self::Declaration => "XML_ERROR_PARSING_DECLARATION",
            Self::Unknown => "XML_ERROR_PARSING_UNKNOWN",
            Self::General => "XML_ERROR_PARSING",
        }
    }
}

impl fmt::Display for XmlError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoAttribute => write!(f, "attribute not found"),
            Self::WrongAttributeType => write!(f, "wrong attribute type"),
            Self::Io(err) => write!(f, "I/O error: {err}"),
            Self::Parse {
                kind,
                line,
                message,
            } => {
                write!(f, "parse error ({}) at line {line}", kind.error_name())?;
                if let Some(msg) = message {
                    write!(f, ": {msg}")?;
                }
                Ok(())
            }
            Self::EmptyDocument => write!(f, "empty document"),
            Self::MismatchedElement {
                line,
                expected,
                found,
            } => write!(
                f,
                "mismatched element at line {line}: expected </{expected}>, found </{found}>"
            ),
            Self::CanNotConvertText => write!(f, "cannot convert text to requested type"),
            Self::NoTextNode => write!(f, "no text node found"),
            Self::ElementDepthExceeded { line, max_depth } => {
                write!(
                    f,
                    "element depth exceeded maximum of {max_depth} at line {line}"
                )
            }
            Self::InvalidNodeId => write!(f, "invalid node ID (node was deleted or never existed)"),
        }
    }
}

impl std::error::Error for XmlError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::io::Error> for XmlError {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

impl Clone for XmlError {
    fn clone(&self) -> Self {
        match self {
            Self::NoAttribute => Self::NoAttribute,
            Self::WrongAttributeType => Self::WrongAttributeType,
            Self::Io(err) => Self::Io(std::io::Error::new(err.kind(), err.to_string())),
            Self::Parse {
                kind,
                line,
                message,
            } => Self::Parse {
                kind: *kind,
                line: *line,
                message: message.clone(),
            },
            Self::EmptyDocument => Self::EmptyDocument,
            Self::MismatchedElement {
                line,
                expected,
                found,
            } => Self::MismatchedElement {
                line: *line,
                expected: expected.clone(),
                found: found.clone(),
            },
            Self::CanNotConvertText => Self::CanNotConvertText,
            Self::NoTextNode => Self::NoTextNode,
            Self::ElementDepthExceeded { line, max_depth } => Self::ElementDepthExceeded {
                line: *line,
                max_depth: *max_depth,
            },
            Self::InvalidNodeId => Self::InvalidNodeId,
        }
    }
}

impl PartialEq for XmlError {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::NoAttribute, Self::NoAttribute)
            | (Self::WrongAttributeType, Self::WrongAttributeType)
            | (Self::EmptyDocument, Self::EmptyDocument)
            | (Self::CanNotConvertText, Self::CanNotConvertText)
            | (Self::NoTextNode, Self::NoTextNode)
            | (Self::InvalidNodeId, Self::InvalidNodeId) => true,
            (
                Self::Parse {
                    kind: k1, line: l1, ..
                },
                Self::Parse {
                    kind: k2, line: l2, ..
                },
            ) => k1 == k2 && l1 == l2,
            (
                Self::MismatchedElement {
                    line: l1,
                    expected: e1,
                    found: f1,
                },
                Self::MismatchedElement {
                    line: l2,
                    expected: e2,
                    found: f2,
                },
            ) => l1 == l2 && e1 == e2 && f1 == f2,
            (
                Self::ElementDepthExceeded {
                    line: l1,
                    max_depth: m1,
                },
                Self::ElementDepthExceeded {
                    line: l2,
                    max_depth: m2,
                },
            ) => l1 == l2 && m1 == m2,
            (Self::Io(a), Self::Io(b)) => a.kind() == b.kind(),
            _ => false,
        }
    }
}

impl Eq for XmlError {}

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

    #[test]
    fn error_names_match_tinyxml2() {
        assert_eq!(XmlError::NoAttribute.name(), "XML_NO_ATTRIBUTE");
        assert_eq!(
            XmlError::WrongAttributeType.name(),
            "XML_WRONG_ATTRIBUTE_TYPE"
        );
        assert_eq!(XmlError::EmptyDocument.name(), "XML_ERROR_EMPTY_DOCUMENT");
        assert_eq!(
            XmlError::CanNotConvertText.name(),
            "XML_CAN_NOT_CONVERT_TEXT"
        );
        assert_eq!(XmlError::NoTextNode.name(), "XML_NO_TEXT_NODE");
        assert_eq!(
            XmlError::ElementDepthExceeded {
                line: 1,
                max_depth: 500
            }
            .name(),
            "XML_ELEMENT_DEPTH_EXCEEDED"
        );
    }

    #[test]
    fn parse_error_names() {
        let cases = [
            (ParseErrorKind::Element, "XML_ERROR_PARSING_ELEMENT"),
            (ParseErrorKind::Attribute, "XML_ERROR_PARSING_ATTRIBUTE"),
            (ParseErrorKind::Text, "XML_ERROR_PARSING_TEXT"),
            (ParseErrorKind::Cdata, "XML_ERROR_PARSING_CDATA"),
            (ParseErrorKind::Comment, "XML_ERROR_PARSING_COMMENT"),
            (ParseErrorKind::Declaration, "XML_ERROR_PARSING_DECLARATION"),
            (ParseErrorKind::Unknown, "XML_ERROR_PARSING_UNKNOWN"),
            (ParseErrorKind::General, "XML_ERROR_PARSING"),
        ];
        for (kind, expected_name) in cases {
            let err = XmlError::Parse {
                kind,
                line: 1,
                message: None,
            };
            assert_eq!(err.name(), expected_name);
        }
    }

    #[test]
    fn error_line_numbers() {
        assert_eq!(XmlError::NoAttribute.line(), None);
        assert_eq!(
            XmlError::Parse {
                kind: ParseErrorKind::Element,
                line: 42,
                message: None,
            }
            .line(),
            Some(42)
        );
        assert_eq!(
            XmlError::MismatchedElement {
                line: 10,
                expected: "foo".into(),
                found: "bar".into(),
            }
            .line(),
            Some(10)
        );
        assert_eq!(
            XmlError::ElementDepthExceeded {
                line: 500,
                max_depth: 100,
            }
            .line(),
            Some(500)
        );
    }

    #[test]
    fn display_formatting() {
        assert_eq!(XmlError::NoAttribute.to_string(), "attribute not found");
        assert_eq!(XmlError::EmptyDocument.to_string(), "empty document");

        let parse_err = XmlError::Parse {
            kind: ParseErrorKind::Element,
            line: 5,
            message: Some("unexpected character".into()),
        };
        assert_eq!(
            parse_err.to_string(),
            "parse error (XML_ERROR_PARSING_ELEMENT) at line 5: unexpected character"
        );

        let mismatch = XmlError::MismatchedElement {
            line: 3,
            expected: "div".into(),
            found: "span".into(),
        };
        assert_eq!(
            mismatch.to_string(),
            "mismatched element at line 3: expected </div>, found </span>"
        );
    }

    #[test]
    fn error_is_send_sync() {
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}
        // XmlError contains std::io::Error which is Send + Sync
        assert_send::<XmlError>();
        assert_sync::<XmlError>();
    }

    #[test]
    fn io_error_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let xml_err: XmlError = io_err.into();
        assert_eq!(xml_err.name(), "XML_ERROR_FILE_READ_ERROR");
        assert!(xml_err.to_string().contains("file not found"));
    }

    #[test]
    fn error_clone_and_eq() {
        let err = XmlError::Parse {
            kind: ParseErrorKind::Element,
            line: 10,
            message: Some("test".into()),
        };
        let cloned = err.clone();
        assert_eq!(err, cloned);
    }

    #[test]
    fn std_error_source() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "not found");
        let xml_err = XmlError::Io(io_err);
        assert!(std::error::Error::source(&xml_err).is_some());

        assert!(std::error::Error::source(&XmlError::NoAttribute).is_none());
    }
}