Add BasicEventContent derive and create AnyBasicEventContent enum

Also remove PresenceEventContent derive and expand code gen
This commit is contained in:
Ragotzy.devin 2020-06-08 17:51:22 -04:00 committed by GitHub
parent d38e385aab
commit 6a0a10fcff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 84 deletions

View File

@ -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(),
}
}

View File

@ -64,6 +64,30 @@ fn expand_event_content(input: DeriveInput) -> syn::Result<TokenStream> {
})
}
/// Create a `BasicEventContent` implementation for a struct
pub fn expand_basic_event_content(input: DeriveInput) -> syn::Result<TokenStream> {
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<TokenStream> {
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<TokenStream
impl ::ruma_events::StateEventContent for #ident { }
})
}
/// Create a `PresenceEventContent` implementation for a struct
pub fn expand_presence_event_content(input: DeriveInput) -> syn::Result<TokenStream> {
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<TokenStream> {
let ident = input.ident.clone();
let event_content_impl = expand_event_content(input)?;
Ok(quote! {
#event_content_impl
impl ::ruma_events::EphemeralRoomEventContent for #ident { }
})
}

View File

@ -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 {

View File

@ -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" ]
}

View File

@ -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<C: BasicEventContent> {
/// Data specific to the event type.
pub content: C,
}

View File

@ -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<UserId>,
},
}
#[serde(with = "ruma_serde::vec_as_map_of_empty")]
pub ignored_users: Vec<UserId>,
}
#[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::<EventJson<IgnoredUserListEvent>>(json)
from_json_value::<EventJson<BasicEvent<AnyBasicEventContent>>>(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()]
);
}

View File

@ -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,
};

View File

@ -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;

View File

@ -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(),