ibb: Port to TryFrom/Into.

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-04 23:11:10 +01:00
parent 033cbe777b
commit e3acb55b49
2 changed files with 86 additions and 81 deletions

View file

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::convert::TryFrom;
use std::str::FromStr; use std::str::FromStr;
use minidom::{Element, IntoAttributeValue}; use minidom::{Element, IntoAttributeValue};
@ -64,20 +65,23 @@ pub enum IBB {
}, },
} }
fn required_attr<T: FromStr>(root: &Element, attr: &str, err: Error) -> Result<T, Error> { fn required_attr<T: FromStr>(elem: &Element, attr: &str, err: Error) -> Result<T, Error> {
root.attr(attr) elem.attr(attr)
.and_then(|value| value.parse().ok()) .and_then(|value| value.parse().ok())
.ok_or(err) .ok_or(err)
} }
pub fn parse_ibb(root: &Element) -> Result<IBB, Error> { impl<'a> TryFrom<&'a Element> for IBB {
if root.is("open", ns::IBB) { type Error = Error;
for _ in root.children() {
fn try_from(elem: &'a Element) -> Result<IBB, Error> {
if elem.is("open", ns::IBB) {
for _ in elem.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 block_size = required_attr(elem, "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 sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
let stanza = match root.attr("stanza") { let stanza = match elem.attr("stanza") {
Some(stanza) => stanza.parse()?, Some(stanza) => stanza.parse()?,
None => Default::default(), None => Default::default(),
}; };
@ -86,21 +90,21 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
sid: sid, sid: sid,
stanza: stanza stanza: stanza
}) })
} else if root.is("data", ns::IBB) { } else if elem.is("data", ns::IBB) {
for _ in root.children() { for _ in elem.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 seq = required_attr(elem, "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 sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
let data = base64::decode(&root.text())?; let data = base64::decode(&elem.text())?;
Ok(IBB::Data { Ok(IBB::Data {
seq: seq, seq: seq,
sid: sid, sid: sid,
data: data data: data
}) })
} else if root.is("close", ns::IBB) { } else if elem.is("close", ns::IBB) {
let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?; let sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
for _ in root.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in close element.")); return Err(Error::ParseError("Unknown child in close element."));
} }
Ok(IBB::Close { Ok(IBB::Close {
@ -109,10 +113,12 @@ pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
} else { } else {
Err(Error::ParseError("This is not an ibb element.")) Err(Error::ParseError("This is not an ibb element."))
} }
}
} }
pub fn serialise(ibb: &IBB) -> Element { impl<'a> Into<Element> for &'a IBB {
match *ibb { fn into(self) -> Element {
match *self {
IBB::Open { ref block_size, ref sid, ref stanza } => { IBB::Open { ref block_size, ref sid, ref stanza } => {
Element::builder("open") Element::builder("open")
.ns(ns::IBB) .ns(ns::IBB)
@ -136,31 +142,30 @@ pub fn serialise(ibb: &IBB) -> Element {
.build() .build()
}, },
} }
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use minidom::Element; use super::*;
use error::Error;
use ibb;
#[test] #[test]
fn test_simple() { fn test_simple() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
let open = ibb::parse_ibb(&elem).unwrap(); let open = IBB::try_from(&elem).unwrap();
match open { match open {
ibb::IBB::Open { block_size, sid, stanza } => { IBB::Open { block_size, sid, stanza } => {
assert_eq!(block_size, 3); assert_eq!(block_size, 3);
assert_eq!(sid, "coucou"); assert_eq!(sid, "coucou");
assert_eq!(stanza, ibb::Stanza::Iq); assert_eq!(stanza, Stanza::Iq);
}, },
_ => panic!(), _ => panic!(),
} }
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();
let data = ibb::parse_ibb(&elem).unwrap(); let data = IBB::try_from(&elem).unwrap();
match data { match data {
ibb::IBB::Data { seq, sid, data } => { IBB::Data { seq, sid, data } => {
assert_eq!(seq, 0); assert_eq!(seq, 0);
assert_eq!(sid, "coucou"); assert_eq!(sid, "coucou");
assert_eq!(data, vec!(0, 0, 0)); assert_eq!(data, vec!(0, 0, 0));
@ -169,9 +174,9 @@ mod tests {
} }
let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap(); let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
let close = ibb::parse_ibb(&elem).unwrap(); let close = IBB::try_from(&elem).unwrap();
match close { match close {
ibb::IBB::Close { sid } => { IBB::Close { sid } => {
assert_eq!(sid, "coucou"); assert_eq!(sid, "coucou");
}, },
_ => panic!(), _ => panic!(),
@ -181,7 +186,7 @@ mod tests {
#[test] #[test]
fn test_invalid() { fn test_invalid() {
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap();
let error = ibb::parse_ibb(&elem).unwrap_err(); let error = IBB::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -190,7 +195,7 @@ mod tests {
// TODO: maybe make a better error message here. // TODO: maybe make a better error message here.
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap();
let error = ibb::parse_ibb(&elem).unwrap_err(); let error = IBB::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -198,7 +203,7 @@ mod tests {
assert_eq!(message, "Required attribute 'block-size' missing in open element."); assert_eq!(message, "Required attribute 'block-size' missing in open element.");
let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap(); let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap();
let error = ibb::parse_ibb(&elem).unwrap_err(); let error = IBB::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),
@ -209,7 +214,7 @@ mod tests {
#[test] #[test]
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::try_from(&elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
_ => panic!(), _ => panic!(),

View file

@ -18,7 +18,7 @@ use ns;
use stanza_error; use stanza_error;
use disco; use disco;
use ibb; use ibb::IBB;
use jingle::Jingle; use jingle::Jingle;
use ping::Ping; use ping::Ping;
@ -26,7 +26,7 @@ use ping::Ping;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum IqPayload { pub enum IqPayload {
Disco(disco::Disco), Disco(disco::Disco),
IBB(ibb::IBB), IBB(IBB),
Jingle(Jingle), Jingle(Jingle),
Ping(Ping), Ping(Ping),
} }
@ -97,7 +97,7 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
} else { } else {
let parsed_payload = if let Ok(disco) = disco::parse_disco(elem) { let parsed_payload = if let Ok(disco) = disco::parse_disco(elem) {
Some(IqPayload::Disco(disco)) Some(IqPayload::Disco(disco))
} else if let Ok(ibb) = ibb::parse_ibb(elem) { } else if let Ok(ibb) = IBB::try_from(elem) {
Some(IqPayload::IBB(ibb)) Some(IqPayload::IBB(ibb))
} else if let Ok(jingle) = Jingle::try_from(elem) { } else if let Ok(jingle) = Jingle::try_from(elem) {
Some(IqPayload::Jingle(jingle)) Some(IqPayload::Jingle(jingle))
@ -153,7 +153,7 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
pub fn serialise_payload(payload: &IqPayload) -> Element { pub fn serialise_payload(payload: &IqPayload) -> Element {
match *payload { match *payload {
IqPayload::Disco(ref disco) => disco::serialise_disco(disco), IqPayload::Disco(ref disco) => disco::serialise_disco(disco),
IqPayload::IBB(ref ibb) => ibb::serialise(ibb), IqPayload::IBB(ref ibb) => ibb.into(),
IqPayload::Jingle(ref jingle) => jingle.into(), IqPayload::Jingle(ref jingle) => jingle.into(),
IqPayload::Ping(ref ping) => ping.into(), IqPayload::Ping(ref ping) => ping.into(),
} }