stanza_error: Switch to Into/TryFrom.
This commit is contained in:
parent
418956c720
commit
04d90f22ee
4 changed files with 100 additions and 97 deletions
20
src/iq.rs
20
src/iq.rs
|
|
@ -16,7 +16,7 @@ use error::Error;
|
||||||
|
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use stanza_error;
|
use stanza_error::StanzaError;
|
||||||
use disco::Disco;
|
use disco::Disco;
|
||||||
use ibb::IBB;
|
use ibb::IBB;
|
||||||
use jingle::Jingle;
|
use jingle::Jingle;
|
||||||
|
|
@ -42,7 +42,7 @@ pub enum IqType {
|
||||||
Get(IqPayloadType),
|
Get(IqPayloadType),
|
||||||
Set(IqPayloadType),
|
Set(IqPayloadType),
|
||||||
Result(Option<IqPayloadType>),
|
Result(Option<IqPayloadType>),
|
||||||
Error(stanza_error::StanzaError),
|
Error(StanzaError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoAttributeValue for IqType {
|
impl IntoAttributeValue for IqType {
|
||||||
|
|
@ -90,7 +90,7 @@ pub fn parse_iq(root: &Element) -> Result<Iq, Error> {
|
||||||
if error_payload.is_some() {
|
if error_payload.is_some() {
|
||||||
return Err(Error::ParseError("Wrong number of children in iq element."));
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
||||||
}
|
}
|
||||||
error_payload = Some(stanza_error::parse_stanza_error(elem)?);
|
error_payload = Some(StanzaError::try_from(elem)?);
|
||||||
} else if root.children().collect::<Vec<_>>().len() != 2 {
|
} else if root.children().collect::<Vec<_>>().len() != 2 {
|
||||||
return Err(Error::ParseError("Wrong number of children in iq element."));
|
return Err(Error::ParseError("Wrong number of children in iq element."));
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +171,7 @@ pub fn serialise(iq: &Iq) -> Element {
|
||||||
IqType::Get(IqPayloadType::XML(elem))
|
IqType::Get(IqPayloadType::XML(elem))
|
||||||
| IqType::Set(IqPayloadType::XML(elem))
|
| IqType::Set(IqPayloadType::XML(elem))
|
||||||
| IqType::Result(Some(IqPayloadType::XML(elem))) => elem,
|
| IqType::Result(Some(IqPayloadType::XML(elem))) => elem,
|
||||||
IqType::Error(error) => stanza_error::serialise(&error),
|
IqType::Error(error) => (&error).into(),
|
||||||
IqType::Get(IqPayloadType::Parsed(payload))
|
IqType::Get(IqPayloadType::Parsed(payload))
|
||||||
| IqType::Set(IqPayloadType::Parsed(payload))
|
| IqType::Set(IqPayloadType::Parsed(payload))
|
||||||
| IqType::Result(Some(IqPayloadType::Parsed(payload))) => serialise_payload(&payload),
|
| IqType::Result(Some(IqPayloadType::Parsed(payload))) => serialise_payload(&payload),
|
||||||
|
|
@ -183,11 +183,9 @@ pub fn serialise(iq: &Iq) -> Element {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use iq;
|
use iq;
|
||||||
use stanza_error;
|
use stanza_error::{ErrorType, DefinedCondition};
|
||||||
use disco;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_require_type() {
|
fn test_require_type() {
|
||||||
|
|
@ -275,9 +273,9 @@ mod tests {
|
||||||
assert_eq!(iq.id, None);
|
assert_eq!(iq.id, None);
|
||||||
match iq.payload {
|
match iq.payload {
|
||||||
iq::IqType::Error(error) => {
|
iq::IqType::Error(error) => {
|
||||||
assert_eq!(error.type_, stanza_error::ErrorType::Cancel);
|
assert_eq!(error.type_, ErrorType::Cancel);
|
||||||
assert_eq!(error.by, None);
|
assert_eq!(error.by, None);
|
||||||
assert_eq!(error.defined_condition, stanza_error::DefinedCondition::ServiceUnavailable);
|
assert_eq!(error.defined_condition, DefinedCondition::ServiceUnavailable);
|
||||||
assert_eq!(error.texts.len(), 0);
|
assert_eq!(error.texts.len(), 0);
|
||||||
assert_eq!(error.other, None);
|
assert_eq!(error.other, None);
|
||||||
},
|
},
|
||||||
|
|
@ -314,7 +312,7 @@ mod tests {
|
||||||
let elem: Element = "<iq xmlns='jabber:client' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
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::parse_iq(&elem).unwrap();
|
||||||
assert!(match iq.payload {
|
assert!(match iq.payload {
|
||||||
iq::IqType::Get(iq::IqPayloadType::Parsed(iq::IqPayload::Disco(disco::Disco { .. }))) => true,
|
IqType::Get(IqPayloadType::Parsed(IqPayload::Disco(Disco { .. }))) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use error::Error;
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use body;
|
use body;
|
||||||
use stanza_error;
|
use stanza_error::StanzaError;
|
||||||
use chatstates::ChatState;
|
use chatstates::ChatState;
|
||||||
use receipts::Receipt;
|
use receipts::Receipt;
|
||||||
use delay::Delay;
|
use delay::Delay;
|
||||||
|
|
@ -28,7 +28,7 @@ use eme::ExplicitMessageEncryption;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum MessagePayload {
|
pub enum MessagePayload {
|
||||||
Body(body::Body),
|
Body(body::Body),
|
||||||
StanzaError(stanza_error::StanzaError),
|
StanzaError(StanzaError),
|
||||||
ChatState(ChatState),
|
ChatState(ChatState),
|
||||||
Receipt(Receipt),
|
Receipt(Receipt),
|
||||||
Delay(Delay),
|
Delay(Delay),
|
||||||
|
|
@ -113,7 +113,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
||||||
for elem in root.children() {
|
for elem in root.children() {
|
||||||
let payload = if let Ok(body) = body::parse_body(elem) {
|
let payload = if let Ok(body) = body::parse_body(elem) {
|
||||||
Some(MessagePayload::Body(body))
|
Some(MessagePayload::Body(body))
|
||||||
} else if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
|
} else if let Ok(stanza_error) = StanzaError::try_from(elem) {
|
||||||
Some(MessagePayload::StanzaError(stanza_error))
|
Some(MessagePayload::StanzaError(stanza_error))
|
||||||
} else if let Ok(chatstate) = ChatState::try_from(elem) {
|
} else if let Ok(chatstate) = ChatState::try_from(elem) {
|
||||||
Some(MessagePayload::ChatState(chatstate))
|
Some(MessagePayload::ChatState(chatstate))
|
||||||
|
|
@ -147,7 +147,7 @@ pub fn parse_message(root: &Element) -> Result<Message, Error> {
|
||||||
pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
||||||
match *payload {
|
match *payload {
|
||||||
MessagePayload::Body(ref body) => body::serialise(body),
|
MessagePayload::Body(ref body) => body::serialise(body),
|
||||||
MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
MessagePayload::StanzaError(ref stanza_error) => stanza_error.into(),
|
||||||
MessagePayload::Attention(ref attention) => attention.into(),
|
MessagePayload::Attention(ref attention) => attention.into(),
|
||||||
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
||||||
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use error::Error;
|
||||||
|
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
use stanza_error;
|
use stanza_error::StanzaError;
|
||||||
use delay::Delay;
|
use delay::Delay;
|
||||||
use ecaps2::ECaps2;
|
use ecaps2::ECaps2;
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ pub enum PresencePayload {
|
||||||
Show(Show),
|
Show(Show),
|
||||||
Status(Status),
|
Status(Status),
|
||||||
Priority(Priority),
|
Priority(Priority),
|
||||||
StanzaError(stanza_error::StanzaError),
|
StanzaError(StanzaError),
|
||||||
Delay(Delay),
|
Delay(Delay),
|
||||||
ECaps2(ECaps2),
|
ECaps2(ECaps2),
|
||||||
}
|
}
|
||||||
|
|
@ -179,7 +179,7 @@ pub fn parse_presence(root: &Element) -> Result<Presence, Error> {
|
||||||
}
|
}
|
||||||
priority = Some(Priority::from_str(elem.text().as_ref())?);
|
priority = Some(Priority::from_str(elem.text().as_ref())?);
|
||||||
} else {
|
} else {
|
||||||
let payload = if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
|
let payload = if let Ok(stanza_error) = StanzaError::try_from(elem) {
|
||||||
Some(PresencePayload::StanzaError(stanza_error))
|
Some(PresencePayload::StanzaError(stanza_error))
|
||||||
} else if let Ok(delay) = Delay::try_from(elem) {
|
} else if let Ok(delay) = Delay::try_from(elem) {
|
||||||
Some(PresencePayload::Delay(delay))
|
Some(PresencePayload::Delay(delay))
|
||||||
|
|
@ -226,7 +226,7 @@ pub fn serialise_payload(payload: &PresencePayload) -> Element {
|
||||||
.append(format!("{}", priority))
|
.append(format!("{}", priority))
|
||||||
.build()
|
.build()
|
||||||
},
|
},
|
||||||
PresencePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
PresencePayload::StanzaError(ref stanza_error) => stanza_error.into(),
|
||||||
PresencePayload::Delay(ref delay) => delay.into(),
|
PresencePayload::Delay(ref delay) => delay.into(),
|
||||||
PresencePayload::ECaps2(ref ecaps2) => ecaps2.into(),
|
PresencePayload::ECaps2(ref ecaps2) => ecaps2.into(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
|
@ -149,104 +150,108 @@ pub struct StanzaError {
|
||||||
pub other: Option<Element>,
|
pub other: Option<Element>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_stanza_error(root: &Element) -> Result<StanzaError, Error> {
|
impl<'a> TryFrom<&'a Element> for StanzaError {
|
||||||
if !root.is("error", ns::JABBER_CLIENT) {
|
type Error = Error;
|
||||||
return Err(Error::ParseError("This is not an error element."));
|
|
||||||
}
|
|
||||||
|
|
||||||
let type_ = root.attr("type")
|
fn try_from(elem: &'a Element) -> Result<StanzaError, Error> {
|
||||||
.ok_or(Error::ParseError("Error must have a 'type' attribute."))?
|
if !elem.is("error", ns::JABBER_CLIENT) {
|
||||||
.parse()?;
|
return Err(Error::ParseError("This is not an error element."));
|
||||||
let by = root.attr("by")
|
|
||||||
.and_then(|by| by.parse().ok());
|
|
||||||
let mut defined_condition = None;
|
|
||||||
let mut texts = BTreeMap::new();
|
|
||||||
let mut other = None;
|
|
||||||
|
|
||||||
for child in root.children() {
|
|
||||||
if child.is("text", ns::XMPP_STANZAS) {
|
|
||||||
for _ in child.children() {
|
|
||||||
return Err(Error::ParseError("Unknown element in error text."));
|
|
||||||
}
|
|
||||||
let lang = child.attr("xml:lang").unwrap_or("").to_owned();
|
|
||||||
if let Some(_) = texts.insert(lang, child.text()) {
|
|
||||||
return Err(Error::ParseError("Text element present twice for the same xml:lang."));
|
|
||||||
}
|
|
||||||
} else if child.ns() == Some(ns::XMPP_STANZAS) {
|
|
||||||
if defined_condition.is_some() {
|
|
||||||
return Err(Error::ParseError("Error must not have more than one defined-condition."));
|
|
||||||
}
|
|
||||||
for _ in child.children() {
|
|
||||||
return Err(Error::ParseError("Unknown element in defined-condition."));
|
|
||||||
}
|
|
||||||
let condition = DefinedCondition::from_str(child.name())?;
|
|
||||||
defined_condition = Some(condition);
|
|
||||||
} else {
|
|
||||||
if other.is_some() {
|
|
||||||
return Err(Error::ParseError("Error must not have more than one other element."));
|
|
||||||
}
|
|
||||||
other = Some(child.clone());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if defined_condition.is_none() {
|
let type_ = elem.attr("type")
|
||||||
return Err(Error::ParseError("Error must have a defined-condition."));
|
.ok_or(Error::ParseError("Error must have a 'type' attribute."))?
|
||||||
}
|
.parse()?;
|
||||||
let defined_condition = defined_condition.unwrap();
|
let by = elem.attr("by")
|
||||||
|
.and_then(|by| by.parse().ok());
|
||||||
|
let mut defined_condition = None;
|
||||||
|
let mut texts = BTreeMap::new();
|
||||||
|
let mut other = None;
|
||||||
|
|
||||||
Ok(StanzaError {
|
for child in elem.children() {
|
||||||
type_: type_,
|
if child.is("text", ns::XMPP_STANZAS) {
|
||||||
by: by,
|
for _ in child.children() {
|
||||||
defined_condition: defined_condition,
|
return Err(Error::ParseError("Unknown element in error text."));
|
||||||
texts: texts,
|
}
|
||||||
other: other,
|
let lang = child.attr("xml:lang").unwrap_or("").to_owned();
|
||||||
})
|
if let Some(_) = texts.insert(lang, child.text()) {
|
||||||
|
return Err(Error::ParseError("Text element present twice for the same xml:lang."));
|
||||||
|
}
|
||||||
|
} else if child.ns() == Some(ns::XMPP_STANZAS) {
|
||||||
|
if defined_condition.is_some() {
|
||||||
|
return Err(Error::ParseError("Error must not have more than one defined-condition."));
|
||||||
|
}
|
||||||
|
for _ in child.children() {
|
||||||
|
return Err(Error::ParseError("Unknown element in defined-condition."));
|
||||||
|
}
|
||||||
|
let condition = DefinedCondition::from_str(child.name())?;
|
||||||
|
defined_condition = Some(condition);
|
||||||
|
} else {
|
||||||
|
if other.is_some() {
|
||||||
|
return Err(Error::ParseError("Error must not have more than one other element."));
|
||||||
|
}
|
||||||
|
other = Some(child.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if defined_condition.is_none() {
|
||||||
|
return Err(Error::ParseError("Error must have a defined-condition."));
|
||||||
|
}
|
||||||
|
let defined_condition = defined_condition.unwrap();
|
||||||
|
|
||||||
|
Ok(StanzaError {
|
||||||
|
type_: type_,
|
||||||
|
by: by,
|
||||||
|
defined_condition: defined_condition,
|
||||||
|
texts: texts,
|
||||||
|
other: other,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialise(error: &StanzaError) -> Element {
|
impl<'a> Into<Element> for &'a StanzaError {
|
||||||
let mut root = Element::builder("error")
|
fn into(self) -> Element {
|
||||||
.ns(ns::JABBER_CLIENT)
|
let mut root = Element::builder("error")
|
||||||
.attr("type", String::from(error.type_.clone()))
|
.ns(ns::JABBER_CLIENT)
|
||||||
.attr("by", match error.by {
|
.attr("type", String::from(self.type_.clone()))
|
||||||
Some(ref by) => Some(String::from(by.clone())),
|
.attr("by", match self.by {
|
||||||
None => None,
|
Some(ref by) => Some(String::from(by.clone())),
|
||||||
})
|
None => None,
|
||||||
.append(Element::builder(error.defined_condition.clone())
|
})
|
||||||
.ns(ns::XMPP_STANZAS)
|
.append(Element::builder(self.defined_condition.clone())
|
||||||
.build())
|
.ns(ns::XMPP_STANZAS)
|
||||||
.build();
|
.build())
|
||||||
for (lang, text) in error.texts.clone() {
|
.build();
|
||||||
let elem = Element::builder("text")
|
for (lang, text) in self.texts.clone() {
|
||||||
.ns(ns::XMPP_STANZAS)
|
let elem = Element::builder("text")
|
||||||
.attr("xml:lang", lang)
|
.ns(ns::XMPP_STANZAS)
|
||||||
.append(text)
|
.attr("xml:lang", lang)
|
||||||
.build();
|
.append(text)
|
||||||
root.append_child(elem);
|
.build();
|
||||||
|
root.append_child(elem);
|
||||||
|
}
|
||||||
|
if let Some(ref other) = self.other {
|
||||||
|
root.append_child(other.clone());
|
||||||
|
}
|
||||||
|
root
|
||||||
}
|
}
|
||||||
if let Some(ref other) = error.other {
|
|
||||||
root.append_child(other.clone());
|
|
||||||
}
|
|
||||||
root
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use minidom::Element;
|
use super::*;
|
||||||
use error::Error;
|
|
||||||
use stanza_error;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
let elem: Element = "<error xmlns='jabber:client' type='cancel'><undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>".parse().unwrap();
|
let elem: Element = "<error xmlns='jabber:client' type='cancel'><undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>".parse().unwrap();
|
||||||
let error = stanza_error::parse_stanza_error(&elem).unwrap();
|
let error = StanzaError::try_from(&elem).unwrap();
|
||||||
assert_eq!(error.type_, stanza_error::ErrorType::Cancel);
|
assert_eq!(error.type_, ErrorType::Cancel);
|
||||||
assert_eq!(error.defined_condition, stanza_error::DefinedCondition::UndefinedCondition);
|
assert_eq!(error.defined_condition, DefinedCondition::UndefinedCondition);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_type() {
|
fn test_invalid_type() {
|
||||||
let elem: Element = "<error xmlns='jabber:client'/>".parse().unwrap();
|
let elem: Element = "<error xmlns='jabber:client'/>".parse().unwrap();
|
||||||
let error = stanza_error::parse_stanza_error(&elem).unwrap_err();
|
let error = StanzaError::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
@ -254,7 +259,7 @@ mod tests {
|
||||||
assert_eq!(message, "Error must have a 'type' attribute.");
|
assert_eq!(message, "Error must have a 'type' attribute.");
|
||||||
|
|
||||||
let elem: Element = "<error xmlns='jabber:client' type='coucou'/>".parse().unwrap();
|
let elem: Element = "<error xmlns='jabber:client' type='coucou'/>".parse().unwrap();
|
||||||
let error = stanza_error::parse_stanza_error(&elem).unwrap_err();
|
let error = StanzaError::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
@ -265,7 +270,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_condition() {
|
fn test_invalid_condition() {
|
||||||
let elem: Element = "<error xmlns='jabber:client' type='cancel'/>".parse().unwrap();
|
let elem: Element = "<error xmlns='jabber:client' type='cancel'/>".parse().unwrap();
|
||||||
let error = stanza_error::parse_stanza_error(&elem).unwrap_err();
|
let error = StanzaError::try_from(&elem).unwrap_err();
|
||||||
let message = match error {
|
let message = match error {
|
||||||
Error::ParseError(string) => string,
|
Error::ParseError(string) => string,
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue