facet-xml 0.44.0

XML serialization for facet using the new format architecture - successor to facet-xml
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
//! Basic tests for facet-xml behavior that needs to be implemented.

use facet::Facet;
use facet_testhelpers::test;
use facet_xml as xml;

// ============================================================================
// xml::element - explicit single child element
// ============================================================================

#[test]
fn xml_element_single_child() {
    #[derive(Facet, Debug)]
    struct Inner {
        value: String,
    }

    #[derive(Facet, Debug)]
    struct Outer {
        #[facet(xml::element)]
        inner: Inner,
    }

    let result: Outer =
        facet_xml::from_str("<outer><inner><value>hello</value></inner></outer>").unwrap();
    assert_eq!(result.inner.value, "hello");
}

// ============================================================================
// xml::tag - capture element tag name
// ============================================================================

#[test]
fn xml_tag_captures_element_name() {
    #[derive(Facet, Debug)]
    struct AnyElement {
        #[facet(xml::tag)]
        tag: String,
        #[facet(xml::text)]
        content: String,
    }

    let result: AnyElement = facet_xml::from_str("<foo>hello</foo>").unwrap();
    assert_eq!(result.tag, "foo");
    assert_eq!(result.content, "hello");
}

#[test]
fn xml_tag_captures_different_tags() {
    #[derive(Facet, Debug)]
    struct AnyElement {
        #[facet(xml::tag)]
        tag: String,
        #[facet(xml::text)]
        content: String,
    }

    let result: AnyElement = facet_xml::from_str("<bar>world</bar>").unwrap();
    assert_eq!(result.tag, "bar");
    assert_eq!(result.content, "world");
}

// ============================================================================
// default - missing elements get default values
// ============================================================================

#[test]
fn default_for_missing_element() {
    #[derive(Facet, Debug)]
    struct Config {
        #[facet(default)]
        name: String,
        #[facet(default)]
        count: u32,
    }

    let result: Config = facet_xml::from_str("<config></config>").unwrap();
    assert_eq!(result.name, "");
    assert_eq!(result.count, 0);
}

#[test]
fn default_for_missing_attribute() {
    #[derive(Facet, Debug)]
    struct Element {
        #[facet(xml::attribute, default)]
        id: String,
        #[facet(xml::attribute, default)]
        count: u32,
    }

    let result: Element = facet_xml::from_str("<element/>").unwrap();
    assert_eq!(result.id, "");
    assert_eq!(result.count, 0);
}

#[test]
fn default_with_custom_value() {
    fn default_name() -> String {
        "unnamed".to_string()
    }

    #[derive(Facet, Debug)]
    struct Item {
        #[facet(default = default_name())]
        name: String,
    }

    let result: Item = facet_xml::from_str("<item></item>").unwrap();
    assert_eq!(result.name, "unnamed");
}

// ============================================================================
// alias - alternative names for fields
// ============================================================================

#[test]
fn alias_matches_alternative_name() {
    #[derive(Facet, Debug)]
    struct Person {
        #[facet(alias = "fullName")]
        name: String,
    }

    // Primary name works
    let result: Person = facet_xml::from_str("<person><name>Alice</name></person>").unwrap();
    assert_eq!(result.name, "Alice");

    // Alias also works
    let result: Person = facet_xml::from_str("<person><fullName>Bob</fullName></person>").unwrap();
    assert_eq!(result.name, "Bob");
}

