facet-diff-core 0.43.1

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
//! Test layout rendering with build_layout showing all three flavors.

use facet::Facet;
use facet_diff::FacetDiff;
// Import facet_xml as `xml` to bring the xml attribute grammar into scope
use facet_diff_core::{
    BuildOptions, JsonFlavor, RenderOptions, RustFlavor, XmlFlavor, build_layout, render_to_string,
};
use facet_reflect::Peek;
use facet_value::{VObject, Value};
use facet_xml as xml;

fn print_all_flavors<'a, T: facet::Facet<'a>>(label: &str, from: &T, to: &T) {
    let opts = RenderOptions::default();
    let build_opts = BuildOptions::default();

    println!("=== {} ===\n", label);

    let diff = from.diff(to);

    // Rust flavor
    println!("--- Rust ---");
    let layout = build_layout(
        &diff,
        Peek::new(from),
        Peek::new(to),
        &build_opts,
        &RustFlavor,
    );
    println!("{}", render_to_string(&layout, &opts, &RustFlavor));

    // JSON flavor
    println!("--- JSON ---");
    let layout = build_layout(
        &diff,
        Peek::new(from),
        Peek::new(to),
        &build_opts,
        &JsonFlavor,
    );
    println!("{}", render_to_string(&layout, &opts, &JsonFlavor));

    // XML flavor
    println!("--- XML ---");
    let layout = build_layout(
        &diff,
        Peek::new(from),
        Peek::new(to),
        &build_opts,
        &XmlFlavor,
    );
    println!("{}", render_to_string(&layout, &opts, &XmlFlavor));

    println!();
}

