Clean up before starting to implement eq/ord trait
This commit is contained in:
parent
2fbdd674ab
commit
b6c289c3d2
@ -9,14 +9,10 @@ use crate::event_parse::{to_kind_variation, EventKind, EventKindVariation};
|
|||||||
/// Derive `Event` macro code generation.
|
/// Derive `Event` macro code generation.
|
||||||
pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
||||||
let ident = &input.ident;
|
let ident = &input.ident;
|
||||||
|
|
||||||
let (kind, var) = to_kind_variation(ident).ok_or_else(|| {
|
let (kind, var) = to_kind_variation(ident).ok_or_else(|| {
|
||||||
syn::Error::new(Span::call_site(), "not a valid ruma event struct identifier")
|
syn::Error::new(Span::call_site(), "not a valid ruma event struct identifier")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let (impl_gen, ty_gen, where_clause) = input.generics.split_for_impl();
|
|
||||||
let is_generic = !input.generics.params.is_empty();
|
|
||||||
|
|
||||||
let fields = if let Data::Struct(DataStruct { fields, .. }) = input.data.clone() {
|
let fields = if let Data::Struct(DataStruct { fields, .. }) = input.data.clone() {
|
||||||
if let Fields::Named(FieldsNamed { named, .. }) = fields {
|
if let Fields::Named(FieldsNamed { named, .. }) = fields {
|
||||||
if !named.iter().any(|f| f.ident.as_ref().unwrap() == "content") {
|
if !named.iter().any(|f| f.ident.as_ref().unwrap() == "content") {
|
||||||
@ -40,6 +36,28 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
));
|
));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let serialize_impl = expand_serialize_event(&input, &var, &fields)?;
|
||||||
|
|
||||||
|
let deserialize_impl = expand_deserialize_event(&input, &var, &fields)?;
|
||||||
|
|
||||||
|
let conversion_impl = expand_from_into(&input, &kind, &var, &fields);
|
||||||
|
|
||||||
|
Ok(quote! {
|
||||||
|
#conversion_impl
|
||||||
|
|
||||||
|
#serialize_impl
|
||||||
|
|
||||||
|
#deserialize_impl
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expand_serialize_event(
|
||||||
|
input: &DeriveInput,
|
||||||
|
var: &EventKindVariation,
|
||||||
|
fields: &[Field],
|
||||||
|
) -> syn::Result<TokenStream> {
|
||||||
|
let ident = &input.ident;
|
||||||
|
let (impl_gen, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||||
let serialize_fields = fields
|
let serialize_fields = fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
@ -80,7 +98,7 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let serialize_impl = quote! {
|
Ok(quote! {
|
||||||
impl #impl_gen ::serde::ser::Serialize for #ident #ty_gen #where_clause {
|
impl #impl_gen ::serde::ser::Serialize for #ident #ty_gen #where_clause {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
@ -97,18 +115,6 @@ pub fn expand_event(input: DeriveInput) -> syn::Result<TokenStream> {
|
|||||||
state.end()
|
state.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let deserialize_impl = expand_deserialize_event(&input, &var, &fields, is_generic)?;
|
|
||||||
|
|
||||||
let conversion_impl = expand_from_into(&input, &kind, &var, &fields);
|
|
||||||
|
|
||||||
Ok(quote! {
|
|
||||||
#conversion_impl
|
|
||||||
|
|
||||||
#serialize_impl
|
|
||||||
|
|
||||||
#deserialize_impl
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +122,6 @@ fn expand_deserialize_event(
|
|||||||
input: &DeriveInput,
|
input: &DeriveInput,
|
||||||
var: &EventKindVariation,
|
var: &EventKindVariation,
|
||||||
fields: &[Field],
|
fields: &[Field],
|
||||||
is_generic: bool,
|
|
||||||
) -> syn::Result<TokenStream> {
|
) -> syn::Result<TokenStream> {
|
||||||
let ident = &input.ident;
|
let ident = &input.ident;
|
||||||
// we know there is a content field already
|
// we know there is a content field already
|
||||||
@ -128,6 +133,7 @@ fn expand_deserialize_event(
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl();
|
let (impl_generics, ty_gen, where_clause) = input.generics.split_for_impl();
|
||||||
|
let is_generic = !input.generics.params.is_empty();
|
||||||
|
|
||||||
let enum_variants = fields
|
let enum_variants = fields
|
||||||
.iter()
|
.iter()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user