From eff2ed3b51fecf4e6799690c09ce35daa6e51280 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 08:59:46 +1300 Subject: Implement deserialization for unit and struct variants. Issue #225 --- examples/enum_external.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/enum_external.rs (limited to 'examples') 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, +} + +#[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); +} -- cgit v1.2.3 From b6559e9e8dbbe2d9dacc1b5960541315a9765816 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 09:16:03 +1300 Subject: Added `NewType` variant to example. --- examples/enum_external.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'examples') diff --git a/examples/enum_external.rs b/examples/enum_external.rs index 1feff77..095dca5 100644 --- a/examples/enum_external.rs +++ b/examples/enum_external.rs @@ -14,6 +14,7 @@ struct Config { // tuple: MyEnum, #[serde(rename = "struct")] structv: MyEnum, + newtype: MyEnum, my_enum: Vec, } @@ -21,6 +22,7 @@ struct Config { enum MyEnum { Plain, Tuple(i64, bool), + NewType(String), Struct { value: i64 }, } @@ -29,9 +31,11 @@ fn main() { plain = "Plain" # tuple = { 0 = 123, 1 = true } struct = { Struct = { value = 123 } } + newtype = { NewType = "value" } my_enum = [ { Plain = {} }, # { Tuple = { 0 = 123, 1 = true } }, + { NewType = "value" }, { Struct = { value = 123 } } ]"#; -- cgit v1.2.3 From 9ce37a6b0278d4c422e5cac7003cf908cc2c9e3f Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Wed, 10 Oct 2018 11:36:46 +1300 Subject: Implemented enum tuple deserialization. Issue #225 --- examples/enum_external.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/enum_external.rs b/examples/enum_external.rs index 095dca5..e2ac4f8 100644 --- a/examples/enum_external.rs +++ b/examples/enum_external.rs @@ -11,7 +11,8 @@ extern crate serde_derive; #[derive(Debug, Deserialize)] struct Config { plain: MyEnum, - // tuple: MyEnum, + plain_table: MyEnum, + tuple: MyEnum, #[serde(rename = "struct")] structv: MyEnum, newtype: MyEnum, @@ -29,12 +30,13 @@ enum MyEnum { fn main() { let toml_str = r#" plain = "Plain" - # tuple = { 0 = 123, 1 = true } + plain_table = { Plain = {} } + tuple = { Tuple = { 0 = 123, 1 = true } } struct = { Struct = { value = 123 } } newtype = { NewType = "value" } my_enum = [ { Plain = {} }, - # { Tuple = { 0 = 123, 1 = true } }, + { Tuple = { 0 = 123, 1 = true } }, { NewType = "value" }, { Struct = { value = 123 } } ]"#; -- cgit v1.2.3