From e05aad184e3a1d0571779405e92dff28e8e5161f Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Fri, 18 Jun 2021 08:21:01 -0400 Subject: [PATCH] events: Make all pub structs non_exhaustive --- .../ruma-events-macros/src/event_content.rs | 17 +++++++++++++++ crates/ruma-events-macros/src/event_enum.rs | 4 +--- crates/ruma-events/src/custom.rs | 2 ++ crates/ruma-events/src/direct.rs | 1 + crates/ruma-events/src/dummy.rs | 10 ++++++++- crates/ruma-events/src/error.rs | 1 + crates/ruma-events/src/event_kinds.rs | 2 ++ crates/ruma-events/src/forwarded_room_key.rs | 1 + .../src/key/verification/accept.rs | 2 ++ .../ruma-events/src/key/verification/key.rs | 1 + .../ruma-events/src/key/verification/mac.rs | 3 ++- .../ruma-events/src/key/verification/start.rs | 2 ++ crates/ruma-events/src/lib.rs | 19 +++++++++++++++++ crates/ruma-events/src/pdu.rs | 11 ++++++++++ crates/ruma-events/src/policy/rule/room.rs | 1 + crates/ruma-events/src/policy/rule/server.rs | 1 + crates/ruma-events/src/policy/rule/user.rs | 1 + crates/ruma-events/src/presence.rs | 1 + crates/ruma-events/src/receipt.rs | 1 + crates/ruma-events/src/relation.rs | 4 ++-- crates/ruma-events/src/room.rs | 2 ++ crates/ruma-events/src/room/aliases.rs | 19 ++++++++++++++++- crates/ruma-events/src/room/avatar.rs | 10 ++++++++- crates/ruma-events/src/room/encrypted.rs | 1 + crates/ruma-events/src/room/message.rs | 1 + crates/ruma-events/src/room/redaction.rs | 2 ++ crates/ruma-events/src/room/relationships.rs | 1 + crates/ruma-events/src/tag.rs | 1 + crates/ruma-events/tests/pdu.rs | 12 +++++++---- crates/ruma-events/tests/redacted.rs | 21 ++++++++++--------- crates/ruma-events/tests/room_message.rs | 12 +++++------ 31 files changed, 137 insertions(+), 30 deletions(-) diff --git a/crates/ruma-events-macros/src/event_content.rs b/crates/ruma-events-macros/src/event_content.rs index 319bcb2d..88f3e6e7 100644 --- a/crates/ruma-events-macros/src/event_content.rs +++ b/crates/ruma-events-macros/src/event_content.rs @@ -204,6 +204,20 @@ pub fn expand_event_content( quote! { true } }; + let initializer = if kept_redacted_fields.is_empty() { + let doc = format!("Creates an empty {}.", redacted_ident); + quote! { + impl #redacted_ident { + #[doc = #doc] + pub fn new() -> Self { + Self + } + } + } + } else { + TokenStream::new() + }; + let redacted_event_content = generate_event_content_impl(&redacted_ident, event_type, ruma_events); @@ -237,8 +251,11 @@ pub fn expand_event_content( #[doc = #doc] #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] + #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct #redacted_ident #redacted_fields + #initializer + #redacted_event_content #[automatically_derived] diff --git a/crates/ruma-events-macros/src/event_enum.rs b/crates/ruma-events-macros/src/event_enum.rs index 9aeb9ebe..ed2df496 100644 --- a/crates/ruma-events-macros/src/event_enum.rs +++ b/crates/ruma-events-macros/src/event_enum.rs @@ -638,9 +638,7 @@ fn generate_redacted_fields( if name == "unsigned" { quote! { - unsigned: #ruma_events::RedactedUnsigned { - redacted_because: Some(::std::boxed::Box::new(redaction)), - }, + unsigned: #ruma_events::RedactedUnsigned::new_because(::std::boxed::Box::new(redaction)), } } else { quote! { diff --git a/crates/ruma-events/src/custom.rs b/crates/ruma-events/src/custom.rs index 307afbff..1b4de043 100644 --- a/crates/ruma-events/src/custom.rs +++ b/crates/ruma-events/src/custom.rs @@ -15,6 +15,7 @@ use crate::{ /// A custom event's type and `content` JSON object. #[derive(Clone, Debug, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct CustomEventContent { /// The event type string. #[serde(skip)] @@ -60,6 +61,7 @@ impl StateEventContent for CustomEventContent {} /// A custom event that has been redacted. #[derive(Clone, Debug, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct RedactedCustomEventContent { // This field is marked skipped but will be present because deserialization // passes the `type` field of the JSON event to the events `EventContent::from_parts` method. diff --git a/crates/ruma-events/src/direct.rs b/crates/ruma-events/src/direct.rs index 949c4f0e..dae1200a 100644 --- a/crates/ruma-events/src/direct.rs +++ b/crates/ruma-events/src/direct.rs @@ -17,6 +17,7 @@ pub type DirectEvent = crate::GlobalAccountDataEvent; /// A mapping of `UserId`s to a list of `RoomId`s which are considered *direct* for that /// particular user. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.direct", kind = GlobalAccountData)] pub struct DirectEventContent(pub BTreeMap>); diff --git a/crates/ruma-events/src/dummy.rs b/crates/ruma-events/src/dummy.rs index 6bb2d655..74dc0f6f 100644 --- a/crates/ruma-events/src/dummy.rs +++ b/crates/ruma-events/src/dummy.rs @@ -19,10 +19,18 @@ use serde::{ /// this *m.dummy* event as the most recent event and using the keyshare request to set up the /// session. The keyshare request and *m.dummy* combination should result in the original sending /// client receiving keys over the newly established session. -#[derive(Clone, Debug, EventContent)] +#[derive(Clone, Debug, Default, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.dummy", kind = ToDevice)] pub struct DummyToDeviceEventContent; +impl DummyToDeviceEventContent { + /// Create a new `DummyToDeviceEventContent`. + pub fn new() -> Self { + Self + } +} + impl<'de> Deserialize<'de> for DummyToDeviceEventContent { fn deserialize(deserializer: D) -> Result where diff --git a/crates/ruma-events/src/error.rs b/crates/ruma-events/src/error.rs index 5909788b..d2cd1672 100644 --- a/crates/ruma-events/src/error.rs +++ b/crates/ruma-events/src/error.rs @@ -17,6 +17,7 @@ impl Error for InvalidInput {} /// An error when attempting to create a value from a string via the `FromStr` trait. #[derive(Clone, Eq, Debug, Hash, PartialEq)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct FromStrError; impl fmt::Display for FromStrError { diff --git a/crates/ruma-events/src/event_kinds.rs b/crates/ruma-events/src/event_kinds.rs index fcb15d9e..20cf1284 100644 --- a/crates/ruma-events/src/event_kinds.rs +++ b/crates/ruma-events/src/event_kinds.rs @@ -1,3 +1,5 @@ +#![allow(clippy::exhaustive_structs)] + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events_macros::Event; use ruma_identifiers::{EventId, RoomId, UserId}; diff --git a/crates/ruma-events/src/forwarded_room_key.rs b/crates/ruma-events/src/forwarded_room_key.rs index 74127e93..02c7c62a 100644 --- a/crates/ruma-events/src/forwarded_room_key.rs +++ b/crates/ruma-events/src/forwarded_room_key.rs @@ -48,6 +48,7 @@ pub struct ForwardedRoomKeyToDeviceEventContent { /// This struct will not be updated even if additional fields are added to `ConditionalPushRule` in /// a new (non-breaking) release of the Matrix specification. #[derive(Debug)] +#[allow(clippy::exhaustive_structs)] pub struct ForwardedRoomKeyToDeviceEventContentInit { /// The encryption algorithm the key in this event is to be used with. pub algorithm: EventEncryptionAlgorithm, diff --git a/crates/ruma-events/src/key/verification/accept.rs b/crates/ruma-events/src/key/verification/accept.rs index 7525d71c..a7ff2fb3 100644 --- a/crates/ruma-events/src/key/verification/accept.rs +++ b/crates/ruma-events/src/key/verification/accept.rs @@ -83,6 +83,7 @@ pub enum AcceptMethod { /// Method specific content of a unknown key verification method. #[doc(hidden)] #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct _CustomContent { /// The name of the method. pub method: String, @@ -124,6 +125,7 @@ pub struct SasV1Content { /// Mandatory initial set of fields for creating an accept `SasV1Content`. #[derive(Clone, Debug, Deserialize)] +#[allow(clippy::exhaustive_structs)] pub struct SasV1ContentInit { /// The key agreement protocol the device is choosing to use, out of the /// options in the *m.key.verification.start* message. diff --git a/crates/ruma-events/src/key/verification/key.rs b/crates/ruma-events/src/key/verification/key.rs index fc32263e..ef7d9e37 100644 --- a/crates/ruma-events/src/key/verification/key.rs +++ b/crates/ruma-events/src/key/verification/key.rs @@ -15,6 +15,7 @@ pub type KeyEvent = MessageEvent; /// The payload for a to-device `KeyEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.key.verification.key", kind = ToDevice)] pub struct KeyToDeviceEventContent { /// An opaque identifier for the verification process. diff --git a/crates/ruma-events/src/key/verification/mac.rs b/crates/ruma-events/src/key/verification/mac.rs index 62bfd660..ce148f11 100644 --- a/crates/ruma-events/src/key/verification/mac.rs +++ b/crates/ruma-events/src/key/verification/mac.rs @@ -45,9 +45,10 @@ impl MacToDeviceEventContent { /// The payload for an in-room `MacEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] -#[ruma_event(type = "m.key.verification.mac", 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.mac", kind = Message)] pub struct MacEventContent { /// A map of the key ID to the MAC of the key, using the algorithm in the verification process. /// diff --git a/crates/ruma-events/src/key/verification/start.rs b/crates/ruma-events/src/key/verification/start.rs index e797b5fc..19c180cb 100644 --- a/crates/ruma-events/src/key/verification/start.rs +++ b/crates/ruma-events/src/key/verification/start.rs @@ -100,6 +100,7 @@ pub enum StartMethod { /// Method specific content of a unknown key verification method. #[doc(hidden)] #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct _CustomContent { /// The name of the method. pub method: String, @@ -158,6 +159,7 @@ pub struct SasV1Content { /// Mandatory initial set of fields for creating an `SasV1Content`. #[derive(Clone, Debug, Deserialize)] +#[allow(clippy::exhaustive_structs)] pub struct SasV1ContentInit { /// The key agreement protocols the sending device understands. /// diff --git a/crates/ruma-events/src/lib.rs b/crates/ruma-events/src/lib.rs index 4b0b88da..99bc5f8c 100644 --- a/crates/ruma-events/src/lib.rs +++ b/crates/ruma-events/src/lib.rs @@ -195,6 +195,7 @@ pub use self::{ /// Extra information about an event that is not incorporated into the event's /// hash. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct Unsigned { /// The time in milliseconds that has elapsed since the event was sent. This /// field is generated by the local homeserver, and may be incorrect if the @@ -216,6 +217,11 @@ pub struct Unsigned { } impl Unsigned { + /// Create a new `Unsigned` with fields set to `None`. + pub fn new() -> Self { + Self::default() + } + /// Whether this unsigned data is empty (all fields are `None`). /// /// This method is used to determine whether to skip serializing the @@ -230,6 +236,7 @@ impl Unsigned { /// Extra information about a redacted event that is not incorporated into the event's /// hash. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct RedactedUnsigned { /// The event that redacted this event, if any. #[serde(skip_serializing_if = "Option::is_none")] @@ -237,6 +244,16 @@ pub struct RedactedUnsigned { } impl RedactedUnsigned { + /// Create a new `RedactedUnsigned` with field set to `None`. + pub fn new() -> Self { + Self::default() + } + + /// Create a new `RedactedUnsigned` with the given redacted because. + pub fn new_because(redacted_because: Box) -> Self { + Self { redacted_because: Some(redacted_because) } + } + /// Whether this unsigned data is empty (`redacted_because` is `None`). /// /// This method is used to determine whether to skip serializing the @@ -366,6 +383,7 @@ pub enum HasDeserializeFields { /// Helper struct to determine if the event has been redacted. #[doc(hidden)] #[derive(Debug, Deserialize)] +#[allow(clippy::exhaustive_structs)] pub struct UnsignedDeHelper { /// This is the field that signals an event has been redacted. pub redacted_because: Option, @@ -374,6 +392,7 @@ pub struct UnsignedDeHelper { /// Helper struct to determine the event kind from a `serde_json::value::RawValue`. #[doc(hidden)] #[derive(Debug, Deserialize)] +#[allow(clippy::exhaustive_structs)] pub struct EventDeHelper { /// the Matrix event type string "m.room.whatever". #[serde(rename = "type")] diff --git a/crates/ruma-events/src/pdu.rs b/crates/ruma-events/src/pdu.rs index e532bfe4..be427cb7 100644 --- a/crates/ruma-events/src/pdu.rs +++ b/crates/ruma-events/src/pdu.rs @@ -20,6 +20,7 @@ use crate::EventType; /// Enum for PDU schemas #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[serde(untagged)] pub enum Pdu { /// PDU for room versions 1 and 2. @@ -31,6 +32,7 @@ pub enum Pdu { /// A 'persistent data unit' (event) for room versions 1 and 2. #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct RoomV1Pdu { /// Event ID for the PDU. pub event_id: EventId, @@ -93,6 +95,7 @@ pub struct RoomV1Pdu { /// A 'persistent data unit' (event) for room versions 3 and beyond. #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct RoomV3Pdu { /// The room this event belongs to. pub room_id: RoomId, @@ -150,7 +153,15 @@ pub struct RoomV3Pdu { /// Content hashes of a PDU. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct EventHash { /// The SHA-256 hash. pub sha256: String, } + +impl EventHash { + /// Create a new `EventHash` with the given SHA256 hash. + pub fn new(sha256: String) -> Self { + Self { sha256 } + } +} diff --git a/crates/ruma-events/src/policy/rule/room.rs b/crates/ruma-events/src/policy/rule/room.rs index 2995800c..2a3b5b90 100644 --- a/crates/ruma-events/src/policy/rule/room.rs +++ b/crates/ruma-events/src/policy/rule/room.rs @@ -10,6 +10,7 @@ pub type RoomEvent = StateEvent; /// The payload for `RoomEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.policy.rule.room", kind = State)] pub struct RoomEventContent(pub PolicyRuleEventContent); diff --git a/crates/ruma-events/src/policy/rule/server.rs b/crates/ruma-events/src/policy/rule/server.rs index d8cd41cb..d771410c 100644 --- a/crates/ruma-events/src/policy/rule/server.rs +++ b/crates/ruma-events/src/policy/rule/server.rs @@ -10,5 +10,6 @@ pub type ServerEvent = StateEvent; /// The payload for `ServerEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.policy.rule.server", kind = State)] pub struct ServerEventContent(pub PolicyRuleEventContent); diff --git a/crates/ruma-events/src/policy/rule/user.rs b/crates/ruma-events/src/policy/rule/user.rs index f4025200..4367582d 100644 --- a/crates/ruma-events/src/policy/rule/user.rs +++ b/crates/ruma-events/src/policy/rule/user.rs @@ -10,5 +10,6 @@ pub type UserEvent = StateEvent; /// The payload for `UserEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.policy.rule.user", kind = State)] pub struct UserEventContent(pub PolicyRuleEventContent); diff --git a/crates/ruma-events/src/presence.rs b/crates/ruma-events/src/presence.rs index af706b3b..b278bf45 100644 --- a/crates/ruma-events/src/presence.rs +++ b/crates/ruma-events/src/presence.rs @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize}; /// Presence event. #[derive(Clone, Debug, Event)] +#[allow(clippy::exhaustive_structs)] pub struct PresenceEvent { /// Data specific to the event type. pub content: PresenceEventContent, diff --git a/crates/ruma-events/src/receipt.rs b/crates/ruma-events/src/receipt.rs index 1138816d..10c30ec7 100644 --- a/crates/ruma-events/src/receipt.rs +++ b/crates/ruma-events/src/receipt.rs @@ -20,6 +20,7 @@ pub type ReceiptEvent = EphemeralRoomEvent; /// A mapping of event ID to a collection of receipts for this event ID. The event ID is the ID of /// the event being acknowledged and *not* an ID for the receipt itself. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[allow(clippy::exhaustive_structs)] #[ruma_event(type = "m.receipt", kind = EphemeralRoom)] pub struct ReceiptEventContent(pub BTreeMap); diff --git a/crates/ruma-events/src/relation.rs b/crates/ruma-events/src/relation.rs index 170bcfd2..ba16f06a 100644 --- a/crates/ruma-events/src/relation.rs +++ b/crates/ruma-events/src/relation.rs @@ -55,7 +55,7 @@ pub struct AnnotationChunk { } impl AnnotationChunk { - /// Creates a new `AnnotationChunk`. + /// Creates a new `AnnotationChunk` with the given chunk and next batch token. pub fn new(chunk: Vec, next_batch: Option) -> Self { Self { chunk, next_batch } } @@ -71,7 +71,7 @@ pub struct Relations { } impl Relations { - /// Creates a new `Relations` struct. + /// Creates a new `Relations` with the given annotation. pub fn new(annotation: Option) -> Self { Self { annotation } } diff --git a/crates/ruma-events/src/room.rs b/crates/ruma-events/src/room.rs index 63ddbcb0..c35c22d6 100644 --- a/crates/ruma-events/src/room.rs +++ b/crates/ruma-events/src/room.rs @@ -135,6 +135,7 @@ pub struct EncryptedFile { /// /// This struct will not be updated even if additional fields are added to `EncryptedFile` in a new /// (non-breaking) release of the Matrix specification. +#[allow(clippy::exhaustive_structs)] pub struct EncryptedFileInit { /// The URL to the file. pub url: MxcUri, @@ -188,6 +189,7 @@ pub struct JsonWebKey { /// /// This struct will not be updated even if additional fields are added to `JsonWebKey` in a new /// (non-breaking) release of the Matrix specification. +#[allow(clippy::exhaustive_structs)] pub struct JsonWebKeyInit { /// Key type. Must be `oct`. pub kty: String, diff --git a/crates/ruma-events/src/room/aliases.rs b/crates/ruma-events/src/room/aliases.rs index d5eff14a..d86df4a2 100644 --- a/crates/ruma-events/src/room/aliases.rs +++ b/crates/ruma-events/src/room/aliases.rs @@ -49,7 +49,8 @@ impl RedactContent for AliasesEventContent { } /// An aliases event that has been redacted. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct RedactedAliasesEventContent { /// A list of room aliases. /// @@ -58,6 +59,22 @@ pub struct RedactedAliasesEventContent { pub aliases: Option>, } +impl RedactedAliasesEventContent { + /// Create a `RedactedAliasesEventContent` with the given aliases. + /// + /// This is only valid for room version 5 and below. + pub fn new_v1(aliases: Vec) -> Self { + Self { aliases: Some(aliases) } + } + + /// Create a `RedactedAliasesEventContent` with the given aliases. + /// + /// This is only valid for room version 6 and above. + pub fn new_v6() -> Self { + Self::default() + } +} + impl EventContent for RedactedAliasesEventContent { fn event_type(&self) -> &str { "m.room.aliases" diff --git a/crates/ruma-events/src/room/avatar.rs b/crates/ruma-events/src/room/avatar.rs index 81fbc4bd..ab20163a 100644 --- a/crates/ruma-events/src/room/avatar.rs +++ b/crates/ruma-events/src/room/avatar.rs @@ -55,7 +55,8 @@ impl AvatarEventContent { } /// Metadata about an image (specific to avatars). -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct ImageInfo { /// The height of the image in pixels. #[serde(rename = "h", skip_serializing_if = "Option::is_none")] @@ -91,3 +92,10 @@ pub struct ImageInfo { #[serde(skip_serializing_if = "Option::is_none")] pub blurhash: Option, } + +impl ImageInfo { + /// Create a new `ImageInfo` with all fields set to `None`. + pub fn new() -> Self { + Self::default() + } +} diff --git a/crates/ruma-events/src/room/encrypted.rs b/crates/ruma-events/src/room/encrypted.rs index 5fe65d9e..427cf25c 100644 --- a/crates/ruma-events/src/room/encrypted.rs +++ b/crates/ruma-events/src/room/encrypted.rs @@ -125,6 +125,7 @@ pub struct MegolmV1AesSha2Content { /// This struct will not be updated even if additional fields are added to `MegolmV1AesSha2Content` /// in a new (non-breaking) release of the Matrix specification. #[derive(Clone, Debug)] +#[allow(clippy::exhaustive_structs)] pub struct MegolmV1AesSha2ContentInit { /// The encrypted content of the event. pub ciphertext: String, diff --git a/crates/ruma-events/src/room/message.rs b/crates/ruma-events/src/room/message.rs index 177e236e..97f30d95 100644 --- a/crates/ruma-events/src/room/message.rs +++ b/crates/ruma-events/src/room/message.rs @@ -643,6 +643,7 @@ impl MessageFormat { /// Common message event content fields for message types that have separate plain-text and /// formatted representations. #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_structs)] pub struct FormattedBody { /// The format used in the `formatted_body`. pub format: MessageFormat, diff --git a/crates/ruma-events/src/room/redaction.rs b/crates/ruma-events/src/room/redaction.rs index d2b23234..4cde1aae 100644 --- a/crates/ruma-events/src/room/redaction.rs +++ b/crates/ruma-events/src/room/redaction.rs @@ -9,6 +9,7 @@ use crate::Unsigned; /// Redaction event. #[derive(Clone, Debug, Event)] +#[allow(clippy::exhaustive_structs)] pub struct RedactionEvent { /// Data specific to the event type. pub content: RedactionEventContent, @@ -34,6 +35,7 @@ pub struct RedactionEvent { /// Redaction event without a `room_id`. #[derive(Clone, Debug, Event)] +#[allow(clippy::exhaustive_structs)] pub struct SyncRedactionEvent { /// Data specific to the event type. pub content: RedactionEventContent, diff --git a/crates/ruma-events/src/room/relationships.rs b/crates/ruma-events/src/room/relationships.rs index d0685dba..a4775fb6 100644 --- a/crates/ruma-events/src/room/relationships.rs +++ b/crates/ruma-events/src/room/relationships.rs @@ -45,6 +45,7 @@ pub enum Relation { #[derive(Clone, Debug)] #[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 Replacement { /// The ID of the event being replacing. pub event_id: EventId, diff --git a/crates/ruma-events/src/tag.rs b/crates/ruma-events/src/tag.rs index f093d971..a0ecdc54 100644 --- a/crates/ruma-events/src/tag.rs +++ b/crates/ruma-events/src/tag.rs @@ -63,6 +63,7 @@ impl FromStr for UserTagName { /// An error returned when attempting to create a UserTagName with a string that would make it /// invalid. #[derive(Debug)] +#[allow(clippy::exhaustive_structs)] pub struct InvalidUserTagName; impl fmt::Display for InvalidUserTagName { diff --git a/crates/ruma-events/tests/pdu.rs b/crates/ruma-events/tests/pdu.rs index dfa76817..22dede0e 100644 --- a/crates/ruma-events/tests/pdu.rs +++ b/crates/ruma-events/tests/pdu.rs @@ -34,16 +34,16 @@ fn serialize_pdu_as_v1() { state_key: Some("state".into()), prev_events: vec![( event_id!("$previousevent:matrix.org"), - EventHash { sha256: "123567".into() }, + EventHash::new("123567".into()), )], depth: 2_u32.into(), auth_events: vec![( event_id!("$someauthevent:matrix.org"), - EventHash { sha256: "21389CFEDABC".into() }, + EventHash::new("21389CFEDABC".into()), )], redacts: Some(event_id!("$9654:matrix.org")), unsigned, - hashes: EventHash { sha256: "1233543bABACDEF".into() }, + hashes: EventHash::new("1233543bABACDEF".into()), signatures, }; let pdu = Pdu::RoomV1Pdu(v1_pdu); @@ -103,7 +103,7 @@ fn serialize_pdu_as_v3() { auth_events: vec![event_id!("$someauthevent:matrix.org")], redacts: Some(event_id!("$9654:matrix.org")), unsigned, - hashes: EventHash { sha256: "1233543bABACDEF".into() }, + hashes: EventHash::new("1233543bABACDEF".into()), signatures, }; let pdu_stub = Pdu::RoomV3Pdu(v3_pdu); @@ -188,6 +188,8 @@ fn deserialize_pdu_as_v1() { ); } Pdu::RoomV3Pdu(_) => panic!("Matched V3 PDU"), + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } @@ -233,5 +235,7 @@ fn deserialize_pdu_as_v3() { Pdu::RoomV3Pdu(v3_pdu) => { assert_eq!(v3_pdu.auth_events.first().unwrap(), &event_id!("$abc123:matrix.org")); } + #[cfg(not(feature = "unstable-exhaustive-types"))] + _ => unreachable!("new PDU version"), } } diff --git a/crates/ruma-events/tests/redacted.rs b/crates/ruma-events/tests/redacted.rs index 688f201c..c1e74a7e 100644 --- a/crates/ruma-events/tests/redacted.rs +++ b/crates/ruma-events/tests/redacted.rs @@ -38,7 +38,7 @@ fn unsigned() -> RedactedUnsigned { #[test] fn redacted_message_event_serialize() { let redacted = RedactedSyncMessageEvent { - content: RedactedMessageEventContent, + content: RedactedMessageEventContent::new(), event_id: event_id!("$h29iv0s8:example.com"), origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), @@ -59,7 +59,7 @@ fn redacted_message_event_serialize() { #[test] fn redacted_aliases_event_serialize_no_content() { let redacted = RedactedSyncStateEvent { - content: RedactedAliasesEventContent { aliases: None }, + content: RedactedAliasesEventContent::default(), event_id: event_id!("$h29iv0s8:example.com"), state_key: "".into(), origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), @@ -82,7 +82,7 @@ fn redacted_aliases_event_serialize_no_content() { #[test] fn redacted_aliases_event_serialize_with_content() { let redacted = RedactedSyncStateEvent { - content: RedactedAliasesEventContent { aliases: Some(vec![]) }, + content: RedactedAliasesEventContent::new_v1(vec![]), event_id: event_id!("$h29iv0s8:example.com"), state_key: "".to_owned(), origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), @@ -125,7 +125,7 @@ fn redacted_aliases_deserialize() { .unwrap(), AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::RoomAliases( RedactedSyncStateEvent { - content: RedactedAliasesEventContent { aliases }, + content: RedactedAliasesEventContent { aliases, .. }, event_id, .. }, @@ -153,7 +153,7 @@ fn redacted_deserialize_any_room() { .deserialize() .unwrap(), AnyRoomEvent::RedactedMessage(AnyRedactedMessageEvent::RoomMessage(RedactedMessageEvent { - content: RedactedMessageEventContent, + content: RedactedMessageEventContent { .. }, event_id, room_id, .. })) if event_id == event_id!("$h29iv0s8:example.com") && room_id == room_id!("!roomid:room.com") @@ -192,7 +192,7 @@ fn redacted_deserialize_any_room_sync() { .unwrap(), AnySyncRoomEvent::RedactedMessage(AnyRedactedSyncMessageEvent::RoomMessage( RedactedSyncMessageEvent { - content: RedactedMessageEventContent, + content: RedactedMessageEventContent { .. }, event_id, .. } @@ -222,7 +222,7 @@ fn redacted_state_event_deserialize() { AnySyncRoomEvent::RedactedState(AnyRedactedSyncStateEvent::RoomCreate( RedactedSyncStateEvent { content: RedactedCreateEventContent { - creator, + creator, .. }, event_id, state_key, @@ -311,7 +311,7 @@ fn redact_method_properly_redacts() { assert_matches!( event.redact(redaction, &RoomVersionId::Version6), AnyRedactedMessageEvent::RoomMessage(RedactedMessageEvent { - content: RedactedMessageEventContent, + content: RedactedMessageEventContent { .. }, event_id, room_id, sender, @@ -338,7 +338,7 @@ fn redact_message_content() { assert_matches!( content.redact(&RoomVersionId::Version6), - AnyRedactedMessageEventContent::RoomMessage(RedactedMessageEventContent) + AnyRedactedMessageEventContent::RoomMessage(RedactedMessageEventContent { .. }) ); } @@ -356,7 +356,8 @@ fn redact_state_content() { assert_matches!( content.redact(&RoomVersionId::Version6), AnyRedactedStateEventContent::RoomCreate(RedactedCreateEventContent { - creator + creator, + .. }) if creator == user_id!("@carl:example.com") ); } diff --git a/crates/ruma-events/tests/room_message.rs b/crates/ruma-events/tests/room_message.rs index 38a72a87..deb5aeef 100644 --- a/crates/ruma-events/tests/room_message.rs +++ b/crates/ruma-events/tests/room_message.rs @@ -277,7 +277,7 @@ fn edit_deserialization_future() { formatted: None, .. }), - relates_to: Some(Relation::Replacement(Replacement { event_id, new_content })), + relates_to: Some(Relation::Replacement(Replacement { event_id, new_content, .. })), .. } if body == "s/foo/bar" && event_id == ev_id @@ -321,6 +321,7 @@ fn verification_request_deserialization() { to, from_device, methods, + .. }), .. } if body == "@example:localhost is requesting to verify your key, ..." @@ -351,12 +352,9 @@ fn verification_request_serialization() { "methods": methods }); - let content = MessageType::VerificationRequest(KeyVerificationRequestEventContent { - to: user_id, - from_device: device_id, - body, - methods, - }); + let content = MessageType::VerificationRequest(KeyVerificationRequestEventContent::new( + body, methods, device_id, user_id, + )); assert_eq!(to_json_value(&content).unwrap(), json_data,); }