Merge pull request #9 from Charlesetc/toml-example

first examples on toml page
This commit is contained in:
Bradley Anderson 2017-02-25 13:21:29 -06:00 committed by GitHub
commit 25f19d4363
3 changed files with 87 additions and 6 deletions

19
Cargo.lock generated
View file

@ -5,7 +5,7 @@ dependencies = [
"byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"json 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)",
"skeptic 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"skeptic 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -31,6 +31,11 @@ dependencies = [
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.0.0"
@ -89,9 +94,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pulldown-cmark"
version = "0.0.3"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -110,10 +116,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "skeptic"
version = "0.6.1"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -138,6 +144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum backtrace 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f551bc2ddd53aea015d453ef0b635af89444afa5ed2405dd0b2062ad5d600d80"
"checksum backtrace-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d192fd129132fbc97497c1f2ec2c2c5174e376b95f535199ef4fe0a293d33842"
"checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23"
"checksum byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c40977b0ee6b9885c9013cd41d9feffdd22deb3bb4dc3a71d901cc7a77de18c8"
"checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c"
"checksum dbghelp-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97590ba53bcb8ac28279161ca943a924d1fd4a8fb3fa63302591647c4fc5b850"
@ -147,10 +154,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum json 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5e7eb285e773498f9473a6e2255feffe95db9c55579c7931a6db83c9e02a4673"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5"
"checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07"
"checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1430d286cadb237c17c885e25447c982c97113926bb579f4379c0eca8d9586dc"
"checksum skeptic 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "34c7f11b6755efa4abfd2739426c17de0a36153510bacd6147113fd3a9f2634d"
"checksum skeptic 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "47cea349248a53bda9dca7fd0bb60e46972bf4f6c57f26786d14c27111208312"
"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

View file

@ -7,6 +7,7 @@ A practical guide to the Rust crate ecosystem.
### [File IO](pages/fileio.md) [![file][file-badge]][file]
### [Command Line Parsing](pages/cliparsing.md) [![clap][clap-badge]][clap]
### [JSON](pages/json.md) [![json][json-badge]][json]
### [TOML](pages/toml.md) [![toml][toml-badge]][toml]
## A note about error handling
@ -72,5 +73,7 @@ MIT/Apache-2.0
[clap]: https://kbknapp.github.io/clap-rs/clap/struct.Arg.html
[json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg?label=json
[json]: http://json.rs/doc/json
[toml-badge]: https://img.shields.io/crates/v/rustc-serialize.svg?label=toml
[toml]: http://alexcrichton.com/toml-rs/toml/
[error-docs]: https://doc.rust-lang.org/book/error-handling.html
[error-blog]: https://brson.github.io/2016/11/30/starting-with-error-chain

71
pages/toml.md Normal file
View file

@ -0,0 +1,71 @@
# TOML
Parse TOML into a `toml::Value` and then operate on it:
```rust
extern crate toml;
fn main() {
let toml_source = "
[package]
name = \"your package!\"
version = \"0.1.0\"
authors = [\"You! <you@example.org>\"]
[dependencies]
cool = \"0.2.1\"";
let package_info = toml_source.parse::<toml::Value>().unwrap();
assert_eq!(package_info["dependencies"]["cool"].as_str(), Some("0.2.1"));
assert_eq!(package_info["package"]["name"].as_str(), Some("your package!"));
}
```
Parse TOML into your own structs using the `serde` crate:
```rust
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[derive(Deserialize)]
struct Config {
package: Package,
dependencies: std::collections::HashMap<String, String>,
}
#[derive(Deserialize)]
struct Package {
name: String,
version: String,
authors: Vec<String>,
}
fn main() {
let toml_source = "
[package]
name = \"your package!\"
version = \"0.1.0\"
authors = [\"You! <you@example.org>\"]
[dependencies]
cool = \"0.2.1\"";
let package_info : Config = toml::from_str(toml_source).unwrap();
assert_eq!(package_info.package.name, "your package!");
assert_eq!(package_info.package.version, "0.1.0");
assert_eq!(package_info.package.authors, vec!["You! <you@example.org>"]);
assert_eq!(package_info.dependencies["cool"], "0.2.1");
}
```