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
use crate::{FromValue, Shared, ToValue, Value, ValueError};

macro_rules! impl_map {
    ($($tt:tt)*) => {
        value_types!(impl crate::OBJECT_TYPE, $($tt)*<String, T> => T $($tt)*<String, T>);

        impl<T> FromValue for $($tt)*<String, T>
        where
            T: FromValue,
        {
            fn from_value(value: Value) -> Result<Self, ValueError> {
                let object = value.into_object()?;
                let object = object.take()?;

                let mut output = $($tt)*::with_capacity(object.len());

                for (key, value) in object {
                    output.insert(key, T::from_value(value)?);
                }

                Ok(output)
            }
        }

        impl<T> ToValue for $($tt)*<String, T>
        where
            T: ToValue,
        {
            fn to_value(self) -> Result<Value, ValueError> {
                let mut output = crate::collections::HashMap::with_capacity(self.len());

                for (key, value) in self {
                    output.insert(key, value.to_value()?);
                }

                Ok(Value::Object(Shared::new(output)))
            }
        }
    }
}

impl_map!(std::collections::HashMap);