From 0d039a4ebb9c3acf0e844d6260a6d58fd395d883 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 17 May 2021 00:17:54 +0200 Subject: [PATCH] events: Make remaining types in key::* non-exhaustive --- .../src/key/verification/accept.rs | 22 ++++++++++++++-- .../src/key/verification/cancel.rs | 21 ++++++++++++++-- .../ruma-events/src/key/verification/done.rs | 19 ++++++++++++-- .../ruma-events/src/key/verification/key.rs | 18 ++++++++++++- .../ruma-events/src/key/verification/mac.rs | 17 +++++++++++++ .../ruma-events/src/key/verification/ready.rs | 25 +++++++++++++++++++ .../src/key/verification/request.rs | 14 +++++++++++ .../ruma-events/src/key/verification/start.rs | 20 ++++++++++++++- 8 files changed, 148 insertions(+), 8 deletions(-) diff --git a/crates/ruma-events/src/key/verification/accept.rs b/crates/ruma-events/src/key/verification/accept.rs index d5202a2f..a9c02189 100644 --- a/crates/ruma-events/src/key/verification/accept.rs +++ b/crates/ruma-events/src/key/verification/accept.rs @@ -21,12 +21,12 @@ pub type AcceptEvent = MessageEvent; /// The payload for a to-device `AcceptEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.accept", kind = ToDevice)] pub struct AcceptToDeviceEventContent { /// An opaque identifier for the verification process. /// - /// Must be the same as the one used for the *m.key.verification.start* - /// message. + /// Must be the same as the one used for the *m.key.verification.start* message. pub transaction_id: String, /// The method specific content. @@ -34,11 +34,20 @@ pub struct AcceptToDeviceEventContent { pub method: AcceptMethod, } +impl AcceptToDeviceEventContent { + /// Creates a new `AcceptToDeviceEventContent` with the given transaction ID and method-specific + /// content. + pub fn new(transaction_id: String, method: AcceptMethod) -> Self { + Self { transaction_id, method } + } +} + /// The payload for a in-room `AcceptEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[ruma_event(type = "m.key.verification.accept", kind = Message)] #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct AcceptEventContent { /// The method specific content. #[serde(flatten)] @@ -49,6 +58,15 @@ pub struct AcceptEventContent { pub relation: Relation, } +#[cfg(feature = "unstable-pre-spec")] +impl AcceptEventContent { + /// Creates a new `AcceptToDeviceEventContent` with the given method-specific content and + /// relation. + pub fn new(method: AcceptMethod, relation: Relation) -> Self { + Self { method, relation } + } +} + /// An enum representing the different method specific /// *m.key.verification.accept* content. #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/crates/ruma-events/src/key/verification/cancel.rs b/crates/ruma-events/src/key/verification/cancel.rs index e69f55db..e6396a32 100644 --- a/crates/ruma-events/src/key/verification/cancel.rs +++ b/crates/ruma-events/src/key/verification/cancel.rs @@ -16,6 +16,7 @@ pub type CancelEvent = MessageEvent; /// The payload for a to-device `CancelEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.cancel", kind = ToDevice)] pub struct CancelToDeviceEventContent { /// The opaque identifier for the verification process/request. @@ -26,15 +27,23 @@ pub struct CancelToDeviceEventContent { /// 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. + /// The error code for why the process / request was cancelled by the user. pub code: CancelCode, } +impl CancelToDeviceEventContent { + /// Creates a new `CancelToDeviceEventContent` with the given transaction ID, reason and code. + pub fn new(transaction_id: String, reason: String, code: CancelCode) -> Self { + Self { transaction_id, reason, code } + } +} + /// The payload for an in-room `CancelEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] -#[ruma_event(type = "m.key.verification.cancel", kind = Message)] #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[ruma_event(type = "m.key.verification.cancel", kind = Message)] pub struct CancelEventContent { /// A human readable description of the `code`. /// @@ -49,6 +58,14 @@ pub struct CancelEventContent { pub relation: Relation, } +#[cfg(feature = "unstable-pre-spec")] +impl CancelEventContent { + /// Creates a new `CancelEventContent` with the given reason, code and relation. + pub fn new(reason: String, code: CancelCode, relation: Relation) -> Self { + Self { reason, code, relation } + } +} + /// An error code for why the process/request was cancelled by the user. /// /// Custom error codes should use the Java package naming convention. diff --git a/crates/ruma-events/src/key/verification/done.rs b/crates/ruma-events/src/key/verification/done.rs index 7e691841..8db3faa4 100644 --- a/crates/ruma-events/src/key/verification/done.rs +++ b/crates/ruma-events/src/key/verification/done.rs @@ -12,6 +12,7 @@ pub type DoneEvent = MessageEvent; /// The payload for a to-device `m.key.verification.done` event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.done", kind = ToDevice)] pub struct DoneToDeviceEventContent { /// An opaque identifier for the verification process. @@ -20,16 +21,30 @@ pub struct DoneToDeviceEventContent { pub transaction_id: String, } +impl DoneToDeviceEventContent { + /// Creates a new `DoneToDeviceEventContent` with the given transaction ID. + pub fn new(transaction_id: String) -> Self { + Self { transaction_id } + } +} + /// The payload for a in-room `m.key.verification.done` event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.done", kind = Message)] pub struct DoneEventContent { - /// Relation signaling which verification request this event is responding - /// to. + /// Relation signaling which verification request this event is responding to. #[serde(rename = "m.relates_to")] pub relation: Relation, } +impl DoneEventContent { + /// Creates a new `DoneEventContent` with the given relation: + pub fn new(relation: Relation) -> Self { + Self { relation } + } +} + #[cfg(test)] mod tests { use matches::assert_matches; diff --git a/crates/ruma-events/src/key/verification/key.rs b/crates/ruma-events/src/key/verification/key.rs index 1a41634e..fc32263e 100644 --- a/crates/ruma-events/src/key/verification/key.rs +++ b/crates/ruma-events/src/key/verification/key.rs @@ -26,11 +26,19 @@ pub struct KeyToDeviceEventContent { pub key: String, } +impl KeyToDeviceEventContent { + /// Creates a new `KeyToDeviceEventContent` with the given transaction ID and key. + pub fn new(transaction_id: String, key: String) -> Self { + Self { transaction_id, key } + } +} + /// The payload for in-room `KeyEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] -#[ruma_event(type = "m.key.verification.key", kind = Message)] #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[ruma_event(type = "m.key.verification.key", kind = Message)] pub struct KeyEventContent { /// The device's ephemeral public key, encoded as unpadded Base64. pub key: String, @@ -39,3 +47,11 @@ pub struct KeyEventContent { #[serde(rename = "m.relates_to")] pub relation: Relation, } + +#[cfg(feature = "unstable-pre-spec")] +impl KeyEventContent { + /// Creates a new `KeyEventContent` with the given key and relation. + pub fn new(key: String, relation: Relation) -> Self { + Self { key, relation } + } +} diff --git a/crates/ruma-events/src/key/verification/mac.rs b/crates/ruma-events/src/key/verification/mac.rs index c4acc331..62bfd660 100644 --- a/crates/ruma-events/src/key/verification/mac.rs +++ b/crates/ruma-events/src/key/verification/mac.rs @@ -17,6 +17,7 @@ pub type MacEvent = MessageEvent; /// The payload for a to-device `MacEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.mac", kind = ToDevice)] pub struct MacToDeviceEventContent { /// An opaque identifier for the verification process. @@ -34,6 +35,14 @@ pub struct MacToDeviceEventContent { pub keys: String, } +impl MacToDeviceEventContent { + /// Creates a new `MacToDeviceEventContent` with the given transaction ID, key ID to MAC map and + /// key MAC. + pub fn new(transaction_id: String, mac: BTreeMap, keys: String) -> Self { + Self { transaction_id, mac, keys } + } +} + /// The payload for an in-room `MacEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[ruma_event(type = "m.key.verification.mac", kind = Message)] @@ -53,3 +62,11 @@ pub struct MacEventContent { #[serde(rename = "m.relates_to")] pub relation: Relation, } + +#[cfg(feature = "unstable-pre-spec")] +impl MacEventContent { + /// Creates a new `MacEventContent` with the given key ID to MAC map, key MAC and relation. + pub fn new(mac: BTreeMap, keys: String, relation: Relation) -> Self { + Self { mac, keys, relation } + } +} diff --git a/crates/ruma-events/src/key/verification/ready.rs b/crates/ruma-events/src/key/verification/ready.rs index 5c401c96..2b41638c 100644 --- a/crates/ruma-events/src/key/verification/ready.rs +++ b/crates/ruma-events/src/key/verification/ready.rs @@ -12,6 +12,7 @@ pub type ReadyEvent = MessageEvent; /// The payload for a to-device `m.key.verification.ready` event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.ready", kind = ToDevice)] pub struct ReadyToDeviceEventContent { /// The device ID which is initiating the request. @@ -28,8 +29,21 @@ pub struct ReadyToDeviceEventContent { pub transaction_id: String, } +impl ReadyToDeviceEventContent { + /// Creates a new `ReadyToDeviceEventContent` with the given device ID, verification methods and + /// transaction ID. + pub fn new( + from_device: DeviceIdBox, + methods: Vec, + transaction_id: String, + ) -> Self { + Self { from_device, methods, transaction_id } + } +} + /// The payload for an in-room `m.key.verification.ready` event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.ready", kind = Message)] pub struct ReadyEventContent { /// The device ID which is initiating the request. @@ -44,6 +58,17 @@ pub struct ReadyEventContent { pub relation: Relation, } +impl ReadyEventContent { + /// Creates a new `ReadyEventContent` with the given device ID, methods and relation. + pub fn new( + from_device: DeviceIdBox, + methods: Vec, + relation: Relation, + ) -> Self { + Self { from_device, methods, relation } + } +} + #[cfg(test)] mod tests { use matches::assert_matches; diff --git a/crates/ruma-events/src/key/verification/request.rs b/crates/ruma-events/src/key/verification/request.rs index 71a3caaa..a33b23db 100644 --- a/crates/ruma-events/src/key/verification/request.rs +++ b/crates/ruma-events/src/key/verification/request.rs @@ -9,6 +9,7 @@ use super::VerificationMethod; /// The payload for `RequestEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.request", kind = ToDevice)] pub struct RequestToDeviceEventContent { /// The device ID which is initiating the request. @@ -28,3 +29,16 @@ pub struct RequestToDeviceEventContent { /// the past, the message should be ignored by the receiver. pub timestamp: MilliSecondsSinceUnixEpoch, } + +impl RequestToDeviceEventContent { + /// Creates a new `RequestToDeviceEventContent` with the given device ID, transaction ID, + /// methods and timestamp. + pub fn new( + from_device: DeviceIdBox, + transaction_id: String, + methods: Vec, + timestamp: MilliSecondsSinceUnixEpoch, + ) -> Self { + Self { from_device, transaction_id, methods, timestamp } + } +} diff --git a/crates/ruma-events/src/key/verification/start.rs b/crates/ruma-events/src/key/verification/start.rs index 098551b5..60acc396 100644 --- a/crates/ruma-events/src/key/verification/start.rs +++ b/crates/ruma-events/src/key/verification/start.rs @@ -23,6 +23,7 @@ pub type StartEvent = MessageEvent; /// The payload of a to-device *m.key.verification.start* event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.start", kind = ToDevice)] pub struct StartToDeviceEventContent { /// The device ID which is initiating the process. @@ -40,11 +41,20 @@ pub struct StartToDeviceEventContent { pub method: StartMethod, } +impl StartToDeviceEventContent { + /// Creates a new `StartToDeviceEventContent` with the given device ID, transaction ID and + /// method specific content. + pub fn new(from_device: DeviceIdBox, transaction_id: String, method: StartMethod) -> Self { + Self { from_device, transaction_id, method } + } +} + /// The payload of an in-room *m.key.verification.start* event. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] -#[ruma_event(type = "m.key.verification.start", kind = Message)] #[cfg(feature = "unstable-pre-spec")] #[cfg_attr(docsrs, doc(cfg(feature = "unstable-pre-spec")))] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[ruma_event(type = "m.key.verification.start", kind = Message)] pub struct StartEventContent { /// The device ID which is initiating the process. pub from_device: DeviceIdBox, @@ -58,6 +68,14 @@ pub struct StartEventContent { pub relation: Relation, } +#[cfg(feature = "unstable-pre-spec")] +impl StartEventContent { + /// Creates a new `StartEventContent` with the given device ID, method and relation. + pub fn new(from_device: DeviceIdBox, method: StartMethod, relation: Relation) -> Self { + Self { from_device, method, relation } + } +} + /// An enum representing the different method specific /// *m.key.verification.start* content. #[derive(Clone, Debug, Deserialize, Serialize)]