fastxml 0.8.1

A fast, memory-efficient XML library with XPath and XSD validation support
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
480
481
482
483
484
485
486
487
488
//! Error types for fastxml.

use std::fmt;
use std::io;

use crate::namespace::error::NamespaceError;
use crate::node::error::NodeError;
use crate::parser::error::ParseError;
use crate::schema::error::SchemaError;
use crate::schema::fetcher::error::FetchError;
use crate::schema::xsd::error::XsdParseError;
use crate::xpath::error::{XPathEvalError, XPathSyntaxError};

/// Location information for errors, providing line, column, byte offset, and optional XPath.
///
/// This struct provides a lightweight way to attach location information to any error.
/// It can be used across various modules (transform, parser, validator) to provide
/// consistent error location reporting.
///
/// # Examples
///
/// ```
/// use fastxml::error::ErrorLocation;
///
/// // Create from byte offset with line/column calculation
/// let input = "line1\nline2\nline3";
/// let loc = ErrorLocation::from_offset_with_input(6, input);
/// assert_eq!(loc.line, Some(2));
/// assert_eq!(loc.column, Some(1));
///
/// // Multi-byte UTF-8 characters are counted as single columns
/// let input = "あいう\nえお";
/// // "あいう" is 9 bytes (3 bytes each), "\n" is 1 byte, "え" starts at byte 10
/// let loc = ErrorLocation::from_offset_with_input(10, input);
/// assert_eq!(loc.line, Some(2));
/// assert_eq!(loc.column, Some(1)); // First char of line 2
///
/// // Add XPath information
/// let loc = loc.with_xpath("/root/item[1]".to_string());
/// assert!(loc.to_string().contains("/root/item[1]"));
/// ```
#[derive(Debug, Clone, Default)]
pub struct ErrorLocation {
    /// Line number (1-indexed)
    pub line: Option<usize>,
    /// Column number (1-indexed)
    pub column: Option<usize>,
    /// Byte offset from the beginning of the input
    pub byte_offset: Option<usize>,
    /// XPath-like path to the error location
    pub xpath: Option<String>,
}

impl ErrorLocation {
    /// Creates an empty error location.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates an error location with only byte offset.
    pub fn from_offset(byte_offset: usize) -> Self {
        Self {
            byte_offset: Some(byte_offset),
            ..Default::default()
        }
    }

    /// Creates an error location with line and column (calculated from byte offset).
    pub fn from_offset_with_input(byte_offset: usize, input: &str) -> Self {
        let (line, column) = Self::calculate_line_column(input, byte_offset);
        Self {
            line: Some(line),
            column: Some(column),
            byte_offset: Some(byte_offset),
            xpath: None,
        }
    }

    /// Creates an error location with line and column directly.
    pub fn from_line_column(line: usize, column: usize) -> Self {
        Self {
            line: Some(line),
            column: Some(column),
            byte_offset: None,
            xpath: None,
        }
    }

    /// Sets the XPath-like path.
    pub fn with_xpath(mut self, xpath: String) -> Self {
        self.xpath = Some(xpath);
        self
    }

    /// Sets the byte offset.
    pub fn with_offset(mut self, offset: usize) -> Self {
        self.byte_offset = Some(offset);
        self
    }

    /// Returns true if this location has any position information.
    pub fn has_position(&self) -> bool {
        self.line.is_some() || self.byte_offset.is_some()
    }

    /// Calculates line and column from byte offset in the input string.
    ///
    /// Returns (line, column) where both are 1-indexed.
    /// Column is counted in Unicode characters (not bytes), so multi-byte
    /// characters like Japanese are counted as single columns.
    pub fn calculate_line_column(input: &str, byte_offset: usize) -> (usize, usize) {
        let mut line = 1;
        let mut column = 1;

        for (pos, ch) in input.char_indices() {
            if pos >= byte_offset {
                break;
            }
            if ch == '\n' {
                line += 1;
                column = 1;
            } else {
                column += 1;
            }
        }

        (line, column)
    }
}

impl fmt::Display for ErrorLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut parts = Vec::new();

        if let (Some(line), Some(col)) = (self.line, self.column) {
            parts.push(format!("line {}:{}", line, col));
        } else if let Some(offset) = self.byte_offset {
            parts.push(format!("position {}", offset));
        }

        if let Some(xpath) = &self.xpath {
            parts.push(format!("at {}", xpath));
        }

        write!(f, "{}", parts.join(", "))
    }
}

