diff --git a/ruma-events-macros/src/gen.rs b/ruma-events-macros/src/gen.rs deleted file mode 100644 index a2c1947d..00000000 --- a/ruma-events-macros/src/gen.rs +++ /dev/null @@ -1,127 +0,0 @@ -//! Details of generating code for the `ruma_event` procedural macro. - -#![allow(dead_code)] - -use proc_macro2::{Span, TokenStream}; -use quote::{format_ident, quote, ToTokens}; -use syn::{ - parse::{self, Parse, ParseStream}, - parse_quote, - punctuated::Punctuated, - Attribute, Field, Ident, LitStr, Token, -}; - -use crate::parse::{Content, EventKind, RumaEventInput}; - -/// The result of processing the `ruma_event` macro, ready for output back to source code. -pub struct RumaEvent { - /// Outer attributes on the field, such as a docstring. - attrs: Vec, - - /// Information for generating the type used for the event's `content` field. - content: Content, - - /// The name of the type of the event's `content` field. - content_name: Ident, - - /// The variant of `ruma_events::EventType` for this event, determined by the `event_type` - /// field. - event_type: LitStr, - - /// Struct fields of the event. - fields: Vec, - - /// The kind of event. - kind: EventKind, - - /// The name of the event. - name: Ident, -} - -impl From for RumaEvent { - fn from(input: RumaEventInput) -> Self { - let kind = input.kind; - let name = input.name; - let content_name = format_ident!("{}Content", name, span = Span::call_site()); - let event_type = input.event_type; - - let mut fields = - populate_event_fields(content_name.clone(), input.fields.unwrap_or_else(Vec::new)); - - fields.sort_unstable_by_key(|field| field.ident.clone().unwrap()); - - Self { - attrs: input.attrs, - content: input.content, - content_name, - event_type, - fields, - kind, - name, - } - } -} - -impl ToTokens for RumaEvent { - fn to_tokens(&self, tokens: &mut TokenStream) { - // let attrs = &self.attrs; - let content_name = &self.content_name; - // let event_fields = &self.fields; - // let event_type = &self.event_type; - - let name = &self.name; - let content_docstring = format!("The payload for `{}`.", name); - - let content = match &self.content { - Content::Struct(fields) => { - quote! { - #[doc = #content_docstring] - #[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize)] - pub struct #content_name { - #(#fields),* - } - } - } - Content::Typedef(typedef) => { - let content_attrs = &typedef.attrs; - let path = &typedef.path; - - quote! { - #(#content_attrs)* - pub type #content_name = #path; - } - } - }; - - content.to_tokens(tokens); - } -} - -/// Fills in the event's struct definition with fields common to all basic events. -fn populate_event_fields(content_name: Ident, mut fields: Vec) -> Vec { - let punctuated_fields: Punctuated = parse_quote! { - /// The event's content. - pub content: #content_name, - }; - - fields.extend(punctuated_fields.into_iter().map(|p| p.field)); - - fields -} - -/// A wrapper around `syn::Field` that makes it possible to parse `Punctuated` -/// from a `TokenStream`. -/// -/// See https://github.com/dtolnay/syn/issues/651 for more context. -struct ParsableNamedField { - /// The wrapped `Field`. - pub field: Field, -} - -impl Parse for ParsableNamedField { - fn parse(input: ParseStream<'_>) -> parse::Result { - let field = Field::parse_named(input)?; - - Ok(Self { field }) - } -} diff --git a/ruma-events-macros/src/lib.rs b/ruma-events-macros/src/lib.rs index a4bedf6c..f565e319 100644 --- a/ruma-events-macros/src/lib.rs +++ b/ruma-events-macros/src/lib.rs @@ -11,7 +11,6 @@ extern crate proc_macro; use proc_macro::TokenStream; -use quote::ToTokens; use syn::{parse_macro_input, DeriveInput}; use self::{ @@ -22,109 +21,12 @@ use self::{ expand_message_event_content, expand_room_event_content, expand_state_event_content, }, event_enum::{expand_event_enum, EventEnumInput}, - gen::RumaEvent, - parse::RumaEventInput, }; mod content_enum; mod event; mod event_content; mod event_enum; -mod gen; -mod parse; - -// A note about the `example` modules that appears in doctests: -// -// This is necessary because otherwise the expanded code appears in function context, which makes -// the compiler interpret the output of the macro as a statement, and proc macros currently aren't -// allowed to expand to statements, resulting in a compiler error. - -/// Generates a Rust type for a Matrix event. -/// -/// # Examples -/// -/// The most common form of event is a struct with all the standard fields for an event of its -/// kind and a struct for its `content` field: -/// -/// ```ignore -/// # pub mod example { -/// # use ruma_events_macros::ruma_event; -/// ruma_event! { -/// /// Informs the room about what room aliases it has been given. -/// AliasesEvent { -/// kind: StateEvent, -/// event_type: RoomAliases, -/// content: { -/// /// A list of room aliases. -/// pub aliases: Vec, -/// } -/// } -/// } -/// # } -/// ``` -/// -/// Occasionally an event will have non-standard fields at its top level (outside the `content` -/// field). These extra fields are declared in block labeled with `fields`: -/// -/// ```ignore -/// # pub mod example { -/// # use ruma_events_macros::ruma_event; -/// ruma_event! { -/// /// A redaction of an event. -/// RedactionEvent { -/// kind: RoomEvent, -/// event_type: RoomRedaction, -/// fields: { -/// /// The ID of the event that was redacted. -/// pub redacts: ruma_identifiers::EventId -/// }, -/// content: { -/// /// The reason for the redaction, if any. -/// pub reason: Option, -/// }, -/// } -/// } -/// # } -/// ``` -/// -/// Sometimes the type of the `content` should be a type alias rather than a struct or enum. This -/// is designated with `content_type_alias`: -/// -/// ```ignore -/// # pub mod example { -/// # use ruma_events_macros::ruma_event; -/// ruma_event! { -/// /// Informs the client about the rooms that are considered direct by a user. -/// DirectEvent { -/// kind: Event, -/// event_type: Direct, -/// content_type_alias: { -/// /// The payload of a `DirectEvent`. -/// /// -/// /// A mapping of `UserId`'s to a collection of `RoomId`'s which are considered -/// /// *direct* for that particular user. -/// std::collections::BTreeMap> -/// } -/// } -/// } -/// # } -/// ``` -/// -/// If `content` and `content_type_alias` are both supplied, the second one listed will overwrite -/// the first. -/// -/// The event type and content type will have copies generated inside a private `raw` module. These -/// "raw" versions are the same, except they implement `serde::Deserialize`. An implementation of -/// `FromRaw` will be provided, which will allow the user to deserialize the event type as -/// `EventJson`. -#[proc_macro] -pub fn ruma_event(input: TokenStream) -> TokenStream { - let ruma_event_input = syn::parse_macro_input!(input as RumaEventInput); - - let ruma_event = RumaEvent::from(ruma_event_input); - - ruma_event.into_token_stream().into() -} /// Generates an enum to represent the various Matrix event types. /// diff --git a/ruma-events-macros/src/parse.rs b/ruma-events-macros/src/parse.rs deleted file mode 100644 index 990361f1..00000000 --- a/ruma-events-macros/src/parse.rs +++ /dev/null @@ -1,254 +0,0 @@ -//! Details of parsing input for the `ruma_event` procedural macro. - -use syn::{ - braced, - parse::{self, Parse, ParseStream}, - token::Colon, - Attribute, Expr, ExprLit, Field, FieldValue, Ident, Lit, LitStr, Member, Token, TypePath, -}; - -/// The entire `ruma_event!` macro structure directly as it appears in the source code.. -pub struct RumaEventInput { - /// Outer attributes on the field, such as a docstring. - pub attrs: Vec, - - /// The name of the event. - pub name: Ident, - - /// The kind of event, determined by the `kind` field. - pub kind: EventKind, - - /// The value for the `type` field in the JSON representation of this event. There needs to be a - /// corresponding variant in `ruma_events::EventType` for this event (converted to a valid - /// Rust-style type name by stripping `m.`, replacing the remaining dots by underscores and then - /// converting from snake_case to CamelCase). - pub event_type: LitStr, - - /// Additional named struct fields in the top level event struct. - pub fields: Option>, - - /// A struct definition or type alias to be used as the event's `content` field. - pub content: Content, -} - -impl Parse for RumaEventInput { - fn parse(input: ParseStream<'_>) -> parse::Result { - let attrs = input.call(Attribute::parse_outer)?; - let name: Ident = input.parse()?; - let body; - braced!(body in input); - - let mut kind = None; - let mut event_type = None; - let mut fields = None; - let mut content = None; - - for field_value_inline_struct in - body.parse_terminated::(RumaEventField::parse)? - { - match field_value_inline_struct { - RumaEventField::Block(field_block) => { - let ident = match field_block.member { - Member::Named(ident) => ident, - Member::Unnamed(_) => panic!("fields with block values in `ruma_event!` must named `content_type_alias`"), - }; - - if ident == "content_type_alias" { - content = Some(Content::Typedef(field_block.typedef)); - } - } - RumaEventField::InlineStruct(field_inline_struct) => { - let ident = match field_inline_struct.member { - Member::Named(ident) => ident, - Member::Unnamed(_) => panic!("fields with inline struct values in `ruma_event!` must be named `fields` or `content`."), - }; - - if ident == "fields" { - fields = Some(field_inline_struct.fields); - } else if ident == "content" { - content = Some(Content::Struct(field_inline_struct.fields)); - } - } - RumaEventField::Value(field_value) => { - let ident = match field_value.member { - Member::Named(ident) => ident, - Member::Unnamed(_) => panic!("fields with expression values in `ruma_event!` must be named `kind` or `event_type`, ."), - }; - - if ident == "kind" { - let event_kind = match field_value.expr { - Expr::Path(expr_path) => { - if expr_path.path.is_ident("Event") { - EventKind::Event - } else { - panic!("value of field `kind` must be one of `Event` or `RoomEvent`"); - } - } - _ => panic!( - "value of field `kind` is required to be an ident by `ruma_event!`" - ), - }; - - kind = Some(event_kind); - } else if ident == "event_type" { - event_type = Some(match field_value.expr { - Expr::Lit(ExprLit { lit: Lit::Str(s), .. }) => s, - // TODO: Span info - _ => panic!( - "value of field `event_type` is required to be a string literal by `ruma_event!`" - ), - }) - } else { - panic!("unexpected field-value pair with field name `{}`", ident); - } - } - } - } - - if kind.is_none() { - panic!("field `kind` is required by `ruma_event!`"); - } else if event_type.is_none() { - panic!("field `event_type` is required by `ruma_event!`"); - } else if content.is_none() { - panic!( - "one field named `content` or `content_type_alias` is required by `ruma_event!`" - ); - } - - Ok(Self { - attrs, - name, - kind: kind.unwrap(), - event_type: event_type.unwrap(), - fields, - content: content.unwrap(), - }) - } -} - -/// Which kind of event is being generated. -/// -/// Determined by the `kind` field in the macro body. -#[derive(PartialEq)] -pub enum EventKind { - /// A basic event. - Event, -} - -/// Information for generating the type used for the event's `content` field. -pub enum Content { - /// A struct, e.g. `ExampleEventContent { ... }`. - Struct(Vec), - - /// A type alias, e.g. `type ExampleEventContent = SomeExistingType` - Typedef(Typedef), -} - -/// The style of field within the macro body. -#[allow(clippy::large_enum_variant)] -enum RumaEventField { - /// The value of a field is a block with a type alias in it. - /// - /// Used for `content_type_alias`. - Block(FieldBlock), - - /// The value of a field is a block with named struct fields in it. - /// - /// Used for `content`. - InlineStruct(FieldInlineStruct), - - /// A standard named struct field. - /// - /// Used for `kind` and `event_type`. - Value(FieldValue), -} - -impl Parse for RumaEventField { - fn parse(input: ParseStream<'_>) -> parse::Result { - let ahead = input.fork(); - let field_ident: Ident = ahead.parse()?; - - match field_ident.to_string().as_ref() { - "content" | "fields" => { - let attrs = input.call(Attribute::parse_outer)?; - let member = input.parse()?; - let colon_token = input.parse()?; - let body; - braced!(body in input); - let fields = body - .parse_terminated::(Field::parse_named)? - .into_iter() - .collect(); - - Ok(RumaEventField::InlineStruct(FieldInlineStruct { - attrs, - member, - colon_token, - fields, - })) - } - "content_type_alias" => Ok(RumaEventField::Block(FieldBlock { - attrs: input.call(Attribute::parse_outer)?, - member: input.parse()?, - colon_token: input.parse()?, - typedef: input.parse()?, - })), - _ => Ok(RumaEventField::Value(input.parse()?)), - } - } -} - -/// The value of a field is a block with a type alias in it. -/// -/// Used for `content_type_alias`. -struct FieldBlock { - /// Outer attributes on the field, such as a docstring. - pub attrs: Vec, - - /// The name of the field. - pub member: Member, - - /// The colon that appears between the field name and type. - pub colon_token: Colon, - - /// The path to the type that will be used in a type alias for the event's `content` type. - pub typedef: Typedef, -} - -/// The value of a field is a block with named struct fields in it. -/// -/// Used for `content`. -struct FieldInlineStruct { - /// Outer attributes on the field, such as a docstring. - pub attrs: Vec, - - /// The name of the field. - pub member: Member, - - /// The colon that appears between the field name and type. - pub colon_token: Colon, - - /// The fields that define the `content` struct. - pub fields: Vec, -} - -/// Path to a type to be used in a type alias for an event's `content` type. -pub struct Typedef { - /// Outer attributes on the field, such as a docstring. - pub attrs: Vec, - - /// Path to the type. - pub path: TypePath, -} - -impl Parse for Typedef { - fn parse(input: ParseStream<'_>) -> parse::Result { - let body; - braced!(body in input); - - Ok(Self { - attrs: body.call(Attribute::parse_outer)?, - path: body.parse()?, - }) - } -} diff --git a/src/enums.rs b/src/enums.rs index fb7916fe..d1915591 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -16,7 +16,11 @@ event_enum! { event_enum! { /// Any ephemeral room event. name: AnyEphemeralRoomEvent, - events: [ "m.typing", "m.receipt" ] + events: [ + "m.fully_read", + "m.receipt", + "m.typing", + ] } event_enum! { @@ -39,20 +43,21 @@ event_enum! { events: [ "m.room.aliases", "m.room.avatar", - // "m.room.canonical_alias", - // "m.room.create", - // "m.room.encryption", - // "m.room.guest_access", - // "m.room.history_visibility", - // "m.room.join_rules", + "m.room.canonical_alias", + "m.room.create", + "m.room.encryption", + "m.room.guest_access", + "m.room.history_visibility", + "m.room.join_rules", "m.room.member", - // "m.room.name", - // "m.room.pinned_events", - // "m.room.power_levels", - // "m.room.server_acl", - // "m.room.third_party_invite", - // "m.room.tombstone", - // "m.room.topic", + "m.room.name", + "m.room.pinned_events", + "m.room.power_levels", + "m.room.redaction", + "m.room.server_acl", + "m.room.third_party_invite", + "m.room.tombstone", + "m.room.topic", ] } @@ -62,14 +67,14 @@ event_enum! { events: [ "m.dummy", "m.room_key", - //"m.room_key_request", - //"m.forwarded_room_key", - //"m.key.verification.request", + "m.room_key_request", + "m.forwarded_room_key", + "m.key.verification.request", "m.key.verification.start", - //"m.key.verification.cancel", - //"m.key.verification.accept", - //"m.key.verification.key", - //"m.key.verification.mac", - //"m.room.encrypted", + "m.key.verification.cancel", + "m.key.verification.accept", + "m.key.verification.key", + "m.key.verification.mac", + "m.room.encrypted", ] } diff --git a/src/forwarded_room_key.rs b/src/forwarded_room_key.rs index bcb20340..c8edd0b8 100644 --- a/src/forwarded_room_key.rs +++ b/src/forwarded_room_key.rs @@ -1,47 +1,48 @@ //! Types for the *m.forwarded_room_key* event. -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; use ruma_identifiers::RoomId; +use serde::{Deserialize, Serialize}; use super::Algorithm; +use crate::BasicEvent; -ruma_event! { - /// This event type is used to forward keys for end-to-end encryption. +/// This event type is used to forward keys for end-to-end encryption. +/// +/// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event. +pub type ForwardedRoomKeyEvent = BasicEvent; + +/// The payload for `ForwardedRoomKeyEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.forwarded_room_key")] +pub struct ForwardedRoomKeyEventContent { + /// The encryption algorithm the key in this event is to be used with. + pub algorithm: Algorithm, + + /// The room where the key is used. + pub room_id: RoomId, + + /// The Curve25519 key of the device which initiated the session originally. + pub sender_key: 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 Ed25519 key of the device which initiated the session originally. /// - /// Typically it is encrypted as an *m.room.encrypted* event, then sent as a to-device event. - ForwardedRoomKeyEvent { - kind: Event, - event_type: "m.forwarded_room_key", - content: { - /// The encryption algorithm the key in this event is to be used with. - pub algorithm: Algorithm, + /// It is "claimed" because the receiving device has no way to tell that the original + /// room_key actually came from a device which owns the private part of this key unless + /// they have done device verification. + pub sender_claimed_ed25519_key: String, - /// The room where the key is used. - pub room_id: RoomId, - - /// The Curve25519 key of the device which initiated the session originally. - pub sender_key: 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 Ed25519 key of the device which initiated the session originally. - /// - /// It is "claimed" because the receiving device has no way to tell that the original - /// room_key actually came from a device which owns the private part of this key unless - /// they have done device verification. - pub sender_claimed_ed25519_key: String, - - /// Chain of Curve25519 keys. - /// - /// It starts out empty, but each time the key is forwarded to another device, the - /// previous sender in the chain is added to the end of the list. For example, if the - /// key is forwarded from A to B to C, this field is empty between A and B, and contains - /// A's Curve25519 key between B and C. - pub forwarding_curve25519_key_chain: Vec, - }, - } + /// Chain of Curve25519 keys. + /// + /// It starts out empty, but each time the key is forwarded to another device, the + /// previous sender in the chain is added to the end of the list. For example, if the + /// key is forwarded from A to B to C, this field is empty between A and B, and contains + /// A's Curve25519 key between B and C. + pub forwarding_curve25519_key_chain: Vec, } diff --git a/src/fully_read.rs b/src/fully_read.rs index 69e10c16..27e77db9 100644 --- a/src/fully_read.rs +++ b/src/fully_read.rs @@ -1,26 +1,21 @@ //! Types for the *m.fully_read* event. -use ruma_events_macros::ruma_event; -use ruma_identifiers::{EventId, RoomId}; +use ruma_events_macros::EphemeralRoomEventContent; +use ruma_identifiers::EventId; +use serde::{Deserialize, Serialize}; -ruma_event! { - /// The current location of the user's read marker in a room. - /// - /// This event appears in the user's room account data for the room the marker is applicable - /// for. - FullyReadEvent { - kind: Event, - event_type: "m.fully_read", - fields: { - /// The unique identifier for the room associated with this event. - /// - /// `None` if the room is known through other means (such as this even being part of an - /// event list scoped to a room in a `/sync` response) - pub room_id: Option, - }, - content: { - /// The event the user's read marker is located at in the room. - pub event_id: EventId, - }, - } +use crate::EphemeralRoomEvent; + +/// The current location of the user's read marker in a room. +/// +/// This event appears in the user's room account data for the room the marker is applicable +/// for. +pub type FullyReadEvent = EphemeralRoomEvent; + +/// The payload for `FullyReadEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, EphemeralRoomEventContent)] +#[ruma_event(type = "m.fully_read")] +pub struct FullyReadEventContent { + /// The event the user's read marker is located at in the room. + pub event_id: EventId, } diff --git a/src/key/verification/accept.rs b/src/key/verification/accept.rs index e2e5aa4a..f9a6c8be 100644 --- a/src/key/verification/accept.rs +++ b/src/key/verification/accept.rs @@ -1,51 +1,52 @@ //! Types for the *m.key.verification.accept* event. -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; +use serde::{Deserialize, Serialize}; use super::{ HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, VerificationMethod, }; +use crate::BasicEvent; -ruma_event! { - /// Accepts a previously sent *m.key.verification.start* messge. +/// Accepts a previously sent *m.key.verification.start* message. +/// +/// Typically sent as a to-device event. +pub type AcceptEvent = BasicEvent; + +/// The payload for `AcceptEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.key.verification.accept")] +pub struct AcceptEventContent { + /// An opaque identifier for the verification process. /// - /// Typically sent as a to-device event. - AcceptEvent { - kind: Event, - event_type: "m.key.verification.accept", - content: { - /// An opaque identifier for the verification process. - /// - /// Must be the same as the one used for the *m.key.verification.start* message. - pub transaction_id: String, + /// Must be the same as the one used for the *m.key.verification.start* message. + pub transaction_id: String, - /// The verification method to use. - /// - /// Must be `m.sas.v1`. - pub method: VerificationMethod, + /// The verification method to use. + /// + /// Must be `m.sas.v1`. + pub method: VerificationMethod, - /// The key agreement protocol the device is choosing to use, out of the options in the - /// *m.key.verification.start* message. - pub key_agreement_protocol: KeyAgreementProtocol, + /// The key agreement protocol the device is choosing to use, out of the options in the + /// *m.key.verification.start* message. + pub key_agreement_protocol: KeyAgreementProtocol, - /// The hash method the device is choosing to use, out of the options in the - /// *m.key.verification.start* message. - pub hash: HashAlgorithm, + /// The hash method the device is choosing to use, out of the options in the + /// *m.key.verification.start* message. + pub hash: HashAlgorithm, - /// The message authentication code the device is choosing to use, out of the options in the - /// *m.key.verification.start* message. - pub message_authentication_code: MessageAuthenticationCode, + /// The message authentication code the device is choosing to use, out of the options in the + /// *m.key.verification.start* message. + pub message_authentication_code: MessageAuthenticationCode, - /// The SAS methods both devices involved in the verification process understand. - /// - /// Must be a subset of the options in the *m.key.verification.start* message. - pub short_authentication_string: Vec, + /// The SAS methods both devices involved in the verification process understand. + /// + /// Must be a subset of the options in the *m.key.verification.start* message. + pub short_authentication_string: Vec, - /// The hash (encoded as unpadded base64) of the concatenation of the device's ephemeral public - /// key (encoded as unpadded base64) and the canonical JSON representation of the - /// *m.key.verification.start* message. - pub commitment: String, - }, - } + /// The hash (encoded as unpadded base64) of the concatenation of the device's ephemeral public + /// key (encoded as unpadded base64) and the canonical JSON representation of the + /// *m.key.verification.start* message. + pub commitment: String, } diff --git a/src/key/verification/cancel.rs b/src/key/verification/cancel.rs index 9c9a0283..1847d786 100644 --- a/src/key/verification/cancel.rs +++ b/src/key/verification/cancel.rs @@ -2,29 +2,30 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; use serde::{Deserialize, Serialize}; -ruma_event! { - /// Cancels a key verification process/request. +use crate::BasicEvent; + +/// Cancels a key verification process/request. +/// +/// Typically sent as a to-device event. +pub type CancelEvent = BasicEvent; + +/// The payload for `CancelEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.key.verification.cancel")] +pub struct CancelEventContent { + /// The opaque identifier for the verification process/request. + pub transaction_id: String, + + /// A human readable description of the `code`. /// - /// Typically sent as a to-device event. - CancelEvent { - kind: Event, - event_type: "m.key.verification.cancel", - content: { - /// The opaque identifier for the verification process/request. - pub transaction_id: String, + /// The client should only rely on this string if it does not understand the `code`. + pub reason: String, - /// A human readable description of the `code`. - /// - /// The client should only rely on this string if it does not understand the `code`. - pub reason: String, - - /// The error code for why the process/request was cancelled by the user. - pub code: CancelCode, - }, - } + /// The error code for why the process/request was cancelled by the user. + pub code: CancelCode, } /// An error code for why the process/request was cancelled by the user. diff --git a/src/key/verification/key.rs b/src/key/verification/key.rs index 807bf9ed..f4cb18ca 100644 --- a/src/key/verification/key.rs +++ b/src/key/verification/key.rs @@ -1,22 +1,24 @@ //! Types for the *m.key.verification.key* event. -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; +use serde::{Deserialize, Serialize}; -ruma_event! { - /// Sends the ephemeral public key for a device to the partner device. +use crate::BasicEvent; + +/// Sends the ephemeral public key for a device to the partner device. +/// +/// Typically sent as a to-device event. +pub type KeyEvent = BasicEvent; + +/// The payload for `KeyEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.key.verification.key")] +pub struct KeyEventContent { + /// An opaque identifier for the verification process. /// - /// Typically sent as a to-device event. - KeyEvent { - kind: Event, - event_type: "m.key.verification.key", - content: { - /// An opaque identifier for the verification process. - /// - /// Must be the same as the one used for the *m.key.verification.start* message. - pub transaction_id: String, + /// Must be the same as the one used for the *m.key.verification.start* message. + pub transaction_id: String, - /// The device's ephemeral public key, encoded as unpadded Base64. - pub key: String, - }, - } + /// The device's ephemeral public key, encoded as unpadded Base64. + pub key: String, } diff --git a/src/key/verification/mac.rs b/src/key/verification/mac.rs index 488d2d3f..98b90c53 100644 --- a/src/key/verification/mac.rs +++ b/src/key/verification/mac.rs @@ -2,29 +2,31 @@ use std::collections::BTreeMap; -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; +use serde::{Deserialize, Serialize}; -ruma_event! { - /// Sends the MAC of a device's key to the partner device. +use crate::BasicEvent; + +/// Sends the MAC of a device's key to the partner device. +/// +/// Typically sent as a to-device event. +pub type MacEvent = BasicEvent; + +/// The payload for `MacEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.key.verification.mac")] +pub struct MacEventContent { + /// An opaque identifier for the verification process. /// - /// Typically sent as a to-device event. - MacEvent { - kind: Event, - event_type: "m.key.verification.mac", - content: { - /// An opaque identifier for the verification process. - /// - /// Must be the same as the one used for the *m.key.verification.start* message. - pub transaction_id: String, + /// Must be the same as the one used for the *m.key.verification.start* message. + pub transaction_id: String, - /// A map of the key ID to the MAC of the key, using the algorithm in the verification process. - /// - /// The MAC is encoded as unpadded Base64. - pub mac: BTreeMap, + /// A map of the key ID to the MAC of the key, using the algorithm in the verification process. + /// + /// The MAC is encoded as unpadded Base64. + pub mac: BTreeMap, - /// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded - /// as unpadded Base64. - pub keys: String, - }, - } + /// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded + /// as unpadded Base64. + pub keys: String, } diff --git a/src/key/verification/request.rs b/src/key/verification/request.rs index 34bc7814..14ddea75 100644 --- a/src/key/verification/request.rs +++ b/src/key/verification/request.rs @@ -2,36 +2,37 @@ use std::time::SystemTime; -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; use ruma_identifiers::DeviceId; +use serde::{Deserialize, Serialize}; use super::VerificationMethod; +use crate::BasicEvent; -ruma_event! { - /// Requests a key verification with another user's devices. +/// Requests a key verification with another user's devices. +/// +/// Typically sent as a to-device event. +pub type RequestEvent = BasicEvent; + +/// The payload for `RequestEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.key.verification.request")] +pub struct RequestEventContent { + /// The device ID which is initiating the request. + pub from_device: DeviceId, + + /// An opaque identifier for the verification request. /// - /// Typically sent as a to-device event. - RequestEvent { - kind: Event, - event_type: "m.key.verification.request", - content: { - /// The device ID which is initiating the request. - pub from_device: DeviceId, + /// Must be unique with respect to the devices involved. + pub transaction_id: String, - /// An opaque identifier for the verification request. - /// - /// Must be unique with respect to the devices involved. - pub transaction_id: String, + /// The verification methods supported by the sender. + pub methods: Vec, - /// The verification methods supported by the sender. - pub methods: Vec, - - /// The time in milliseconds for when the request was made. - /// - /// If the request is in the future by more than 5 minutes or more than 10 minutes in - /// the past, the message should be ignored by the receiver. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub timestamp: SystemTime, - }, - } + /// The time in milliseconds for when the request was made. + /// + /// If the request is in the future by more than 5 minutes or more than 10 minutes in + /// the past, the message should be ignored by the receiver. + #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] + pub timestamp: SystemTime, } diff --git a/src/push_rules.rs b/src/push_rules.rs index 85aa831c..908f4349 100644 --- a/src/push_rules.rs +++ b/src/push_rules.rs @@ -24,7 +24,7 @@ pub use ruma_common::push::Action; /// room, or by default. The push ruleset contains the entire set of scopes and rules. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Ruleset { - /// These rules configure behaviour for (unencrypted) messages that match certain patterns. + /// These rules configure behavior for (unencrypted) messages that match certain patterns. pub content: Vec, /// These user-configured rules are given the highest priority. @@ -34,10 +34,10 @@ pub struct Ruleset { #[serde(rename = "override")] pub override_: Vec, - /// These rules change the behaviour of all messages for a given room. + /// These rules change the behavior of all messages for a given room. pub room: Vec, - /// These rules configure notification behaviour for messages from a specific Matrix user ID. + /// These rules configure notification behavior for messages from a specific Matrix user ID. pub sender: Vec, /// These rules are identical to override rules, but have a lower priority than `content`, diff --git a/src/room/canonical_alias.rs b/src/room/canonical_alias.rs index 7aaf3dcc..bdf74747 100644 --- a/src/room/canonical_alias.rs +++ b/src/room/canonical_alias.rs @@ -4,7 +4,12 @@ use ruma_events_macros::StateEventContent; use ruma_identifiers::RoomAliasId; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// Informs the room as to which alias is the canonical one. +pub type CanonicalAliasEvent = StateEvent; + +/// The payload for `CanonicalAliasEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.canonical_alias")] pub struct CanonicalAliasEventContent { diff --git a/src/room/create.rs b/src/room/create.rs index 765f2d51..7895e9cd 100644 --- a/src/room/create.rs +++ b/src/room/create.rs @@ -6,8 +6,13 @@ use ruma_events_macros::StateEventContent; use ruma_identifiers::{EventId, RoomId, RoomVersionId, UserId}; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// This is the first event in a room and cannot be changed. It acts as the root of all other /// events. +pub type CreateEvent = StateEvent; + +/// The payload for `CreateEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.create")] pub struct CreateEventContent { diff --git a/src/room/encrypted.rs b/src/room/encrypted.rs index 5f07e57a..840d5c5a 100644 --- a/src/room/encrypted.rs +++ b/src/room/encrypted.rs @@ -1,16 +1,21 @@ //! Types for the *m.room.encrypted* event. -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use js_int::UInt; -use ruma_identifiers::{DeviceId, EventId, RoomId, UserId}; +use ruma_events_macros::StateEventContent; +use ruma_identifiers::DeviceId; use serde::{Deserialize, Serialize}; -use crate::UnsignedData; +use crate::StateEvent; + +/// An event that defines how messages sent in this room should be encrypted. +pub type EncryptedEvent = StateEvent; /// The payload for `EncryptedEvent`. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[non_exhaustive] +#[ruma_event(type = "m.room.encrypted")] #[serde(tag = "algorithm")] pub enum EncryptedEventContent { /// An event encrypted with *m.olm.v1.curve25519-aes-sha2*. diff --git a/src/room/encryption.rs b/src/room/encryption.rs index fa918040..b92df634 100644 --- a/src/room/encryption.rs +++ b/src/room/encryption.rs @@ -4,9 +4,12 @@ use js_int::UInt; use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use crate::Algorithm; +use crate::{Algorithm, StateEvent}; /// Defines how messages sent in this room should be encrypted. +pub type EncryptionEvent = StateEvent; + +/// The payload for `EncryptionEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.encryption")] pub struct EncryptionEventContent { diff --git a/src/room/guest_access.rs b/src/room/guest_access.rs index c8fd8341..ed6d72ae 100644 --- a/src/room/guest_access.rs +++ b/src/room/guest_access.rs @@ -4,10 +4,15 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; use strum::{Display, EnumString}; +use crate::StateEvent; + /// Controls whether guest users are allowed to join rooms. /// /// This event controls whether guest users are allowed to join rooms. If this event is absent, /// servers should act as if it is present and has the value `GuestAccess::Forbidden`. +pub type GuestAccessEvent = StateEvent; + +/// The payload for `GuestAccessEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.guest_access")] pub struct GuestAccessEventContent { diff --git a/src/room/history_visibility.rs b/src/room/history_visibility.rs index 2e8da039..e518f54c 100644 --- a/src/room/history_visibility.rs +++ b/src/room/history_visibility.rs @@ -4,8 +4,13 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; use strum::{Display, EnumString}; +use crate::StateEvent; + /// This event controls whether a member of a room can see the events that happened in a room /// from before they joined. +pub type HistoryVisibilityEvent = StateEvent; + +/// The payload for `HistoryVisibilityEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.history_visibility")] pub struct HistoryVisibilityEventContent { diff --git a/src/room/join_rules.rs b/src/room/join_rules.rs index 9ae7ee25..1df547dd 100644 --- a/src/room/join_rules.rs +++ b/src/room/join_rules.rs @@ -4,7 +4,12 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; use strum::{Display, EnumString}; +use crate::StateEvent; + /// Describes how users are allowed to join the room. +pub type JoinRulesEvent = StateEvent; + +/// The payload for `JoinRulesEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.join_rules")] pub struct JoinRulesEventContent { diff --git a/src/room/name.rs b/src/room/name.rs index 5c86a09a..54d07535 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -3,7 +3,10 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use crate::InvalidInput; +use crate::{InvalidInput, StateEvent}; + +/// The room name is a human-friendly string designed to be displayed to the end-user. +pub type NameEvent = StateEvent; /// The payload for `NameEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] diff --git a/src/room/pinned_events.rs b/src/room/pinned_events.rs index 941f8291..2cd55177 100644 --- a/src/room/pinned_events.rs +++ b/src/room/pinned_events.rs @@ -4,7 +4,12 @@ use ruma_events_macros::StateEventContent; use ruma_identifiers::EventId; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// Used to "pin" particular events in a room for other participants to review later. +pub type PinnedEventsEvent = StateEvent; + +/// The payload for `PinnedEventsEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.pinned_events")] pub struct PinnedEventsEventContent { diff --git a/src/room/power_levels.rs b/src/room/power_levels.rs index 6fc45485..8efacf45 100644 --- a/src/room/power_levels.rs +++ b/src/room/power_levels.rs @@ -7,9 +7,12 @@ use ruma_events_macros::StateEventContent; use ruma_identifiers::UserId; use serde::{Deserialize, Serialize}; -use crate::EventType; +use crate::{EventType, StateEvent}; /// Defines the power levels (privileges) of users in the room. +pub type PowerLevelsEvent = StateEvent; + +/// The payload for `PowerLevelsEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.power_levels")] pub struct PowerLevelsEventContent { diff --git a/src/room/server_acl.rs b/src/room/server_acl.rs index 965dd255..70d446fa 100644 --- a/src/room/server_acl.rs +++ b/src/room/server_acl.rs @@ -3,7 +3,12 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// An event to indicate which servers are permitted to participate in the room. +pub type ServerAclEvent = StateEvent; + +/// The payload for `ServerAclEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.server_acl")] pub struct ServerAclEventContent { diff --git a/src/room/third_party_invite.rs b/src/room/third_party_invite.rs index fbac559b..f32657f7 100644 --- a/src/room/third_party_invite.rs +++ b/src/room/third_party_invite.rs @@ -3,11 +3,16 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// An invitation to a room issued to a third party identifier, rather than a matrix user ID. /// /// Acts as an *m.room.member* invite event, where there isn't a target user_id to invite. This /// event contains a token and a public key whose private key must be used to sign the token. /// Any user who can present that signature may use this invitation to join the target room. +pub type ThirdPartyInviteEvent = StateEvent; + +/// The payload for `ThirdPartyInviteEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.third_party_invite")] pub struct ThirdPartyInviteEventContent { diff --git a/src/room/tombstone.rs b/src/room/tombstone.rs index 0531cd53..2ab25eef 100644 --- a/src/room/tombstone.rs +++ b/src/room/tombstone.rs @@ -4,8 +4,13 @@ use ruma_events_macros::StateEventContent; use ruma_identifiers::RoomId; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// A state event signifying that a room has been upgraded to a different room version, and that /// clients should go there. +pub type TombstoneEvent = StateEvent; + +/// The payload for `TombstoneEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.tombstone")] pub struct TombstoneEventContent { diff --git a/src/room/topic.rs b/src/room/topic.rs index 0b1e3ae0..434f66f9 100644 --- a/src/room/topic.rs +++ b/src/room/topic.rs @@ -3,7 +3,12 @@ use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; +use crate::StateEvent; + /// A topic is a short message detailing what is currently being discussed in the room. +pub type TopicEvent = StateEvent; + +/// The payload for `TopicEvent`. #[derive(Clone, Debug, Deserialize, Serialize, StateEventContent)] #[ruma_event(type = "m.room.topic")] pub struct TopicEventContent { diff --git a/src/room_key_request.rs b/src/room_key_request.rs index 45be72a8..b8510965 100644 --- a/src/room_key_request.rs +++ b/src/room_key_request.rs @@ -1,38 +1,38 @@ //! Types for the *m.room_key_request* event. -use ruma_events_macros::ruma_event; +use ruma_events_macros::BasicEventContent; use ruma_identifiers::{DeviceId, RoomId}; use serde::{Deserialize, Serialize}; use strum::{Display, EnumString}; use super::Algorithm; +use crate::BasicEvent; -ruma_event! { - /// This event type is used to request keys for end-to-end encryption. +/// This event type is used to request keys for end-to-end encryption. +/// +/// It is sent as an unencrypted to-device event. +pub type RoomKeyRequestEvent = BasicEvent; + +/// The payload for `RoomKeyRequestEvent`. +#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] +#[ruma_event(type = "m.room_key_request")] +pub struct RoomKeyRequestEventContent { + /// Whether this is a new key request or a cancellation of a previous request. + pub action: Action, + + /// Information about the requested key. /// - /// It is sent as an unencrypted to-device event. - RoomKeyRequestEvent { - kind: Event, - event_type: "m.room_key_request", - content: { - /// Whether this is a new key request or a cancellation of a previous request. - pub action: Action, + /// Required when action is `request`. + pub body: Option, - /// Information about the requested key. - /// - /// Required when action is `request`. - pub body: Option, + /// ID of the device requesting the key. + pub requesting_device_id: DeviceId, - /// ID of the device requesting the key. - pub requesting_device_id: DeviceId, - - /// A random string uniquely identifying the request for a key. - /// - /// If the key is requested multiple times, it should be reused. It should also reused - /// in order to cancel a request. - pub request_id: String, - }, - } + /// A random string uniquely identifying the request for a key. + /// + /// If the key is requested multiple times, it should be reused. It should also reused + /// in order to cancel a request. + pub request_id: String, } /// A new key request or a cancellation of a previous request. diff --git a/src/tag.rs b/src/tag.rs index ffe91e29..3b396445 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -2,10 +2,11 @@ use std::collections::BTreeMap; -use crate::BasicEvent; use ruma_events_macros::BasicEventContent; use serde::{Deserialize, Serialize}; +use crate::BasicEvent; + /// Informs the client of tags on a room. pub type TagEvent = BasicEvent;