mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
iq: Switch to Into/TryFrom.
This commit is contained in:
parent
04d90f22ee
commit
fffaca316f
1 changed files with 128 additions and 121 deletions
61
src/iq.rs
61
src/iq.rs
|
@ -64,7 +64,10 @@ pub struct Iq {
|
|||
pub payload: IqType,
|
||||
}
|
||||
|
||||
pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
|
||||
impl<'a> TryFrom<&'a Element> for Iq {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(root: &'a Element) -> Result<Iq, Error> {
|
||||
if !root.is("iq", ns::JABBER_CLIENT) {
|
||||
return Err(Error::ParseError("This is not an iq element."));
|
||||
}
|
||||
|
@ -149,48 +152,52 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
|
|||
payload: type_,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise_payload(payload: &IqPayload) -> Element {
|
||||
match *payload {
|
||||
impl<'a> Into<Element> for &'a IqPayload {
|
||||
fn into(self) -> Element {
|
||||
match *self {
|
||||
IqPayload::Disco(ref disco) => disco.into(),
|
||||
IqPayload::IBB(ref ibb) => ibb.into(),
|
||||
IqPayload::Jingle(ref jingle) => jingle.into(),
|
||||
IqPayload::Ping(ref ping) => ping.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn serialise(iq: &Iq) -> Element {
|
||||
impl<'a> Into<Element> for &'a Iq {
|
||||
fn into(self) -> Element {
|
||||
let mut stanza = Element::builder("iq")
|
||||
.ns(ns::JABBER_CLIENT)
|
||||
.attr("from", iq.from.clone().and_then(|value| Some(String::from(value))))
|
||||
.attr("to", iq.to.clone().and_then(|value| Some(String::from(value))))
|
||||
.attr("id", iq.id.clone())
|
||||
.attr("type", iq.payload.clone())
|
||||
.attr("from", self.from.clone().and_then(|value| Some(String::from(value))))
|
||||
.attr("to", self.to.clone().and_then(|value| Some(String::from(value))))
|
||||
.attr("id", self.id.clone())
|
||||
.attr("type", self.payload.clone())
|
||||
.build();
|
||||
let elem = match iq.payload.clone() {
|
||||
let elem = match self.payload.clone() {
|
||||
IqType::Get(IqPayloadType::XML(elem))
|
||||
| IqType::Set(IqPayloadType::XML(elem))
|
||||
| IqType::Result(Some(IqPayloadType::XML(elem))) => elem,
|
||||
IqType::Error(error) => (&error).into(),
|
||||
IqType::Get(IqPayloadType::Parsed(payload))
|
||||
| IqType::Set(IqPayloadType::Parsed(payload))
|
||||
| IqType::Result(Some(IqPayloadType::Parsed(payload))) => serialise_payload(&payload),
|
||||
| IqType::Result(Some(IqPayloadType::Parsed(payload))) => (&payload).into(),
|
||||
IqType::Result(None) => return stanza,
|
||||
};
|
||||
stanza.append_child(elem);
|
||||
stanza
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use iq;
|
||||
use stanza_error::{ErrorType, DefinedCondition};
|
||||
|
||||
#[test]
|
||||
fn test_require_type() {
|
||||
let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap();
|
||||
let error = iq::parse_iq(&elem).unwrap_err();
|
||||
let error = Iq::try_from(&elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
@ -203,13 +210,13 @@ mod tests {
|
|||
let elem: Element = "<iq xmlns='jabber:client' type='get'>
|
||||
<foo/>
|
||||
</iq>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
let query: Element = "<foo xmlns='jabber:client'/>".parse().unwrap();
|
||||
assert_eq!(iq.from, None);
|
||||
assert_eq!(iq.to, None);
|
||||
assert_eq!(iq.id, None);
|
||||
assert!(match iq.payload {
|
||||
iq::IqType::Get(iq::IqPayloadType::XML(element)) => element == query,
|
||||
IqType::Get(IqPayloadType::XML(element)) => element == query,
|
||||
_ => false
|
||||
});
|
||||
}
|
||||
|
@ -219,13 +226,13 @@ mod tests {
|
|||
let elem: Element = "<iq xmlns='jabber:client' type='set'>
|
||||
<vCard xmlns='vcard-temp'/>
|
||||
</iq>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
||||
assert_eq!(iq.from, None);
|
||||
assert_eq!(iq.to, None);
|
||||
assert_eq!(iq.id, None);
|
||||
assert!(match iq.payload {
|
||||
iq::IqType::Set(iq::IqPayloadType::XML(element)) => element == vcard,
|
||||
IqType::Set(IqPayloadType::XML(element)) => element == vcard,
|
||||
_ => false
|
||||
});
|
||||
}
|
||||
|
@ -233,12 +240,12 @@ mod tests {
|
|||
#[test]
|
||||
fn test_result_empty() {
|
||||
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
assert_eq!(iq.from, None);
|
||||
assert_eq!(iq.to, None);
|
||||
assert_eq!(iq.id, None);
|
||||
assert!(match iq.payload {
|
||||
iq::IqType::Result(None) => true,
|
||||
IqType::Result(None) => true,
|
||||
_ => false,
|
||||
});
|
||||
}
|
||||
|
@ -248,13 +255,13 @@ mod tests {
|
|||
let elem: Element = "<iq xmlns='jabber:client' type='result'>
|
||||
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
||||
</iq>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
|
||||
assert_eq!(iq.from, None);
|
||||
assert_eq!(iq.to, None);
|
||||
assert_eq!(iq.id, None);
|
||||
assert!(match iq.payload {
|
||||
iq::IqType::Result(Some(iq::IqPayloadType::XML(element))) => element == query,
|
||||
IqType::Result(Some(IqPayloadType::XML(element))) => element == query,
|
||||
_ => false,
|
||||
});
|
||||
}
|
||||
|
@ -267,12 +274,12 @@ mod tests {
|
|||
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
||||
</error>
|
||||
</iq>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
assert_eq!(iq.from, None);
|
||||
assert_eq!(iq.to, None);
|
||||
assert_eq!(iq.id, None);
|
||||
match iq.payload {
|
||||
iq::IqType::Error(error) => {
|
||||
IqType::Error(error) => {
|
||||
assert_eq!(error.type_, ErrorType::Cancel);
|
||||
assert_eq!(error.by, None);
|
||||
assert_eq!(error.defined_condition, DefinedCondition::ServiceUnavailable);
|
||||
|
@ -286,7 +293,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_children_invalid() {
|
||||
let elem: Element = "<iq xmlns='jabber:client' type='error'></iq>".parse().unwrap();
|
||||
let error = iq::parse_iq(&elem).unwrap_err();
|
||||
let error = Iq::try_from(&elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
_ => panic!(),
|
||||
|
@ -297,20 +304,20 @@ mod tests {
|
|||
#[test]
|
||||
fn test_serialise() {
|
||||
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
|
||||
let iq2 = iq::Iq {
|
||||
let iq2 = Iq {
|
||||
from: None,
|
||||
to: None,
|
||||
id: None,
|
||||
payload: iq::IqType::Result(None),
|
||||
payload: IqType::Result(None),
|
||||
};
|
||||
let elem2 = iq::serialise(&iq2);
|
||||
let elem2 = (&iq2).into();
|
||||
assert_eq!(elem, elem2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_disco() {
|
||||
let elem: Element = "<iq xmlns='jabber:client' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
||||
let iq = iq::parse_iq(&elem).unwrap();
|
||||
let iq = Iq::try_from(&elem).unwrap();
|
||||
assert!(match iq.payload {
|
||||
IqType::Get(IqPayloadType::Parsed(IqPayload::Disco(Disco { .. }))) => true,
|
||||
_ => false,
|
||||
|
|
Loading…
Reference in a new issue