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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! XSD Identity Constraints.
//!
//! This module implements validation for XSD identity constraints:
//!
//! ## Constraint Types
//! - `unique` - values must be unique within scope
//! - `key` - values must be unique and non-null
//! - `keyref` - values must reference an existing key
//!
//! ## Components
//! - `selector` - XPath expression selecting the scope
//! - `field` - XPath expression selecting the key value(s)

use std::collections::{HashMap, HashSet};

/// Type of identity constraint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstraintType {
    /// Values must be unique (null allowed)
    Unique,
    /// Values must be unique and non-null
    Key,
    /// Values must reference an existing key
    KeyRef,
}

/// Identity constraint definition from XSD.
#[derive(Debug, Clone)]
pub struct IdentityConstraint {
    /// Constraint name
    pub name: String,
    /// Type of constraint
    pub constraint_type: ConstraintType,
    /// XPath selector expression
    pub selector: String,
    /// XPath field expressions (one or more for composite keys)
    pub fields: Vec<String>,
    /// For keyref: the key being referenced
    pub refer: Option<String>,
}

impl IdentityConstraint {
    /// Creates a new unique constraint.
    pub fn unique(name: impl Into<String>, selector: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            constraint_type: ConstraintType::Unique,
            selector: selector.into(),
            fields: Vec::new(),
            refer: None,
        }
    }

    /// Creates a new key constraint.
    pub fn key(name: impl Into<String>, selector: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            constraint_type: ConstraintType::Key,
            selector: selector.into(),
            fields: Vec::new(),
            refer: None,
        }
    }

    /// Creates a new keyref constraint.
    pub fn keyref(
        name: impl Into<String>,
        selector: impl Into<String>,
        refer: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            constraint_type: ConstraintType::KeyRef,
            selector: selector.into(),
            fields: Vec::new(),
            refer: Some(refer.into()),
        }
    }

    /// Adds a field expression.
    pub fn with_field(mut self, field: impl Into<String>) -> Self {
        self.fields.push(field.into());
        self
    }

    /// Adds multiple field expressions.
    pub fn with_fields(mut self, fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.fields.extend(fields.into_iter().map(Into::into));
        self
    }
}

/// A key value (possibly composite).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyValue {
    /// Field values making up the key
    pub values: Vec<String>,
}

impl KeyValue {
    /// Creates a new key value from field values.
    pub fn new(values: Vec<String>) -> Self {
        Self { values }
    }

    /// Creates a single-field key value.
    pub fn single(value: impl Into<String>) -> Self {
        Self {
            values: vec![value.into()],
        }
    }

    /// Returns true if any field is null/empty.
    pub fn has_null(&self) -> bool {
        self.values.iter().any(|v| v.is_empty())
    }

    /// Returns true if all fields are present.
    pub fn is_complete(&self, expected_fields: usize) -> bool {
        self.values.len() == expected_fields && !self.has_null()
    }
}

/// Error types for constraint validation.
#[derive(Debug, Clone)]
pub enum ConstraintError {
    /// Duplicate key value found
    DuplicateKey {
        /// Name of the constraint that found the duplicate
        constraint: String,
        /// The duplicate key value
        value: KeyValue,
    },
    /// Null value in key (not allowed for key, allowed for unique)
    NullKeyValue {
        /// Name of the constraint with null value
        constraint: String,
        /// Index of the field that is null
        field_index: usize,
    },
    /// Key reference not found
    KeyRefNotFound {
        /// Name of the keyref constraint
        constraint: String,
        /// Name of the referenced key constraint
        refer: String,
        /// The value that was not found
        value: KeyValue,
    },
    /// Selector expression error
    SelectorError {
        /// Name of the constraint with selector error
        constraint: String,
        /// Error message
        message: String,
    },
    /// Field expression error
    FieldError {
        /// Name of the constraint with field error
        constraint: String,
        /// Index of the field with error
        field_index: usize,
        /// Error message
        message: String,
    },
}

impl std::fmt::Display for ConstraintError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConstraintError::DuplicateKey { constraint, value } => {
                write!(
                    f,
                    "duplicate value {:?} in constraint '{}'",
                    value.values, constraint
                )
            }
            ConstraintError::NullKeyValue {
                constraint,
                field_index,
            } => {
                write!(
                    f,
                    "null value in key field {} of constraint '{}'",
                    field_index, constraint
                )
            }
            ConstraintError::KeyRefNotFound {
                constraint,
                refer,
                value,
            } => {
                write!(
                    f,
                    "keyref '{}' value {:?} not found in key '{}'",
                    constraint, value.values, refer
                )
            }
            ConstraintError::SelectorError {
                constraint,
                message,
            } => {
                write!(
                    f,
                    "selector error in constraint '{}': {}",
                    constraint, message
                )
            }
            ConstraintError::FieldError {
                constraint,
                field_index,
                message,
            } => {
                write!(
                    f,
                    "field {} error in constraint '{}': {}",
                    field_index, constraint, message
                )
            }
        }
    }
}

