diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-26 00:09:41 -0800 |
---|---|---|
committer | Ryan Leckey <leckey.ryan@gmail.com> | 2017-01-26 00:09:41 -0800 |
commit | a6fb2f92dc8d53660c1d2d066f146ef261052330 (patch) | |
tree | d66d90b8e563f0df6bee8563847ba9e67edfa9b7 /examples | |
parent | 7c696e6b457debb0faca4fdb0d55e3ed43773484 (diff) |
Add some examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/basic-file/Cargo.toml | 7 | ||||
-rw-r--r-- | examples/basic-file/src/main.rs | 9 | ||||
-rw-r--r-- | examples/basic/Cargo.toml | 7 | ||||
-rw-r--r-- | examples/basic/src/main.rs | 31 |
4 files changed, 54 insertions, 0 deletions
diff --git a/examples/basic-file/Cargo.toml b/examples/basic-file/Cargo.toml new file mode 100644 index 0000000..ffef864 --- /dev/null +++ b/examples/basic-file/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "basic-file" +version = "0.1.0" +authors = ["Ryan Leckey <leckey.ryan@gmail.com>"] + +[dependencies] +config = { path = "../.." } diff --git a/examples/basic-file/src/main.rs b/examples/basic-file/src/main.rs new file mode 100644 index 0000000..ae394f9 --- /dev/null +++ b/examples/basic-file/src/main.rs @@ -0,0 +1,9 @@ +extern crate config; + +fn main() { + // Read configuration from $(cwd)/Cargo.toml + config::merge(config::File::with_name("Cargo")).unwrap(); + + println!("package.name = {:?}", config::get_str("package.name")); + println!("package.version = {:?}", config::get_str("package.version")); +} diff --git a/examples/basic/Cargo.toml b/examples/basic/Cargo.toml new file mode 100644 index 0000000..2cb273b --- /dev/null +++ b/examples/basic/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "basic" +version = "0.1.0" +authors = ["Ryan Leckey <leckey.ryan@gmail.com>"] + +[dependencies] +config = { path = "../.." } diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs new file mode 100644 index 0000000..a0e40bf --- /dev/null +++ b/examples/basic/src/main.rs @@ -0,0 +1,31 @@ +extern crate config; + +fn main() { + // Set defaults for `window.width` and `window.height` + config::set_default("window.title", "Basic"); + config::set_default("window.width", 640); + config::set_default("window.height", 480); + config::set_default("debug", true); + + // Note that you can retrieve the stored values as any type as long + // as there exists a reasonable conversion + println!("window.title : {:?}", config::get_str("window.title")); + println!("window.width : {:?}", config::get_str("window.width")); + println!("window.width : {:?}", config::get_int("window.width")); + println!("debug : {:?}", config::get_bool("debug")); + println!("debug : {:?}", config::get_str("debug")); + println!("debug : {:?}", config::get_int("debug")); + + // Attempting to get a value as a type that cannot be reasonably + // converted to will return None + println!("window.title : {:?}", config::get_bool("window.title")); + + // Instead of using a get_* function you can get the variant + // directly + println!("debug : {:?}", config::get("debug")); + println!("debug : {:?}", + config::get("debug").unwrap().as_int()); + + // Attempting to get a value that does not exist will return None + println!("not-found : {:?}", config::get("not-found")); +} |