xso: add support for overriding names of generated types

In 1265f4b, we introduced a change which may cause a conflict of type
names when deriving the traits on two different types. While a
workaround existed (use `mod`s to isolate the implementation), that is
ugly.

This commit allows overriding the choice of type names.
This commit is contained in:
Jonas Schäfer 2024-07-27 08:52:08 +02:00 committed by Link Mauve
commit c90752aa51
5 changed files with 77 additions and 12 deletions

View file

@ -157,6 +157,12 @@ pub(crate) struct XmlCompoundMeta {
/// The debug flag.
pub(crate) debug: Flag,
/// The value assigned to `builder` inside `#[xml(..)]`, if any.
pub(crate) builder: Option<Ident>,
/// The value assigned to `iterator` inside `#[xml(..)]`, if any.
pub(crate) iterator: Option<Ident>,
}
impl XmlCompoundMeta {
@ -167,6 +173,8 @@ impl XmlCompoundMeta {
fn parse_from_attribute(attr: &Attribute) -> Result<Self> {
let mut namespace = None;
let mut name = None;
let mut builder = None;
let mut iterator = None;
let mut debug = Flag::Absent;
attr.parse_nested_meta(|meta| {
@ -188,6 +196,18 @@ impl XmlCompoundMeta {
}
debug = (&meta.path).into();
Ok(())
} else if meta.path.is_ident("builder") {
if builder.is_some() {
return Err(Error::new_spanned(meta.path, "duplicate `builder` key"));
}
builder = Some(meta.value()?.parse()?);
Ok(())
} else if meta.path.is_ident("iterator") {
if iterator.is_some() {
return Err(Error::new_spanned(meta.path, "duplicate `iterator` key"));
}
iterator = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(Error::new_spanned(meta.path, "unsupported key"))
}
@ -198,6 +218,8 @@ impl XmlCompoundMeta {
namespace,
name,
debug,
builder,
iterator,
})
}

View file

@ -85,13 +85,23 @@ impl StructDef {
return Err(Error::new(meta.span, "`name` is required on structs"));
};
let builder_ty_ident = match meta.builder {
Some(v) => v,
None => concat_camel_case(ident, "FromXmlBuilder"),
};
let item_iter_ty_ident = match meta.iterator {
Some(v) => v,
None => concat_camel_case(ident, "AsXmlIterator"),
};
Ok(Self {
namespace,
name,
inner: Compound::from_fields(fields)?,
target_ty_ident: ident.clone(),
builder_ty_ident: concat_camel_case(ident, "FromXmlBuilder"),
item_iter_ty_ident: concat_camel_case(ident, "AsXmlIterator"),
builder_ty_ident,
item_iter_ty_ident,
debug: meta.debug.is_set(),
})
}