impl std::error::Error for ConstraintError {}

/// Collected key values for a constraint.
#[derive(Debug, Clone, Default)]
pub struct KeyValueSet {
    /// Unique key values
    values: HashSet<KeyValue>,
    /// Constraint name
    constraint_name: String,
    /// Expected number of fields (for composite key validation)
    #[allow(dead_code)]
    field_count: usize,
}

impl KeyValueSet {
    /// Creates a new key value set.
    pub fn new(constraint_name: impl Into<String>, field_count: usize) -> Self {
        Self {
            values: HashSet::new(),
            constraint_name: constraint_name.into(),
            field_count,
        }
    }

    /// Adds a key value, checking for duplicates.
    ///
    /// Returns error if duplicate (for unique/key constraints).
    pub fn add(&mut self, value: KeyValue) -> Result<(), ConstraintError> {
        if !self.values.insert(value.clone()) {
            return Err(ConstraintError::DuplicateKey {
                constraint: self.constraint_name.clone(),
                value,
            });
        }
        Ok(())
    }

    /// Checks if a value exists in the set.
    pub fn contains(&self, value: &KeyValue) -> bool {
        self.values.contains(value)
    }

    /// Returns all values in the set.
    pub fn values(&self) -> impl Iterator<Item = &KeyValue> {
        self.values.iter()
    }

    /// Returns the number of values.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns true if empty.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }
}

/// Constraint validator that tracks key values during validation.
#[derive(Debug, Default)]
pub struct ConstraintValidator {
    /// Collected key values indexed by constraint name
    key_values: HashMap<String, KeyValueSet>,
    /// Pending keyref validations (checked at end)
    pending_keyrefs: Vec<PendingKeyRef>,
}

#[derive(Debug)]
struct PendingKeyRef {
    constraint_name: String,
    refer: String,
    value: KeyValue,
}

impl ConstraintValidator {
    /// Creates a new constraint validator.
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers a key constraint and initializes its value set.
    pub fn register_key(&mut self, name: &str, field_count: usize) {
        self.key_values
            .insert(name.to_string(), KeyValueSet::new(name, field_count));
    }

    /// Adds a key value from a unique or key constraint.
    pub fn add_key_value(
        &mut self,
        constraint: &IdentityConstraint,
        value: KeyValue,
    ) -> Result<(), ConstraintError> {
        // For key constraints, null values are not allowed
        if constraint.constraint_type == ConstraintType::Key && value.has_null() {
            for (idx, v) in value.values.iter().enumerate() {
                if v.is_empty() {
                    return Err(ConstraintError::NullKeyValue {
                        constraint: constraint.name.clone(),
                        field_index: idx,
                    });
                }
            }
        }

        // Get or create the key value set
        let set = self
            .key_values
            .entry(constraint.name.clone())
            .or_insert_with(|| KeyValueSet::new(&constraint.name, constraint.fields.len()));

        // For unique constraints, null values don't participate in uniqueness check
        if constraint.constraint_type == ConstraintType::Unique && value.has_null() {
            return Ok(());
        }

        set.add(value)
    }

    /// Adds a keyref value to be validated at the end.
    pub fn add_keyref_value(&mut self, constraint: &IdentityConstraint, value: KeyValue) {
        if let Some(refer) = &constraint.refer {
            // Null keyref values don't need to match
            if value.has_null() {
                return;
            }

            self.pending_keyrefs.push(PendingKeyRef {
                constraint_name: constraint.name.clone(),
                refer: refer.clone(),
                value,
            });
        }
    }

