diff --git a/ruma-events-macros/src/content_enum.rs b/ruma-events-macros/src/content_enum.rs index eb4312e4..085d63b0 100644 --- a/ruma-events-macros/src/content_enum.rs +++ b/ruma-events-macros/src/content_enum.rs @@ -19,6 +19,9 @@ fn marker_traits(ident: &Ident) -> TokenStream { "AnyEphemeralRoomEventContent" => quote! { impl ::ruma_events::EphemeralRoomEventContent for #ident {} }, + "AnyBasicEventContent" => quote! { + impl ::ruma_events::BasicEventContent for #ident {} + }, _ => TokenStream::new(), } } diff --git a/ruma-events-macros/src/event_content.rs b/ruma-events-macros/src/event_content.rs index f48405e9..61558c1a 100644 --- a/ruma-events-macros/src/event_content.rs +++ b/ruma-events-macros/src/event_content.rs @@ -64,6 +64,30 @@ fn expand_event_content(input: DeriveInput) -> syn::Result { }) } +/// Create a `BasicEventContent` implementation for a struct +pub fn expand_basic_event_content(input: DeriveInput) -> syn::Result { + let ident = input.ident.clone(); + let event_content_impl = expand_event_content(input)?; + + Ok(quote! { + #event_content_impl + + impl ::ruma_events::BasicEventContent for #ident { } + }) +} + +/// Create a `EphemeralRoomEventContent` implementation for a struct +pub fn expand_ephemeral_event_content(input: DeriveInput) -> syn::Result { + let ident = input.ident.clone(); + let event_content_impl = expand_event_content(input)?; + + Ok(quote! { + #event_content_impl + + impl ::ruma_events::EphemeralRoomEventContent for #ident { } + }) +} + /// Create a `RoomEventContent` implementation for a struct. /// /// This is used internally for code sharing as `RoomEventContent` is not derivable. @@ -101,27 +125,3 @@ pub fn expand_state_event_content(input: DeriveInput) -> syn::Result syn::Result { - let ident = input.ident.clone(); - let event_content_impl = expand_event_content(input)?; - - Ok(quote! { - #event_content_impl - - impl ::ruma_events::PresenceEventContent for #ident { } - }) -} - -/// Create a `EphemeralRoomEventContent` implementation for a struct -pub fn expand_ephemeral_event_content(input: DeriveInput) -> syn::Result { - let ident = input.ident.clone(); - let event_content_impl = expand_event_content(input)?; - - Ok(quote! { - #event_content_impl - - impl ::ruma_events::EphemeralRoomEventContent for #ident { } - }) -} diff --git a/ruma-events-macros/src/lib.rs b/ruma-events-macros/src/lib.rs index 5df6c8d4..aef3bb38 100644 --- a/ruma-events-macros/src/lib.rs +++ b/ruma-events-macros/src/lib.rs @@ -18,8 +18,8 @@ use self::{ content_enum::{expand_content_enum, parse::ContentEnumInput}, event::expand_event, event_content::{ - expand_ephemeral_event_content, expand_message_event_content, - expand_presence_event_content, expand_state_event_content, + expand_basic_event_content, expand_ephemeral_event_content, expand_message_event_content, + expand_state_event_content, }, gen::RumaEvent, parse::RumaEventInput, @@ -137,6 +137,15 @@ pub fn event_content_enum(input: TokenStream) -> TokenStream { .into() } +/// Generates an implementation of `ruma_events::BasicEventContent` and it's super traits. +#[proc_macro_derive(BasicEventContent, attributes(ruma_event))] +pub fn derive_basic_event_content(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + expand_basic_event_content(input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() +} + /// Generates an implementation of `ruma_events::MessageEventContent` and it's super traits. #[proc_macro_derive(MessageEventContent, attributes(ruma_event))] pub fn derive_message_event_content(input: TokenStream) -> TokenStream { @@ -155,15 +164,6 @@ pub fn derive_state_event_content(input: TokenStream) -> TokenStream { .into() } -/// Generates an implementation of `ruma_events::PresenceEventContent` and it's super traits. -#[proc_macro_derive(PresenceEventContent, attributes(ruma_event))] -pub fn derive_presence_event_content(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - expand_presence_event_content(input) - .unwrap_or_else(|err| err.to_compile_error()) - .into() -} - /// Generates an implementation of `ruma_events::EphemeralRoomEventContent` and it's super traits. #[proc_macro_derive(EphemeralRoomEventContent, attributes(ruma_event))] pub fn derive_ephemeral_event_content(input: TokenStream) -> TokenStream { diff --git a/src/content_enums.rs b/src/content_enums.rs index 8181962c..f7e5edd9 100644 --- a/src/content_enums.rs +++ b/src/content_enums.rs @@ -40,3 +40,9 @@ event_content_enum! { name: AnyEphemeralRoomEventContent, events: [ "m.typing", "m.receipt" ] } + +event_content_enum! { + /// A basic event. + name: AnyBasicEventContent, + events: [ "m.ignored_user_list", "m.room_key" ] +} diff --git a/src/event_kinds.rs b/src/event_kinds.rs index 01b2ca26..62ae9f29 100644 --- a/src/event_kinds.rs +++ b/src/event_kinds.rs @@ -11,6 +11,7 @@ use crate::{ /// A basic event – one that consists only of it's type and the `content` object. #[derive(Clone, Debug, Event)] pub struct BasicEvent { + /// Data specific to the event type. pub content: C, } diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index e68c1c37..fd06c685 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -1,19 +1,16 @@ //! Types for the *m.ignored_user_list* event. -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; use ruma_identifiers::UserId; +use serde::{Deserialize, Serialize}; -ruma_event! { +/// A list of users to ignore. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.ignored_user_list")] +pub struct IgnoredUserListEventContent { /// A list of users to ignore. - IgnoredUserListEvent { - kind: Event, - event_type: "m.ignored_user_list", - content: { - /// A list of users to ignore. - #[serde(with = "ruma_serde::vec_as_map_of_empty")] - pub ignored_users: Vec, - }, - } + #[serde(with = "ruma_serde::vec_as_map_of_empty")] + pub ignored_users: Vec, } #[cfg(test)] @@ -24,12 +21,12 @@ mod tests { use ruma_identifiers::UserId; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; - use super::{IgnoredUserListEvent, IgnoredUserListEventContent}; - use crate::EventJson; + use super::IgnoredUserListEventContent; + use crate::{AnyBasicEventContent, BasicEvent, EventJson}; #[test] fn serialization() { - let ignored_user_list_event = IgnoredUserListEvent { + let ignored_user_list_event = BasicEvent { content: IgnoredUserListEventContent { ignored_users: vec![UserId::try_from("@carl:example.com").unwrap()], }, @@ -59,12 +56,12 @@ mod tests { }); assert_matches!( - from_json_value::>(json) + from_json_value::>>(json) .unwrap() .deserialize() .unwrap(), - IgnoredUserListEvent { - content: IgnoredUserListEventContent { ignored_users, }, + BasicEvent { + content: AnyBasicEventContent::IgnoredUserList(IgnoredUserListEventContent { ignored_users, }), } if ignored_users == vec![UserId::try_from("@carl:example.com").unwrap()] ); } diff --git a/src/lib.rs b/src/lib.rs index bd91867d..d8e70fad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,13 +149,13 @@ pub mod custom; // pub mod dummy; pub mod forwarded_room_key; pub mod fully_read; -// pub mod ignored_user_list; +pub mod ignored_user_list; pub mod key; pub mod presence; // pub mod push_rules; pub mod receipt; pub mod room; -// pub mod room_key; +pub mod room_key; pub mod room_key_request; pub mod sticker; // pub mod stripped; @@ -165,10 +165,13 @@ pub mod typing; pub use self::{ algorithm::Algorithm, - content_enums::{AnyEphemeralRoomEventContent, AnyMessageEventContent, AnyStateEventContent}, + content_enums::{ + AnyBasicEventContent, AnyEphemeralRoomEventContent, AnyMessageEventContent, + AnyStateEventContent, + }, error::{FromStrError, InvalidEvent, InvalidInput}, event_enums::AnyStateEvent, - event_kinds::{EphemeralRoomEvent, MessageEvent, StateEvent}, + event_kinds::{BasicEvent, EphemeralRoomEvent, MessageEvent, StateEvent}, event_type::EventType, json::EventJson, }; diff --git a/src/presence.rs b/src/presence.rs index 0256c537..dd44a47b 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -1,7 +1,6 @@ -//! A presence event is represented by a parameterized struct. +//! A presence event is represented by a struct with a set content field. //! -//! There is only one type that will satisfy the bounds of `PresenceEventContent` -//! as this event has only one possible content value according to Matrix spec. +//! The only content valid for this event is `PresenceEventContent. use js_int::UInt; pub use ruma_common::presence::PresenceState; diff --git a/src/room_key.rs b/src/room_key.rs index 5067d33c..01ebdeb4 100644 --- a/src/room_key.rs +++ b/src/room_key.rs @@ -1,33 +1,29 @@ //! Types for the *m.room_key* event. -use ruma_events_macros::ruma_event; -use ruma_identifiers::RoomId; - use super::Algorithm; +use ruma_events_macros::BasicEventContent; +use ruma_identifiers::RoomId; +use serde::{Deserialize, Serialize}; -ruma_event! { - /// This event type is used to exchange keys for end-to-end encryption. +/// This event type is used to exchange keys for end-to-end encryption. +/// +/// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.room_key")] +pub struct RoomKeyEventContent { + /// The encryption algorithm the key in this event is to be used with. /// - /// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event. - RoomKeyEvent { - kind: Event, - event_type: "m.room_key", - content: { - /// The encryption algorithm the key in this event is to be used with. - /// - /// Must be `m.megolm.v1.aes-sha2`. - pub algorithm: Algorithm, + /// Must be `m.megolm.v1.aes-sha2`. + pub algorithm: Algorithm, - /// The room where the key is used. - pub room_id: RoomId, + /// The room where the key is used. + pub room_id: RoomId, - /// The ID of the session that the key is for. - pub session_id: String, + /// The ID of the session that the key is for. + pub session_id: String, - /// The key to be exchanged. - pub session_key: String, - } - } + /// The key to be exchanged. + pub session_key: String, } #[cfg(test)] @@ -37,12 +33,12 @@ mod tests { use ruma_identifiers::RoomId; use serde_json::{json, to_value as to_json_value}; - use super::{RoomKeyEvent, RoomKeyEventContent}; - use crate::Algorithm; + use super::RoomKeyEventContent; + use crate::{Algorithm, BasicEvent}; #[test] fn serialization() { - let ev = RoomKeyEvent { + let ev = BasicEvent { content: RoomKeyEventContent { algorithm: Algorithm::MegolmV1AesSha2, room_id: RoomId::try_from("!testroomid:example.org").unwrap(),