xso-proc: fix renaming attributes using a static item

This was broken by d4d520e by accident, and of course we didn't have
tests for that :-).

(we do, now)
This commit is contained in:
Jonas Schäfer 2024-06-24 14:41:43 +02:00
parent 9e07882531
commit c0d109d9be
2 changed files with 27 additions and 24 deletions

View file

@ -66,6 +66,13 @@ type Result = ((),);
static NS1: &str = "urn:example:ns1"; static NS1: &str = "urn:example:ns1";
static SOME_NAME: &::xso::exports::rxml::strings::NcNameStr = {
#[allow(unsafe_code)]
unsafe {
::xso::exports::rxml::strings::NcNameStr::from_str_unchecked("bar")
}
};
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = "foo")] #[xml(namespace = NS1, name = "foo")]
struct Empty; struct Empty;
@ -149,13 +156,6 @@ fn empty_qname_check_has_precedence_over_attr_check() {
} }
} }
static SOME_NAME: &::xso::exports::rxml::strings::NcNameStr = {
#[allow(unsafe_code)]
unsafe {
::xso::exports::rxml::strings::NcNameStr::from_str_unchecked("bar")
}
};
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = NS1, name = SOME_NAME)] #[xml(namespace = NS1, name = SOME_NAME)]
struct NamePath; struct NamePath;
@ -234,6 +234,8 @@ fn required_attribute_missing() {
struct RenamedAttribute { struct RenamedAttribute {
#[xml(attribute = "a1")] #[xml(attribute = "a1")]
foo: String, foo: String,
#[xml(attribute = SOME_NAME)]
bar: String,
} }
#[test] #[test]
@ -243,7 +245,7 @@ fn renamed_attribute_roundtrip() {
option::Option::{None, Some}, option::Option::{None, Some},
result::Result::{Err, Ok}, result::Result::{Err, Ok},
}; };
roundtrip_full::<RenamedAttribute>("<attr xmlns='urn:example:ns1' a1='bar'/>"); roundtrip_full::<RenamedAttribute>("<attr xmlns='urn:example:ns1' a1='bar' bar='baz'/>");
} }
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]

View file

@ -207,6 +207,12 @@ impl XmlCompoundMeta {
fn parse_prefixed_name( fn parse_prefixed_name(
value: syn::parse::ParseStream<'_>, value: syn::parse::ParseStream<'_>,
) -> Result<(Option<NamespaceRef>, NameRef)> { ) -> Result<(Option<NamespaceRef>, NameRef)> {
if !value.peek(LitStr) {
// if we don't have a string literal next, we delegate to the default
// `NameRef` parser.
return Ok((None, value.parse()?));
}
let name: LitStr = value.parse()?; let name: LitStr = value.parse()?;
let name_span = name.span(); let name_span = name.span();
let (prefix, name) = match name let (prefix, name) = match name
@ -284,23 +290,18 @@ impl XmlFieldMeta {
return Err(Error::new_spanned(meta.path, "duplicate `name` key")); return Err(Error::new_spanned(meta.path, "duplicate `name` key"));
} }
let value = meta.value()?; let value = meta.value()?;
name = if value.peek(LitStr) { let name_span = value.span();
let name_span = value.span(); let (new_namespace, new_name) = parse_prefixed_name(value)?;
let (new_namespace, name) = parse_prefixed_name(value)?; if let Some(new_namespace) = new_namespace {
if let Some(new_namespace) = new_namespace { if namespace.is_some() {
if namespace.is_some() { return Err(Error::new(
return Err(Error::new( name_span,
name_span, "cannot combine `namespace` key with prefixed `name`",
"cannot combine `namespace` key with prefixed `name`", ));
));
}
namespace = Some(new_namespace);
} }
Some(name) namespace = Some(new_namespace);
} else { }
// just use the normal parser name = Some(new_name);
Some(value.parse()?)
};
Ok(()) Ok(())
} else if meta.path.is_ident("namespace") { } else if meta.path.is_ident("namespace") {
if namespace.is_some() { if namespace.is_some() {