disco: Switch to Into/TryFrom.
This commit is contained in:
parent
f963715e77
commit
2b49d8aa53
3 changed files with 120 additions and 117 deletions
48
src/disco.rs
48
src/disco.rs
|
@ -34,8 +34,11 @@ pub struct Disco {
|
||||||
pub extensions: Vec<DataForm>,
|
pub extensions: Vec<DataForm>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
|
impl<'a> TryFrom<&'a Element> for Disco {
|
||||||
if !root.is("query", ns::DISCO_INFO) {
|
type Error = Error;
|
||||||
|
|
||||||
|
fn try_from(elem: &'a Element) -> Result<Disco, Error> {
|
||||||
|
if !elem.is("query", ns::DISCO_INFO) {
|
||||||
return Err(Error::ParseError("This is not a disco#info element."));
|
return Err(Error::ParseError("This is not a disco#info element."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,10 +46,10 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
|
||||||
let mut features: Vec<Feature> = vec!();
|
let mut features: Vec<Feature> = vec!();
|
||||||
let mut extensions: Vec<DataForm> = vec!();
|
let mut extensions: Vec<DataForm> = vec!();
|
||||||
|
|
||||||
let node = root.attr("node")
|
let node = elem.attr("node")
|
||||||
.and_then(|node| node.parse().ok());
|
.and_then(|node| node.parse().ok());
|
||||||
|
|
||||||
for child in root.children() {
|
for child in elem.children() {
|
||||||
if child.is("feature", ns::DISCO_INFO) {
|
if child.is("feature", ns::DISCO_INFO) {
|
||||||
let feature = child.attr("var")
|
let feature = child.attr("var")
|
||||||
.ok_or(Error::ParseError("Feature must have a 'var' attribute."))?;
|
.ok_or(Error::ParseError("Feature must have a 'var' attribute."))?;
|
||||||
|
@ -110,13 +113,15 @@ pub fn parse_disco(root: &Element) -> Result<Disco, Error> {
|
||||||
extensions: extensions
|
extensions: extensions
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn serialise_disco(disco: &Disco) -> Element {
|
impl<'a> Into<Element> for &'a Disco {
|
||||||
|
fn into(self) -> Element {
|
||||||
let mut root = Element::builder("query")
|
let mut root = Element::builder("query")
|
||||||
.ns(ns::DISCO_INFO)
|
.ns(ns::DISCO_INFO)
|
||||||
.attr("node", disco.node.clone())
|
.attr("node", self.node.clone())
|
||||||
.build();
|
.build();
|
||||||
for identity in &disco.identities {
|
for identity in &self.identities {
|
||||||
let identity_element = Element::builder("identity")
|
let identity_element = Element::builder("identity")
|
||||||
.ns(ns::DISCO_INFO)
|
.ns(ns::DISCO_INFO)
|
||||||
.attr("category", identity.category.clone())
|
.attr("category", identity.category.clone())
|
||||||
|
@ -126,29 +131,28 @@ pub fn serialise_disco(disco: &Disco) -> Element {
|
||||||
.build();
|
.build();
|
||||||
root.append_child(identity_element);
|
root.append_child(identity_element);
|
||||||
}
|
}
|
||||||
for feature in &disco.features {
|
for feature in &self.features {
|
||||||
let feature_element = Element::builder("feature")
|
let feature_element = Element::builder("feature")
|
||||||
.ns(ns::DISCO_INFO)
|
.ns(ns::DISCO_INFO)
|
||||||
.attr("var", feature.var.clone())
|
.attr("var", feature.var.clone())
|
||||||
.build();
|
.build();
|
||||||
root.append_child(feature_element);
|
root.append_child(feature_element);
|
||||||
}
|
}
|
||||||
for _ in &disco.extensions {
|
for _ in &self.extensions {
|
||||||
panic!("Not yet implemented!");
|
panic!("Not yet implemented!");
|
||||||
}
|
}
|
||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use disco;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
||||||
let query = disco::parse_disco(&elem).unwrap();
|
let query = Disco::try_from(&elem).unwrap();
|
||||||
assert!(query.node.is_none());
|
assert!(query.node.is_none());
|
||||||
assert_eq!(query.identities.len(), 1);
|
assert_eq!(query.identities.len(), 1);
|
||||||
assert_eq!(query.features.len(), 1);
|
assert_eq!(query.features.len(), 1);
|
||||||
|
@ -158,7 +162,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid() {
|
fn test_invalid() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -169,7 +173,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_identity() {
|
fn test_invalid_identity() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -177,7 +181,7 @@ mod tests {
|
||||||
assert_eq!(message, "Identity must have a 'category' attribute.");
|
assert_eq!(message, "Identity must have a 'category' attribute.");
|
||||||
|
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -185,7 +189,7 @@ mod tests {
|
||||||
assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
|
assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
|
||||||
|
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -193,7 +197,7 @@ mod tests {
|
||||||
assert_eq!(message, "Identity must have a 'type' attribute.");
|
assert_eq!(message, "Identity must have a 'type' attribute.");
|
||||||
|
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -204,7 +208,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_feature() {
|
fn test_invalid_feature() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -216,7 +220,7 @@ mod tests {
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn test_invalid_result() {
|
fn test_invalid_result() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -224,7 +228,7 @@ mod tests {
|
||||||
assert_eq!(message, "There must be at least one identity in disco#info.");
|
assert_eq!(message, "There must be at least one identity in disco#info.");
|
||||||
|
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
@ -232,7 +236,7 @@ mod tests {
|
||||||
assert_eq!(message, "There must be at least one feature in disco#info.");
|
assert_eq!(message, "There must be at least one feature in disco#info.");
|
||||||
|
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#items'/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#items'/></query>".parse().unwrap();
|
||||||
let error = disco::parse_disco(&elem).unwrap_err();
|
let error = Disco::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
|
@ -166,7 +166,6 @@ pub fn hash_ecaps2(data: &[u8], algo: &str) -> String {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use disco;
|
|
||||||
use ecaps2;
|
use ecaps2;
|
||||||
use base64;
|
use base64;
|
||||||
|
|
||||||
|
@ -195,7 +194,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
||||||
let disco = disco::parse_disco(&elem).unwrap();
|
let disco = Disco::try_from(&elem).unwrap();
|
||||||
let ecaps2 = ecaps2::compute_disco(&disco);
|
let ecaps2 = ecaps2::compute_disco(&disco);
|
||||||
assert_eq!(ecaps2.len(), 54);
|
assert_eq!(ecaps2.len(), 54);
|
||||||
}
|
}
|
||||||
|
@ -258,7 +257,7 @@ mod tests {
|
||||||
105, 109, 101, 31, 28, 99, 108, 105, 101, 110, 116, 31, 109, 111,
|
105, 109, 101, 31, 28, 99, 108, 105, 101, 110, 116, 31, 109, 111,
|
||||||
98, 105, 108, 101, 31, 31, 66, 111, 109, 98, 117, 115, 77, 111,
|
98, 105, 108, 101, 31, 31, 66, 111, 109, 98, 117, 115, 77, 111,
|
||||||
100, 31, 30, 28, 28];
|
100, 31, 30, 28, 28];
|
||||||
let disco = disco::parse_disco(&elem).unwrap();
|
let disco = Disco::try_from(&elem).unwrap();
|
||||||
let ecaps2 = ecaps2::compute_disco(&disco);
|
let ecaps2 = ecaps2::compute_disco(&disco);
|
||||||
assert_eq!(ecaps2.len(), 0x1d9);
|
assert_eq!(ecaps2.len(), 0x1d9);
|
||||||
assert_eq!(ecaps2, expected);
|
assert_eq!(ecaps2, expected);
|
||||||
|
@ -430,7 +429,7 @@ mod tests {
|
||||||
111, 110, 31, 48, 46, 49, 49, 46, 49, 45, 115, 118, 110, 45, 50,
|
111, 110, 31, 48, 46, 49, 49, 46, 49, 45, 115, 118, 110, 45, 50,
|
||||||
48, 49, 49, 49, 50, 49, 54, 45, 109, 111, 100, 32, 40, 84, 99, 108,
|
48, 49, 49, 49, 50, 49, 54, 45, 109, 111, 100, 32, 40, 84, 99, 108,
|
||||||
47, 84, 107, 32, 56, 46,54, 98, 50, 41, 31, 30, 29, 28];
|
47, 84, 107, 32, 56, 46,54, 98, 50, 41, 31, 30, 29, 28];
|
||||||
let disco = disco::parse_disco(&elem).unwrap();
|
let disco = Disco::try_from(&elem).unwrap();
|
||||||
let ecaps2 = ecaps2::compute_disco(&disco);
|
let ecaps2 = ecaps2::compute_disco(&disco);
|
||||||
assert_eq!(ecaps2.len(), 0x543);
|
assert_eq!(ecaps2.len(), 0x543);
|
||||||
assert_eq!(ecaps2, expected);
|
assert_eq!(ecaps2, expected);
|
||||||
|
|
|
@ -17,7 +17,7 @@ use error::Error;
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use stanza_error;
|
use stanza_error;
|
||||||
use disco;
|
use disco::Disco;
|
||||||
use ibb::IBB;
|
use ibb::IBB;
|
||||||
use jingle::Jingle;
|
use jingle::Jingle;
|
||||||
use ping::Ping;
|
use ping::Ping;
|
||||||
|
@ -25,7 +25,7 @@ use ping::Ping;
|
||||||
/// Lists every known payload of a `<iq/>`.
|
/// Lists every known payload of a `<iq/>`.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum IqPayload {
|
pub enum IqPayload {
|
||||||
Disco(disco::Disco),
|
Disco(Disco),
|
||||||
IBB(IBB),
|
IBB(IBB),
|
||||||
Jingle(Jingle),
|
Jingle(Jingle),
|
||||||
Ping(Ping),
|
Ping(Ping),
|
||||||
|
@ -95,7 +95,7 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
|
||||||
return Err(Error::ParseError("Wrong number of children in iq element."));
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let parsed_payload = if let Ok(disco) = disco::parse_disco(elem) {
|
let parsed_payload = if let Ok(disco) = Disco::try_from(elem) {
|
||||||
Some(IqPayload::Disco(disco))
|
Some(IqPayload::Disco(disco))
|
||||||
} else if let Ok(ibb) = IBB::try_from(elem) {
|
} else if let Ok(ibb) = IBB::try_from(elem) {
|
||||||
Some(IqPayload::IBB(ibb))
|
Some(IqPayload::IBB(ibb))
|
||||||
|
@ -152,7 +152,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.into(),
|
||||||
IqPayload::IBB(ref ibb) => ibb.into(),
|
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