serde-structprop 0.1.0

Serde serializer and deserializer for the structprop config file format
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use serde::{Deserialize, Serialize};
use serde_structprop::{from_str, to_string};

// ---------------------------------------------------------------------------
// Deserialization tests
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize, PartialEq)]
struct Simple {
    hostname: String,
    port: u16,
}

#[test]
fn de_simple_struct() {
    let input = "hostname = localhost\nport = 8080\n";
    let cfg: Simple = from_str(input).unwrap();
    assert_eq!(cfg.hostname, "localhost");
    assert_eq!(cfg.port, 8080);
}

#[test]
fn de_quoted_value() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        message: String,
    }
    let input = "message = \"hello world\"";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.message, "hello world");
}

#[test]
fn de_nested_struct() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct DbConn {
        hostname: String,
        port: u16,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Config {
        database: DbConn,
    }

    let input = "database {\n  hostname = localhost\n  port = 5432\n}\n";
    let cfg: Config = from_str(input).unwrap();
    assert_eq!(cfg.database.hostname, "localhost");
    assert_eq!(cfg.database.port, 5432);
}

#[test]
fn de_vec_of_strings() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        tables: Vec<String>,
    }
    let input = "tables = { Table1 Table2 Table3 }\n";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.tables, vec!["Table1", "Table2", "Table3"]);
}

#[test]
fn de_vec_of_integers() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        ports: Vec<u16>,
    }
    let input = "ports = { 80 443 8080 }\n";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.ports, vec![80u16, 443, 8080]);
}

#[test]
fn de_bool_field() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        enabled: bool,
    }
    let input = "enabled = true\n";
    let s: S = from_str(input).unwrap();
    assert!(s.enabled);
}

#[test]
fn de_option_some() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        value: Option<u32>,
    }
    let input = "value = 42\n";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.value, Some(42));
}

#[test]
fn de_comments_ignored() {
    let input = "# This is a comment\nhostname = server\nport = 9000\n";
    let cfg: Simple = from_str(input).unwrap();
    assert_eq!(cfg.hostname, "server");
    assert_eq!(cfg.port, 9000);
}

#[test]
fn de_full_example() {
    // Mirrors the README example from structprop.
    #[derive(Debug, Deserialize, PartialEq)]
    struct DbConn {
        hostname: String,
        username: String,
        password: String,
        port: u16,
        name: String,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Config {
        database: DbConn,
        tables: Vec<String>,
    }

    let input = "
# This is a simple example config file
database {
  hostname = localhost
  username = dbuser
  password = secret
  port = 12361
  name = TheDatabase
}

tables = { Table1 Table2 }
";

    let cfg: Config = from_str(input).unwrap();
    assert_eq!(cfg.database.hostname, "localhost");
    assert_eq!(cfg.database.username, "dbuser");
    assert_eq!(cfg.database.password, "secret");
    assert_eq!(cfg.database.port, 12361);
    assert_eq!(cfg.database.name, "TheDatabase");
    assert_eq!(cfg.tables, vec!["Table1", "Table2"]);
}

// ---------------------------------------------------------------------------
// Serialization tests
// ---------------------------------------------------------------------------

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Config {
    hostname: String,
    port: u16,
}

#[test]
fn ser_simple_struct() {
    let cfg = Config {
        hostname: "localhost".into(),
        port: 8080,
    };
    let out = to_string(&cfg).unwrap();
    assert!(out.contains("hostname = localhost"), "got: {out}");
    assert!(out.contains("port = 8080"), "got: {out}");
}

#[test]
fn ser_quoted_strings() {
    #[derive(Serialize)]
    struct S {
        message: String,
    }
    let s = S {
        message: "hello world".into(),
    };
    let out = to_string(&s).unwrap();
    assert!(out.contains("message = \"hello world\""), "got: {out}");
}

#[test]
fn ser_nested_struct() {
    #[derive(Serialize)]
    struct Inner {
        x: u32,
    }
    #[derive(Serialize)]
    struct Outer {
        inner: Inner,
    }
    let o = Outer {
        inner: Inner { x: 7 },
    };
    let out = to_string(&o).unwrap();
    assert!(out.contains("inner {"), "got: {out}");
    assert!(out.contains("x = 7"), "got: {out}");
}

#[test]
fn ser_vec_of_strings() {
    #[derive(Serialize)]
    struct S {
        items: Vec<String>,
    }
    let s = S {
        items: vec!["a".into(), "b".into(), "c".into()],
    };
    let out = to_string(&s).unwrap();
    assert!(out.contains("items"), "got: {out}");
    assert!(out.contains('a'), "got: {out}");
    assert!(out.contains('b'), "got: {out}");
}

// ---------------------------------------------------------------------------
// Round-trip tests
// ---------------------------------------------------------------------------

#[test]
fn roundtrip_simple() {
    let original = Config {
        hostname: "myserver".into(),
        port: 3000,
    };
    let serialized = to_string(&original).unwrap();
    let deserialized: Config = from_str(&serialized).unwrap();
    assert_eq!(original, deserialized);
}

#[test]
fn roundtrip_nested() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Inner {
        value: String,
        count: u32,
    }
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Outer {
        name: String,
        inner: Inner,
    }

    let original = Outer {
        name: "test".into(),
        inner: Inner {
            value: "foo".into(),
            count: 42,
        },
    };
    let serialized = to_string(&original).unwrap();
    let deserialized: Outer = from_str(&serialized).unwrap();
    assert_eq!(original, deserialized);
}

