File: 447_compact_maps_structs.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 985 bytes parent folder | download | duplicates (20)
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
use std::collections::BTreeMap;

use ron::ser::{to_string, to_string_pretty, PrettyConfig};

#[derive(serde_derive::Serialize)]
struct Struct {
    a: u8,
    b: u8,
}

#[test]
fn compact_structs() {
    let s = Struct { a: 4, b: 2 };

    assert_eq!(to_string(&s).unwrap(), "(a:4,b:2)");
    assert_eq!(
        to_string_pretty(&s, PrettyConfig::default()).unwrap(),
        "(\n    a: 4,\n    b: 2,\n)"
    );
    assert_eq!(
        to_string_pretty(&s, PrettyConfig::default().compact_structs(true)).unwrap(),
        "(a: 4, b: 2)"
    );
}

#[test]
fn compact_maps() {
    let m: BTreeMap<&str, i32> = BTreeMap::from_iter([("a", 4), ("b", 2)]);

    assert_eq!(to_string(&m).unwrap(), "{\"a\":4,\"b\":2}");
    assert_eq!(
        to_string_pretty(&m, PrettyConfig::default()).unwrap(),
        "{\n    \"a\": 4,\n    \"b\": 2,\n}"
    );
    assert_eq!(
        to_string_pretty(&m, PrettyConfig::default().compact_maps(true)).unwrap(),
        "{\"a\": 4, \"b\": 2}"
    );
}