This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from azriel91/feature/225/externally-tagged-e…
…nums Issue 255: Externally tagged enum deserialization
- Loading branch information
Showing
3 changed files
with
675 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.