aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2016-10-02 23:23:24 -0700
committerGitHub <noreply@github.com>2016-10-02 23:23:24 -0700
commit680e5dc0701e198e32b8ea5a192e84c01e6a16de (patch)
treee6a77b7bd5de1a273a892f084ab0118d6a19799e
parenta626c36166e8248bc891f88222ee287bf8d86a79 (diff)
parent5280a6c7680226c46bef2711b3d013bf81282c65 (diff)
downloadmilf-rs-680e5dc0701e198e32b8ea5a192e84c01e6a16de.tar.gz
milf-rs-680e5dc0701e198e32b8ea5a192e84c01e6a16de.zip
Merge pull request #114 from kper/master
Add example for decoding toml to struct
-rw-r--r--examples/struct.rs51
1 files changed, 51 insertions, 0 deletions
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<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);
+}