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
//! Contains macro for wrapping serde format.

macro_rules! text_format {
    ($type:ident based on $format:ident) => {
        impl<'a, T> Into<$crate::format::Text> for $type<&'a T>
        where
            T: ::serde::Serialize,
        {
            fn into(self) -> $crate::format::Text {
                $format::to_string(&self.0).map_err(::failure::Error::from)
            }
        }

        impl<T> From<$crate::format::Text> for $type<Result<T, ::failure::Error>>
        where
            T: for<'de> ::serde::Deserialize<'de>,
        {
            fn from(value: $crate::format::Text) -> Self {
                match value {
                    Ok(data) => $type($format::from_str(&data).map_err(::failure::Error::from)),
                    Err(reason) => $type(Err(reason)),
                }
            }
        }
    };
}

macro_rules! binary_format {
    ($type:ident based on $format:ident) => {
        impl<'a, T> Into<$crate::format::Binary> for $type<&'a T>
        where
            T: ::serde::Serialize,
        {
            fn into(self) -> $crate::format::Binary {
                $format::to_vec(&self.0).map_err(::failure::Error::from)
            }
        }

        impl<T> From<$crate::format::Binary> for $type<Result<T, ::failure::Error>>
        where
            T: for<'de> ::serde::Deserialize<'de>,
        {
            fn from(value: $crate::format::Binary) -> Self {
                match value {
                    Ok(data) => $type($format::from_slice(&data).map_err(::failure::Error::from)),
                    Err(reason) => $type(Err(reason)),
                }
            }
        }
    };
}