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
#[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. macro_rules! forward_from_str_to_serde { ($type:ty) => { impl ::std::str::FromStr for $type { type Err = $crate::Error; fn from_str(s: &str) -> Result<$type, Self::Err> { $crate::from_str(s) } } }; ($type:ty, $err_type:ty) => { impl ::std::str::FromStr for $type { type Err = $err_type; fn from_str(s: &str) -> 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()) } } } }