events: Remove EventKind
The EventType associated type on EventContent practically provides the same information.
This commit is contained in:
parent
19f68065f1
commit
49a0650f62
@ -21,7 +21,7 @@
|
||||
//! # Extending Ruma with custom events
|
||||
//!
|
||||
//! For our examples we will start with a simple custom state event. `ruma_event`
|
||||
//! specifies the state event's `type` and it's [`kind`](EventKind).
|
||||
//! specifies the state event's `type` and its `kind`.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use ruma_common::events::macros::EventContent;
|
||||
|
@ -34,61 +34,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for abstracting over event content structs.
|
||||
///
|
||||
/// … but *not* enums which don't always have an event type and kind (e.g. message vs state) that's
|
||||
/// fixed / known at compile time.
|
||||
/// An event content type with a statically-known event `type` value.
|
||||
pub trait StaticEventContent: EventContent {
|
||||
/// The event's "kind".
|
||||
///
|
||||
/// See the type's documentation.
|
||||
const KIND: EventKind;
|
||||
|
||||
/// The event type.
|
||||
const TYPE: &'static str;
|
||||
}
|
||||
|
||||
/// The "kind" of an event.
|
||||
///
|
||||
/// This corresponds directly to the event content marker traits.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum EventKind {
|
||||
/// Global account data event kind.
|
||||
GlobalAccountData,
|
||||
|
||||
/// Room account data event kind.
|
||||
RoomAccountData,
|
||||
|
||||
/// Ephemeral room event kind.
|
||||
EphemeralRoomData,
|
||||
|
||||
/// Message-like event kind.
|
||||
///
|
||||
/// Since redacted / non-redacted message-like events are used in the same places but have
|
||||
/// different sets of fields, these two variations are treated as two closely-related event
|
||||
/// kinds.
|
||||
MessageLike {
|
||||
/// Redacted variation?
|
||||
redacted: bool,
|
||||
},
|
||||
|
||||
/// State event kind.
|
||||
///
|
||||
/// Since redacted / non-redacted state events are used in the same places but have different
|
||||
/// sets of fields, these two variations are treated as two closely-related event kinds.
|
||||
State {
|
||||
/// Redacted variation?
|
||||
redacted: bool,
|
||||
},
|
||||
|
||||
/// To-device event kind.
|
||||
ToDevice,
|
||||
|
||||
/// Presence event kind.
|
||||
Presence,
|
||||
}
|
||||
|
||||
/// Content of a global account-data event.
|
||||
pub trait GlobalAccountDataEventContent:
|
||||
EventContent<EventType = GlobalAccountDataEventType>
|
||||
|
@ -6,7 +6,7 @@ use js_int::UInt;
|
||||
use ruma_macros::{Event, EventContent};
|
||||
use serde::{ser::SerializeStruct, Deserialize, Serialize};
|
||||
|
||||
use super::{EventContent, EventKind, StaticEventContent};
|
||||
use super::EventContent;
|
||||
use crate::{presence::PresenceState, OwnedMxcUri, OwnedUserId};
|
||||
|
||||
/// Presence event.
|
||||
@ -85,11 +85,6 @@ impl PresenceEventContent {
|
||||
}
|
||||
}
|
||||
|
||||
impl StaticEventContent for PresenceEventContent {
|
||||
const KIND: EventKind = EventKind::Presence;
|
||||
const TYPE: &'static str = "m.presence";
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use js_int::uint;
|
||||
|
@ -357,8 +357,8 @@ pub fn expand_event_content(
|
||||
true,
|
||||
)
|
||||
.unwrap_or_else(syn::Error::into_compile_error);
|
||||
let static_event_content_impl = event_kind
|
||||
.map(|k| generate_static_event_content_impl(ident, k, false, &event_type, ruma_common));
|
||||
let static_event_content_impl =
|
||||
generate_static_event_content_impl(ident, &event_type, ruma_common);
|
||||
let type_aliases = event_kind.map(|k| {
|
||||
generate_event_type_aliases(k, ident, &event_type.value(), ruma_common)
|
||||
.unwrap_or_else(syn::Error::into_compile_error)
|
||||
@ -453,13 +453,8 @@ fn generate_redacted_event_content<'a>(
|
||||
|
||||
let sub_trait_name = format_ident!("Redacted{event_kind}Content");
|
||||
|
||||
let static_event_content_impl = generate_static_event_content_impl(
|
||||
&redacted_ident,
|
||||
event_kind,
|
||||
true,
|
||||
event_type,
|
||||
ruma_common,
|
||||
);
|
||||
let static_event_content_impl =
|
||||
generate_static_event_content_impl(&redacted_ident, event_type, ruma_common);
|
||||
|
||||
Ok(quote! {
|
||||
// this is the non redacted event content's impl
|
||||
@ -613,13 +608,8 @@ fn generate_possibly_redacted_event_content<'a>(
|
||||
)
|
||||
.unwrap_or_else(syn::Error::into_compile_error);
|
||||
|
||||
let static_event_content_impl = generate_static_event_content_impl(
|
||||
&possibly_redacted_ident,
|
||||
event_kind,
|
||||
true,
|
||||
event_type,
|
||||
ruma_common,
|
||||
);
|
||||
let static_event_content_impl =
|
||||
generate_static_event_content_impl(&possibly_redacted_ident, event_type, ruma_common);
|
||||
|
||||
Ok(quote! {
|
||||
#[doc = #doc]
|
||||
@ -961,30 +951,11 @@ fn generate_event_content_impl<'a>(
|
||||
|
||||
fn generate_static_event_content_impl(
|
||||
ident: &Ident,
|
||||
event_kind: EventKind,
|
||||
redacted: bool,
|
||||
event_type: &LitStr,
|
||||
ruma_common: &TokenStream,
|
||||
) -> TokenStream {
|
||||
let event_kind = match event_kind {
|
||||
EventKind::GlobalAccountData => quote! { GlobalAccountData },
|
||||
EventKind::RoomAccountData => quote! { RoomAccountData },
|
||||
EventKind::Ephemeral => quote! { EphemeralRoomData },
|
||||
EventKind::MessageLike => quote! { MessageLike { redacted: #redacted } },
|
||||
EventKind::State => quote! { State { redacted: #redacted } },
|
||||
EventKind::ToDevice => quote! { ToDevice },
|
||||
EventKind::RoomRedaction
|
||||
| EventKind::Presence
|
||||
| EventKind::Decrypted
|
||||
| EventKind::HierarchySpaceChild => {
|
||||
unreachable!("not a valid event content kind")
|
||||
}
|
||||
};
|
||||
|
||||
quote! {
|
||||
impl #ruma_common::events::StaticEventContent for #ident {
|
||||
const KIND: #ruma_common::events::EventKind =
|
||||
#ruma_common::events::EventKind::#event_kind;
|
||||
const TYPE: &'static ::std::primitive::str = #event_type;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user