mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
ibb: Port to TryFrom/Into.
This commit is contained in:
parent
033cbe777b
commit
e3acb55b49
2 changed files with 86 additions and 81 deletions
159
src/ibb.rs
159
src/ibb.rs
|
@ -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,103 +65,107 @@ 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() {
|
|
||||||
return Err(Error::ParseError("Unknown child in open element."));
|
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."));
|
||||||
|
}
|
||||||
|
let block_size = required_attr(elem, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?;
|
||||||
|
let sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
|
||||||
|
let stanza = match elem.attr("stanza") {
|
||||||
|
Some(stanza) => stanza.parse()?,
|
||||||
|
None => Default::default(),
|
||||||
|
};
|
||||||
|
Ok(IBB::Open {
|
||||||
|
block_size: block_size,
|
||||||
|
sid: sid,
|
||||||
|
stanza: stanza
|
||||||
|
})
|
||||||
|
} else if elem.is("data", ns::IBB) {
|
||||||
|
for _ in elem.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in data element."));
|
||||||
|
}
|
||||||
|
let seq = required_attr(elem, "seq", Error::ParseError("Required attribute 'seq' missing in data element."))?;
|
||||||
|
let sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
|
||||||
|
let data = base64::decode(&elem.text())?;
|
||||||
|
Ok(IBB::Data {
|
||||||
|
seq: seq,
|
||||||
|
sid: sid,
|
||||||
|
data: data
|
||||||
|
})
|
||||||
|
} else if elem.is("close", ns::IBB) {
|
||||||
|
let sid = required_attr(elem, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
|
||||||
|
for _ in elem.children() {
|
||||||
|
return Err(Error::ParseError("Unknown child in close element."));
|
||||||
|
}
|
||||||
|
Ok(IBB::Close {
|
||||||
|
sid: sid,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err(Error::ParseError("This is not an ibb 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 {
|
|
||||||
block_size: block_size,
|
|
||||||
sid: sid,
|
|
||||||
stanza: stanza
|
|
||||||
})
|
|
||||||
} else if root.is("data", ns::IBB) {
|
|
||||||
for _ in root.children() {
|
|
||||||
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 {
|
|
||||||
seq: seq,
|
|
||||||
sid: sid,
|
|
||||||
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 {
|
|
||||||
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 {
|
||||||
IBB::Open { ref block_size, ref sid, ref stanza } => {
|
match *self {
|
||||||
Element::builder("open")
|
IBB::Open { ref block_size, ref sid, ref stanza } => {
|
||||||
.ns(ns::IBB)
|
Element::builder("open")
|
||||||
.attr("block-size", format!("{}", block_size))
|
.ns(ns::IBB)
|
||||||
.attr("sid", sid.to_owned())
|
.attr("block-size", format!("{}", block_size))
|
||||||
.attr("stanza", stanza.to_owned())
|
.attr("sid", sid.to_owned())
|
||||||
.build()
|
.attr("stanza", stanza.to_owned())
|
||||||
},
|
.build()
|
||||||
IBB::Data { ref seq, ref sid, ref data } => {
|
},
|
||||||
Element::builder("data")
|
IBB::Data { ref seq, ref sid, ref data } => {
|
||||||
.ns(ns::IBB)
|
Element::builder("data")
|
||||||
.attr("seq", format!("{}", seq))
|
.ns(ns::IBB)
|
||||||
.attr("sid", sid.to_owned())
|
.attr("seq", format!("{}", seq))
|
||||||
.append(base64::encode(&data))
|
.attr("sid", sid.to_owned())
|
||||||
.build()
|
.append(base64::encode(&data))
|
||||||
},
|
.build()
|
||||||
IBB::Close { ref sid } => {
|
},
|
||||||
Element::builder("close")
|
IBB::Close { ref sid } => {
|
||||||
.ns(ns::IBB)
|
Element::builder("close")
|
||||||
.attr("sid", sid.to_owned())
|
.ns(ns::IBB)
|
||||||
.build()
|
.attr("sid", sid.to_owned())
|
||||||
},
|
.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!(),
|
||||||
|
|
|
@ -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(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue