delay: Use the new helper macro.

This commit is contained in:
Emmanuel Gil Peyrot 2017-11-23 16:19:24 +00:00
parent 32e373be03
commit 682bb34b0b

View file

@ -13,43 +13,15 @@ use error::Error;
use jid::Jid; use jid::Jid;
use ns; use ns;
use helpers::PlainText;
#[derive(Debug, Clone)] generate_element_with_text!(Delay, "delay", ns::DELAY,
pub struct Delay { [
pub from: Option<Jid>, from: Option<Jid> = "from" => optional,
pub stamp: DateTime, stamp: DateTime = "stamp" => required
pub data: Option<String>, ],
} data: PlainText<Option<String>>
);
impl TryFrom<Element> for Delay {
type Err = Error;
fn try_from(elem: Element) -> Result<Delay, Error> {
check_self!(elem, "delay", ns::DELAY);
check_no_children!(elem, "delay");
check_no_unknown_attributes!(elem, "delay", ["from", "stamp"]);
let data = match elem.text().as_ref() {
"" => None,
text => Some(text.to_owned()),
};
Ok(Delay {
from: get_attr!(elem, "from", optional),
stamp: get_attr!(elem, "stamp", required),
data: data,
})
}
}
impl From<Delay> for Element {
fn from(delay: Delay) -> Element {
Element::builder("delay")
.ns(ns::DELAY)
.attr("from", delay.from)
.attr("stamp", delay.stamp)
.append(delay.data)
.build()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {