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
|
|
@ -5,8 +5,8 @@
|
|||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
use crate::ns;
|
||||
use crate::util::error::Error;
|
||||
use crate::Element;
|
||||
use xso::error::{Error, FromElementError};
|
||||
|
||||
/// Requests paging through a potentially big set of items (represented by an
|
||||
/// UID).
|
||||
|
|
@ -28,9 +28,9 @@ pub struct SetQuery {
|
|||
}
|
||||
|
||||
impl TryFrom<Element> for SetQuery {
|
||||
type Error = Error;
|
||||
type Error = FromElementError;
|
||||
|
||||
fn try_from(elem: Element) -> Result<SetQuery, Error> {
|
||||
fn try_from(elem: Element) -> Result<SetQuery, FromElementError> {
|
||||
check_self!(elem, "set", RSM, "RSM set");
|
||||
let mut set = SetQuery {
|
||||
max: None,
|
||||
|
|
@ -41,26 +41,26 @@ impl TryFrom<Element> for SetQuery {
|
|||
for child in elem.children() {
|
||||
if child.is("max", ns::RSM) {
|
||||
if set.max.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one max."));
|
||||
return Err(Error::Other("Set can’t have more than one max.").into());
|
||||
}
|
||||
set.max = Some(child.text().parse()?);
|
||||
set.max = Some(child.text().parse().map_err(Error::text_parse_error)?);
|
||||
} else if child.is("after", ns::RSM) {
|
||||
if set.after.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one after."));
|
||||
return Err(Error::Other("Set can’t have more than one after.").into());
|
||||
}
|
||||
set.after = Some(child.text());
|
||||
} else if child.is("before", ns::RSM) {
|
||||
if set.before.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one before."));
|
||||
return Err(Error::Other("Set can’t have more than one before.").into());
|
||||
}
|
||||
set.before = Some(child.text());
|
||||
} else if child.is("index", ns::RSM) {
|
||||
if set.index.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one index."));
|
||||
return Err(Error::Other("Set can’t have more than one index.").into());
|
||||
}
|
||||
set.index = Some(child.text().parse()?);
|
||||
set.index = Some(child.text().parse().map_err(Error::text_parse_error)?);
|
||||
} else {
|
||||
return Err(Error::ParseError("Unknown child in set element."));
|
||||
return Err(Error::Other("Unknown child in set element.").into());
|
||||
}
|
||||
}
|
||||
Ok(set)
|
||||
|
|
@ -111,9 +111,9 @@ pub struct SetResult {
|
|||
}
|
||||
|
||||
impl TryFrom<Element> for SetResult {
|
||||
type Error = Error;
|
||||
type Error = FromElementError;
|
||||
|
||||
fn try_from(elem: Element) -> Result<SetResult, Error> {
|
||||
fn try_from(elem: Element) -> Result<SetResult, FromElementError> {
|
||||
check_self!(elem, "set", RSM, "RSM set");
|
||||
let mut set = SetResult {
|
||||
first: None,
|
||||
|
|
@ -124,22 +124,22 @@ impl TryFrom<Element> for SetResult {
|
|||
for child in elem.children() {
|
||||
if child.is("first", ns::RSM) {
|
||||
if set.first.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one first."));
|
||||
return Err(Error::Other("Set can’t have more than one first.").into());
|
||||
}
|
||||
set.first_index = get_attr!(child, "index", Option);
|
||||
set.first = Some(child.text());
|
||||
} else if child.is("last", ns::RSM) {
|
||||
if set.last.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one last."));
|
||||
return Err(Error::Other("Set can’t have more than one last.").into());
|
||||
}
|
||||
set.last = Some(child.text());
|
||||
} else if child.is("count", ns::RSM) {
|
||||
if set.count.is_some() {
|
||||
return Err(Error::ParseError("Set can’t have more than one count."));
|
||||
return Err(Error::Other("Set can’t have more than one count.").into());
|
||||
}
|
||||
set.count = Some(child.text().parse()?);
|
||||
set.count = Some(child.text().parse().map_err(Error::text_parse_error)?);
|
||||
} else {
|
||||
return Err(Error::ParseError("Unknown child in set element."));
|
||||
return Err(Error::Other("Unknown child in set element.").into());
|
||||
}
|
||||
}
|
||||
Ok(set)
|
||||
|
|
@ -215,7 +215,7 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = SetQuery::try_from(elem.clone()).unwrap_err();
|
||||
let returned_elem = match error {
|
||||
Error::TypeMismatch(_, _, elem) => elem,
|
||||
FromElementError::Mismatch(elem) => elem,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(elem, returned_elem);
|
||||
|
|
@ -225,7 +225,7 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = SetResult::try_from(elem.clone()).unwrap_err();
|
||||
let returned_elem = match error {
|
||||
Error::TypeMismatch(_, _, elem) => elem,
|
||||
FromElementError::Mismatch(elem) => elem,
|
||||
_ => panic!(),
|
||||
};
|
||||
assert_eq!(elem, returned_elem);
|
||||
|
|
@ -238,7 +238,7 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = SetQuery::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 set element.");
|
||||
|
|
@ -248,7 +248,7 @@ mod tests {
|
|||
.unwrap();
|
||||
let error = SetResult::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 set element.");
|
||||
|
|
|
|||
Loading…
Reference in a new issue