diff --git a/crates/ruma-common/src/events/kinds.rs b/crates/ruma-common/src/events/kinds.rs index 282d83c4..e6309ee9 100644 --- a/crates/ruma-common/src/events/kinds.rs +++ b/crates/ruma-common/src/events/kinds.rs @@ -439,6 +439,19 @@ where Redacted(C::Redacted), } +impl FullStateEventContent +where + C::Redacted: RedactedStateEventContent, +{ + /// Get the event’s type, like `m.room.create`. + pub fn event_type(&self) -> StateEventType { + match self { + Self::Original { content, .. } => content.event_type(), + Self::Redacted(content) => content.event_type(), + } + } +} + macro_rules! impl_possibly_redacted_event { ( $ty:ident ( $content_trait:ident, $redacted_content_trait:ident, $event_type:ident ) diff --git a/crates/ruma-macros/src/events/event_enum.rs b/crates/ruma-macros/src/events/event_enum.rs index 07dd7d31..b22e25a7 100644 --- a/crates/ruma-macros/src/events/event_enum.rs +++ b/crates/ruma-macros/src/events/event_enum.rs @@ -432,6 +432,8 @@ fn expand_full_content_enum( ) -> syn::Result { let ident = kind.to_full_content_enum(); + let event_type_enum = kind.to_event_type_enum(); + let content: Vec<_> = events .iter() .map(|event| { @@ -441,6 +443,7 @@ fn expand_full_content_enum( .collect::>()?; let variant_decls = variants.iter().map(|v| v.decl()).collect::>(); + let variant_arms = variants.iter().map(|v| v.match_arm(quote! { Self })).collect::>(); Ok(quote! { #( #attrs )* @@ -458,6 +461,16 @@ fn expand_full_content_enum( redacted: bool, }, } + + impl #ident { + /// Get the event’s type, like `m.room.create`. + pub fn event_type(&self) -> #ruma_common::events::#event_type_enum { + match self { + #( #variant_arms(content) => content.event_type(), )* + Self::_Custom { event_type, .. } => ::std::convert::From::from(&event_type.0[..]), + } + } + } }) }