deser-xml 0.10.1

XML support for deser
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::collections::BTreeMap;

use deser::Deserialize;
use deser::de::{DuplicateKeys, Limits};
use deser_path::{Path, PathLayer};
use deser_value::Value;
use deser_xml::{Deserializer, DeserializerConfig, from_str};

fn to_json(xml: &str) -> String {
    deser_json::to_string(&from_str::<Value>(xml).unwrap()).unwrap()
}

#[test]
fn test_data_model() {
    assert_eq!(to_json("<a>1</a>"), r#""1""#);
    assert_eq!(to_json("<a/>"), r#""""#);
    assert_eq!(to_json("<a></a>"), r#""""#);
    assert_eq!(to_json(r#"<a href="x"/>"#), r#"{"@href":"x"}"#);
    assert_eq!(
        to_json(r#"<a href="x">y</a>"#),
        r#"{"@href":"x","$text":"y"}"#
    );
    assert_eq!(
        to_json("<a> <b>1</b>\n  <c>2</c> </a>"),
        r#"{"b":"1","c":"2"}"#
    );
    // repeated elements are grouped by values
    assert_eq!(
        to_json("<a><b>1</b><c/><b>2</b></a>"),
        r#"{"b":["1","2"],"c":""}"#
    );
    // mixed content
    assert_eq!(
        to_json("<p>x <b>y</b> z</p>"),
        r#"{"$text":["x "," z"],"b":"y"}"#
    );
    // the prolog, comments and processing instructions are not data
    assert_eq!(
        to_json("<?xml version=\"1.0\"?>\n<!-- c --><!DOCTYPE a><a><?pi x?>1<!-- c -->2</a>\n"),
        r#""12""#
    );
}

#[test]
fn test_text() {
    // references, CDATA and line breaks
    assert_eq!(
        from_str::<String>("<a>a &amp; b &lt;&#x41;&#66;&gt; <![CDATA[<c>]]></a>").unwrap(),
        "a & b <AB> <c>"
    );
    assert_eq!(from_str::<String>("<a>x\r\ny</a>").unwrap(), "x\ny");
    // attribute values are normalized, references are kept
    let value: BTreeMap<String, String> = from_str("<a x=\"1 &amp;\n2&#10;\"/>").unwrap();
    assert_eq!(value["@x"], "1 & 2\n");
    let err = from_str::<String>("<a>&custom;</a>").unwrap_err();
    assert_eq!(err.message(), "unknown entity `&custom;`");

    // text without references is borrowed
    #[derive(Debug, Deserialize)]
    struct Borrowed<'a> {
        name: &'a str,
        #[deser(rename = "@id")]
        id: &'a str,
    }
    let input = String::from(r#"<a id="x1"><name>jane</name></a>"#);
    let value: Borrowed = from_str(&input).unwrap();
    assert_eq!((value.name, value.id), ("jane", "x1"));
}

#[test]
fn test_structs() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct Address {
        city: String,
        #[deser(rename = "@country")]
        country: String,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Person {
        #[deser(rename = "@id")]
        id: u32,
        name: String,
        age: Option<u32>,
        email: Option<String>,
        active: bool,
        address: Address,
        tag: Vec<String>,
        phone: Vec<String>,
    }

    let person: Person = from_str(
        r#"
        <person id="7">
          <name>Jane</name>
          <tag>a</tag>
          <age/>
          <active>true</active>
          <address country="AT"><city>Vienna</city></address>
          <tag>b</tag>
        </person>
    "#,
    )
    .unwrap();
    assert_eq!(
        person,
        Person {
            id: 7,
            name: "Jane".into(),
            age: None,
            email: None,
            active: true,
            address: Address {
                city: "Vienna".into(),
                country: "AT".into(),
            },
            tag: vec!["a".into(), "b".into()],
            phone: vec![],
        }
    );

    // a single element is a collection of one
    #[derive(Debug, Deserialize, PartialEq)]
    struct Order {
        item: Vec<Item>,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Item {
        #[deser(rename = "@sku")]
        sku: String,
        qty: u32,
    }
    assert_eq!(
        from_str::<Order>(r#"<order><item sku="a"><qty>2</qty></item></order>"#).unwrap(),
        Order {
            item: vec![Item {
                sku: "a".into(),
                qty: 2
            }]
        }
    );

    // a repeated element for a single value
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Single {
        name: String,
    }
    let err = from_str::<Single>("<a><name>x</name><name>y</name></a>").unwrap_err();
    assert_eq!(err.message(), "duplicate field `name`");
    let last = deser::Context::with(DuplicateKeys::Last);
    assert_eq!(
        Deserializer::from_str("<a><name>x</name><name>y</name></a>")
            .deserialize_with::<Single, _>(|driver| driver.set_context(last.clone()))
            .unwrap()
            .name,
        "y"
    );

    // maps of child elements
    let map: BTreeMap<String, u32> = from_str("<a><x>1</x><y>2</y></a>").unwrap();
    assert_eq!(map, BTreeMap::from([("x".into(), 1), ("y".into(), 2)]));
    let map: BTreeMap<String, Vec<u32>> = from_str("<a><x>1</x><y>2</y><x>3</x></a>").unwrap();
    assert_eq!(map["x"], [1, 3]);
}

#[test]
fn test_enums() {
    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(rename_all = "lowercase")]
    enum Shape {
        Circle {
            #[deser(rename = "@r")]
            r: f64,
        },
        Square {
            side: f64,
        },
        Empty,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Drawing {
        shape: Vec<Shape>,
    }

    assert_eq!(
        from_str::<Drawing>(
            r#"<drawing>
                 <shape><circle r="1.5"/></shape>
                 <shape><square><side>2</side></square></shape>
                 <shape>empty</shape>
               </drawing>"#
        )
        .unwrap(),
        Drawing {
            shape: vec![
                Shape::Circle { r: 1.5 },
                Shape::Square { side: 2.0 },
                Shape::Empty
            ]
        }
    );

    // an attribute as tag of internally tagged enums
    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(tag = "@type", rename_all = "lowercase")]
    enum Payment {
        Card { number: String },
        Transfer { iban: String },
    }
    assert_eq!(
        from_str::<Payment>(r#"<payment type="transfer"><iban>AT12</iban></payment>"#).unwrap(),
        Payment::Transfer {
            iban: "AT12".into()
        }
    );
    // the tag can come after the fields (they are buffered)
    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(tag = "kind", rename_all = "lowercase")]
    enum Event {
        Click { x: u32, tag: Vec<String> },
    }
    assert_eq!(
        from_str::<Event>("<e><x>1</x><tag>a</tag><kind>click</kind><tag>b</tag></e>").unwrap(),
        Event::Click {
            x: 1,
            tag: vec!["a".into(), "b".into()]
        }
    );

    // untagged
    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(untagged)]
    enum Value {
        Number(u32),
        Text(String),
    }
    assert_eq!(from_str::<Value>("<v>42</v>").unwrap(), Value::Number(42));
    assert_eq!(
        from_str::<Value>("<v>x</v>").unwrap(),
        Value::Text("x".into())
    );
}

#[test]
fn test_flatten() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct Common {
        #[deser(rename = "@id")]
        id: String,
        #[deser(rename = "@class")]
        class: Option<String>,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Div {
        #[deser(flatten)]
        common: Common,
        p: Vec<String>,
        #[deser(flatten)]
        rest: BTreeMap<String, String>,
    }

    assert_eq!(
        from_str::<Div>(r#"<div id="x" data-a="1"><p>a</p><span>s</span><p>b</p></div>"#).unwrap(),
        Div {
            common: Common {
                id: "x".into(),
                class: None,
            },
            p: vec!["a".into(), "b".into()],
            rest: BTreeMap::from([("@data-a".into(), "1".into()), ("span".into(), "s".into())]),
        }
    );
}

#[test]
fn test_namespaces() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct Envelope {
        #[deser(rename = "soap:Body")]
        body: Body,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Body {
        #[deser(rename = "m:price")]
        price: u32,
    }

    // names as written
    let doc = r#"<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
                               xmlns:m="urn:shop">
                   <soap:Body><m:price>10</m:price></soap:Body>
                 </soap:Envelope>"#;
    assert_eq!(
        from_str::<Envelope>(doc).unwrap(),
        Envelope {
            body: Body { price: 10 }
        }
    );

    // with other prefixes in the document, the configured ones are used
    const CONFIG: DeserializerConfig = DeserializerConfig::new().namespaces(&[
        ("soap", "http://www.w3.org/2003/05/soap-envelope"),
        ("m", "urn:shop"),
    ]);
    let doc = r#"<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
                   <env:Body><price xmlns="urn:shop">10</price></env:Body>
                 </env:Envelope>"#;
    assert_eq!(
        CONFIG.from_str::<Envelope>(doc).unwrap(),
        Envelope {
            body: Body { price: 10 }
        }
    );
    // without the configuration the names are the ones of the document
    assert!(from_str::<Envelope>(doc).is_err());

    // attributes without prefix have no namespace
    const XLINK: DeserializerConfig =
        DeserializerConfig::new().namespaces(&[("xlink", "http://www.w3.org/1999/xlink")]);
    let value: BTreeMap<String, String> = XLINK
        .from_str(r#"<a xmlns:l="http://www.w3.org/1999/xlink" l:href="x" href="y"/>"#)
        .unwrap();
    assert_eq!(value["@xlink:href"], "x");
    assert_eq!(value["@href"], "y");
}

#[test]
fn test_resolve_namespaces() {
    deser_xml::namespace!(
        soap = "http://www.w3.org/2003/05/soap-envelope",
        shop = "urn:shop",
    );

    #[derive(Debug, Deserialize, PartialEq)]
    struct Envelope {
        #[deser(rename = soap!("Body"))]
        body: Body,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Body {
        #[deser(rename = shop!("price"))]
        price: Vec<Price>,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Price {
        #[deser(rename = shop!(@ "currency"))]
        currency: Option<String>,
        #[deser(rename = "@unit")]
        unit: Option<String>,
        #[deser(rename = "$text")]
        amount: u32,
    }

    const CONFIG: DeserializerConfig = DeserializerConfig::builder()
        .resolve_namespaces(true)
        .build();
    let expected = Envelope {
        body: Body {
            price: vec![
                Price {
                    currency: Some("EUR".into()),
                    unit: None,
                    amount: 10,
                },
                Price {
                    currency: None,
                    unit: Some("kg".into()),
                    amount: 20,
                },
            ],
        },
    };

    // the prefixes of the document do not matter
    for doc in [
        r#"<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:m="urn:shop">
             <soap:Body><m:price m:currency="EUR">10</m:price><m:price unit="kg">20</m:price></soap:Body>
           </soap:Envelope>"#,
        r#"<e:Envelope xmlns:e="http://www.w3.org/2003/05/soap-envelope">
             <e:Body xmlns="urn:shop" xmlns:s="urn:shop">
               <price s:currency="EUR">10</price><s:price unit="kg">20</s:price>
             </e:Body>
           </e:Envelope>"#,
    ] {
        assert_eq!(CONFIG.from_str::<Envelope>(doc).unwrap(), expected);
    }

    // other namespaces do not match
    let doc = r#"<e:Envelope xmlns:e="http://www.w3.org/2003/05/soap-envelope">
                   <e:Body><price xmlns="urn:other">10</price></e:Body>
                 </e:Envelope>"#;
    assert_eq!(
        CONFIG.from_str::<Envelope>(doc).unwrap(),
        Envelope {
            body: Body { price: vec![] }
        }
    );

    // the keys of dynamic values
    let value: BTreeMap<String, String> = CONFIG
        .from_str(
            r#"<a xmlns="urn:a" xmlns:l="http://www.w3.org/1999/xlink" xml:lang="en"
                  l:href="x" href="y"><b>1</b></a>"#,
        )
        .unwrap();
    assert_eq!(
        value.keys().collect::<Vec<_>>(),
        [
            "@href",
            "@xml:lang",
            "@{http://www.w3.org/1999/xlink}href",
            "{urn:a}b"
        ]
    );

    // configured prefixes are used instead
    const PREFIXED: DeserializerConfig = CONFIG.namespaces(&[("", "urn:a"), ("xl", "urn:l")]);
    let value: BTreeMap<String, String> = PREFIXED
        .from_str(r#"<a xmlns="urn:a" xmlns:l="urn:l" l:href="x"><b>1</b><l:c>2</l:c></a>"#)
        .unwrap();
    assert_eq!(value.keys().collect::<Vec<_>>(), ["@xl:href", "b", "xl:c"]);

    // prefixes that are not declared
    let err = CONFIG
        .from_str::<BTreeMap<String, String>>("<a><x:b>1</x:b></a>")
        .unwrap_err();
    assert_eq!(err.message(), "the prefix of `x:b` is not declared");
    // are passed on as written otherwise
    let value: BTreeMap<String, String> = from_str("<a><x:b>1</x:b></a>").unwrap();
    assert_eq!(value["x:b"], "1");
}

#[test]
fn test_config() {
    const CONFIG: DeserializerConfig = DeserializerConfig::builder()
        .attribute_prefix("")
        .text_key("#text")
        .build();
    let value: BTreeMap<String, String> = CONFIG.from_str(r#"<a href="x">y</a>"#).unwrap();
    assert_eq!(value["href"], "x");
    assert_eq!(value["#text"], "y");
}

#[test]
fn test_errors() {
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Item {
        qty: u32,
    }

    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Order {
        item: Vec<Item>,
    }

    let input = "<order>\n  <item><qty>1</qty></item>\n  <item><qty>x</qty></item>\n</order>";
    let err = Deserializer::from_str(input)
        .deserialize_with::<Order, _>(|driver| driver.push_layer(PathLayer::new()))
        .unwrap_err();
    assert_eq!(err.message(), "invalid value \"x\", expected u32");
    assert_eq!(err.attachment::<Path>().unwrap().to_string(), "item.qty");
    assert_eq!((err.line(), err.column()), (Some(3), Some(14)));

    // malformed documents
    for (input, message) in [
        ("", "no root element"),
        ("<a>", "unexpected end of input"),
        ("<a></b>", "invalid XML"),
        ("<a/><b/>", "more than one root element"),
        ("<a/>x", "text outside of the root element"),
        (r#"<a x="1" x="2"/>"#, "invalid attribute"),
    ] {
        let err = from_str::<Value>(input).unwrap_err();
        assert!(err.message().starts_with(message), "{input}: {err}");
    }
}

#[test]
fn test_limits() {
    // elements are maps if they have attributes or child elements
    let parse = |max_depth| {
        Deserializer::from_str("<a><b><c/></b></a>").deserialize_with::<Value, _>(|driver| {
            driver.set_context(deser::Context::with(
                Limits::builder().max_depth(max_depth).build(),
            ))
        })
    };
    assert!(parse(2).is_ok());
    assert_eq!(
        parse(1).unwrap_err().to_string(),
        "LimitExceeded: recursion limit exceeded at line 1 column 4"
    );
}

#[test]
fn test_values() {
    let value: Value = from_str(r#"<a x="1"><b>2</b><b>3</b></a>"#).unwrap();
    let map = value.as_map().unwrap();
    assert!(map.is_multimap());
    assert_eq!(map.order(), deser::Order::Significant);
    assert!(value["b"].as_seq().unwrap().is_repeated());

    // converting the value gives the same result as the document
    #[derive(Debug, Deserialize, PartialEq)]
    struct A {
        #[deser(rename = "@x")]
        x: u32,
        b: Vec<u32>,
        c: Vec<u32>,
    }
    assert_eq!(
        deser_value::from_value::<A>(&value).unwrap(),
        A {
            x: 1,
            b: vec![2, 3],
            c: vec![]
        }
    );
}

#[test]
fn test_elements_and_text() {
    // an element with attributes for a type that expects text is its text
    #[derive(Debug, Deserialize, PartialEq)]
    struct Measure {
        count: u32,
        length: Vec<f64>,
        label: Option<String>,
        note: Option<u32>,
    }
    assert_eq!(
        from_str::<Measure>(
            r#"<m>
                 <count unit="x">3</count>
                 <length unit="m">1.5</length><length>2</length>
                 <label lang="en"/>
                 <note xml:lang="en"/>
               </m>"#
        )
        .unwrap(),
        Measure {
            count: 3,
            length: vec![1.5, 2.0],
            label: Some("".into()),
            note: None,
        }
    );
    // elements with child elements are not text
    let err = from_str::<Measure>("<m><count><a/><b/></count></m>").unwrap_err();
    assert!(err.message().starts_with("unexpected map"), "{err}");
    let err = from_str::<Measure>("<m><count>1<b/>2</count></m>").unwrap_err();
    assert!(err.message().contains("more than one content"), "{err}");

    // an element that is only text for a struct is its text
    #[derive(Debug, Deserialize, PartialEq)]
    struct Price {
        #[deser(rename = "@currency")]
        currency: Option<String>,
        #[deser(rename = "$text")]
        amount: f64,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Product {
        price: Vec<Price>,
    }
    assert_eq!(
        from_str::<Product>(r#"<p><price currency="EUR">3</price><price>4.5</price></p>"#).unwrap(),
        Product {
            price: vec![
                Price {
                    currency: Some("EUR".into()),
                    amount: 3.0
                },
                Price {
                    currency: None,
                    amount: 4.5
                }
            ]
        }
    );

    // an empty element is a struct without fields
    #[derive(Debug, Deserialize, PartialEq)]
    struct Options {
        #[deser(default)]
        verbose: bool,
        #[deser(rename = "@level")]
        level: Option<u32>,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Config {
        options: Options,
    }
    assert_eq!(
        from_str::<Config>("<config><options/></config>").unwrap(),
        Config {
            options: Options {
                verbose: false,
                level: None
            }
        }
    );

    // the same through buffering (the tag of an internally tagged enum
    // comes last)
    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(tag = "@kind")]
    enum Item {
        Book { price: Price, pages: u32 },
    }
    assert_eq!(
        from_str::<Item>(r#"<item kind="Book"><price>3</price><pages n="x">10</pages></item>"#)
            .unwrap(),
        Item::Book {
            price: Price {
                currency: None,
                amount: 3.0
            },
            pages: 10
        }
    );
}