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

@ -21,9 +21,9 @@ impl PresencePayload for Idle {}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::error::Error;
use crate::Element;
use std::str::FromStr;
use xso::error::{Error, FromElementError};
#[test]
fn test_size() {
@ -45,7 +45,7 @@ mod tests {
.unwrap();
let error = Idle::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 idle element.");
@ -56,7 +56,7 @@ mod tests {
let elem: Element = "<idle xmlns='urn:xmpp:idle:1'/>".parse().unwrap();
let error = Idle::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 'since' missing.");
@ -70,8 +70,12 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
_ => panic!(),
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
other => panic!("unexpected result: {:?}", other),
};
assert_eq!(message.to_string(), "input is out of range");
@ -81,7 +85,11 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
_ => panic!(),
};
assert_eq!(message.to_string(), "input is out of range");
@ -92,7 +100,11 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
_ => panic!(),
};
assert_eq!(message.to_string(), "input contains invalid characters");
@ -103,7 +115,11 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
_ => panic!(),
};
assert_eq!(message.to_string(), "input contains invalid characters");
@ -114,7 +130,11 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
_ => panic!(),
};
assert_eq!(message.to_string(), "input contains invalid characters");
@ -125,7 +145,11 @@ mod tests {
.unwrap();
let error = Idle::try_from(elem).unwrap_err();
let message = match error {
Error::ChronoParseError(string) => string,
FromElementError::Invalid(Error::TextParseError(string))
if string.is::<chrono::ParseError>() =>
{
string
}
_ => panic!(),
};
assert_eq!(message.to_string(), "premature end of input");