bind: Switch to an enum, only three options are possible.

This commit is contained in:
Emmanuel Gil Peyrot 2018-02-23 12:38:40 +01:00
parent 7c0975dd5d
commit a484150e4b

View file

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