facet-diff-core 0.43.2

Core types and helpers for diff rendering in Facet
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Diff output flavors (Rust, JSON, XML).
//!
//! Each flavor knows how to present struct fields and format values
//! according to its format's conventions.

use std::borrow::Cow;
use std::fmt::Write;

use facet_core::{Def, Field, PrimitiveType, Type};
use facet_reflect::Peek;

/// How a field should be presented in the diff output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldPresentation {
    /// Show as an inline attribute/field on the struct line.
    /// - Rust: `x: 10`
    /// - JSON: `"x": 10`
    /// - XML: `x="10"` (as attribute on opening tag)
    Attribute {
        /// The field name (possibly renamed)
        name: Cow<'static, str>,
    },

    /// Show as a nested child element.
    /// - XML: `<title>...</title>` as child element
    /// - Rust/JSON: same as Attribute (nested structs are inline)
    Child {
        /// The element/field name
        name: Cow<'static, str>,
    },

    /// Show as text content inside the parent.
    /// - XML: `<p>this text</p>`
    /// - Rust/JSON: same as Attribute
    TextContent,

    /// Show as multiple child elements (for sequences).
    /// - XML: `<Item/><Item/>` as siblings
    /// - Rust/JSON: same as Attribute (sequences are `[...]`)
    Children {
        /// The name for each item element
        item_name: Cow<'static, str>,
    },
}

/// A diff output flavor that knows how to format values and present fields.
pub trait DiffFlavor {
    /// Format a scalar/leaf value into a writer.
    ///
    /// The output should NOT include surrounding quotes for strings -
    /// the renderer will add appropriate syntax based on context.
    fn format_value(&self, peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result;

    /// Determine how a field should be presented.
    fn field_presentation(&self, field: &Field) -> FieldPresentation;

    /// Opening syntax for a struct/object.
    /// - Rust: `Point {`
    /// - JSON: `{`
    /// - XML: `<Point`
    fn struct_open(&self, name: &str) -> Cow<'static, str>;

    /// Closing syntax for a struct/object.
    /// - Rust: `}`
    /// - JSON: `}`
    /// - XML: `/>` (self-closing) or `</Point>`
    fn struct_close(&self, name: &str, self_closing: bool) -> Cow<'static, str>;

    /// Separator between fields.
    /// - Rust: `, `
    /// - JSON: `, `
    /// - XML: ` ` (space between attributes)
    fn field_separator(&self) -> &'static str;

    /// Trailing comma/separator (no trailing space).
    /// Used at end of lines when fields are broken across lines.
    /// - Rust: `,`
    /// - JSON: `,`
    /// - XML: `` (empty - no trailing separator)
    fn trailing_separator(&self) -> &'static str {
        ","
    }

    /// Opening syntax for a sequence/array.
    /// - Rust: `[`
    /// - JSON: `[`
    /// - XML: (wrapper element, handled differently)
    fn seq_open(&self) -> Cow<'static, str>;

    /// Closing syntax for a sequence/array.
    /// - Rust: `]`
    /// - JSON: `]`
    /// - XML: (wrapper element, handled differently)
    fn seq_close(&self) -> Cow<'static, str>;

    /// Separator between sequence items.
    /// - Rust: `, `
    /// - JSON: `,`
    /// - XML: (newlines/whitespace)
    fn item_separator(&self) -> &'static str;

