Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit f667495

Browse files
authored
Merge pull request #264 from azriel91/feature/225/externally-tagged-enums
Issue 255: Externally tagged enum deserialization
2 parents 7d93f00 + df7ec3a commit f667495

File tree

3 files changed

+675
-57
lines changed

3 files changed

+675
-57
lines changed

examples/enum_external.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
6+
extern crate toml;
7+
#[macro_use]
8+
extern crate serde_derive;
9+
10+
/// This is what we're going to decode into.
11+
#[derive(Debug, Deserialize)]
12+
struct Config {
13+
plain: MyEnum,
14+
plain_table: MyEnum,
15+
tuple: MyEnum,
16+
#[serde(rename = "struct")]
17+
structv: MyEnum,
18+
newtype: MyEnum,
19+
my_enum: Vec<MyEnum>,
20+
}
21+
22+
#[derive(Debug, Deserialize)]
23+
enum MyEnum {
24+
Plain,
25+
Tuple(i64, bool),
26+
NewType(String),
27+
Struct { value: i64 },
28+
}
29+
30+
fn main() {
31+
let toml_str = r#"
32+
plain = "Plain"
33+
plain_table = { Plain = {} }
34+
tuple = { Tuple = { 0 = 123, 1 = true } }
35+
struct = { Struct = { value = 123 } }
36+
newtype = { NewType = "value" }
37+
my_enum = [
38+
{ Plain = {} },
39+
{ Tuple = { 0 = 123, 1 = true } },
40+
{ NewType = "value" },
41+
{ Struct = { value = 123 } }
42+
]"#;
43+
44+
let decoded: Config = toml::from_str(toml_str).unwrap();
45+
println!("{:#?}", decoded);
46+
}

0 commit comments

Comments
 (0)