events-macros: Refactor accessor method codegen
This commit is contained in:
parent
4229adfc05
commit
c14a0edf15
@ -522,23 +522,13 @@ fn accessor_methods(
|
|||||||
variants: &[EventEnumVariant],
|
variants: &[EventEnumVariant],
|
||||||
ruma_events: &TokenStream,
|
ruma_events: &TokenStream,
|
||||||
) -> Option<TokenStream> {
|
) -> Option<TokenStream> {
|
||||||
use EventKindVariation as V;
|
let ident = kind.to_event_enum_ident(var).unwrap();
|
||||||
|
|
||||||
let ident = kind.to_event_enum_ident(var)?;
|
|
||||||
|
|
||||||
// matching `EventKindVariation`s
|
|
||||||
if let V::Redacted | V::RedactedSync = var {
|
|
||||||
return redacted_accessor_methods(kind, var, variants, ruma_events);
|
|
||||||
}
|
|
||||||
|
|
||||||
let methods = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
let methods = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
||||||
generate_accessor(name, kind, var, *has_field, variants, ruma_events)
|
generate_accessor(name, kind, var, *has_field, variants, ruma_events)
|
||||||
});
|
});
|
||||||
|
|
||||||
let content_enum = kind.to_content_enum();
|
|
||||||
|
|
||||||
let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect();
|
let self_variants: Vec<_> = variants.iter().map(|v| v.match_arm(quote! { Self })).collect();
|
||||||
let content_variants: Vec<_> = variants.iter().map(|v| v.ctor(&content_enum)).collect();
|
|
||||||
|
|
||||||
let event_type = quote! {
|
let event_type = quote! {
|
||||||
/// Returns the `type` of this event.
|
/// Returns the `type` of this event.
|
||||||
@ -552,21 +542,13 @@ fn accessor_methods(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let content = quote! {
|
let content_accessors = (!var.is_redacted()).then(|| {
|
||||||
/// Returns the content enum for this event.
|
let content_enum = kind.to_content_enum();
|
||||||
pub fn content(&self) -> #content_enum {
|
let content_variants: Vec<_> = variants.iter().map(|v| v.ctor(&content_enum)).collect();
|
||||||
match self {
|
|
||||||
#( #self_variants(event) => #content_variants(event.content.clone()), )*
|
|
||||||
Self::_Custom(event) => #content_enum::_Custom {
|
|
||||||
event_type: #ruma_events::EventContent::event_type(&event.content).to_owned(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let prev_content = has_prev_content_field(kind, var).then(|| {
|
let prev_content = has_prev_content_field(kind, var).then(|| {
|
||||||
quote! {
|
quote! {
|
||||||
/// Returns the content enum for this events prev_content.
|
/// Returns the previous content for this event.
|
||||||
pub fn prev_content(&self) -> Option<#content_enum> {
|
pub fn prev_content(&self) -> Option<#content_enum> {
|
||||||
match self {
|
match self {
|
||||||
#(
|
#(
|
||||||
@ -584,33 +566,27 @@ fn accessor_methods(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(quote! {
|
quote! {
|
||||||
#[automatically_derived]
|
/// Returns the content for this event.
|
||||||
impl #ident {
|
pub fn content(&self) -> #content_enum {
|
||||||
#content
|
match self {
|
||||||
#event_type
|
#( #self_variants(event) => #content_variants(event.content.clone()), )*
|
||||||
#prev_content
|
Self::_Custom(event) => #content_enum::_Custom {
|
||||||
#( #methods )*
|
event_type: #ruma_events::EventContent::event_type(&event.content)
|
||||||
|
.to_owned(),
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Redacted events do NOT generate `content` or `prev_content` methods like
|
#prev_content
|
||||||
/// un-redacted events; otherwise, they are the same.
|
}
|
||||||
fn redacted_accessor_methods(
|
|
||||||
kind: EventKind,
|
|
||||||
var: EventKindVariation,
|
|
||||||
variants: &[EventEnumVariant],
|
|
||||||
ruma_events: &TokenStream,
|
|
||||||
) -> Option<TokenStream> {
|
|
||||||
let ident = kind.to_event_enum_ident(var)?;
|
|
||||||
let methods = EVENT_FIELDS.iter().map(|(name, has_field)| {
|
|
||||||
generate_accessor(name, kind, var, *has_field, variants, ruma_events)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl #ident {
|
impl #ident {
|
||||||
|
#event_type
|
||||||
|
#content_accessors
|
||||||
#( #methods )*
|
#( #methods )*
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user