    /// Format a sequence item value, optionally wrapping in element tags.
    /// - Rust: `0` (no wrapping)
    /// - JSON: `0` (no wrapping)
    /// - XML: `<i32>0</i32>` (wrapped in element)
    fn format_seq_item<'a>(&self, _item_type: &str, value: &'a str) -> Cow<'a, str> {
        // Default: no wrapping, just return the value
        Cow::Borrowed(value)
    }

    /// Opening for a sequence that is a struct field value.
    /// - Rust: `fieldname: [`
    /// - JSON: `"fieldname": [`
    /// - XML: `<fieldname>` (wrapper element, not attribute!)
    fn format_seq_field_open(&self, field_name: &str) -> String {
        // Default: use field prefix + seq_open
        format!(
            "{}{}",
            self.format_field_prefix(field_name),
            self.seq_open()
        )
    }

    /// Closing for a sequence that is a struct field value.
    /// - Rust: `]`
    /// - JSON: `]`
    /// - XML: `</fieldname>`
    fn format_seq_field_close(&self, _field_name: &str) -> Cow<'static, str> {
        // Default: just seq_close
        self.seq_close()
    }

    /// Format a comment (for collapsed items).
    /// - Rust: `/* ...5 more */`
    /// - JSON: `// ...5 more`
    /// - XML: `<!-- ...5 more -->`
    fn comment(&self, text: &str) -> String;

    /// Format a field assignment (name and value).
    /// - Rust: `name: value`
    /// - JSON: `"name": value`
    /// - XML: `name="value"`
    fn format_field(&self, name: &str, value: &str) -> String;

    /// Format just the field name with assignment operator.
    /// - Rust: `name: `
    /// - JSON: `"name": `
    /// - XML: `name="`
    fn format_field_prefix(&self, name: &str) -> String;

    /// Suffix after the value (if any).
    /// - Rust: `` (empty)
    /// - JSON: `` (empty)
    /// - XML: `"` (closing quote)
    fn format_field_suffix(&self) -> &'static str;

    /// Close the opening tag when there are children.
    /// - Rust: `` (empty - no separate closing for opening tag)
    /// - JSON: `` (empty)
    /// - XML: `>` (close the opening tag before children)
    fn struct_open_close(&self) -> &'static str {
        ""
    }

    /// Optional type name comment to show after struct_open.
    /// Rendered in muted color for readability.
    /// - Rust: None (type name is in struct_open)
    /// - JSON: Some("/* Point */")
    /// - XML: None
    fn type_comment(&self, _name: &str) -> Option<String> {
        None
    }

    /// Opening wrapper for a child element (nested struct field).
    /// - Rust: `field_name: ` (field prefix)
    /// - JSON: `"field_name": ` (field prefix)
    /// - XML: `` (empty - no wrapper, or could be `<field_name>\n`)
    fn format_child_open(&self, name: &str) -> Cow<'static, str> {
        // Default: use field prefix (works for Rust/JSON)
        Cow::Owned(self.format_field_prefix(name))
    }

    /// Closing wrapper for a child element (nested struct field).
    /// - Rust: `` (empty)
    /// - JSON: `` (empty)
    /// - XML: `` (empty, or `</field_name>` if wrapping)
    fn format_child_close(&self, _name: &str) -> Cow<'static, str> {
        Cow::Borrowed("")
    }
}

/// Rust-style output flavor.
///
/// Produces output like: `Point { x: 10, y: 20 }`
#[derive(Debug, Clone, Default)]
pub struct RustFlavor;

impl DiffFlavor for RustFlavor {
    fn format_value(&self, peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result {
        format_value_quoted(peek, w)
    }

    fn field_presentation(&self, field: &Field) -> FieldPresentation {
        // Rust flavor: all fields are attributes (key: value)
        FieldPresentation::Attribute {
            name: Cow::Borrowed(field.name),
        }
    }

    fn struct_open(&self, name: &str) -> Cow<'static, str> {
        Cow::Owned(format!("{} {{", name))
    }

    fn struct_close(&self, _name: &str, _self_closing: bool) -> Cow<'static, str> {
        Cow::Borrowed("}")
    }

    fn field_separator(&self) -> &'static str {
        ", "
    }

    fn seq_open(&self) -> Cow<'static, str> {
        Cow::Borrowed("[")
    }

    fn seq_close(&self) -> Cow<'static, str> {
        Cow::Borrowed("]")
    }

    fn item_separator(&self) -> &'static str {
        ", "
    }

    fn comment(&self, text: &str) -> String {
        format!("/* {} */", text)
    }

    fn format_field(&self, name: &str, value: &str) -> String {
        format!("{}: {}", name, value)
    }

    fn format_field_prefix(&self, name: &str) -> String {
        format!("{}: ", name)
    }

    fn format_field_suffix(&self) -> &'static str {
        ""
    }
}