#[test]
fn roundtrip_vec() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct S {
        tags: Vec<String>,
    }
    let original = S {
        tags: vec!["rust".into(), "serde".into(), "config".into()],
    };
    let serialized = to_string(&original).unwrap();
    let deserialized: S = from_str(&serialized).unwrap();
    assert_eq!(original, deserialized);
}

// ---------------------------------------------------------------------------
// Python test-suite counterparts
// ---------------------------------------------------------------------------

#[test]
fn de_parser_error_on_missing_term() {
    // A key with no value / block is a parse error.
    let result = from_str::<Simple>("hostname\n");
    assert!(
        result.is_err(),
        "expected parse error for bare key with no value"
    );
}

#[test]
fn de_inline_comment() {
    // Inline comments (after a value) should be ignored.
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        key: String,
    }
    let input = "key = value # this is an inline comment\n";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.key, "value");
}

#[test]
fn de_quoted_key() {
    // A quoted key should be parsed and the quotes stripped.
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        my_key: String,
    }
    let input = "\"my_key\" = hello\n";
    let s: S = from_str(input).unwrap();
    assert_eq!(s.my_key, "hello");
}

#[test]
fn de_empty_object() {
    // An empty block should deserialise without error.
    #[derive(Debug, Deserialize, PartialEq)]
    struct Inner {}
    #[derive(Debug, Deserialize, PartialEq)]
    struct Outer {
        section: Inner,
    }
    let input = "section {\n}\n";
    let outer: Outer = from_str(input).unwrap();
    let _ = outer; // just assert no panic / error
}

#[test]
fn de_object_key_value_single_line() {
    // Block with a single key = value entry.
    #[derive(Debug, Deserialize, PartialEq)]
    struct Inner {
        x: u32,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Outer {
        section: Inner,
    }
    let input = "section {\n  x = 42\n}\n";
    let outer: Outer = from_str(input).unwrap();
    assert_eq!(outer.section.x, 42);
}

#[test]
fn de_nested_objects_mixed_array() {
    // Nested block that also contains an array.
    #[derive(Debug, Deserialize, PartialEq)]
    struct Inner {
        tags: Vec<String>,
    }
    #[derive(Debug, Deserialize, PartialEq)]
    struct Outer {
        section: Inner,
    }
    let input = "section {\n  tags = { alpha beta }\n}\n";
    let outer: Outer = from_str(input).unwrap();
    assert_eq!(outer.section.tags, vec!["alpha", "beta"]);
}

#[test]
fn de_false_bool() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct S {
        flag: bool,
    }
    let input = "flag = false\n";
    let s: S = from_str(input).unwrap();
    assert!(!s.flag);
}

#[test]
fn ser_dump_list_exact() {
    // Mirrors the Python `test_dump_list` expectation.
    #[derive(Serialize)]
    struct S {
        a: Vec<String>,
    }
    let s = S {
        a: vec!["a".into(), "b".into(), "c".into()],
    };
    let out = to_string(&s).unwrap();
    assert_eq!(out, "a = {\n  a\n  b\n  c\n}\n", "got: {out:?}");
}

#[test]
fn ser_dump_dict_exact() {
    // Mirrors the Python `test_dump_dict` expectation.
    #[derive(Serialize)]
    struct Inner {
        b: String,
    }
    #[derive(Serialize)]
    struct Outer {
        a: Inner,
    }
    let o = Outer {
        a: Inner { b: "c".into() },
    };
    let out = to_string(&o).unwrap();
    assert_eq!(out, "a {\n  b = c\n}\n", "got: {out:?}");
}

#[test]
fn ser_dump_true_bool() {
    #[derive(Serialize)]
    struct S {
        flag: bool,
    }
    let out = to_string(&S { flag: true }).unwrap();
    assert_eq!(out, "flag = true\n");
}

