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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#[macro_export]
/// Implements `FromStr` for a type that forwards to serde.
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// #[macro_use] extern crate serde_plain;
/// # fn main() {
///
/// #[derive(Deserialize, Debug)]
/// pub enum MyEnum {
///     VariantA,
///     VariantB,
/// }
///
/// forward_from_str_to_serde!(MyEnum);
/// # }
/// ```
///
/// This automatically implements `FromStr` which will invoke the
/// `from_str` method from this crate.
///
/// Additionally this macro supports a second argument which can be the
/// error type to use.  In that case `From<serde_plain::Error>` needs
/// to be implemented for that error.
///
/// A third form with a conversion function as second argument is supported.
/// The closure needs to be in the form `|err| -> ErrType { ... }`:
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// #[macro_use] extern crate serde_plain;
/// # fn main() {
///
/// #[derive(Deserialize, Debug)]
/// pub enum MyEnum {
///     VariantA,
///     VariantB,
/// }
///
/// #[derive(Debug)]
/// pub struct MyError(String);
///
/// forward_from_str_to_serde!(MyEnum, |err| -> MyError { MyError(err.to_string()) });
/// # }
/// ```
macro_rules! forward_from_str_to_serde {
    ($type:ty) => {
        impl ::std::str::FromStr for $type {
            type Err = $crate::Error;
            fn from_str(s: &str) -> ::std::result::Result<$type, Self::Err> {
                $crate::from_str(s)
            }
        }
    };
    ($type:ty, |$var:ident| -> $err_type:ty { $err_conv:expr }) => {
        impl ::std::str::FromStr for $type {
            type Err = $err_type;
            fn from_str(s: &str) -> ::std::result::Result<$type, Self::Err> {
                $crate::from_str(s).map_err(|$var| ($err_conv))
            }
        }
    };
    ($type:ty, $err_type:ty) => {
        impl ::std::str::FromStr for $type {
            type Err = $err_type;
            fn from_str(s: &str) -> ::std::result::Result<$type, Self::Err> {
                $crate::from_str(s).map_err(|e| e.into())
            }
        }
    };
}

#[macro_export]
/// Implements `fmt::Display` for a type that forwards to serde.
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// #[macro_use] extern crate serde_plain;
/// # fn main() {
///
/// #[derive(Serialize, Debug)]
/// pub enum MyEnum {
///     VariantA,
///     VariantB,
/// }
///
/// forward_display_to_serde!(MyEnum);
/// # }
/// ```
///
/// This automatically implements `fmt::Display` which will invoke the
/// `to_string` method from this crate.  In case that fails the method
/// will panic.
macro_rules! forward_display_to_serde {
    ($type:ty) => {
        impl ::std::fmt::Display for $type {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                write!(f, "{}", $crate::to_string(self).unwrap())
            }
        }
    }
}

/// Derives `serde::Deserialize` a type that implements `FromStr`.
///
/// ```rust
/// use std::str::FromStr;
/// use std::num::ParseIntError;
/// #[macro_use] extern crate serde;
/// #[macro_use] extern crate serde_plain;
/// # fn main() {
///
/// pub struct MyStruct(u32);
///
/// impl FromStr for MyStruct {
///     type Err = ParseIntError;
///     fn from_str(value: &str) -> Result<MyStruct, Self::Err> {
///         Ok(MyStruct(value.parse()?))
///     }
/// }
///
/// derive_deserialize_from_str!(MyStruct, "valid positive number");
/// # }
/// ```
///
/// This automatically implements `fmt::Serialize` which will invoke the
/// `from_str` function on the target type internally. First argument is
/// the name of the type, the second is a message for the expectation
/// error (human readable type effectively).
#[macro_export]
macro_rules! derive_deserialize_from_str {
    ($type:ty, $expectation:expr) => {
        impl<'de> ::serde::de::Deserialize<'de> for $type {
            fn deserialize<D>(deserializer: D) -> ::std::result::Result<Self, D::Error>
            where
                D: ::serde::de::Deserializer<'de>,
            {
                struct V;

                impl<'de> ::serde::de::Visitor<'de> for V {
                    type Value = $type;

                    fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                        formatter.write_str($expectation)
                    }

                    fn visit_str<E>(self, value: &str) -> ::std::result::Result<$type, E>
                    where
                        E: ::serde::de::Error,
                    {
                        value
                            .parse()
                            .map_err(|_| ::serde::de::Error::invalid_value(
                                ::serde::de::Unexpected::Str(value), &self))
                    }
                }

                deserializer.deserialize_str(V)
            }
        }
    }
}

/// Derives `serde::Serialize` a type that implements `fmt::Display`.
///
/// ```rust
/// use std::fmt;
/// #[macro_use] extern crate serde;
/// #[macro_use] extern crate serde_plain;
/// # fn main() {
///
/// pub struct MyStruct(u32);
///
/// impl fmt::Display for MyStruct {
///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
///         write!(f, "{}", self.0)
///     }
/// }
///
/// derive_serialize_from_display!(MyStruct);
/// # }
/// ```
///
/// This automatically implements `fmt::Serialize` which will invoke the
/// `to_string` method on the target.
#[macro_export]
macro_rules! derive_serialize_from_display {
    ($type:ty) => {
        impl ::serde::ser::Serialize for $type {
            fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
            where
                S: ::serde::ser::Serializer,
            {
                serializer.serialize_str(&self.to_string())
            }
        }
    }
}