fn main() {
    // Simple struct diff
    {
        #[derive(Facet, Debug)]
        struct Point {
            x: i32,
            y: i32,
        }

        let from = Point { x: 10, y: 20 };
        let to = Point { x: 30, y: 20 }; // only x changed

        print_all_flavors("Struct diff (one field changed)", &from, &to);
    }

    // All fields changed
    {
        #[derive(Facet, Debug)]
        struct Point {
            x: i32,
            y: i32,
        }

        let from = Point { x: 10, y: 20 };
        let to = Point { x: 3235832, y: 2 };

        print_all_flavors("Struct diff (all fields changed)", &from, &to);
    }

    // Nested struct
    {
        #[derive(Facet, Debug)]
        struct Outer {
            name: &'static str,
            point: Inner,
        }

        #[derive(Facet, Debug)]
        struct Inner {
            x: i32,
            y: i32,
        }

        let from = Outer {
            name: "origin",
            point: Inner { x: 0, y: 0 },
        };
        let to = Outer {
            name: "origin",
            point: Inner { x: 10, y: 0 },
        };

        print_all_flavors("Nested struct diff", &from, &to);
    }

    // Sequence diff
    {
        let from = vec![1, 2, 3, 4, 5];
        let to = vec![1, 2, 99, 4, 5];

        print_all_flavors("Sequence diff", &from, &to);
    }

    // Sequence with collapsing
    {
        let from: Vec<i32> = (0..20).collect();
        let mut to = from.clone();
        to[10] = 999;

        print_all_flavors("Sequence diff (with collapsing)", &from, &to);
    }

    // Longer sequence - shows XML width problem
    // XML tags make items much wider than Rust/JSON
    {
        let from: Vec<i32> = (100..150).collect(); // 50 items, 3-digit numbers
        let mut to = from.clone();
        to[25] = 9999; // change item at index 25

        print_all_flavors("Long sequence (XML width test)", &from, &to);
    }

    // Field additions using facet_value::Value
    {
        let mut from_obj = VObject::new();
        from_obj.insert("name", "server");
        from_obj.insert("port", 8080);
        let from: Value = from_obj.into();

        let mut to_obj = VObject::new();
        to_obj.insert("name", "server");
        to_obj.insert("port", 8080);
        to_obj.insert("host", "localhost");
        to_obj.insert("timeout", 30);
        to_obj.insert("debug", true);
        let to: Value = to_obj.into();

        print_all_flavors("Field additions (multiple)", &from, &to);
    }

    // Field removals using facet_value::Value
    {
        let mut from_obj = VObject::new();
        from_obj.insert("name", "server");
        from_obj.insert("port", 8080);
        from_obj.insert("host", "localhost");
        from_obj.insert("timeout", 30);
        from_obj.insert("debug", true);
        let from: Value = from_obj.into();

        let mut to_obj = VObject::new();
        to_obj.insert("name", "server");
        to_obj.insert("port", 8080);
        let to: Value = to_obj.into();

        print_all_flavors("Field removals (multiple)", &from, &to);
    }

    // Mixed additions, removals, and changes
    {
        let mut from_obj = VObject::new();
        from_obj.insert("name", "old-server");
        from_obj.insert("port", 80);
        from_obj.insert("legacy", true);
        let from: Value = from_obj.into();

        let mut to_obj = VObject::new();
        to_obj.insert("name", "new-server");
        to_obj.insert("port", 443);
        to_obj.insert("secure", true);
        to_obj.insert("tls_version", "1.3");
        let to: Value = to_obj.into();

        print_all_flavors("Mixed changes (add, remove, modify)", &from, &to);
    }

    // Deeper nesting - SVG-like structure with proper element names and XML annotations
    {
        #[derive(Facet, Debug)]
        #[facet(rename = "svg")]
        struct Svg {
            #[facet(xml::attribute)]
            width: u32,
            #[facet(xml::attribute)]
            height: u32,
            #[facet(xml::element, rename = "viewBox")]
            viewbox: ViewBox,
            #[facet(xml::elements)]
            groups: Vec<Group>,
        }

        #[derive(Facet, Debug)]
        #[facet(rename = "viewBox")]
        struct ViewBox {
            #[facet(xml::attribute, rename = "minX")]
            min_x: i32,
            #[facet(xml::attribute, rename = "minY")]
            min_y: i32,
            #[facet(xml::attribute)]
            width: u32,
            #[facet(xml::attribute)]
            height: u32,
        }

        #[derive(Facet, Debug)]
        #[facet(rename = "g")]
        struct Group {
            #[facet(xml::attribute)]
            id: &'static str,
            #[facet(xml::element)]
            transform: Transform,
            #[facet(xml::element)]
            style: Style,
        }

        #[derive(Facet, Debug)]
        #[facet(rename = "transform", rename_all = "kebab-case")]
        struct Transform {
            #[facet(xml::attribute)]
            translate_x: f32,
            #[facet(xml::attribute)]
            translate_y: f32,
            #[facet(xml::attribute)]
            scale: f32,
            #[facet(xml::attribute)]
            rotate: f32,
        }

        #[derive(Facet, Debug)]
        #[facet(rename = "style", rename_all = "kebab-case")]
        struct Style {
            #[facet(xml::attribute)]
            fill: &'static str,
            #[facet(xml::attribute)]
            stroke: &'static str,
            #[facet(xml::attribute)]
            stroke_width: f32,
            #[facet(xml::attribute)]
            opacity: f32,
        }

        let from = Svg {
            width: 800,
            height: 600,
            viewbox: ViewBox {
                min_x: 0,
                min_y: 0,
                width: 800,
                height: 600,
            },
            groups: vec![
                Group {
                    id: "rect-group",
                    transform: Transform {
                        translate_x: 100.0,
                        translate_y: 100.0,
                        scale: 1.0,
                        rotate: 0.0,
                    },
                    style: Style {
                        fill: "blue",
                        stroke: "black",
                        stroke_width: 2.0,
                        opacity: 1.0,
                    },
                },
                Group {
                    id: "circle-group",
                    transform: Transform {
                        translate_x: 400.0,
                        translate_y: 300.0,
                        scale: 1.0,
                        rotate: 0.0,
                    },
                    style: Style {
                        fill: "red",
                        stroke: "none",
                        stroke_width: 0.0,
                        opacity: 0.8,
                    },
                },
            ],
        };

        let to = Svg {
            width: 1024, // changed
            height: 768, // changed
            viewbox: ViewBox {
                min_x: 0,
                min_y: 0,
                width: 1024, // changed
                height: 768, // changed
            },
            groups: vec![
                Group {
                    id: "rect-group",
                    transform: Transform {
                        translate_x: 150.0, // changed
                        translate_y: 120.0, // changed
                        scale: 1.5,         // changed
                        rotate: 45.0,       // changed
                    },
                    style: Style {
                        fill: "green",     // changed
                        stroke: "white",   // changed
                        stroke_width: 3.0, // changed
                        opacity: 0.9,      // changed
                    },
                },
                Group {
                    id: "circle-group",
                    transform: Transform {
                        translate_x: 500.0, // changed
                        translate_y: 400.0, // changed
                        scale: 2.0,         // changed
                        rotate: 0.0,
                    },
                    style: Style {
                        fill: "yellow",    // changed
                        stroke: "orange",  // changed
                        stroke_width: 1.0, // changed
                        opacity: 1.0,      // changed
                    },
                },
            ],
        };

        print_all_flavors("Deep nesting (SVG-like)", &from, &to);
    }

    // Many attributes scenario
    {
        #[derive(Facet, Debug)]
        struct ManyAttrs {
            name: &'static str,
            id: u32,
            enabled: bool,
            visible: bool,
            priority: i32,
            weight: f32,
            category: &'static str,
            tags: &'static str,
            version: u32,
            revision: u32,
            status: &'static str,
            owner: &'static str,
        }

        let from = ManyAttrs {
            name: "widget",
            id: 1001,
            enabled: true,
            visible: true,
            priority: 5,
            weight: 1.0,
            category: "ui",
            tags: "interactive,clickable",
            version: 1,
            revision: 0,
            status: "active",
            owner: "alice",
        };

        let to = ManyAttrs {
            name: "super-widget", // changed
            id: 1001,
            enabled: false, // changed
            visible: true,
            priority: 10, // changed
            weight: 2.5,  // changed
            category: "ui",
            tags: "interactive,draggable,resizable", // changed
            version: 2,                              // changed
            revision: 3,                             // changed
            status: "updated",                       // changed
            owner: "bob",                            // changed
        };

        print_all_flavors("Many attributes (12 fields)", &from, &to);
    }

    // Mixed scalar and struct attributes (XML will be interesting here)
    {
        #[derive(Facet, Debug)]
        struct Metadata {
            created_by: &'static str,
            version: u32,
        }

        #[derive(Facet, Debug)]
        struct Bounds {
            min: i32,
            max: i32,
        }

        #[derive(Facet, Debug)]
        struct Config {
            name: &'static str,
            enabled: bool,
            metadata: Metadata,
            bounds: Bounds,
            tags: &'static str,
        }

        let from = Config {
            name: "widget",
            enabled: true,
            metadata: Metadata {
                created_by: "alice",
                version: 1,
            },
            bounds: Bounds { min: 0, max: 100 },
            tags: "ui,interactive",
        };

        let to = Config {
            name: "super-widget",
            enabled: false,
            metadata: Metadata {
                created_by: "bob",
                version: 2,
            },
            bounds: Bounds { min: -10, max: 200 },
            tags: "ui,draggable",
        };

        print_all_flavors("Mixed scalar and struct fields", &from, &to);
    }

    // Enum variant change (struct variants with fields)
    {
        #[derive(Facet, Debug)]
        struct Circle {
            radius: f32,
            center_x: f32,
            center_y: f32,
        }

        #[derive(Facet, Debug)]
        struct Rectangle {
            width: f32,
            height: f32,
            x: f32,
            y: f32,
        }

        #[derive(Facet, Debug)]
        #[repr(u8)]
        enum Shape {
            #[allow(dead_code)]
            Circle(Circle),
            #[allow(dead_code)]
            Rectangle(Rectangle),
        }

        #[derive(Facet, Debug)]
        struct Drawing {
            name: &'static str,
            shape: Shape,
        }

        let from = Drawing {
            name: "my-shape",
            shape: Shape::Circle(Circle {
                radius: 50.0,
                center_x: 100.0,
                center_y: 100.0,
            }),
        };

        let to = Drawing {
            name: "my-shape",
            shape: Shape::Rectangle(Rectangle {
                width: 80.0,
                height: 60.0,
                x: 50.0,
                y: 70.0,
            }),
        };

        print_all_flavors("Enum variant change", &from, &to);
    }
}