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:
Jonas Schäfer 2024-06-21 16:27:43 +02:00
commit 6ef8dbefa3
62 changed files with 916 additions and 892 deletions

View file

@ -9,9 +9,9 @@ use crate::date::DateTime;
use crate::message::MessagePayload;
use crate::ns;
use crate::pubsub::{Item as PubSubItem, ItemId, NodeName, Subscription, SubscriptionId};
use crate::util::error::Error;
use crate::Element;
use jid::Jid;
use xso::error::{Error, FromElementError};
/// Event wrapper for a PubSub `<item/>`.
#[derive(Debug, Clone, PartialEq)]
@ -97,9 +97,7 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
None => is_retract = Some(false),
Some(false) => (),
Some(true) => {
return Err(Error::ParseError(
"Mix of item and retract in items element.",
));
return Err(Error::Other("Mix of item and retract in items element.").into());
}
}
items.push(Item::try_from(child.clone())?);
@ -108,9 +106,7 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
None => is_retract = Some(true),
Some(true) => (),
Some(false) => {
return Err(Error::ParseError(
"Mix of item and retract in items element.",
));
return Err(Error::Other("Mix of item and retract in items element.").into());
}
}
check_no_children!(child, "retract");
@ -118,7 +114,7 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
let id = get_attr!(child, "id", Required);
retracts.push(id);
} else {
return Err(Error::ParseError("Invalid child in items element."));
return Err(Error::Other("Invalid child in items element.").into());
}
}
Ok(match is_retract {
@ -127,14 +123,14 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
node,
items: retracts,
},
None => return Err(Error::ParseError("Missing children in items element.")),
None => return Err(Error::Other("Missing children in items element.").into()),
})
}
impl TryFrom<Element> for PubSubEvent {
type Error = Error;
type Error = FromElementError;
fn try_from(elem: Element) -> Result<PubSubEvent, Error> {
fn try_from(elem: Element) -> Result<PubSubEvent, FromElementError> {
check_self!(elem, "event", PUBSUB_EVENT);
check_no_attributes!(elem, "event");
@ -145,9 +141,10 @@ impl TryFrom<Element> for PubSubEvent {
let mut payloads = child.children().cloned().collect::<Vec<_>>();
let item = payloads.pop();
if !payloads.is_empty() {
return Err(Error::ParseError(
return Err(Error::Other(
"More than a single payload in configuration element.",
));
)
.into());
}
let form = match item {
None => None,
@ -159,14 +156,14 @@ impl TryFrom<Element> for PubSubEvent {
for item in child.children() {
if item.is("redirect", ns::PUBSUB_EVENT) {
if redirect.is_some() {
return Err(Error::ParseError(
"More than one redirect in delete element.",
));
return Err(
Error::Other("More than one redirect in delete element.").into()
);
}
let uri = get_attr!(item, "uri", Required);
redirect = Some(uri);
} else {
return Err(Error::ParseError("Unknown child in delete element."));
return Err(Error::Other("Unknown child in delete element.").into());
}
}
payload = Some(PubSubEvent::Delete { node, redirect });
@ -185,10 +182,10 @@ impl TryFrom<Element> for PubSubEvent {
subscription: get_attr!(child, "subscription", Option),
});
} else {
return Err(Error::ParseError("Unknown child in event element."));
return Err(Error::Other("Unknown child in event element.").into());
}
}
payload.ok_or(Error::ParseError("No payload in event element."))
payload.ok_or(Error::Other("No payload in event element.").into())
}
}
@ -270,7 +267,7 @@ mod tests {
.unwrap();
let error = PubSubEvent::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "Missing children in items element.");
@ -375,7 +372,7 @@ mod tests {
.unwrap();
let error = PubSubEvent::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in event element.");
@ -389,7 +386,7 @@ mod tests {
.unwrap();
let error = PubSubEvent::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in event element.");

View file

@ -9,9 +9,9 @@ use crate::data_forms::DataForm;
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
use crate::pubsub::{AffiliationAttribute, NodeName, Subscription};
use crate::util::error::Error;
use crate::Element;
use jid::Jid;
use xso::error::{Error, FromElementError};
generate_element!(
/// A list of affiliations you have on a service, or on a node.
@ -143,9 +143,9 @@ impl IqSetPayload for PubSubOwner {}
impl IqResultPayload for PubSubOwner {}
impl TryFrom<Element> for PubSubOwner {
type Error = Error;
type Error = FromElementError;
fn try_from(elem: Element) -> Result<PubSubOwner, Error> {
fn try_from(elem: Element) -> Result<PubSubOwner, FromElementError> {
check_self!(elem, "pubsub", PUBSUB_OWNER);
check_no_attributes!(elem, "pubsub");
@ -153,17 +153,18 @@ impl TryFrom<Element> for PubSubOwner {
for child in elem.children() {
if child.is("configure", ns::PUBSUB_OWNER) {
if payload.is_some() {
return Err(Error::ParseError(
return Err(Error::Other(
"Payload is already defined in pubsub owner element.",
));
)
.into());
}
let configure = Configure::try_from(child.clone())?;
payload = Some(PubSubOwner::Configure(configure));
} else {
return Err(Error::ParseError("Unknown child in pubsub element."));
return Err(Error::Other("Unknown child in pubsub element.").into());
}
}
payload.ok_or(Error::ParseError("No payload in pubsub element."))
payload.ok_or(Error::Other("No payload in pubsub element.").into())
}
}

View file

@ -10,9 +10,9 @@ use crate::ns;
use crate::pubsub::{
AffiliationAttribute, Item as PubSubItem, NodeName, Subscription, SubscriptionId,
};
use crate::util::error::Error;
use crate::Element;
use jid::Jid;
use xso::error::{Error, FromElementError};
// TODO: a better solution would be to split this into a query and a result elements, like for
// XEP-0030.
@ -181,24 +181,23 @@ pub struct SubscribeOptions {
}
impl TryFrom<Element> for SubscribeOptions {
type Error = Error;
type Error = FromElementError;
fn try_from(elem: Element) -> Result<Self, Error> {
fn try_from(elem: Element) -> Result<Self, FromElementError> {
check_self!(elem, "subscribe-options", PUBSUB);
check_no_attributes!(elem, "subscribe-options");
let mut required = false;
for child in elem.children() {
if child.is("required", ns::PUBSUB) {
if required {
return Err(Error::ParseError(
return Err(Error::Other(
"More than one required element in subscribe-options.",
));
)
.into());
}
required = true;
} else {
return Err(Error::ParseError(
"Unknown child in subscribe-options element.",
));
return Err(Error::Other("Unknown child in subscribe-options element.").into());
}
}
Ok(SubscribeOptions { required })
@ -338,9 +337,9 @@ impl IqSetPayload for PubSub {}
impl IqResultPayload for PubSub {}
impl TryFrom<Element> for PubSub {
type Error = Error;
type Error = FromElementError;
fn try_from(elem: Element) -> Result<PubSub, Error> {
fn try_from(elem: Element) -> Result<PubSub, FromElementError> {
check_self!(elem, "pubsub", PUBSUB);
check_no_attributes!(elem, "pubsub");
@ -348,9 +347,9 @@ impl TryFrom<Element> for PubSub {
for child in elem.children() {
if child.is("create", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let create = Create::try_from(child.clone())?;
payload = Some(PubSub::Create {
@ -359,9 +358,9 @@ impl TryFrom<Element> for PubSub {
});
} else if child.is("subscribe", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let subscribe = Subscribe::try_from(child.clone())?;
payload = Some(PubSub::Subscribe {
@ -371,9 +370,9 @@ impl TryFrom<Element> for PubSub {
} else if child.is("options", ns::PUBSUB) {
if let Some(PubSub::Subscribe { subscribe, options }) = payload {
if options.is_some() {
return Err(Error::ParseError(
"Options is already defined in pubsub element.",
));
return Err(
Error::Other("Options is already defined in pubsub element.").into(),
);
}
let options = Some(Options::try_from(child.clone())?);
payload = Some(PubSub::Subscribe { subscribe, options });
@ -384,29 +383,30 @@ impl TryFrom<Element> for PubSub {
options: Some(options),
});
} else {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
} else if child.is("configure", ns::PUBSUB) {
if let Some(PubSub::Create { create, configure }) = payload {
if configure.is_some() {
return Err(Error::ParseError(
return Err(Error::Other(
"Configure is already defined in pubsub element.",
));
)
.into());
}
let configure = Some(Configure::try_from(child.clone())?);
payload = Some(PubSub::Create { create, configure });
} else {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
} else if child.is("publish", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let publish = Publish::try_from(child.clone())?;
payload = Some(PubSub::Publish {
@ -420,9 +420,10 @@ impl TryFrom<Element> for PubSub {
}) = payload
{
if publish_options.is_some() {
return Err(Error::ParseError(
return Err(Error::Other(
"Publish-options are already defined in pubsub element.",
));
)
.into());
}
let publish_options = Some(PublishOptions::try_from(child.clone())?);
payload = Some(PubSub::Publish {
@ -430,71 +431,71 @@ impl TryFrom<Element> for PubSub {
publish_options,
});
} else {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
} else if child.is("affiliations", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let affiliations = Affiliations::try_from(child.clone())?;
payload = Some(PubSub::Affiliations(affiliations));
} else if child.is("default", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let default = Default::try_from(child.clone())?;
payload = Some(PubSub::Default(default));
} else if child.is("items", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let items = Items::try_from(child.clone())?;
payload = Some(PubSub::Items(items));
} else if child.is("retract", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let retract = Retract::try_from(child.clone())?;
payload = Some(PubSub::Retract(retract));
} else if child.is("subscription", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let subscription = SubscriptionElem::try_from(child.clone())?;
payload = Some(PubSub::Subscription(subscription));
} else if child.is("subscriptions", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let subscriptions = Subscriptions::try_from(child.clone())?;
payload = Some(PubSub::Subscriptions(subscriptions));
} else if child.is("unsubscribe", ns::PUBSUB) {
if payload.is_some() {
return Err(Error::ParseError(
"Payload is already defined in pubsub element.",
));
return Err(
Error::Other("Payload is already defined in pubsub element.").into(),
);
}
let unsubscribe = Unsubscribe::try_from(child.clone())?;
payload = Some(PubSub::Unsubscribe(unsubscribe));
} else {
return Err(Error::ParseError("Unknown child in pubsub element."));
return Err(Error::Other("Unknown child in pubsub element.").into());
}
}
payload.ok_or(Error::ParseError("No payload in pubsub element."))
payload.ok_or(Error::Other("No payload in pubsub element.").into())
}
}
@ -678,7 +679,7 @@ mod tests {
.unwrap();
let error = PubSub::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "No payload in pubsub element.");