Better errors for unknown prefixes

This commit is contained in:
Dirkjan Ochtman 2022-11-29 11:43:06 +01:00
parent 48450b3700
commit 8233884207
3 changed files with 10 additions and 8 deletions

View File

@ -134,7 +134,9 @@ impl<'xml> Context<'xml> {
pub(crate) fn element_id(&self, element: &Element<'xml>) -> Result<Id<'xml>, Error> {
Ok(Id {
ns: match (element.default_ns, element.prefix) {
(_, Some(prefix)) => self.lookup(prefix).ok_or(Error::WrongNamespace)?,
(_, Some(prefix)) => self
.lookup(prefix)
.ok_or_else(|| Error::UnknownPrefix(prefix.to_owned()))?,
(Some(ns), None) => ns,
(None, None) => self.default_ns(),
},
@ -145,7 +147,9 @@ impl<'xml> Context<'xml> {
fn attribute_id(&self, attr: &Attribute<'xml>) -> Result<Id<'xml>, Error> {
Ok(Id {
ns: match attr.prefix {
Some(ns) => self.lookup(ns).ok_or(Error::WrongNamespace)?,
Some(ns) => self
.lookup(ns)
.ok_or_else(|| Error::UnknownPrefix(ns.to_owned()))?,
None => self.default_ns(),
},
name: attr.local,

View File

@ -108,16 +108,14 @@ pub enum Error {
MissingValue(&'static Kind<'static>),
#[error("unexpected token: {0}")]
UnexpectedToken(String),
#[error("missing prefix")]
MissingdPrefix,
#[error("unknown prefix: {0}")]
UnknownPrefix(String),
#[error("unexpected node: {0}")]
UnexpectedNode(String),
#[error("unexpected state: {0}")]
UnexpectedState(&'static str),
#[error("expected scalar")]
ExpectedScalar,
#[error("wrong namespace")]
WrongNamespace,
#[error("duplicate value")]
DuplicateValue,
}

View File

@ -105,7 +105,7 @@ fn other_namespaces() {
from_str(
"<NestedOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><wrong:flag>true</wrong:flag></NestedOtherNamespace>"
),
Err::<NestedOtherNamespace, _>(Error::WrongNamespace)
Err::<NestedOtherNamespace, _>(Error::UnknownPrefix("wrong".to_owned()))
);
// Other namespace not-nested - wrong parser namespace
@ -153,6 +153,6 @@ fn other_namespaces() {
from_str(
"<StructOtherNamespace xmlns=\"URI\" xmlns:bar=\"BAZ\"><NestedOtherNamespace><wrong:flag>true</wrong:flag></NestedOtherNamespace></StructOtherNamespace>"
),
Err::<StructOtherNamespace, _>(Error::WrongNamespace)
Err::<StructOtherNamespace, _>(Error::UnknownPrefix("wrong".to_owned()))
);
}