diff --git a/src/direct.rs b/src/direct.rs index aed07e11..5cb38d6c 100644 --- a/src/direct.rs +++ b/src/direct.rs @@ -37,9 +37,7 @@ mod tests { content.insert(alice.clone(), room.clone()); - let event = DirectEvent { - content, - }; + let event = DirectEvent { content }; assert_eq!( to_string(&event).unwrap(), diff --git a/src/dummy.rs b/src/dummy.rs index a58c863e..9a5fee29 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -29,10 +29,8 @@ mod tests { use super::{DummyEvent, Empty}; #[test] - fn serialization() { - let dummy_event = DummyEvent { - content: Empty, - }; + fn serialization() { + let dummy_event = DummyEvent { content: Empty }; let actual = serde_json::to_string(&dummy_event).unwrap(); let expected = r#"{"content":{},"type":"m.dummy"}"#; diff --git a/src/ignored_user_list.rs b/src/ignored_user_list.rs index 2db1576a..23f53025 100644 --- a/src/ignored_user_list.rs +++ b/src/ignored_user_list.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use ruma_identifiers::UserId; use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; -use crate::{Empty, Event}; +use crate::{Empty, Event, EventType}; /// A list of users to ignore. #[derive(Clone, Debug, PartialEq)] @@ -37,7 +37,7 @@ impl IgnoredUserListEvent { impl Serialize for IgnoredUserListEvent { fn serialize(&self, serializer: S) -> Result where - S: Serializer + S: Serializer, { let mut state = serializer.serialize_struct("IgnoredUserListEvent", 2)?; @@ -49,9 +49,6 @@ impl Serialize for IgnoredUserListEvent { } impl crate::Event for IgnoredUserListEvent { - /// The type of the event. - const EVENT_TYPE: crate::EventType = crate::EventType::IgnoredUserList; - /// The type of this event's `content` field. type Content = IgnoredUserListEventContent; @@ -59,12 +56,17 @@ impl crate::Event for IgnoredUserListEvent { fn content(&self) -> &Self::Content { &self.content } + + /// The type of the event. + fn event_type(&self) -> EventType { + EventType::IgnoredUserList + } } impl Serialize for IgnoredUserListEventContent { fn serialize(&self, serializer: S) -> Result where - S: Serializer + S: Serializer, { let mut map = HashMap::new(); @@ -72,17 +74,15 @@ impl Serialize for IgnoredUserListEventContent { map.insert(user_id.clone(), Empty); } - let raw = raw::IgnoredUserListEventContent { - ignored_users: map, - }; + let raw = raw::IgnoredUserListEventContent { ignored_users: map }; raw.serialize(serializer) } } mod raw { - use crate::Empty; use super::*; + use crate::Empty; /// A list of users to ignore. #[derive(Clone, Debug, Deserialize)] diff --git a/src/key/verification.rs b/src/key/verification.rs index 0d44031e..636450b0 100644 --- a/src/key/verification.rs +++ b/src/key/verification.rs @@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize}; pub mod accept; pub mod cancel; pub mod key; -pub mod mac; +// pub mod mac; pub mod request; -pub mod start; +// pub mod start; /// A hash algorithm. #[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] diff --git a/src/key/verification/accept.rs b/src/key/verification/accept.rs index 74fdd662..140a42b9 100644 --- a/src/key/verification/accept.rs +++ b/src/key/verification/accept.rs @@ -1,51 +1,51 @@ //! Types for the *m.key.verification.accept* event. -use serde::{Deserialize, Serialize}; +use ruma_events_macros::ruma_event; use super::{ HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, VerificationMethod, }; -event! { +ruma_event! { /// Accepts a previously sent *m.key.verification.start* messge. /// /// Typically sent as a to-device event. - pub struct AcceptEvent(AcceptEventContent) {} -} - -/// The payload of an *m.key.verification.accept* event. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct AcceptEventContent { - /// 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, - - /// 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 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 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, + AcceptEvent { + kind: Event, + event_type: KeyVerificationAccept, + 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, + + /// 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 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 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, + }, + } } diff --git a/src/key/verification/cancel.rs b/src/key/verification/cancel.rs index adb73c59..ee462a8b 100644 --- a/src/key/verification/cancel.rs +++ b/src/key/verification/cancel.rs @@ -2,31 +2,32 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; +use ruma_events_macros::ruma_event; use serde::{ de::{Error as SerdeError, Visitor}, Deserialize, Deserializer, Serialize, Serializer, }; -event! { +ruma_event! { /// Cancels a key verification process/request. /// /// Typically sent as a to-device event. - pub struct CancelEvent(CancelEventContent) {} -} + CancelEvent { + kind: Event, + event_type: KeyVerificationCancel, + content: { + /// The opaque identifier for the verification process/request. + pub transaction_id: String, -/// The payload of an *m.key.verification.cancel* event. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct CancelEventContent { - /// The opaque identifier for the verification process/request. - pub transaction_id: 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, - /// 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 1947d687..9c27e133 100644 --- a/src/key/verification/key.rs +++ b/src/key/verification/key.rs @@ -1,22 +1,22 @@ //! Types for the *m.key.verification.key* event. -use serde::{Deserialize, Serialize}; +use ruma_events_macros::ruma_event; -event! { +ruma_event! { /// Sends the ephemeral public key for a device to the partner device. /// /// Typically sent as a to-device event. - pub struct KeyEvent(KeyEventContent) {} -} + KeyEvent { + kind: Event, + event_type: KeyVerificationKey, + 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, -/// The payload of an *m.key.verification.key* event. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct KeyEventContent { - /// 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, - - /// 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 8a8b0df1..27927e31 100644 --- a/src/key/verification/mac.rs +++ b/src/key/verification/mac.rs @@ -1,29 +1,29 @@ //! Types for the *m.key.verification.mac* event. +use ruma_events_macros::ruma_event; use ruma_signatures::SignatureSet; -use serde::{Deserialize, Serialize}; -event! { +ruma_event! { /// Sends the MAC of a device's key to the partner device. /// /// Typically sent as a to-device event. - pub struct MacEvent(MacEventContent) {} -} - -/// The payload for an *m.key.verification.mac* event. -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct MacEventContent { - /// 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, - - /// 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: SignatureSet, - - /// The MAC of the comma-separated, sorted, list of key IDs given in the `mac` property, encoded - /// as unpadded Base64. - pub keys: String, + MacEvent { + kind: Event, + event_type: KeyVerificationMac, + 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, + + /// 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: SignatureSet, + + /// 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 513ad1fb..778154ad 100644 --- a/src/key/verification/request.rs +++ b/src/key/verification/request.rs @@ -1,35 +1,35 @@ //! Types for the *m.key.verification.request* event. use js_int::UInt; +use ruma_events_macros::ruma_event; use ruma_identifiers::DeviceId; -use serde::{Deserialize, Serialize}; use super::VerificationMethod; -event! { +ruma_event! { /// Requests a key verification with another user's devices. /// /// Typically sent as a to-device event. - pub struct RequestEvent(RequestEventContent) {} -} - -/// The payload of an *m.key.verification.request* event. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct RequestEventContent { - /// The device ID which is initiating the request. - pub from_device: DeviceId, - - /// 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 POSIX timestamp 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. - pub timestamp: UInt, + RequestEvent { + kind: Event, + event_type: KeyVerificationRequest, + content: { + /// The device ID which is initiating the request. + pub from_device: DeviceId, + + /// 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 POSIX timestamp 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. + pub timestamp: UInt, + }, + } } diff --git a/src/lib.rs b/src/lib.rs index e6260522..cedcf5ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,6 +113,10 @@ use serde::{ }; use serde_json::Value; +pub use custom::CustomEvent; +pub use custom_room::CustomRoomEvent; +pub use custom_state::CustomStateEvent; + #[macro_use] mod macros; @@ -127,12 +131,12 @@ pub mod dummy; pub mod forwarded_room_key; pub mod fully_read; pub mod ignored_user_list; -// pub mod key; +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; @@ -223,7 +227,7 @@ pub struct Empty; impl Serialize for Empty { fn serialize(&self, serializer: S) -> Result where - S: Serializer + S: Serializer, { serializer.serialize_map(Some(0))?.end() } @@ -232,11 +236,11 @@ impl Serialize for Empty { impl<'de> Deserialize<'de> for Empty { fn deserialize(deserializer: D) -> Result where - D: Deserializer<'de> + D: Deserializer<'de>, { struct EmptyMapVisitor; - impl <'de> Visitor<'de> for EmptyMapVisitor { + impl<'de> Visitor<'de> for EmptyMapVisitor { type Value = Empty; fn expecting(&self, f: &mut Formatter) -> FmtResult { @@ -245,7 +249,7 @@ impl<'de> Deserialize<'de> for Empty { fn visit_map(self, _map: A) -> Result where - A: MapAccess<'de> + A: MapAccess<'de>, { Ok(Empty) } @@ -401,9 +405,6 @@ pub trait Event where Self: Debug + Serialize, { - /// The type of the event. - const EVENT_TYPE: EventType; - /// The type of this event's `content` field. type Content: Debug + Serialize; @@ -411,9 +412,7 @@ where fn content(&self) -> &Self::Content; /// The type of the event. - fn event_type(&self) -> EventType { - Self::EVENT_TYPE - } + fn event_type(&self) -> EventType; } /// An event within the context of a room. @@ -447,20 +446,56 @@ pub trait StateEvent: RoomEvent { fn state_key(&self) -> &str; } -// event! { -// /// A custom basic event not covered by the Matrix specification. -// pub struct CustomEvent(Value) {} -// } +mod custom { + use ruma_events_macros::ruma_event; + use serde_json::Value; -// room_event! { -// /// A custom room event not covered by the Matrix specification. -// pub struct CustomRoomEvent(Value) {} -// } + ruma_event! { + /// A custom basic event not covered by the Matrix specification. + CustomEvent { + kind: Event, + event_type: Custom, + content_type_alias: { + /// The payload for `CustomEvent`. + Value + }, + } + } +} -// state_event! { -// /// A custom state event not covered by the Matrix specification. -// pub struct CustomStateEvent(Value) {} -// } +mod custom_room { + use ruma_events_macros::ruma_event; + use serde_json::Value; + + ruma_event! { + /// A custom room event not covered by the Matrix specification. + CustomRoomEvent { + kind: RoomEvent, + event_type: Custom, + content_type_alias: { + /// The payload for `CustomRoomEvent`. + Value + }, + } + } +} + +mod custom_state { + use ruma_events_macros::ruma_event; + use serde_json::Value; + + ruma_event! { + /// A custom state event not covered by the Matrix specification. + CustomStateEvent { + kind: StateEvent, + event_type: Custom, + content_type_alias: { + /// The payload for `CustomStateEvent`. + Value + }, + } + } +} impl Display for EventType { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { diff --git a/src/room_key.rs b/src/room_key.rs index cfebcd93..c876c704 100644 --- a/src/room_key.rs +++ b/src/room_key.rs @@ -1,31 +1,31 @@ //! Types for the *m.room_key* event. +use ruma_events_macros::ruma_event; use ruma_identifiers::RoomId; -use serde::{Deserialize, Serialize}; use super::Algorithm; -event! { +ruma_event! { /// 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. - pub struct RoomKeyEvent(RoomKeyEventContent) {} -} - -/// The payload of an *m.room_key* event. -#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] -pub struct RoomKeyEventContent { - /// The encryption algorithm the key in this event is to be used with. - /// - /// Must be `m.megolm.v1.aes-sha2`. - pub algorithm: Algorithm, - - /// 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 key to be exchanged. - pub session_key: String, + RoomKeyEvent { + kind: Event, + event_type: RoomKey, + content: { + /// The encryption algorithm the key in this event is to be used with. + /// + /// Must be `m.megolm.v1.aes-sha2`. + pub algorithm: Algorithm, + + /// 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 key to be exchanged. + pub session_key: String, + } + } }