diff options
author | Azriel Hoh <azriel91@gmail.com> | 2018-10-10 08:59:46 +1300 |
---|---|---|
committer | Azriel Hoh <azriel91@gmail.com> | 2018-11-10 11:06:34 +1300 |
commit | eff2ed3b51fecf4e6799690c09ce35daa6e51280 (patch) | |
tree | bde557a12a1ed32e0870a3f70a93581d0832f3ee /examples | |
parent | 3aec5a918a6cb39fa32e8a56725a3c2fc0ed6bc2 (diff) | |
download | milf-rs-eff2ed3b51fecf4e6799690c09ce35daa6e51280.tar.gz milf-rs-eff2ed3b51fecf4e6799690c09ce35daa6e51280.zip |
Implement deserialization for unit and struct variants.
Issue #225
Diffstat (limited to 'examples')
-rw-r--r-- | examples/enum_external.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/examples/enum_external.rs b/examples/enum_external.rs new file mode 100644 index 0000000..1feff77 --- /dev/null +++ b/examples/enum_external.rs @@ -0,0 +1,40 @@ +//! An example showing off the usage of `Deserialize` to automatically decode +//! TOML into a Rust `struct`, with enums. + +#![deny(warnings)] + +extern crate toml; +#[macro_use] +extern crate serde_derive; + +/// This is what we're going to decode into. +#[derive(Debug, Deserialize)] +struct Config { + plain: MyEnum, + // tuple: MyEnum, + #[serde(rename = "struct")] + structv: MyEnum, + my_enum: Vec<MyEnum>, +} + +#[derive(Debug, Deserialize)] +enum MyEnum { + Plain, + Tuple(i64, bool), + Struct { value: i64 }, +} + +fn main() { + let toml_str = r#" + plain = "Plain" + # tuple = { 0 = 123, 1 = true } + struct = { Struct = { value = 123 } } + my_enum = [ + { Plain = {} }, + # { Tuple = { 0 = 123, 1 = true } }, + { Struct = { value = 123 } } + ]"#; + + let decoded: Config = toml::from_str(toml_str).unwrap(); + println!("{:#?}", decoded); +} |