diff --git a/src/bind.rs b/src/bind.rs index 6974ce5..3e76dba 100644 --- a/src/bind.rs +++ b/src/bind.rs @@ -13,17 +13,18 @@ use error::Error; use jid::Jid; use ns; -#[derive(Debug, Clone)] -pub struct Bind { - pub resource: Option, - pub jid: Option, +#[derive(Debug, Clone, PartialEq)] +pub enum Bind { + None, + Resource(String), + Jid(Jid), } impl Bind { pub fn new(resource: Option) -> Bind { - Bind { - resource, - jid: None, + match resource { + None => Bind::None, + Some(resource) => Bind::Resource(resource), } } } @@ -35,23 +36,17 @@ impl TryFrom for Bind { check_self!(elem, "bind", ns::BIND); check_no_attributes!(elem, "bind"); - let mut bind = Bind { - resource: None, - jid: None, - }; - let mut already_set = false; + let mut bind = Bind::None; for child in elem.children() { - if already_set { + if bind != Bind::None { return Err(Error::ParseError("Bind can only have one child.")); } if child.is("resource", ns::BIND) { check_no_children!(child, "resource"); - bind.resource = Some(child.text()); - already_set = true; + bind = Bind::Resource(child.text()); } else if child.is("jid", ns::BIND) { check_no_children!(child, "jid"); - bind.jid = Some(Jid::from_str(&child.text())?); - already_set = true; + bind = Bind::Jid(Jid::from_str(&child.text())?); } else { return Err(Error::ParseError("Unknown element in bind.")); } @@ -65,16 +60,21 @@ impl From for Element { fn from(bind: Bind) -> Element { Element::builder("bind") .ns(ns::BIND) - .append(bind.resource.map(|resource| - Element::builder("resource") - .ns(ns::BIND) - .append(resource) - .build())) - .append(bind.jid.map(|jid| - Element::builder("jid") - .ns(ns::BIND) - .append(jid) - .build())) + .append(match bind { + Bind::None => vec!(), + Bind::Resource(resource) => vec!( + Element::builder("resource") + .ns(ns::BIND) + .append(resource) + .build() + ), + Bind::Jid(jid) => vec!( + Element::builder("jid") + .ns(ns::BIND) + .append(jid) + .build() + ), + }) .build() } } @@ -87,7 +87,6 @@ mod tests { fn test_simple() { let elem: Element = "".parse().unwrap(); let bind = Bind::try_from(elem).unwrap(); - assert_eq!(bind.resource, None); - assert_eq!(bind.jid, None); + assert_eq!(bind, Bind::None); } }