/// JSON-style output flavor (JSONC with comments for type names).
///
/// Produces output like: `{ // Point\n  "x": 10, "y": 20\n}`
#[derive(Debug, Clone, Default)]
pub struct JsonFlavor;

impl DiffFlavor for JsonFlavor {
    fn format_value(&self, peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result {
        format_value_quoted(peek, w)
    }

    fn field_presentation(&self, field: &Field) -> FieldPresentation {
        // JSON flavor: all fields are attributes ("key": value)
        FieldPresentation::Attribute {
            name: Cow::Borrowed(field.name),
        }
    }

    fn struct_open(&self, _name: &str) -> Cow<'static, str> {
        Cow::Borrowed("{")
    }

    fn type_comment(&self, name: &str) -> Option<String> {
        Some(format!("/* {} */", name))
    }

    fn struct_close(&self, _name: &str, _self_closing: bool) -> Cow<'static, str> {
        Cow::Borrowed("}")
    }

    fn field_separator(&self) -> &'static str {
        ", "
    }

    fn seq_open(&self) -> Cow<'static, str> {
        Cow::Borrowed("[")
    }

    fn seq_close(&self) -> Cow<'static, str> {
        Cow::Borrowed("]")
    }

    fn item_separator(&self) -> &'static str {
        ", "
    }

    fn comment(&self, text: &str) -> String {
        format!("// {}", text)
    }

    fn format_field(&self, name: &str, value: &str) -> String {
        format!("\"{}\": {}", name, value)
    }

    fn format_field_prefix(&self, name: &str) -> String {
        format!("\"{}\": ", name)
    }

    fn format_field_suffix(&self) -> &'static str {
        ""
    }
}

/// XML-style output flavor.
///
/// Produces output like: `<Point x="10" y="20"/>`
///
/// Respects `#[facet(xml::attribute)]`, `#[facet(xml::element)]`, etc.
#[derive(Debug, Clone, Default)]
pub struct XmlFlavor;

impl DiffFlavor for XmlFlavor {
    fn format_value(&self, peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result {
        format_value_raw(peek, w)
    }

    fn field_presentation(&self, field: &Field) -> FieldPresentation {
        // Check for XML-specific attributes
        //
        // NOTE: We detect XML attributes by namespace string "xml" (e.g., `field.has_attr(Some("xml"), "attribute")`).
        // This works because the namespace is defined in the `define_attr_grammar!` macro in facet-xml
        // with `ns "xml"`, NOT by the import alias. So even if someone writes `use facet_xml as html;`
        // and uses `#[facet(html::attribute)]`, the namespace stored in the attribute is still "xml".
        // This should be tested to confirm, but not now.
        if field.has_attr(Some("xml"), "attribute") {
            FieldPresentation::Attribute {
                name: Cow::Borrowed(field.name),
            }
        } else if field.has_attr(Some("xml"), "elements") {
            FieldPresentation::Children {
                item_name: Cow::Borrowed(field.name),
            }
        } else if field.has_attr(Some("xml"), "text") {
            FieldPresentation::TextContent
        } else if field.has_attr(Some("xml"), "element") {
            FieldPresentation::Child {
                name: Cow::Borrowed(field.name),
            }
        } else {
            // Default: treat as child element (XML's default for non-attributed fields)
            // In XML, fields without explicit annotation typically become child elements
            FieldPresentation::Child {
                name: Cow::Borrowed(field.name),
            }
        }
    }

    fn struct_open(&self, name: &str) -> Cow<'static, str> {
        Cow::Owned(format!("<{}", name))
    }

    fn struct_close(&self, name: &str, self_closing: bool) -> Cow<'static, str> {
        if self_closing {
            Cow::Borrowed("/>")
        } else {
            Cow::Owned(format!("</{}>", name))
        }
    }

    fn field_separator(&self) -> &'static str {
        " "
    }

    fn seq_open(&self) -> Cow<'static, str> {
        // XML sequences don't need wrapper elements - items render as siblings
        Cow::Borrowed("")
    }

    fn seq_close(&self) -> Cow<'static, str> {
        // XML sequences don't need wrapper elements - items render as siblings
        Cow::Borrowed("")
    }

    fn item_separator(&self) -> &'static str {
        " "
    }

    fn format_seq_item<'a>(&self, item_type: &str, value: &'a str) -> Cow<'a, str> {
        // Wrap each item in an element tag: <i32>0</i32>
        Cow::Owned(format!("<{}>{}</{}>", item_type, value, item_type))
    }

    fn comment(&self, text: &str) -> String {
        format!("<!-- {} -->", text)
    }

    fn format_field(&self, name: &str, value: &str) -> String {
        format!("{}=\"{}\"", name, value)
    }

    fn format_field_prefix(&self, name: &str) -> String {
        format!("{}=\"", name)
    }

    fn format_field_suffix(&self) -> &'static str {
        "\""
    }

    fn struct_open_close(&self) -> &'static str {
        ">"
    }

    fn format_child_open(&self, _name: &str) -> Cow<'static, str> {
        // XML: nested elements don't use attribute-style prefix
        // The nested element tag is self-describing
        Cow::Borrowed("")
    }

    fn format_child_close(&self, _name: &str) -> Cow<'static, str> {
        Cow::Borrowed("")
    }

    fn trailing_separator(&self) -> &'static str {
        // XML doesn't use trailing commas/separators
        ""
    }

    fn format_seq_field_open(&self, _field_name: &str) -> String {
        // XML: sequences render items directly without wrapper elements
        // The items are children of the parent element
        String::new()
    }

    fn format_seq_field_close(&self, _field_name: &str) -> Cow<'static, str> {
        // XML: sequences render items directly without wrapper elements
        Cow::Borrowed("")
    }
}

