From 60898cdf16e024b1c98a929a3f24da56c6865709 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Fri, 17 Feb 2023 12:31:35 +0100 Subject: [PATCH] Improve error message on unexpected root element --- instant-xml/src/lib.rs | 11 +++++++---- instant-xml/tests/de-ns.rs | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/instant-xml/src/lib.rs b/instant-xml/src/lib.rs index 5de63c4..66efba0 100644 --- a/instant-xml/src/lib.rs +++ b/instant-xml/src/lib.rs @@ -58,10 +58,13 @@ pub fn from_str<'xml, T: FromXml<'xml>>(input: &'xml str) -> Result { let id = context.element_id(&root)?; if !T::matches(id, None) { - return Err(Error::UnexpectedValue(format!( - "unexpected root element {} in namespace {}", - id.name, id.ns - ))); + return Err(Error::UnexpectedValue(match id.ns.is_empty() { + true => format!("unexpected root element {:?}", id.name), + false => format!( + "unexpected root element {:?} in namespace {:?}", + id.name, id.ns + ), + })); } let mut value = None; diff --git a/instant-xml/tests/de-ns.rs b/instant-xml/tests/de-ns.rs index a94ff4c..7fd41f3 100644 --- a/instant-xml/tests/de-ns.rs +++ b/instant-xml/tests/de-ns.rs @@ -40,7 +40,7 @@ fn default_namespaces() { "true" ), Err::(Error::UnexpectedValue( - "unexpected root element NestedDe in namespace WRONG".to_owned() + "unexpected root element \"NestedDe\" in namespace \"WRONG\"".to_owned() )) );