Use an associated const for the element names

This commit is contained in:
Dirkjan Ochtman 2021-11-18 13:26:53 +01:00 committed by masalachai
parent bc96fd60f2
commit 09f0719b4f
3 changed files with 5 additions and 12 deletions

View File

@ -39,9 +39,7 @@ fn element_name_macro(ast: &syn::DeriveInput) -> TokenStream {
let implement = quote! {
impl #impl_generics ElementName for #name #type_generics {
fn element_name(&self) -> &'static str {
#elem_name
}
const ELEMENT: &'static str = #elem_name;
}
};
implement.into()

View File

@ -44,7 +44,7 @@ impl StringValueTrait for String {
/// Trait to set correct value for xml tags when tags are being generated from generic types
pub trait ElementName {
fn element_name(&self) -> &'static str;
const ELEMENT: &'static str;
}
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
@ -72,11 +72,9 @@ impl<T: ElementName + Serialize> Serialize for EppObject<T> {
where
S: Serializer,
{
let data_name = self.data.element_name();
let mut state = serializer.serialize_struct("epp", 4)?;
state.serialize_field("xmlns", &self.xmlns)?;
state.serialize_field(data_name, &self.data)?;
state.serialize_field(T::ELEMENT, &self.data)?;
state.end()
}
}
@ -115,10 +113,8 @@ impl<E: ElementName + Serialize> Serialize for Extension<E> {
where
S: Serializer,
{
let data_name = self.data.element_name();
let mut state = serializer.serialize_struct("extension", 1)?;
state.serialize_field(data_name, &self.data)?;
state.serialize_field(E::ELEMENT, &self.data)?;
state.end()
}
}

View File

@ -48,9 +48,8 @@ impl<T: ElementName + Serialize, E: ElementName + Serialize> Serialize
where
S: Serializer,
{
let command_name = self.command.element_name();
let mut state = serializer.serialize_struct("command", 3)?;
state.serialize_field(command_name, &self.command)?;
state.serialize_field(T::ELEMENT, &self.command)?;
state.serialize_field("extension", &self.extension)?;
state.serialize_field("clTRID", &self.client_tr_id)?;
state.end()