    /// Validates all pending keyrefs against collected keys.
    pub fn validate_keyrefs(&self) -> Result<(), Vec<ConstraintError>> {
        let mut errors = Vec::new();

        for keyref in &self.pending_keyrefs {
            if let Some(key_set) = self.key_values.get(&keyref.refer) {
                if !key_set.contains(&keyref.value) {
                    errors.push(ConstraintError::KeyRefNotFound {
                        constraint: keyref.constraint_name.clone(),
                        refer: keyref.refer.clone(),
                        value: keyref.value.clone(),
                    });
                }
            } else {
                // Referenced key doesn't exist
                errors.push(ConstraintError::KeyRefNotFound {
                    constraint: keyref.constraint_name.clone(),
                    refer: keyref.refer.clone(),
                    value: keyref.value.clone(),
                });
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Resets the validator state.
    pub fn reset(&mut self) {
        self.key_values.clear();
        self.pending_keyrefs.clear();
    }

    /// Gets the key value set for a constraint.
    pub fn get_key_values(&self, name: &str) -> Option<&KeyValueSet> {
        self.key_values.get(name)
    }
}

/// Compiled identity constraint ready for validation.
#[derive(Debug, Clone)]
pub struct CompiledConstraint {
    /// Constraint name
    pub name: String,
    /// Constraint type
    pub constraint_type: ConstraintType,
    /// Compiled selector XPath (as string for now)
    pub selector_xpath: String,
    /// Compiled field XPaths
    pub field_xpaths: Vec<String>,
    /// Referenced key name (for keyref)
    pub refer: Option<String>,
}

impl CompiledConstraint {
    /// Creates a new compiled constraint.
    pub fn new(constraint: &IdentityConstraint) -> Self {
        Self {
            name: constraint.name.clone(),
            constraint_type: constraint.constraint_type,
            selector_xpath: constraint.selector.clone(),
            field_xpaths: constraint.fields.clone(),
            refer: constraint.refer.clone(),
        }
    }
}

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

    #[test]
    fn test_unique_constraint() {
        let constraint = IdentityConstraint::unique("id-unique", ".//item").with_field("@id");

        let mut validator = ConstraintValidator::new();

        // Add first value
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::single("1"))
                .is_ok()
        );

        // Add different value
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::single("2"))
                .is_ok()
        );

        // Add duplicate - should fail
        let result = validator.add_key_value(&constraint, KeyValue::single("1"));
        assert!(matches!(result, Err(ConstraintError::DuplicateKey { .. })));
    }

    #[test]
    fn test_key_constraint_no_nulls() {
        let constraint = IdentityConstraint::key("item-key", ".//item").with_field("@id");

        let mut validator = ConstraintValidator::new();

        // Null value should fail for key
        let result = validator.add_key_value(&constraint, KeyValue::single(""));
        assert!(matches!(result, Err(ConstraintError::NullKeyValue { .. })));
    }

    #[test]
    fn test_unique_constraint_allows_nulls() {
        let constraint = IdentityConstraint::unique("id-unique", ".//item").with_field("@id");

        let mut validator = ConstraintValidator::new();

        // Null values are allowed for unique (they don't participate in uniqueness)
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::single(""))
                .is_ok()
        );
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::single(""))
                .is_ok()
        );
    }

    #[test]
    fn test_keyref_validation() {
        let key = IdentityConstraint::key("category-key", ".//category").with_field("@id");
        let keyref = IdentityConstraint::keyref("item-category", ".//item", "category-key")
            .with_field("@category");

        let mut validator = ConstraintValidator::new();

        // Add key values
        validator
            .add_key_value(&key, KeyValue::single("cat1"))
            .unwrap();
        validator
            .add_key_value(&key, KeyValue::single("cat2"))
            .unwrap();

        // Add valid keyref
        validator.add_keyref_value(&keyref, KeyValue::single("cat1"));

        // Add invalid keyref
        validator.add_keyref_value(&keyref, KeyValue::single("cat3"));

        // Validate keyrefs
        let result = validator.validate_keyrefs();
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert_eq!(errors.len(), 1);
    }

    #[test]
    fn test_composite_key() {
        let constraint =
            IdentityConstraint::key("composite-key", ".//item").with_fields(["@type", "@id"]);

        let mut validator = ConstraintValidator::new();

        // Add composite key
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::new(vec!["A".into(), "1".into()]))
                .is_ok()
        );

        // Same first field, different second - OK
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::new(vec!["A".into(), "2".into()]))
                .is_ok()
        );

        // Different first field, same second - OK
        assert!(
            validator
                .add_key_value(&constraint, KeyValue::new(vec!["B".into(), "1".into()]))
                .is_ok()
        );

        // Duplicate composite - should fail
        let result =
            validator.add_key_value(&constraint, KeyValue::new(vec!["A".into(), "1".into()]));
        assert!(matches!(result, Err(ConstraintError::DuplicateKey { .. })));
    }

    #[test]
    fn test_key_value_set() {
        let mut set = KeyValueSet::new("test", 1);

        assert!(set.add(KeyValue::single("a")).is_ok());
        assert!(set.add(KeyValue::single("b")).is_ok());
        assert!(set.contains(&KeyValue::single("a")));
        assert!(!set.contains(&KeyValue::single("c")));
        assert_eq!(set.len(), 2);
    }
}