This repository was archived by the owner on Sep 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +675
-57
lines changed Expand file tree Collapse file tree 3 files changed +675
-57
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments