1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#![warn(clippy::pedantic, clippy::nursery)]
//! This crate is based on
//! <https://cprimozic.net/blog/writing-a-hashmap-to-struct-procedural-macro-in-rust/>
//!
//! There was sadly no crate available, so I had to make my own :(
//!
//! I made some improvements to the code and ported it to a newer version of
//! `syn`. For example the [`FromMap`] trait doesn't need a type parameter
//! and the [`HashMap`] should contain a static str (field names should be known
//! at compile time).
pub use hashmap_derive::FromMap;
use std::collections::HashMap;

pub trait FromMap: Default {
    type Value;

    #[must_use]
    fn from_map(input: &HashMap<&'static str, Self::Value>) -> Self {
        let mut result = Self::default();
        FromMap::with_map(&mut result, input);
        result
    }

    fn with_map(&mut self, input: &HashMap<&'static str, Self::Value>);

    fn as_map(&self) -> HashMap<&'static str, Self::Value> { HashMap::new() }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Default, FromMap, Debug, PartialEq)]
    struct Attributes {
        pub option_as_ref: bool,
        pub const_fn: bool,
        pub primitive_copy: bool,
        pub inline: bool,
        pub must_use: bool,
        pub copy: bool,
        pub get: bool,
        pub set: bool,
        pub ignore_phantomdata: bool,
        pub skip: bool,
        pub rename: bool,
    }

    #[test]
    fn it_works() {
        let mut data = HashMap::new();
        data.insert("option_as_ref", true);
        data.insert("const_fn", true);
        data.insert("primitive_copy", true);
        data.insert("inline", true);
        data.insert("must_use", true);
        data.insert("copy", true);
        data.insert("get", true);
        data.insert("set", true);
        data.insert("ignore_phantomdata", true);
        data.insert("skip", true);
        data.insert("rename", true);

        let result = Attributes::from_map(&data);

        assert_eq!(
            result,
            Attributes {
                option_as_ref: true,
                const_fn: true,
                primitive_copy: true,
                inline: true,
                must_use: true,
                copy: true,
                get: true,
                set: true,
                ignore_phantomdata: true,
                skip: true,
                rename: true,
            }
        );
    }
}