macros: Allow non-String in generate_elem_id!().

This commit is contained in:
Emmanuel Gil Peyrot 2019-09-05 15:34:21 +02:00
commit c77221e437
3 changed files with 16 additions and 5 deletions

View file

@ -302,7 +302,11 @@ mod tests {
let elem: Element = "<text xmlns='http://jabber.org/protocol/mood'>Yay!</text>" let elem: Element = "<text xmlns='http://jabber.org/protocol/mood'>Yay!</text>"
.parse() .parse()
.unwrap(); .unwrap();
let elem2 = elem.clone();
let text = Text::try_from(elem).unwrap(); let text = Text::try_from(elem).unwrap();
assert_eq!(text.0, String::from("Yay!")); assert_eq!(text.0, String::from("Yay!"));
let elem3 = text.into();
assert_eq!(elem2, elem3);
} }
} }

View file

@ -511,8 +511,12 @@ mod tests {
<reason xmlns='http://jabber.org/protocol/muc#user'>Reason</reason>" <reason xmlns='http://jabber.org/protocol/muc#user'>Reason</reason>"
.parse() .parse()
.unwrap(); .unwrap();
let elem2 = elem.clone();
let reason = Reason::try_from(elem).unwrap(); let reason = Reason::try_from(elem).unwrap();
assert_eq!(reason.0, "Reason".to_owned()); assert_eq!(reason.0, "Reason".to_owned());
let elem3 = reason.into();
assert_eq!(elem2, elem3);
} }
#[cfg(not(feature = "disable-validation"))] #[cfg(not(feature = "disable-validation"))]

View file

@ -410,9 +410,7 @@ macro_rules! generate_id {
macro_rules! generate_elem_id { macro_rules! generate_elem_id {
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident) => ( ($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident) => (
$(#[$meta])* generate_elem_id!($(#[$meta])* $elem, $name, $ns, String);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $elem(pub String);
impl ::std::str::FromStr for $elem { impl ::std::str::FromStr for $elem {
type Err = crate::util::error::Error; type Err = crate::util::error::Error;
fn from_str(s: &str) -> Result<$elem, 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))) 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 { impl ::std::convert::TryFrom<::minidom::Element> for $elem {
type Error = crate::util::error::Error; type Error = crate::util::error::Error;
fn try_from(elem: ::minidom::Element) -> Result<$elem, 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_children!(elem, $name);
check_no_attributes!(elem, $name); check_no_attributes!(elem, $name);
// TODO: add a way to parse that differently when needed. // 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 { impl From<$elem> for ::minidom::Element {
fn from(elem: $elem) -> ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element {
::minidom::Element::builder($name) ::minidom::Element::builder($name)
.ns(crate::ns::$ns) .ns(crate::ns::$ns)
.append(elem.0) .append(elem.0.to_string())
.build() .build()
} }
} }