ibb: Add a parser for the <close/> element.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-21 03:09:10 +01:00
parent fb373c2b66
commit 189f17c569

View file

@ -37,7 +37,7 @@ impl FromStr for Stanza {
pub enum IBB { pub enum IBB {
Open { block_size: u16, sid: String, stanza: Stanza }, Open { block_size: u16, sid: String, stanza: Stanza },
Data { seq: u16, sid: String, data: Vec<u8> }, Data { seq: u16, sid: String, data: Vec<u8> },
Close(String), Close { sid: String },
} }
fn optional_attr<T: FromStr>(root: &Element, attr: &str) -> Option<T> { fn optional_attr<T: FromStr>(root: &Element, attr: &str) -> Option<T> {
@ -76,6 +76,14 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
sid: sid, sid: sid,
data: data data: data
}) })
} else if root.is("close", ns::IBB) {
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
for _ in root.children() {
return Err(Error::ParseError("Unknown child in close element."));
}
Ok(IBB::Close {
sid: sid,
})
} else { } else {
Err(Error::ParseError("This is not an ibb element.")) Err(Error::ParseError("This is not an ibb element."))
} }
@ -94,6 +102,9 @@ mod tests {
let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap(); let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
ibb::parse_ibb(&elem).unwrap(); ibb::parse_ibb(&elem).unwrap();
let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
ibb::parse_ibb(&elem).unwrap();
} }
#[test] #[test]