#[test]
fn ser_dump_false_bool() {
    #[derive(Serialize)]
    struct S {
        flag: bool,
    }
    let out = to_string(&S { flag: false }).unwrap();
    assert_eq!(out, "flag = false\n");
}

#[test]
fn ser_escape_space_in_value() {
    // Values containing spaces must be quoted on output.
    #[derive(Serialize)]
    struct S {
        msg: String,
    }
    let out = to_string(&S {
        msg: "hello world".into(),
    })
    .unwrap();
    assert!(out.contains("msg = \"hello world\""), "got: {out:?}");
}

#[test]
fn ser_object_order_is_kept() {
    // Field declaration order must be preserved in serialized output.
    #[derive(Serialize)]
    struct S {
        first: u32,
        second: u32,
        third: u32,
    }
    let out = to_string(&S {
        first: 1,
        second: 2,
        third: 3,
    })
    .unwrap();
    let pos_first = out.find("first").unwrap();
    let pos_second = out.find("second").unwrap();
    let pos_third = out.find("third").unwrap();
    assert!(
        pos_first < pos_second && pos_second < pos_third,
        "got: {out:?}"
    );
}

// ---------------------------------------------------------------------------
// Bug fix tests
// ---------------------------------------------------------------------------

// Cycle 1: serialize_struct_variant must not corrupt output via insert_str(0)
#[test]
fn ser_struct_variant_does_not_corrupt_preceding_fields() {
    #[derive(Serialize)]
    enum Shape {
        Circle { radius: u32 },
    }
    #[derive(Serialize)]
    struct Config {
        name: String,
        shape: Shape,
    }
    let cfg = Config {
        name: "test".into(),
        shape: Shape::Circle { radius: 5 },
    };
    let out = to_string(&cfg).unwrap();
    // "name" must appear before "Circle" — insert_str(0) bug put the variant
    // header at position 0, before any previously-written fields.
    let pos_name = out.find("name").expect("missing 'name'");
    let pos_circle = out.find("Circle").expect("missing 'Circle'");
    assert!(
        pos_name < pos_circle,
        "'name' should appear before 'Circle', got:\n{out}"
    );
}

// Cycle 2: tuple variant with zero elements must not panic
#[test]
fn ser_tuple_variant_zero_elements_does_not_panic() {
    #[derive(Serialize)]
    enum E {
        Empty(),
    }
    // Must not panic — previously items.remove(0) panicked on empty vec.
    let result = to_string(&E::Empty());
    assert!(result.is_ok(), "expected Ok, got {result:?}");
}

// Cycle 2b: tuple variant sentinel collision — variant named "__variant__Foo"
#[test]
fn ser_tuple_variant_sentinel_collision() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    enum E {
        #[serde(rename = "__variant__Tricky")]
        Tricky(u32),
    }
    let out = to_string(&E::Tricky(42)).unwrap();
    // The variant name in the output must be the full "__variant__Tricky",
    // not just "Tricky" (which would happen if sentinel stripping ate the prefix).
    assert!(
        out.contains("__variant__Tricky"),
        "variant name mangled, got:\n{out}"
    );
}

// Cycle 3: escape() must quote strings containing newlines
#[test]
fn ser_string_with_newline_is_quoted() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct S {
        msg: String,
    }
    let s = S {
        msg: "hello\nworld".into(),
    };
    let out = to_string(&s).unwrap();
    // A bare newline in the output would break the line-oriented parser.
    // The value must be wrapped in quotes.
    assert!(
        out.contains('"'),
        "expected quoted output for newline-containing string, got:\n{out}"
    );
    // And it must round-trip.
    let back: S = from_str(&out).unwrap();
    assert_eq!(back, s);
}

// Nested struct containing a string with a newline must round-trip correctly.
// Regression for write_kv re-indent bug: blindly prefixing every line with
// spaces would corrupt the continuation lines of a quoted multi-line scalar.
#[test]
fn roundtrip_nested_struct_with_newline_string() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Inner {
        msg: String,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Outer {
        name: String,
        inner: Inner,
    }
    let orig = Outer {
        name: "test".into(),
        inner: Inner {
            msg: "hello\nworld".into(),
        },
    };
    let out = to_string(&orig).unwrap();
    let back: Outer = from_str(&out).unwrap();
    assert_eq!(back, orig, "round-trip failed; output was:\n{out}");
}

// Cycle 4: serialize_char must quote structprop special characters
#[test]
fn ser_char_special_chars_are_quoted() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct S {
        c: char,
    }
    for special in [' ', '#', '{', '}', '='] {
        let s = S { c: special };
        let out = to_string(&s).unwrap();
        let back: S = from_str(&out).unwrap();
        assert_eq!(
            back, s,
            "char '{special}' did not round-trip; output was:\n{out}"
        );
    }
}

