parsers: use Error type from xso
This is a large change and as such, it needs good motivation. Let me remind you of the ultimate goal: we want a derive macro which allows us to FromXml/IntoXml, and that derive macro should be usable from `xmpp_parsers` and other crates. For that, any code generated by the derive macro mustn't depend on any code in the `xmpp_parsers` crate, because you cannot name the crate you are in portably (`xmpp_parsers::..` wouldn't resolve within `xmpp_parsers`, and `crate::..` would point at other crates if the macro was used in other crates). We also want to interoperate with code already implementing `TryFrom<Element>` and `Into<Element>` on structs. This ultimately requires that we have an error type which is shared by the two implementations and that error type must be declared in the `xso` crate to be usable by the macros. Thus, we port the error type over to use the type declared in `xso`. This changes the structure of the error type greatly; I do not think that `xso` should have to know about all the different types we are parsing there and they don't deserve special treatment. Wrapping them in a `Box<dyn ..>` seems more appropriate.
This commit is contained in:
parent
b3e6e089da
commit
6ef8dbefa3
62 changed files with 916 additions and 892 deletions
|
|
@ -7,12 +7,12 @@
|
|||
use crate::message::MessagePayload;
|
||||
use crate::ns;
|
||||
use crate::presence::PresencePayload;
|
||||
use crate::util::error::Error;
|
||||
use crate::Element;
|
||||
use jid::Jid;
|
||||
use minidom::Node;
|
||||
use std::collections::BTreeMap;
|
||||
use std::convert::TryFrom;
|
||||
use xso::error::{Error, FromElementError};
|
||||
|
||||
generate_attribute!(
|
||||
/// The type of the error.
|
||||
|
|
@ -249,9 +249,9 @@ impl StanzaError {
|
|||
}
|
||||
|
||||
impl TryFrom<Element> for StanzaError {
|
||||
type Error = Error;
|
||||
type Error = FromElementError;
|
||||
|
||||
fn try_from(elem: Element) -> Result<StanzaError, Error> {
|
||||
fn try_from(elem: Element) -> Result<StanzaError, FromElementError> {
|
||||
check_self!(elem, "error", DEFAULT_NS);
|
||||
// The code attribute has been deprecated in [XEP-0086](https://xmpp.org/extensions/xep-0086.html)
|
||||
// which was deprecated in 2007. We don't error when it's here, but don't include it in the final struct.
|
||||
|
|
@ -273,15 +273,16 @@ impl TryFrom<Element> for StanzaError {
|
|||
check_no_unknown_attributes!(child, "text", ["xml:lang"]);
|
||||
let lang = get_attr!(child, "xml:lang", Default);
|
||||
if stanza_error.texts.insert(lang, child.text()).is_some() {
|
||||
return Err(Error::ParseError(
|
||||
"Text element present twice for the same xml:lang.",
|
||||
));
|
||||
return Err(
|
||||
Error::Other("Text element present twice for the same xml:lang.").into(),
|
||||
);
|
||||
}
|
||||
} else if child.has_ns(ns::XMPP_STANZAS) {
|
||||
if defined_condition.is_some() {
|
||||
return Err(Error::ParseError(
|
||||
return Err(Error::Other(
|
||||
"Error must not have more than one defined-condition.",
|
||||
));
|
||||
)
|
||||
.into());
|
||||
}
|
||||
check_no_children!(child, "defined-condition");
|
||||
check_no_attributes!(child, "defined-condition");
|
||||
|
|
@ -297,15 +298,15 @@ impl TryFrom<Element> for StanzaError {
|
|||
defined_condition = Some(condition);
|
||||
} else {
|
||||
if stanza_error.other.is_some() {
|
||||
return Err(Error::ParseError(
|
||||
"Error must not have more than one other element.",
|
||||
));
|
||||
return Err(
|
||||
Error::Other("Error must not have more than one other element.").into(),
|
||||
);
|
||||
}
|
||||
stanza_error.other = Some(child.clone());
|
||||
}
|
||||
}
|
||||
stanza_error.defined_condition =
|
||||
defined_condition.ok_or(Error::ParseError("Error must have a defined-condition."))?;
|
||||
defined_condition.ok_or(Error::Other("Error must have a defined-condition."))?;
|
||||
|
||||
Ok(stanza_error)
|
||||
}
|
||||
|
|
@ -369,7 +370,7 @@ mod tests {
|
|||
let elem: Element = "<error xmlns='jabber:component:accept'/>".parse().unwrap();
|
||||
let error = StanzaError::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
FromElementError::Invalid(Error::Other(string)) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Required attribute 'type' missing.");
|
||||
|
|
@ -384,10 +385,10 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = StanzaError::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
FromElementError::Invalid(Error::TextParseError(string)) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Unknown value for 'type' attribute.");
|
||||
assert_eq!(message.to_string(), "Unknown value for 'type' attribute.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -402,7 +403,7 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = StanzaError::try_from(elem).unwrap_err();
|
||||
let message = match error {
|
||||
Error::ParseError(string) => string,
|
||||
FromElementError::Invalid(Error::Other(string)) => string,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(message, "Error must have a defined-condition.");
|
||||
|
|
|
|||
Loading…
Reference in a new issue