Add event_type accessor method to event enums

This commit is contained in:
Akshay 2021-02-11 17:18:33 +05:30 committed by GitHub
parent ed6e55a22f
commit 6bbb42303b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -711,6 +711,18 @@ fn accessor_methods(
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! {
/// Returns the `type` of this event.
pub fn event_type(&self) -> &str {
match self {
#( #self_variants(event) =>
#ruma_events::EventContent::event_type(&event.content), )*
Self::Custom(event) =>
#ruma_events::EventContent::event_type(&event.content),
}
}
};
let content = quote! {
/// Returns the any content enum for this event.
pub fn content(&self) -> #content_enum {
@ -746,6 +758,8 @@ fn accessor_methods(
impl #ident {
#content
#event_type
#prev_content
#( #methods )*

View File

@ -269,4 +269,5 @@ fn alias_event_field_access() {
} else {
panic!("the `Any*Event` enum's accessor methods may have been altered")
}
assert_eq!(deser.event_type(), "m.room.aliases");
}