ibb: Improve handling of optional stanza attribute.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-22 19:15:29 +01:00
commit 22ec4b1913

View file

@ -28,7 +28,7 @@ impl FromStr for Stanza {
} else if s == "message" { } else if s == "message" {
Ok(Stanza::Message) Ok(Stanza::Message)
} else { } else {
Err(Error::ParseError("Unknown 'stanza' attribute.")) Err(Error::ParseError("Invalid 'stanza' attribute."))
} }
} }
} }
@ -50,37 +50,35 @@ pub enum IBB {
}, },
} }
fn optional_attr<T: FromStr>(root: &Element, attr: &str) -> Option<T> { fn required_attr<T: FromStr>(root: &Element, attr: &str, err: Error) -> Result<T, Error> {
root.attr(attr) root.attr(attr)
.and_then(|value| value.parse().ok()) .and_then(|value| value.parse().ok())
} .ok_or(err)
fn required_attr<T: FromStr>(root: &Element, attr: &str, err: Error) -> Result<T, Error> {
optional_attr(root, attr).ok_or(err)
} }
pub fn parse_ibb(root: &Element) -> Result<IBB, Error> { pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
if root.is("open", ns::IBB) { if root.is("open", ns::IBB) {
let block_size = required_attr(root, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?;
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
let stanza = root.attr("stanza")
.and_then(|value| value.parse().ok())
.unwrap_or_default();
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in open element.")); return Err(Error::ParseError("Unknown child in open element."));
} }
let block_size = required_attr(root, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?;
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
let stanza = match root.attr("stanza") {
Some(stanza) => stanza.parse()?,
None => Default::default(),
};
Ok(IBB::Open { Ok(IBB::Open {
block_size: block_size, block_size: block_size,
sid: sid, sid: sid,
stanza: stanza stanza: stanza
}) })
} else if root.is("data", ns::IBB) { } else if root.is("data", ns::IBB) {
let seq = required_attr(root, "seq", Error::ParseError("Required attribute 'seq' missing in data element."))?;
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
let data = base64::decode(&root.text())?;
for _ in root.children() { for _ in root.children() {
return Err(Error::ParseError("Unknown child in data element.")); return Err(Error::ParseError("Unknown child in data element."));
} }
let seq = required_attr(root, "seq", Error::ParseError("Required attribute 'seq' missing in data element."))?;
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
let data = base64::decode(&root.text())?;
Ok(IBB::Data { Ok(IBB::Data {
seq: seq, seq: seq,
sid: sid, sid: sid,
@ -168,7 +166,6 @@ mod tests {
} }
#[test] #[test]
#[ignore]
fn test_invalid_stanza() { fn test_invalid_stanza() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
let error = ibb::parse_ibb(&elem).unwrap_err(); let error = ibb::parse_ibb(&elem).unwrap_err();
@ -176,6 +173,6 @@ mod tests {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Wrong value for 'stanza' attribute in open."); assert_eq!(message, "Invalid 'stanza' attribute.");
} }
} }