#hash-map #struct #parser #macro-derive #string #try #deserialize

macro try-from-map

A derive macro for converting a HashMap<String, String> into a struct

5 releases

0.2.1 Mar 13, 2024
0.2.0 Mar 13, 2024
0.1.2 Mar 5, 2024
0.1.1 Mar 5, 2024
0.1.0 Mar 5, 2024

#981 in Procedural macros

Download history 12/week @ 2024-09-23

156 downloads per month

MIT license

8KB
131 lines

Derive TryFrom<HashMap<String, String>> for a struct.

Supports structs with named fields that either impl FromStr or serde::Deserialize. Fields that implement serde::Deserialize can be annotated with #[serde_json] to parse the value as JSON.

Example

use try_from_map::TryFromMap;

#[derive(TryFromMap, Debug)]
struct Foo {
   a: i32,
   b: f32,
   c: Option<bool>,
   #[serde_json] // Parse as JSON as Vec<f64> does not impl FromStr
   d: Vec<f64>,
}

let map = std::collections::HashMap::from([
    ("a".to_string(), "42".to_string()),
    ("b".to_string(), "3.14".to_string()),
    ("d".to_string(), "[3.14, 2.71]".to_string()),
]);

let foo = Foo::try_from(map).unwrap();
println!("{:?}", foo);

assert_eq!(foo.a, 42);
assert_eq!(foo.b, 3.14);
assert_eq!(foo.c, None);
assert_eq!(foo.d, vec![3.14, 2.71]);

Dependencies

~245–690KB
~17K SLoC