From 5280a6c7680226c46bef2711b3d013bf81282c65 Mon Sep 17 00:00:00 2001 From: kper Date: Sun, 2 Oct 2016 12:30:24 +0100 Subject: Add example for decoding toml to struct --- examples/struct.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 examples/struct.rs diff --git a/examples/struct.rs b/examples/struct.rs new file mode 100644 index 0000000..52b2eb7 --- /dev/null +++ b/examples/struct.rs @@ -0,0 +1,51 @@ +#![deny(warnings)] +extern crate toml; +extern crate rustc_serialize; + +use rustc_serialize::Decodable; + +#[derive(Debug,RustcDecodable)] +struct Config { + global_string: Option, + global_integer: Option, + server: Option, + peers: Option>, +} + +#[derive(Debug,RustcDecodable)] +struct ServerConfig { + ip: Option, + port: Option, +} + +#[derive(Debug,RustcDecodable)] +struct PeerConfig { + ip: Option, + port: Option, +} + +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); +} -- cgit v1.2.3