// Cycle 5: duplicate keys must produce an error, not silently overwrite
#[test]
fn de_duplicate_key_is_an_error() {
    use std::collections::HashMap;
    let input = "port = 1234\nport = 5678\n";
    let result: Result<HashMap<String, String>, _> = from_str(input);
    assert!(
        result.is_err(),
        "expected error for duplicate key, got {result:?}"
    );
}

// Cycle 6: unterminated quoted string must produce an error, not silently drop content
#[test]
fn de_unterminated_quoted_string_is_an_error() {
    use std::collections::HashMap;
    let input = "key = \"unterminated";
    let result: Result<HashMap<String, String>, _> = from_str(input);
    assert!(
        result.is_err(),
        "expected error for unterminated string, got {result:?}"
    );
}

// Cycle 7: large integer (> i64::MAX) must not silently become a float
#[test]
fn de_large_integer_is_not_silently_coerced_to_float() {
    // u64::MAX cannot be represented as i64; previously deserialize_any would
    // fall through i64 parse failure → f64 parse, silently losing precision.
    #[derive(Debug, Deserialize)]
    struct S {
        val: u64,
    }
    let input = format!("val = {}\n", u64::MAX);
    let s: S = from_str(&input).unwrap();
    assert_eq!(s.val, u64::MAX);
}

// Cycle 8: enum round-trips — unit, newtype, tuple, struct variants
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Color {
    Red,
    Green,
    Blue,
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(u8, u8, u8),
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct WithEnum {
    color: Color,
}

#[test]
fn roundtrip_unit_enum_variant() {
    let s = WithEnum { color: Color::Red };
    let out = to_string(&s).unwrap();
    let back: WithEnum = from_str(&out).unwrap();
    assert_eq!(back, s);
}

#[test]
fn roundtrip_newtype_enum_variant() {
    let msg = Message::Write("hello".into());
    let out = to_string(&msg).unwrap();
    let back: Message = from_str(&out).unwrap();
    assert_eq!(back, msg);
}

#[test]
fn roundtrip_struct_enum_variant() {
    let msg = Message::Move { x: 10, y: 20 };
    let out = to_string(&msg).unwrap();
    let back: Message = from_str(&out).unwrap();
    assert_eq!(back, msg);
}

#[test]
fn roundtrip_tuple_enum_variant() {
    let msg = Message::ChangeColor(255, 128, 0);
    let out = to_string(&msg).unwrap();
    let back: Message = from_str(&out).unwrap();
    assert_eq!(back, msg);
}

// Cycle 9: Option::None round-trip
#[test]
fn roundtrip_option_none() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct S {
        #[serde(default)]
        value: Option<u32>,
    }
    // None serializes to `null`; must deserialize back to None.
    let s = S { value: None };
    let out = to_string(&s).unwrap();
    assert!(
        out.contains("null"),
        "expected 'null' in output, got:\n{out}"
    );
    let back: S = from_str(&out).unwrap();
    assert_eq!(back, s);
}

// Option field absent from document should deserialize as None via serde default
#[test]
fn de_option_field_absent_defaults_to_none() {
    #[derive(Deserialize, PartialEq, Debug)]
    struct S {
        name: String,
        #[serde(default)]
        count: Option<u32>,
    }
    let s: S = from_str("name = foo\n").unwrap();
    assert_eq!(s.count, None);
}

// deserialize_unit must reject non-null values, and accept "null"
#[test]
fn de_unit_field_roundtrip() {
    #[derive(Debug, Deserialize, PartialEq)]
    struct Doc {
        key: (),
    }
    // "null" is the structprop representation of a unit value — must succeed.
    let doc: Doc = from_str("key = null\n").expect("expected Ok deserializing null as unit");
    assert_eq!(doc.key, ());

    // Any non-null scalar must be rejected when the target type is unit.
    let err: Result<Doc, _> = from_str("key = hello\n");
    assert!(
        err.is_err(),
        "expected error deserializing non-null as unit"
    );
}

// serialize_key must return KeyMustBeString for non-string key types.
// Only str/String keys are accepted; char and enum variants are rejected too.
#[test]
fn ser_non_string_map_key_is_an_error() {
    use std::collections::HashMap;

    // Integer key
    let mut int_map: HashMap<u32, &str> = HashMap::new();
    int_map.insert(42, "hello");
    assert!(
        to_string(&int_map).is_err(),
        "expected error serializing integer map key"
    );

    // char key
    let mut char_map: HashMap<char, &str> = HashMap::new();
    char_map.insert('x', "hello");
    assert!(
        to_string(&char_map).is_err(),
        "expected error serializing char map key"
    );
}