Import toml 0.7.3
[platform/upstream/rust-toml.git] / examples / enum_external.rs
1 //! An example showing off the usage of `Deserialize` to automatically decode
2 //! TOML into a Rust `struct`, with enums.
3
4 #![deny(warnings)]
5 #![allow(dead_code)]
6
7 use serde::Deserialize;
8
9 /// This is what we're going to decode into.
10 #[derive(Debug, Deserialize)]
11 struct Config {
12     plain: MyEnum,
13     plain_table: MyEnum,
14     tuple: MyEnum,
15     #[serde(rename = "struct")]
16     structv: MyEnum,
17     newtype: MyEnum,
18     my_enum: Vec<MyEnum>,
19 }
20
21 #[derive(Debug, Deserialize)]
22 enum MyEnum {
23     Plain,
24     Tuple(i64, bool),
25     NewType(String),
26     Struct { value: i64 },
27 }
28
29 fn main() {
30     let toml_str = r#"
31     plain = "Plain"
32     plain_table = { Plain = {} }
33     tuple = { Tuple = { 0 = 123, 1 = true } }
34     struct = { Struct = { value = 123 } }
35     newtype = { NewType = "value" }
36     my_enum = [
37         { Plain = {} },
38         { Tuple = { 0 = 123, 1 = true } },
39         { NewType = "value" },
40         { Struct = { value = 123 } }
41     ]"#;
42
43     let decoded: Config = toml::from_str(toml_str).unwrap();
44     println!("{:#?}", decoded);
45 }