/// Main error type for fastxml operations.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// XML parsing error
    #[error("parse error: {0}")]
    Parse(#[from] ParseError),

    /// IO error
    #[error("io error: {0}")]
    Io(#[from] io::Error),

    /// XPath syntax error
    #[error("xpath syntax error: {0}")]
    XPathSyntax(#[from] XPathSyntaxError),

    /// XPath evaluation error
    #[error("xpath evaluation error: {0}")]
    XPathEval(#[from] XPathEvalError),

    /// Schema error
    #[error("schema error: {0}")]
    Schema(#[from] SchemaError),

    /// Validation error
    #[error("validation error: {message}")]
    Validation {
        /// Error message
        message: String,
        /// Line number where the error occurred
        line: Option<usize>,
        /// Column number where the error occurred
        column: Option<usize>,
    },

    /// Namespace error
    #[error("namespace error: {0}")]
    Namespace(#[from] NamespaceError),

    /// Node-related error
    #[error("node error: {0}")]
    Node(#[from] NodeError),

    /// Invalid operation
    #[error("invalid operation: {0}")]
    InvalidOperation(String),

    /// Network/fetch error
    #[error("fetch error: {0}")]
    Fetch(#[from] FetchError),

    /// UTF-8 encoding error
    #[error("utf8 error: {0}")]
    Utf8(#[from] std::str::Utf8Error),

    /// String UTF-8 error
    #[error("string utf8 error: {0}")]
    FromUtf8(#[from] std::string::FromUtf8Error),

    /// XSD parsing error
    #[error("xsd parse error: {0}")]
    XsdParse(#[from] XsdParseError),
}

impl From<quick_xml::Error> for Error {
    fn from(err: quick_xml::Error) -> Self {
        ParseError::Generic {
            message: err.to_string(),
        }
        .into()
    }
}

impl From<quick_xml::events::attributes::AttrError> for Error {
    fn from(err: quick_xml::events::attributes::AttrError) -> Self {
        ParseError::AttributeError {
            message: err.to_string(),
        }
        .into()
    }
}

/// Result type alias for fastxml operations.
pub type Result<T> = std::result::Result<T, Error>;

/// Error severity level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum ErrorLevel {
    /// Warning - validation can continue
    Warning,
    /// Error - validation issue but can continue
    #[default]
    Error,
    /// Fatal - validation cannot continue
    Fatal,
}

impl std::fmt::Display for ErrorLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorLevel::Warning => write!(f, "warning"),
            ErrorLevel::Error => write!(f, "error"),
            ErrorLevel::Fatal => write!(f, "fatal"),
        }
    }
}

/// Structured error for schema validation, compatible with libxml's StructuredError.
#[derive(Debug, Clone)]
pub struct StructuredError {
    /// Error message
    pub message: String,
    /// Location information (line, column, byte_offset, xpath)
    pub location: ErrorLocation,
    /// Error type classification
    pub error_type: ValidationErrorType,
    /// Error severity level
    pub level: ErrorLevel,
    /// Name of the element or attribute that caused the error
    pub node_name: Option<String>,
    /// Expected value or type (for type mismatch errors)
    pub expected: Option<String>,
    /// Actual value found (for type mismatch errors)
    pub found: Option<String>,
}

impl Default for StructuredError {
    fn default() -> Self {
        Self {
            message: String::new(),
            location: ErrorLocation::default(),
            error_type: ValidationErrorType::Other,
            level: ErrorLevel::Error,
            node_name: None,
            expected: None,
            found: None,
        }
    }
}

impl StructuredError {
    /// Creates a new error with the given message and type.
    pub fn new(message: impl Into<String>, error_type: ValidationErrorType) -> Self {
        Self {
            message: message.into(),
            error_type,
            ..Default::default()
        }
    }

    /// Sets the line number.
    pub fn with_line(mut self, line: usize) -> Self {
        self.location.line = Some(line);
        self
    }

    /// Sets the column number.
    pub fn with_column(mut self, column: usize) -> Self {
        self.location.column = Some(column);
        self
    }

    /// Sets the byte offset.
    pub fn with_byte_offset(mut self, offset: usize) -> Self {
        self.location.byte_offset = Some(offset);
        self
    }

    /// Sets the error level.
    pub fn with_level(mut self, level: ErrorLevel) -> Self {
        self.level = level;
        self
    }

    /// Sets the element path (stored in location.xpath).
    pub fn with_element_path(mut self, path: impl Into<String>) -> Self {
        self.location.xpath = Some(path.into());
        self
    }

    /// Sets the node name.
    pub fn with_node_name(mut self, name: impl Into<String>) -> Self {
        self.node_name = Some(name.into());
        self
    }

    /// Sets the expected value.
    pub fn with_expected(mut self, expected: impl Into<String>) -> Self {
        self.expected = Some(expected.into());
        self
    }

    /// Sets the found value.
    pub fn with_found(mut self, found: impl Into<String>) -> Self {
        self.found = Some(found.into());
        self
    }

    /// Returns true if this is a warning.
    pub fn is_warning(&self) -> bool {
        self.level == ErrorLevel::Warning
    }

    /// Returns true if this is an error or fatal.
    pub fn is_error(&self) -> bool {
        self.level >= ErrorLevel::Error
    }

    /// Sets location information from an ErrorLocation (merges non-None fields).
    pub fn with_location(mut self, location: &ErrorLocation) -> Self {
        if let Some(line) = location.line {
            self.location.line = Some(line);
        }
        if let Some(column) = location.column {
            self.location.column = Some(column);
        }
        if let Some(offset) = location.byte_offset {
            self.location.byte_offset = Some(offset);
        }
        if let Some(ref xpath) = location.xpath {
            self.location.xpath = Some(xpath.clone());
        }
        self
    }

    /// Sets the entire location, replacing any existing location.
    pub fn set_location(mut self, location: ErrorLocation) -> Self {
        self.location = location;
        self
    }

    /// Returns the line number (convenience accessor).
    pub fn line(&self) -> Option<usize> {
        self.location.line
    }

    /// Returns the column number (convenience accessor).
    pub fn column(&self) -> Option<usize> {
        self.location.column
    }

    /// Returns the byte offset (convenience accessor).
    pub fn byte_offset(&self) -> Option<usize> {
        self.location.byte_offset
    }

    /// Returns the element path (convenience accessor).
    pub fn element_path(&self) -> Option<&str> {
        self.location.xpath.as_deref()
    }

    /// Calculates and sets line/column from byte_offset using the given input.
    ///
    /// This is useful when you have a byte offset but need to display line/column.
    pub fn calculate_line_column(mut self, input: &str) -> Self {
        if let Some(offset) = self.location.byte_offset {
            let (line, column) = ErrorLocation::calculate_line_column(input, offset);
            self.location.line = Some(line);
            self.location.column = Some(column);
        }
        self
    }
}

impl From<&StructuredError> for ErrorLocation {
    fn from(err: &StructuredError) -> Self {
        err.location.clone()
    }
}

impl std::fmt::Display for StructuredError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Format: [level] location: message
        write!(f, "[{}] ", self.level)?;

        if let Some(ref path) = self.location.xpath {
            write!(f, "{}", path)?;
            if let Some(line) = self.location.line {
                write!(f, " (line {})", line)?;
            }
            write!(f, ": ")?;
        } else if let (Some(line), Some(col)) = (self.location.line, self.location.column) {
            write!(f, "{}:{}: ", line, col)?;
        } else if let Some(line) = self.location.line {
            write!(f, "line {}: ", line)?;
        } else if let Some(offset) = self.location.byte_offset {
            write!(f, "offset {}: ", offset)?;
        }

        write!(f, "{}", self.message)?;

        if let (Some(expected), Some(found)) = (&self.expected, &self.found) {
            write!(f, " (expected: {}, found: {})", expected, found)?;
        }

        Ok(())
    }
}

/// Classification of validation errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationErrorType {
    /// Unknown or unrecognized element
    UnknownElement,
    /// Unknown or unrecognized attribute
    UnknownAttribute,
    /// Missing required element
    MissingRequiredElement,
    /// Missing required attribute
    MissingRequiredAttribute,
    /// Invalid attribute value
    InvalidAttributeValue,
    /// Invalid element content
    InvalidContent,
    /// Invalid text content (type mismatch)
    InvalidTextContent,
    /// Element appears too many times
    TooManyOccurrences,
    /// Element appears too few times
    TooFewOccurrences,
    /// Element out of order (sequence violation)
    ElementOutOfOrder,
    /// Unexpected element in choice/sequence
    UnexpectedElement,
    /// Namespace mismatch
    NamespaceMismatch,
    /// Schema not found
    SchemaNotFound,
    /// Identity constraint violation (unique, key, keyref)
    IdentityConstraint,
    /// Type definition not found
    TypeNotFound,
    /// Facet constraint violation
    FacetViolation,
    /// Content model violation
    ContentModelViolation,
    /// Unclosed element at end of document
    UnclosedElement,
    /// Generic validation error
    Other,
}