Report kind of missing value

This commit is contained in:
Dirkjan Ochtman 2022-11-23 19:03:22 -08:00
parent 2f587240cf
commit 32426d77ba
5 changed files with 16 additions and 14 deletions

View File

@ -127,7 +127,7 @@ fn deserialize_wrapped_enum(
let node = match deserializer.next() { let node = match deserializer.next() {
Some(result) => result?, Some(result) => result?,
None => return Err(Error::MissingValue), None => return Err(Error::MissingValue(&<Self as FromXml>::KIND)),
}; };
let data = match node { let data = match node {

View File

@ -26,7 +26,7 @@ impl<'xml> FromXml<'xml> for bool {
fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error> { fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error> {
FromXmlStr::<Self>::deserialize(deserializer)? FromXmlStr::<Self>::deserialize(deserializer)?
.0 .0
.ok_or(Error::MissingValue) .ok_or(Error::MissingValue(&Kind::Scalar))
} }
const KIND: Kind<'static> = Kind::Scalar; const KIND: Kind<'static> = Kind::Scalar;
@ -72,7 +72,7 @@ macro_rules! from_xml_for_number {
) -> Result<Self, Error> { ) -> Result<Self, Error> {
FromXmlStr::<Self>::deserialize(deserializer)? FromXmlStr::<Self>::deserialize(deserializer)?
.0 .0
.ok_or(Error::MissingValue) .ok_or(Error::MissingValue(&Kind::Scalar))
} }
const KIND: Kind<'static> = Kind::Scalar; const KIND: Kind<'static> = Kind::Scalar;
@ -97,7 +97,7 @@ impl<'xml> FromXml<'xml> for char {
fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error> { fn deserialize<'cx>(deserializer: &'cx mut Deserializer<'cx, 'xml>) -> Result<Self, Error> {
FromXmlStr::<Self>::deserialize(deserializer)? FromXmlStr::<Self>::deserialize(deserializer)?
.0 .0
.ok_or(Error::MissingValue) .ok_or(Error::MissingValue(&Kind::Scalar))
} }
const KIND: Kind<'static> = Kind::Scalar; const KIND: Kind<'static> = Kind::Scalar;

View File

@ -39,7 +39,7 @@ pub trait FromXml<'xml>: Sized {
// If the missing field is of type `Option<T>` then treat is as `None`, // If the missing field is of type `Option<T>` then treat is as `None`,
// otherwise it is an error. // otherwise it is an error.
fn missing_value() -> Result<Self, Error> { fn missing_value() -> Result<Self, Error> {
Err(Error::MissingValue) Err(Error::MissingValue(&Self::KIND))
} }
const KIND: Kind<'static>; const KIND: Kind<'static>;
@ -95,7 +95,7 @@ pub enum Error {
#[error("missing tag")] #[error("missing tag")]
MissingTag, MissingTag,
#[error("missing value")] #[error("missing value")]
MissingValue, MissingValue(&'static Kind<'static>),
#[error("unexpected token: {0}")] #[error("unexpected token: {0}")]
UnexpectedToken(String), UnexpectedToken(String),
#[error("missing prefix")] #[error("missing prefix")]
@ -112,7 +112,7 @@ pub enum Error {
DuplicateValue, DuplicateValue,
} }
#[derive(Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Kind<'a> { pub enum Kind<'a> {
Scalar, Scalar,
Element(Id<'a>), Element(Id<'a>),

View File

@ -1,6 +1,6 @@
use similar_asserts::assert_eq; use similar_asserts::assert_eq;
use instant_xml::{from_str, Error, FromXml}; use instant_xml::{from_str, Error, FromXml, Kind};
#[derive(Debug, Eq, PartialEq, FromXml)] #[derive(Debug, Eq, PartialEq, FromXml)]
#[xml(ns("URI"))] #[xml(ns("URI"))]
@ -24,12 +24,12 @@ fn direct_namespaces() {
from_str( from_str(
"<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"WRONG\">true</flag></StructDirectNamespace>" "<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"WRONG\">true</flag></StructDirectNamespace>"
), ),
Err::<StructDirectNamespace, _>(Error::MissingValue) Err::<StructDirectNamespace, _>(Error::MissingValue(&Kind::Scalar))
); );
// Wrong direct namespace - missing namespace // Wrong direct namespace - missing namespace
assert_eq!( assert_eq!(
from_str("<StructDirectNamespace xmlns=\"URI\"><flag>true</flag></StructDirectNamespace>"), from_str("<StructDirectNamespace xmlns=\"URI\"><flag>true</flag></StructDirectNamespace>"),
Err::<StructDirectNamespace, _>(Error::MissingValue) Err::<StructDirectNamespace, _>(Error::MissingValue(&Kind::Scalar))
); );
} }

View File

@ -1,6 +1,6 @@
use similar_asserts::assert_eq; use similar_asserts::assert_eq;
use instant_xml::{from_str, Error, FromXml}; use instant_xml::{from_str, Error, FromXml, Id, Kind};
#[derive(Debug, Eq, PartialEq, FromXml)] #[derive(Debug, Eq, PartialEq, FromXml)]
struct NestedWrongNamespace { struct NestedWrongNamespace {
@ -71,7 +71,9 @@ fn default_namespaces() {
// Wrong child namespace // Wrong child namespace
assert_eq!( assert_eq!(
from_str("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>"), from_str("<StructWithWrongNestedNamespace xmlns=\"URI\" xmlns:dar=\"BAZ\"><NestedWrongNamespace><flag>true</flag></NestedWrongNamespace></StructWithWrongNestedNamespace>"),
Err::<StructWithWrongNestedNamespace, _>(Error::MissingValue) Err::<StructWithWrongNestedNamespace, _>(
Error::MissingValue(&Kind::Element(Id { ns: "", name: "NestedWrongNamespace" }))
)
); );
} }
@ -111,7 +113,7 @@ fn other_namespaces() {
from_str( from_str(
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"WRONG\"><bar:flag>true</bar:flag></NestedOtherNamespace>" "<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"WRONG\"><bar:flag>true</bar:flag></NestedOtherNamespace>"
), ),
Err::<NestedOtherNamespace, _>(Error::MissingValue) Err::<NestedOtherNamespace, _>(Error::MissingValue(&Kind::Scalar))
); );
// Other namespace not-nested - missing parser prefix // Other namespace not-nested - missing parser prefix
@ -119,7 +121,7 @@ fn other_namespaces() {
from_str( from_str(
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAR\"><flag>true</flag></NestedOtherNamespace>" "<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAR\"><flag>true</flag></NestedOtherNamespace>"
), ),
Err::<NestedOtherNamespace, _>(Error::MissingValue) Err::<NestedOtherNamespace, _>(Error::MissingValue(&Kind::Scalar))
); );
// Correct child other namespace // Correct child other namespace