From c77221e43748aed5336de836fb4076c844b38fb3 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 5 Sep 2019 15:34:21 +0200 Subject: [PATCH] macros: Allow non-String in generate_elem_id!(). --- src/mood.rs | 4 ++++ src/muc/user.rs | 4 ++++ src/util/macros.rs | 13 ++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/mood.rs b/src/mood.rs index 8d27c4f0..f4b529c5 100644 --- a/src/mood.rs +++ b/src/mood.rs @@ -302,7 +302,11 @@ mod tests { let elem: Element = "Yay!" .parse() .unwrap(); + let elem2 = elem.clone(); let text = Text::try_from(elem).unwrap(); assert_eq!(text.0, String::from("Yay!")); + + let elem3 = text.into(); + assert_eq!(elem2, elem3); } } diff --git a/src/muc/user.rs b/src/muc/user.rs index 44af3a9c..d785d235 100644 --- a/src/muc/user.rs +++ b/src/muc/user.rs @@ -511,8 +511,12 @@ mod tests { Reason" .parse() .unwrap(); + let elem2 = elem.clone(); let reason = Reason::try_from(elem).unwrap(); assert_eq!(reason.0, "Reason".to_owned()); + + let elem3 = reason.into(); + assert_eq!(elem2, elem3); } #[cfg(not(feature = "disable-validation"))] diff --git a/src/util/macros.rs b/src/util/macros.rs index 5b54cf65..c4b5c4e0 100644 --- a/src/util/macros.rs +++ b/src/util/macros.rs @@ -410,9 +410,7 @@ macro_rules! generate_id { macro_rules! generate_elem_id { ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident) => ( - $(#[$meta])* - #[derive(Debug, Clone, PartialEq, Eq, Hash)] - pub struct $elem(pub String); + generate_elem_id!($(#[$meta])* $elem, $name, $ns, String); impl ::std::str::FromStr for $elem { type Err = crate::util::error::Error; fn from_str(s: &str) -> Result<$elem, crate::util::error::Error> { @@ -420,6 +418,11 @@ macro_rules! generate_elem_id { Ok($elem(String::from(s))) } } + ); + ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, $type:ty) => ( + $(#[$meta])* + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub struct $elem(pub $type); impl ::std::convert::TryFrom<::minidom::Element> for $elem { type Error = crate::util::error::Error; fn try_from(elem: ::minidom::Element) -> Result<$elem, crate::util::error::Error> { @@ -427,14 +430,14 @@ macro_rules! generate_elem_id { check_no_children!(elem, $name); check_no_attributes!(elem, $name); // TODO: add a way to parse that differently when needed. - Ok($elem(elem.text())) + Ok($elem(elem.text().parse()?)) } } impl From<$elem> for ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element { ::minidom::Element::builder($name) .ns(crate::ns::$ns) - .append(elem.0) + .append(elem.0.to_string()) .build() } }