mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
xml: EnumVariant variant_names for untagged enums
This commit is contained in:
@@ -198,21 +198,43 @@ impl Enum {
|
|||||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||||
let ident = &self.ident;
|
let ident = &self.ident;
|
||||||
|
|
||||||
let tagged_variants = self.variants.iter().map(|variant| {
|
if self.attrs.untagged.is_present() {
|
||||||
let ns = match &variant.attrs.common.ns {
|
let untagged_variants = self.variants.iter().map(|variant| {
|
||||||
Some(ns) => quote! { Some(#ns) },
|
let ty = &variant.deserializer_type();
|
||||||
None => quote! { None },
|
quote! { #ty::variant_names() }
|
||||||
};
|
});
|
||||||
let b_xml_name = variant.xml_name().value();
|
quote! {
|
||||||
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
|
||||||
quote! {(#ns, #xml_name)}
|
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[];
|
||||||
});
|
|
||||||
|
|
||||||
quote! {
|
fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)> {
|
||||||
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
|
[
|
||||||
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[
|
#(#untagged_variants),*
|
||||||
#(#tagged_variants),*
|
].concat()
|
||||||
];
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let tagged_variants = self.variants.iter().map(|variant| {
|
||||||
|
let ns = match &variant.attrs.common.ns {
|
||||||
|
Some(ns) => quote! { Some(#ns) },
|
||||||
|
None => quote! { None },
|
||||||
|
};
|
||||||
|
let b_xml_name = variant.xml_name().value();
|
||||||
|
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
||||||
|
quote! {(#ns, #xml_name)}
|
||||||
|
});
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
|
||||||
|
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[
|
||||||
|
#(#tagged_variants),*
|
||||||
|
];
|
||||||
|
|
||||||
|
fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)> {
|
||||||
|
[Self::TAGGED_VARIANTS,].concat()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -259,7 +259,7 @@ impl NamedStruct {
|
|||||||
.map(|field| {
|
.map(|field| {
|
||||||
let field_index = field.target_field_index();
|
let field_index = field.target_field_index();
|
||||||
quote! {
|
quote! {
|
||||||
let ns = Some(self.#field_index);
|
let ns = self.#field_index;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -25,4 +25,7 @@ pub trait XmlRootTag {
|
|||||||
|
|
||||||
pub trait EnumVariants {
|
pub trait EnumVariants {
|
||||||
const TAGGED_VARIANTS: &'static [(Option<Namespace<'static>>, &'static str)];
|
const TAGGED_VARIANTS: &'static [(Option<Namespace<'static>>, &'static str)];
|
||||||
|
|
||||||
|
// Returns all valid xml names including untagged variants
|
||||||
|
fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,12 @@ pub const NS_CALENDARSERVER: Namespace = Namespace(b"http://calendarserver.org/n
|
|||||||
pub const NS_NEXTCLOUD: Namespace = Namespace(b"http://nextcloud.com/ns");
|
pub const NS_NEXTCLOUD: Namespace = Namespace(b"http://nextcloud.com/ns");
|
||||||
|
|
||||||
#[derive(EnumVariants)]
|
#[derive(EnumVariants)]
|
||||||
pub enum CalendarProp {
|
enum ExtensionProp {
|
||||||
|
Hello,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(EnumVariants)]
|
||||||
|
enum CalendarProp {
|
||||||
// WebDAV (RFC 2518)
|
// WebDAV (RFC 2518)
|
||||||
#[xml(ns = "NS_DAV")]
|
#[xml(ns = "NS_DAV")]
|
||||||
Displayname(Option<String>),
|
Displayname(Option<String>),
|
||||||
@@ -23,7 +28,7 @@ pub enum CalendarProp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enum_variants() {
|
fn test_enum_tagged_variants() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
CalendarProp::TAGGED_VARIANTS,
|
CalendarProp::TAGGED_VARIANTS,
|
||||||
&[
|
&[
|
||||||
@@ -34,3 +39,24 @@ fn test_enum_variants() {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(EnumVariants)]
|
||||||
|
#[xml(untagged)]
|
||||||
|
enum UnionProp {
|
||||||
|
Calendar(CalendarProp),
|
||||||
|
Extension(ExtensionProp),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_enum_untagged_variants() {
|
||||||
|
assert_eq!(
|
||||||
|
UnionProp::variant_names(),
|
||||||
|
vec![
|
||||||
|
(Some(NS_DAV), "displayname"),
|
||||||
|
(Some(NS_DAV), "getcontenttype"),
|
||||||
|
(Some(NS_DAV), "principal-URL"),
|
||||||
|
(None, "topic"),
|
||||||
|
(None, "hello"),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user