#[test]
fn alias_for_attribute() {
    #[derive(Facet, Debug)]
    struct Element {
        #[facet(xml::attribute, alias = "identifier")]
        id: String,
    }

    // Primary name works
    let result: Element = facet_xml::from_str(r#"<element id="123"/>"#).unwrap();
    assert_eq!(result.id, "123");

    // Alias also works
    let result: Element = facet_xml::from_str(r#"<element identifier="456"/>"#).unwrap();
    assert_eq!(result.id, "456");
}

// ============================================================================
// deny_unknown_fields - reject unexpected elements/attributes
// ============================================================================

#[test]
fn deny_unknown_fields_rejects_unknown_element() {
    #[derive(Facet, Debug)]
    #[facet(deny_unknown_fields)]
    struct Strict {
        name: String,
    }

    let result =
        facet_xml::from_str::<Strict>("<strict><name>ok</name><extra>bad</extra></strict>");
    assert!(result.is_err(), "Should reject unknown element <extra>");
}

#[test]
fn deny_unknown_fields_rejects_unknown_attribute() {
    #[derive(Facet, Debug)]
    #[facet(deny_unknown_fields)]
    struct Strict {
        #[facet(xml::attribute)]
        id: String,
    }

    let result = facet_xml::from_str::<Strict>(r#"<strict id="1" extra="bad"/>"#);
    assert!(result.is_err(), "Should reject unknown attribute extra=");
}

#[test]
fn without_deny_unknown_fields_ignores_extra() {
    #[derive(Facet, Debug)]
    struct Lenient {
        name: String,
    }

    // Should succeed, ignoring the extra element
    let result: Lenient =
        facet_xml::from_str("<lenient><name>ok</name><extra>ignored</extra></lenient>").unwrap();
    assert_eq!(result.name, "ok");
}

// ============================================================================
// Option<T> - optional fields
// ============================================================================

#[test]
fn optional_attribute_present() {
    #[derive(Facet, Debug)]
    struct Element {
        #[facet(xml::attribute)]
        id: Option<String>,
    }

    let result: Element = facet_xml::from_str(r#"<element id="123"/>"#).unwrap();
    assert_eq!(result.id, Some("123".to_string()));
}

#[test]
fn optional_attribute_absent() {
    #[derive(Facet, Debug)]
    struct Element {
        #[facet(xml::attribute)]
        id: Option<String>,
    }

    let result: Element = facet_xml::from_str(r#"<element/>"#).unwrap();
    assert_eq!(result.id, None);
}

#[test]
fn optional_child_element_present() {
    #[derive(Facet, Debug)]
    struct Container {
        child: Option<String>,
    }

    let result: Container =
        facet_xml::from_str("<container><child>value</child></container>").unwrap();
    assert_eq!(result.child, Some("value".to_string()));
}

#[test]
fn optional_child_element_absent() {
    #[derive(Facet, Debug)]
    struct Container {
        child: Option<String>,
    }

    let result: Container = facet_xml::from_str("<container></container>").unwrap();
    assert_eq!(result.child, None);
}

#[test]
fn empty_self_closing_element() {
    #[derive(Facet, Debug)]
    struct Empty {
        #[facet(xml::attribute)]
        flag: Option<String>,
    }

    let result: Empty = facet_xml::from_str("<empty/>").unwrap();
    assert_eq!(result.flag, None);
}

// ============================================================================
// lowerCamelCase default
// ============================================================================

#[test]
fn struct_name_defaults_to_lower_camel_case() {
    #[derive(Facet, Debug)]
    struct Banana {
        taste: String,
    }

    // Should match <banana>, not <Banana>
    let result: Banana = facet_xml::from_str("<banana><taste>sweet</taste></banana>").unwrap();
    assert_eq!(result.taste, "sweet");
}

#[test]
fn struct_name_rejects_wrong_case() {
    #[derive(Facet, Debug)]
    struct Banana {
        taste: String,
    }

    // Should FAIL - <Banana> doesn't match expected <banana>
    let result = facet_xml::from_str::<Banana>("<Banana><taste>sweet</taste></Banana>");
    assert!(
        result.is_err(),
        "Should reject <Banana> when expecting <banana>"
    );
}

#[test]
fn struct_name_rejects_completely_wrong_tag() {
    #[derive(Facet, Debug)]
    struct Banana {
        taste: String,
    }

    // Should FAIL - <apple> doesn't match expected <banana>
    let result = facet_xml::from_str::<Banana>("<apple><taste>sweet</taste></apple>");
    assert!(
        result.is_err(),
        "Should reject <apple> when expecting <banana>"
    );
}

#[test]
fn rename_overrides_default() {
    #[derive(Facet, Debug)]
    #[facet(rename = "Banana")]
    struct Banana {
        taste: String,
    }

    // With explicit rename, should match <Banana>
    let result: Banana = facet_xml::from_str("<Banana><taste>sweet</taste></Banana>").unwrap();
    assert_eq!(result.taste, "sweet");
}

// ============================================================================
// Field name lowerCamelCase conversion
// ============================================================================

#[test]
fn field_name_snake_case_becomes_lower_camel() {
    #[derive(Facet, Debug)]
    struct Config {
        user_name: String, // expects <userName>
        max_retries: u32,  // expects <maxRetries>
    }

    let result: Config = facet_xml::from_str(
        "<config><userName>alice</userName><maxRetries>3</maxRetries></config>",
    )
    .unwrap();
    assert_eq!(result.user_name, "alice");
    assert_eq!(result.max_retries, 3);
}

#[test]
fn field_name_already_lower_camel_unchanged() {
    #[derive(Facet, Debug)]
    #[allow(non_snake_case)]
    struct Config {
        userName: String, // already lowerCamelCase
    }

    let result: Config = facet_xml::from_str("<config><userName>bob</userName></config>").unwrap();
    assert_eq!(result.userName, "bob");
}

#[test]
fn field_rename_overrides_conversion() {
    #[derive(Facet, Debug)]
    struct Config {
        #[facet(rename = "USER_NAME")]
        user_name: String, // expects <USER_NAME> not <userName>
    }

    let result: Config =
        facet_xml::from_str("<config><USER_NAME>charlie</USER_NAME></config>").unwrap();
    assert_eq!(result.user_name, "charlie");
}

#[test]
fn attribute_name_snake_case_becomes_lower_camel() {
    #[derive(Facet, Debug)]
    struct Element {
        #[facet(xml::attribute)]
        data_id: String, // expects data-id? or dataId?
        #[facet(xml::attribute)]
        max_size: u32, // expects maxSize
    }

    let result: Element = facet_xml::from_str(r#"<element dataId="abc" maxSize="100"/>"#).unwrap();
    assert_eq!(result.data_id, "abc");
    assert_eq!(result.max_size, 100);
}

#[test]
fn multi_word_struct_name_becomes_lower_camel() {
    #[derive(Facet, Debug)]
    struct MyPlaylist {
        name: String,
    }

    // MyPlaylist → <myPlaylist>
    let result: MyPlaylist =
        facet_xml::from_str("<myPlaylist><name>Favorites</name></myPlaylist>").unwrap();
    assert_eq!(result.name, "Favorites");
}

#[test]
fn serialization_uses_lower_camel_case() {
    #[derive(Facet, Debug)]
    struct Config {
        user_name: String,
        max_retries: u32,
    }

    let config = Config {
        user_name: "alice".to_string(),
        max_retries: 3,
    };

    let xml = facet_xml::to_string(&config).unwrap();
    assert!(xml.contains("<config>"), "root should be <config>");
    assert!(xml.contains("<userName>"), "field should be <userName>");
    assert!(xml.contains("<maxRetries>"), "field should be <maxRetries>");
}

// ============================================================================
// Vec default singularization
// ============================================================================

#[test]
fn vec_defaults_to_singularized_element_name() {
    #[derive(Facet, Debug)]
    struct Playlist {
        tracks: Vec<String>, // "tracks" → expects <track> elements
    }

    let result: Playlist =
        facet_xml::from_str("<playlist><track>Song A</track><track>Song B</track></playlist>")
            .unwrap();
    assert_eq!(result.tracks, vec!["Song A", "Song B"]);
}

#[test]
fn vec_rename_overrides_singularization() {
    #[derive(Facet, Debug)]
    struct Playlist {
        #[facet(rename = "song")]
        tracks: Vec<String>, // expects <song> instead of <track>
    }

    let result: Playlist =
        facet_xml::from_str("<playlist><song>Song A</song><song>Song B</song></playlist>").unwrap();
    assert_eq!(result.tracks, vec!["Song A", "Song B"]);
}

// ============================================================================
// Vec with xml::text - collect text nodes
// ============================================================================

#[test]
fn vec_with_xml_text_collects_text_nodes() {
    #[derive(Facet, Debug)]
    struct Message {
        #[facet(xml::text)]
        parts: Vec<String>,
    }

    let result: Message = facet_xml::from_str("<message>Hello world!</message>").unwrap();
    assert_eq!(result.parts, vec!["Hello world!"]);
}

#[test]
fn vec_with_xml_text_collects_multiple_text_nodes() {
    #[derive(Facet, Debug)]
    struct Message {
        #[facet(xml::text)]
        parts: Vec<String>,
    }

    // Text nodes around a child element (note: XML parser trims whitespace from text nodes)
    let result: Message = facet_xml::from_str("<message>Hello <b>world</b>!</message>").unwrap();
    assert_eq!(result.parts, vec!["Hello", "!"]);
}

// ============================================================================
// Vec with xml::attribute - collect attribute values
// ============================================================================

#[test]
fn vec_with_xml_attribute_collects_all_values() {
    #[derive(Facet, Debug)]
    #[facet(rename = "element")]
    struct Element {
        #[facet(xml::attribute)]
        values: Vec<String>,
    }

    let result: Element = facet_xml::from_str(r#"<element foo="1" bar="2" baz="3"/>"#).unwrap();
    assert_eq!(result.values, vec!["1", "2", "3"]);
}