fixup namespace handling for scalar enums

We need to match the default namespace from the type,
rather than the namespace from the associate id.
This commit is contained in:
Wez Furlong 2024-05-22 10:43:46 -07:00 committed by Dirkjan Ochtman
parent c584137ee5
commit 05de91af02
3 changed files with 50 additions and 5 deletions

View File

@ -66,6 +66,8 @@ fn deserialize_scalar_enum(
variants.extend(quote!(#serialize_as => #ident::#v_ident,));
}
let default_namespace = meta.default_namespace();
let generics = meta.xml_generics(BTreeSet::new());
let (impl_generics, _, _) = generics.split_for_impl();
let (_, ty_generics, where_clause) = input.generics.split_for_impl();
@ -75,10 +77,7 @@ fn deserialize_scalar_enum(
impl #impl_generics FromXml<'xml> for #ident #ty_generics #where_clause {
#[inline]
fn matches(id: ::instant_xml::Id<'_>, field: Option<::instant_xml::Id<'_>>) -> bool {
match field {
Some(field) => id == field,
None => false,
}
id == ::instant_xml::Id { ns: #default_namespace, name: id.name }
}
fn deserialize<'cx>(

View File

@ -56,6 +56,8 @@ fn serialize_scalar_enum(
variants.extend(quote!(#ident::#v_ident => #serialize_as,));
}
let default_namespace = meta.default_namespace();
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
quote!(
impl #impl_generics ToXml for #ident #ty_generics #where_clause {
@ -66,7 +68,7 @@ fn serialize_scalar_enum(
) -> Result<(), instant_xml::Error> {
let prefix = match field {
Some(id) => {
let prefix = serializer.write_start(id.name, id.ns)?;
let prefix = serializer.write_start(id.name, #default_namespace)?;
serializer.end_start()?;
Some((prefix, id.name))
}

View File

@ -0,0 +1,44 @@
use similar_asserts::assert_eq;
use instant_xml::{from_str, to_string, FromXml, ToXml};
#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[xml(scalar)]
enum Foo {
A,
B,
}
#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
struct Container {
foo: Foo,
}
#[test]
fn scalar_enum() {
let v = Container { foo: Foo::A };
let xml = r#"<Container><foo>A</foo></Container>"#;
assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
}
#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[xml(scalar, ns("URI", x = "URI"))]
enum Bar {
A,
B,
}
#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
#[xml(ns("OTHER", x = "URI"))]
struct NsContainer {
bar: Bar,
}
#[test]
fn scalar_enum_ns() {
let v = NsContainer { bar: Bar::A };
let xml = r#"<NsContainer xmlns="OTHER" xmlns:x="URI"><x:bar>A</x:bar></NsContainer>"#;
assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
}