From 2dc6a74b84825f65142c1fa7d3e67cd4f35ee3cb Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Wed, 8 Mar 2017 11:09:37 -0800 Subject: Initial work on deep serde integration --- examples/basic/Cargo.toml | 3 ++- examples/basic/src/main.rs | 50 +++++++++++++++++++++------------------- examples/file-json/Cargo.toml | 3 ++- examples/file-toml/Cargo.toml | 5 +++- examples/file-toml/Settings.toml | 3 ++- examples/file-toml/src/main.rs | 18 +++++++++++---- examples/file-yaml/Cargo.toml | 3 ++- 7 files changed, 52 insertions(+), 33 deletions(-) (limited to 'examples') diff --git a/examples/basic/Cargo.toml b/examples/basic/Cargo.toml index 7ede162..25c3f4d 100644 --- a/examples/basic/Cargo.toml +++ b/examples/basic/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "basic" version = "0.1.0" +workspace = "../../" [dependencies] -config = { path = "../..", default-features = false } +config = { path = "../../lib" } diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index 2f947d8..49059ef 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -1,33 +1,35 @@ extern crate config; +use config::*; + fn main() { - let mut c = config::Config::new(); + let mut c = Config::default(); - // Set defaults for `window.width` and `window.height` - c.set_default("window.title", "Basic").unwrap(); - c.set_default("window.width", 640).unwrap(); - c.set_default("window.height", 480).unwrap(); - c.set_default("debug", true).unwrap(); + // // Set defaults for `window.width` and `window.height` + // c.set_default("window.title", "Basic").unwrap(); + // c.set_default("window.width", 640).unwrap(); + // c.set_default("window.height", 480).unwrap(); + // c.set_default("debug", true).unwrap(); - // Note that you can retrieve the stored values as any type as long - // as there exists a reasonable conversion - println!("window.title : {:?}", c.get_str("window.title")); - println!("window.width : {:?}", c.get_str("window.width")); - println!("window.width : {:?}", c.get_int("window.width")); - println!("debug : {:?}", c.get_bool("debug")); - println!("debug : {:?}", c.get_str("debug")); - println!("debug : {:?}", c.get_int("debug")); + // // Note that you can retrieve the stored values as any type as long + // // as there exists a reasonable conversion + // println!("window.title : {:?}", c.get_str("window.title")); + // println!("window.width : {:?}", c.get_str("window.width")); + // println!("window.width : {:?}", c.get_int("window.width")); + // println!("debug : {:?}", c.get_bool("debug")); + // println!("debug : {:?}", c.get_str("debug")); + // println!("debug : {:?}", c.get_int("debug")); - // Attempting to get a value as a type that cannot be reasonably - // converted to will return None - println!("window.title : {:?}", c.get_bool("window.title")); + // // Attempting to get a value as a type that cannot be reasonably + // // converted to will return None + // println!("window.title : {:?}", c.get_bool("window.title")); - // Instead of using a get_* function you can get the variant - // directly - println!("debug : {:?}", c.get("debug")); - println!("debug : {:?}", - c.get("debug").unwrap().into_int()); + // // Instead of using a get_* function you can get the variant + // // directly + // println!("debug : {:?}", c.get("debug")); + // println!("debug : {:?}", + // c.get("debug").unwrap().into_int()); - // Attempting to get a value that does not exist will return None - println!("not-found : {:?}", c.get("not-found")); + // // Attempting to get a value that does not exist will return None + // println!("not-found : {:?}", c.get("not-found")); } diff --git a/examples/file-json/Cargo.toml b/examples/file-json/Cargo.toml index 7223f35..1e8765e 100644 --- a/examples/file-json/Cargo.toml +++ b/examples/file-json/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "file-json" version = "0.1.0" +workspace = "../../" [dependencies] -config = { path = "../..", default-features = false, features = ["json"] } +config = { path = "../../lib", default-features = false, features = ["json"] } diff --git a/examples/file-toml/Cargo.toml b/examples/file-toml/Cargo.toml index 6f45799..0501895 100644 --- a/examples/file-toml/Cargo.toml +++ b/examples/file-toml/Cargo.toml @@ -1,6 +1,9 @@ [package] name = "file-toml" version = "0.1.0" +workspace = "../../" [dependencies] -config = { path = "../.." } +config = { path = "../../lib", features = ["toml"] } +serde = "^0.9" +serde_derive = "^0.9" diff --git a/examples/file-toml/Settings.toml b/examples/file-toml/Settings.toml index 28c5fc6..21fa1e3 100644 --- a/examples/file-toml/Settings.toml +++ b/examples/file-toml/Settings.toml @@ -1,3 +1,4 @@ -debug = false +debug = true pi = 3.14159 weight = 150 +location = { x = 10, y = 30 } diff --git a/examples/file-toml/src/main.rs b/examples/file-toml/src/main.rs index 85db701..ddca412 100644 --- a/examples/file-toml/src/main.rs +++ b/examples/file-toml/src/main.rs @@ -1,12 +1,22 @@ extern crate config; +#[macro_use] +extern crate serde_derive; + +#[derive(Debug, Deserialize)] +struct Point { x: i64, y: i64 } + fn main() { - let mut c = config::Config::new(); + let mut c = config::Config::default(); // Read configuration from "Settings.toml" c.merge(config::File::new("Settings", config::FileFormat::Toml)).unwrap(); - println!("debug = {:?}", c.get("debug")); - println!("pi = {:?}", c.get("pi")); - println!("weight = {:?}", c.get("weight")); + // Simple key access to values + println!("debug = {}", c.get::("debug").unwrap()); + println!("pi = {}", c.get::("pi").unwrap()); + println!("weight = {}", c.get::("weight").unwrap()); + println!("location = {:?}", c.get::("location").unwrap()); + // println!("location.x = {}", c.get::("location.x").unwrap()); + // println!("location.y = {}", c.get::("location.y").unwrap()); } diff --git a/examples/file-yaml/Cargo.toml b/examples/file-yaml/Cargo.toml index 70176b1..4570078 100644 --- a/examples/file-yaml/Cargo.toml +++ b/examples/file-yaml/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "file-yaml" version = "0.1.0" +workspace = "../../" [dependencies] -config = { path = "../..", default-features = false, features = ["yaml"] } +config = { path = "../../lib", default-features = false, features = ["yaml"] } -- cgit v1.2.3