aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2018-11-28 09:50:38 -0600
committerGitHub <noreply@github.com>2018-11-28 09:50:38 -0600
commitf667495ae34395be9fc3e4bb8801cacf52c2253e (patch)
treef1b9e15634802581529dce49b93d8e87fce5179b /examples
parent7d93f00ea02acbb9f45c68f9d4bcf1f04f392e46 (diff)
parentdf7ec3a4b66433118f09b12d5f32a10368a57852 (diff)
downloadmilf-rs-f667495ae34395be9fc3e4bb8801cacf52c2253e.tar.gz
milf-rs-f667495ae34395be9fc3e4bb8801cacf52c2253e.zip
Merge pull request #264 from azriel91/feature/225/externally-tagged-enums
Issue 255: Externally tagged enum deserialization
Diffstat (limited to 'examples')
-rw-r--r--examples/enum_external.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/enum_external.rs b/examples/enum_external.rs
new file mode 100644
index 0000000..e2ac4f8
--- /dev/null
+++ b/examples/enum_external.rs
@@ -0,0 +1,46 @@
+//! 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,
+ plain_table: MyEnum,
+ tuple: MyEnum,
+ #[serde(rename = "struct")]
+ structv: MyEnum,
+ newtype: MyEnum,
+ my_enum: Vec<MyEnum>,
+}
+
+#[derive(Debug, Deserialize)]
+enum MyEnum {
+ Plain,
+ Tuple(i64, bool),
+ NewType(String),
+ Struct { value: i64 },
+}
+
+fn main() {
+ let toml_str = r#"
+ plain = "Plain"
+ plain_table = { Plain = {} }
+ tuple = { 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 } }
+ ]"#;
+
+ let decoded: Config = toml::from_str(toml_str).unwrap();
+ println!("{:#?}", decoded);
+}