toml-rs/examples/enum_external.rs

45 lines
1 KiB
Rust
Raw Normal View History

//! An example showing off the usage of `Deserialize` to automatically decode
//! TOML into a Rust `struct`, with enums.
#![deny(warnings)]
2019-05-08 19:37:38 -05:00
use serde_derive::Deserialize;
/// 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,
2018-10-09 15:16:03 -05:00
newtype: MyEnum,
my_enum: Vec<MyEnum>,
}
#[derive(Debug, Deserialize)]
enum MyEnum {
Plain,
Tuple(i64, bool),
2018-10-09 15:16:03 -05:00
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 } }
2018-10-09 15:16:03 -05:00
newtype = { NewType = "value" }
my_enum = [
{ Plain = {} },
{ Tuple = { 0 = 123, 1 = true } },
2018-10-09 15:16:03 -05:00
{ NewType = "value" },
{ Struct = { value = 123 } }
]"#;
let decoded: Config = toml::from_str(toml_str).unwrap();
println!("{:#?}", decoded);
}