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

@ -6,8 +6,8 @@
use crate::ns;
use crate::pubsub::PubSubPayload;
use crate::util::error::Error;
use crate::Element;
use xso::error::{Error, FromElementError};
generate_elem_id!(
/// The artist or performer of the song or piece.
@ -106,9 +106,9 @@ impl Tune {
}
impl TryFrom<Element> for Tune {
type Error = Error;
type Error = FromElementError;
fn try_from(elem: Element) -> Result<Tune, Error> {
fn try_from(elem: Element) -> Result<Tune, FromElementError> {
check_self!(elem, "tune", TUNE);
check_no_attributes!(elem, "tune");
@ -116,41 +116,41 @@ impl TryFrom<Element> for Tune {
for child in elem.children() {
if child.is("artist", ns::TUNE) {
if tune.artist.is_some() {
return Err(Error::ParseError("Tune cant have more than one artist."));
return Err(Error::Other("Tune cant have more than one artist.").into());
}
tune.artist = Some(Artist::try_from(child.clone())?);
} else if child.is("length", ns::TUNE) {
if tune.length.is_some() {
return Err(Error::ParseError("Tune cant have more than one length."));
return Err(Error::Other("Tune cant have more than one length.").into());
}
tune.length = Some(Length::try_from(child.clone())?);
} else if child.is("rating", ns::TUNE) {
if tune.rating.is_some() {
return Err(Error::ParseError("Tune cant have more than one rating."));
return Err(Error::Other("Tune cant have more than one rating.").into());
}
tune.rating = Some(Rating::try_from(child.clone())?);
} else if child.is("source", ns::TUNE) {
if tune.source.is_some() {
return Err(Error::ParseError("Tune cant have more than one source."));
return Err(Error::Other("Tune cant have more than one source.").into());
}
tune.source = Some(Source::try_from(child.clone())?);
} else if child.is("title", ns::TUNE) {
if tune.title.is_some() {
return Err(Error::ParseError("Tune cant have more than one title."));
return Err(Error::Other("Tune cant have more than one title.").into());
}
tune.title = Some(Title::try_from(child.clone())?);
} else if child.is("track", ns::TUNE) {
if tune.track.is_some() {
return Err(Error::ParseError("Tune cant have more than one track."));
return Err(Error::Other("Tune cant have more than one track.").into());
}
tune.track = Some(Track::try_from(child.clone())?);
} else if child.is("uri", ns::TUNE) {
if tune.uri.is_some() {
return Err(Error::ParseError("Tune cant have more than one uri."));
return Err(Error::Other("Tune cant have more than one uri.").into());
}
tune.uri = Some(Uri::try_from(child.clone())?);
} else {
return Err(Error::ParseError("Unknown element in User Tune."));
return Err(Error::Other("Unknown element in User Tune.").into());
}
}