aboutsummaryrefslogtreecommitdiff
path: root/examples/struct.rs
blob: 52b2eb7db29d224d3b1003e4c872aaed59b49efa (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
#![deny(warnings)]
extern crate toml;
extern crate rustc_serialize;

use rustc_serialize::Decodable;

#[derive(Debug,RustcDecodable)]
struct Config {
    global_string: Option<String>,
    global_integer: Option<u64>,
    server: Option<ServerConfig>,
    peers: Option<Vec<PeerConfig>>,
}

#[derive(Debug,RustcDecodable)]
struct ServerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

#[derive(Debug,RustcDecodable)]
struct PeerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

fn main() {
    let toml_str = r#"
            global_string = "test"
            global_integer = 5

            [server]
            ip = "127.0.0.1"
            port = 80

            [[peers]]
            ip = "127.0.0.1"
            port = 8080

            [[peers]]
            ip = "127.0.0.1"
            port = 8081
        "#;

    let toml = toml::Parser::new(&toml_str).parse().unwrap();

    let mut decoder = toml::Decoder::new(toml::Value::Table(toml));
    let decoded = Config::decode(&mut decoder).unwrap();

    println!("{:?}", decoded);
}