summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ron/examples/decode.rs
blob: 56a9f300749722e398879d0f80bc2b52fbf4bea0 (plain)
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
#![allow(dead_code)]

use ron::de::from_str;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
struct Config {
    boolean: bool,
    float: f32,
    map: HashMap<u8, char>,
    nested: Nested,
    option: Option<String>,
    tuple: (u32, u32),
}

#[derive(Debug, Deserialize)]
struct Nested {
    a: String,
    b: char,
}

const CONFIG: &str = "
/*
 * RON now has multi-line (C-style) block comments!
 * They can be freely nested:
 * /* This is a nested comment */
 * If you just want a single-line comment,
 * do it like here:
// Just put two slashes before the comment and the rest of the line
// can be used freely!
*/

// Note that block comments can not be started in a line comment
// (Putting a /* here will have no effect)

(
    boolean: true,
    float: 8.2,
    map: {
        1: '1',
        2: '4',
        3: '9',
        4: '1',
        5: '2',
        6: '3',
    },
    nested: Nested(
        a: \"Decode me!\",
        b: 'z',
    ),
    option: Some(\t  \"Weird formatting!\" \n\n ),
    tuple: (3 /*(2 + 1)*/, 7 /*(2 * 5 - 3)*/),
)";

fn main() {
    let config: Config = match from_str(CONFIG) {
        Ok(x) => x,
        Err(e) => {
            println!("Failed to load config: {}", e);

            std::process::exit(1);
        }
    };

    println!("Config: {:?}", &config);
}