/// Value formatting with quotes for strings (Rust/JSON style).
fn format_value_quoted(peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result {
    use facet_core::{PointerType, TextualType};

    let shape = peek.shape();

    match (shape.def, shape.ty) {
        // Strings: write with quotes
        (_, Type::Primitive(PrimitiveType::Textual(TextualType::Str))) => {
            write!(w, "\"{}\"", peek.get::<str>().unwrap())
        }
        // String type (owned)
        (Def::Scalar, _) if shape.id == <String as facet_core::Facet>::SHAPE.id => {
            write!(w, "\"{}\"", peek.get::<String>().unwrap())
        }
        // Reference to str (&str) - check if target is str
        (_, Type::Pointer(PointerType::Reference(ptr)))
            if matches!(
                ptr.target.ty,
                Type::Primitive(PrimitiveType::Textual(TextualType::Str))
            ) =>
        {
            // Use Display which will show the string content
            write!(w, "\"{}\"", peek)
        }
        // Booleans
        (Def::Scalar, Type::Primitive(PrimitiveType::Boolean)) => {
            let b = peek.get::<bool>().unwrap();
            write!(w, "{}", if *b { "true" } else { "false" })
        }
        // Chars: show with single quotes for Rust
        (Def::Scalar, Type::Primitive(PrimitiveType::Textual(TextualType::Char))) => {
            write!(w, "'{}'", peek.get::<char>().unwrap())
        }
        // Everything else: use Display if available, else Debug
        _ => {
            if shape.is_display() {
                write!(w, "{}", peek)
            } else if shape.is_debug() {
                write!(w, "{:?}", peek)
            } else {
                write!(w, "<{}>", shape.type_identifier)
            }
        }
    }
}

/// Value formatting without quotes (XML style - quotes come from attribute syntax).
fn format_value_raw(peek: Peek<'_, '_>, w: &mut dyn Write) -> std::fmt::Result {
    use facet_core::{DynValueKind, TextualType};

    let shape = peek.shape();

    match (shape.def, shape.ty) {
        // Strings: write raw content (no quotes)
        (_, Type::Primitive(PrimitiveType::Textual(TextualType::Str))) => {
            write!(w, "{}", peek.get::<str>().unwrap())
        }
        // String type (owned)
        (Def::Scalar, _) if shape.id == <String as facet_core::Facet>::SHAPE.id => {
            write!(w, "{}", peek.get::<String>().unwrap())
        }
        // Booleans
        (Def::Scalar, Type::Primitive(PrimitiveType::Boolean)) => {
            let b = peek.get::<bool>().unwrap();
            write!(w, "{}", if *b { "true" } else { "false" })
        }
        // Chars: show as-is
        (Def::Scalar, Type::Primitive(PrimitiveType::Textual(TextualType::Char))) => {
            write!(w, "{}", peek.get::<char>().unwrap())
        }
        // Dynamic values: handle based on their kind
        (Def::DynamicValue(_), _) => {
            // Write string without quotes for XML
            if let Ok(dv) = peek.into_dynamic_value()
                && dv.kind() == DynValueKind::String
                && let Some(s) = dv.as_str()
            {
                return write!(w, "{}", s);
            }
            // Fall back to Display for other dynamic values
            if shape.is_display() {
                write!(w, "{}", peek)
            } else if shape.is_debug() {
                write!(w, "{:?}", peek)
            } else {
                write!(w, "<{}>", shape.type_identifier)
            }
        }
        // Everything else: use Display if available, else Debug
        _ => {
            if shape.is_display() {
                write!(w, "{}", peek)
            } else if shape.is_debug() {
                write!(w, "{:?}", peek)
            } else {
                write!(w, "<{}>", shape.type_identifier)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use facet::Facet;
    use facet_core::{Shape, Type, UserType};

    // Helper to get field from a struct shape
    fn get_field<'a>(shape: &'a Shape, name: &str) -> &'a Field {
        if let Type::User(UserType::Struct(st)) = shape.ty {
            st.fields.iter().find(|f| f.name == name).unwrap()
        } else {
            panic!("expected struct type")
        }
    }

    #[test]
    fn test_rust_flavor_field_presentation() {
        #[derive(Facet)]
        struct Point {
            x: i32,
            y: i32,
        }

        let shape = <Point as Facet>::SHAPE;
        let flavor = RustFlavor;

        let x_field = get_field(shape, "x");
        let y_field = get_field(shape, "y");

        // Rust flavor: all fields are attributes
        assert_eq!(
            flavor.field_presentation(x_field),
            FieldPresentation::Attribute {
                name: Cow::Borrowed("x")
            }
        );
        assert_eq!(
            flavor.field_presentation(y_field),
            FieldPresentation::Attribute {
                name: Cow::Borrowed("y")
            }
        );
    }

    #[test]
    fn test_json_flavor_field_presentation() {
        #[derive(Facet)]
        struct Point {
            x: i32,
            y: i32,
        }

        let shape = <Point as Facet>::SHAPE;
        let flavor = JsonFlavor;

        let x_field = get_field(shape, "x");

        // JSON flavor: all fields are attributes
        assert_eq!(
            flavor.field_presentation(x_field),
            FieldPresentation::Attribute {
                name: Cow::Borrowed("x")
            }
        );
    }

    #[test]
    fn test_xml_flavor_field_presentation_default() {
        // Without XML attributes, fields default to Child
        #[derive(Facet)]
        struct Book {
            title: String,
            author: String,
        }

        let shape = <Book as Facet>::SHAPE;
        let flavor = XmlFlavor;

        let title_field = get_field(shape, "title");

        // XML default: child element
        assert_eq!(
            flavor.field_presentation(title_field),
            FieldPresentation::Child {
                name: Cow::Borrowed("title")
            }
        );
    }

    fn format_to_string<F: DiffFlavor>(flavor: &F, peek: Peek<'_, '_>) -> String {
        let mut buf = String::new();
        flavor.format_value(peek, &mut buf).unwrap();
        buf
    }

    #[test]
    fn test_format_value_integers() {
        let value = 42i32;
        let peek = Peek::new(&value);

        assert_eq!(format_to_string(&RustFlavor, peek), "42");
        assert_eq!(format_to_string(&JsonFlavor, peek), "42");
        assert_eq!(format_to_string(&XmlFlavor, peek), "42");
    }

    #[test]
    fn test_format_value_strings() {
        let value = "hello";
        let peek = Peek::new(&value);

        // Rust/JSON add quotes around strings, XML doesn't (quotes come from attr syntax)
        assert_eq!(format_to_string(&RustFlavor, peek), "\"hello\"");
        assert_eq!(format_to_string(&JsonFlavor, peek), "\"hello\"");
        assert_eq!(format_to_string(&XmlFlavor, peek), "hello");
    }

    #[test]
    fn test_format_value_booleans() {
        let t = true;
        let f = false;

        assert_eq!(format_to_string(&RustFlavor, Peek::new(&t)), "true");
        assert_eq!(format_to_string(&RustFlavor, Peek::new(&f)), "false");
        assert_eq!(format_to_string(&JsonFlavor, Peek::new(&t)), "true");
        assert_eq!(format_to_string(&JsonFlavor, Peek::new(&f)), "false");
        assert_eq!(format_to_string(&XmlFlavor, Peek::new(&t)), "true");
        assert_eq!(format_to_string(&XmlFlavor, Peek::new(&f)), "false");
    }

    #[test]
    fn test_syntax_methods() {
        let rust = RustFlavor;
        let json = JsonFlavor;
        let xml = XmlFlavor;

        // struct_open
        assert_eq!(rust.struct_open("Point"), "Point {");
        assert_eq!(json.struct_open("Point"), "{");
        assert_eq!(xml.struct_open("Point"), "<Point");

        // type_comment (rendered separately in muted color)
        assert_eq!(rust.type_comment("Point"), None);
        assert_eq!(json.type_comment("Point"), Some("/* Point */".to_string()));
        assert_eq!(xml.type_comment("Point"), None);

        // struct_close (non-self-closing)
        assert_eq!(rust.struct_close("Point", false), "}");
        assert_eq!(json.struct_close("Point", false), "}");
        assert_eq!(xml.struct_close("Point", false), "</Point>");

        // struct_close (self-closing)
        assert_eq!(rust.struct_close("Point", true), "}");
        assert_eq!(json.struct_close("Point", true), "}");
        assert_eq!(xml.struct_close("Point", true), "/>");

        // field_separator
        assert_eq!(rust.field_separator(), ", ");
        assert_eq!(json.field_separator(), ", ");
        assert_eq!(xml.field_separator(), " ");

        // seq_open/close
        assert_eq!(rust.seq_open(), "[");
        assert_eq!(rust.seq_close(), "]");
        assert_eq!(json.seq_open(), "[");
        assert_eq!(json.seq_close(), "]");
        // XML sequences render items as siblings without wrapper elements
        assert_eq!(xml.seq_open(), "");
        assert_eq!(xml.seq_close(), "");

        // comment
        assert_eq!(rust.comment("5 more"), "/* 5 more */");
        assert_eq!(json.comment("5 more"), "// 5 more");
        assert_eq!(xml.comment("5 more"), "<!-- 5 more -->");

        // format_field
        assert_eq!(rust.format_field("x", "10"), "x: 10");
        assert_eq!(json.format_field("x", "10"), "\"x\": 10");
        assert_eq!(xml.format_field("x", "10"), "x=\"10\"");
    }
}