Example
This commit is contained in:
1 parent
9558c6e30e
commit
7be516b237
2 files changed
+40
No files matched your search
@@ -12,3 +12,7 @@ proc-macro = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
syn = "1.0"
|
syn = "1.0"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
serde = "1.0"
|
||||||
|
serde_json = "1.0"
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate enumscribe;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(EnumStrDeserialize, PartialEq, Eq, Debug)]
|
||||||
|
#[case_insensitive]
|
||||||
|
enum Airport {
|
||||||
|
#[str_name("LHR")]
|
||||||
|
Heathrow,
|
||||||
|
#[str_name("LGW")]
|
||||||
|
Gatwick,
|
||||||
|
#[str_name("LTN")]
|
||||||
|
Luton,
|
||||||
|
#[str_name("BHX")]
|
||||||
|
BirminghamInternational,
|
||||||
|
#[other]
|
||||||
|
Other(Box<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let json_str = r#"
|
||||||
|
{
|
||||||
|
"airport_1": "LTN",
|
||||||
|
"airport_2": "bhx",
|
||||||
|
"airport_3": "lHr",
|
||||||
|
"airport_4": "MAN"
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let json: HashMap<String, Airport> = serde_json::from_str(json_str).unwrap();
|
||||||
|
|
||||||
|
println!("{:?}", json.get("airport_1").unwrap());
|
||||||
|
println!("{:?}", json.get("airport_2").unwrap());
|
||||||
|
println!("{:?}", json.get("airport_3").unwrap());
|
||||||
|
println!("{:?}", json.get("airport_4").unwrap());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user