From 55df2aa26a54e985d6bb5b701322e30221299310 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 16 May 2021 22:45:48 +0200 Subject: [PATCH] events: Make remaining types in room::* non-exhaustive --- crates/ruma-events/src/room/power_levels.rs | 12 +++++-- crates/ruma-events/src/room/redaction.rs | 15 +++++++- crates/ruma-events/src/room/relationships.rs | 34 +++++++++++++++++++ crates/ruma-events/src/room/server_acl.rs | 9 +++++ .../src/room/third_party_invite.rs | 17 ++++++++++ crates/ruma-events/src/room/tombstone.rs | 8 +++++ crates/ruma-events/src/room/topic.rs | 8 +++++ 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/crates/ruma-events/src/room/power_levels.rs b/crates/ruma-events/src/room/power_levels.rs index a925310d..952e14cb 100644 --- a/crates/ruma-events/src/room/power_levels.rs +++ b/crates/ruma-events/src/room/power_levels.rs @@ -17,6 +17,7 @@ pub type PowerLevelsEvent = StateEvent; /// The payload for `PowerLevelsEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.power_levels", kind = State)] pub struct PowerLevelsEventContent { /// The level required to ban a user. @@ -116,8 +117,9 @@ pub struct PowerLevelsEventContent { pub notifications: NotificationPowerLevels, } -impl Default for PowerLevelsEventContent { - fn default() -> Self { +impl PowerLevelsEventContent { + /// Creates a `PowerLevelsEventContent` with all-default values. + pub fn new() -> Self { // events_default and users_default having a default of 0 while the others have a default // of 50 is not an oversight, these defaults are from the Matrix specification. Self { @@ -135,6 +137,12 @@ impl Default for PowerLevelsEventContent { } } +impl Default for PowerLevelsEventContent { + fn default() -> Self { + Self::new() + } +} + /// Used with `#[serde(skip_serializing_if)]` to omit default power levels. #[allow(clippy::trivially_copy_pass_by_ref)] fn is_default_power_level(l: &Int) -> bool { diff --git a/crates/ruma-events/src/room/redaction.rs b/crates/ruma-events/src/room/redaction.rs index 32601be7..fd6c5777 100644 --- a/crates/ruma-events/src/room/redaction.rs +++ b/crates/ruma-events/src/room/redaction.rs @@ -55,7 +55,8 @@ pub struct SyncRedactionEvent { } /// A redaction of an event. -#[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.redaction", kind = Message)] pub struct RedactionEventContent { /// The reason for the redaction, if any. @@ -63,4 +64,16 @@ pub struct RedactionEventContent { pub reason: Option, } +impl RedactionEventContent { + /// Creates an empty `RedactionEventContent`. + pub fn new() -> Self { + Self::default() + } + + /// Creates a `RedactionEventContent` with the given reason. + pub fn with_reason(reason: String) -> Self { + Self { reason: Some(reason) } + } +} + impl RedactedStateEventContent for RedactedRedactionEventContent {} diff --git a/crates/ruma-events/src/room/relationships.rs b/crates/ruma-events/src/room/relationships.rs index 4ec38fb7..05d14629 100644 --- a/crates/ruma-events/src/room/relationships.rs +++ b/crates/ruma-events/src/room/relationships.rs @@ -51,22 +51,40 @@ pub(crate) enum RelationJsonRepr { /// Information about the event a "rich reply" is replying to. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct InReplyTo { /// The event being replied to. pub event_id: EventId, } +impl InReplyTo { + /// Creates a new `InReplyTo` with the given event ID. + pub fn new(event_id: EventId) -> Self { + Self { event_id } + } +} + /// A reference to another event. #[derive(Clone, Debug, Deserialize, Serialize)] #[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 Reference { /// The event we are referencing. pub event_id: EventId, } +#[cfg(feature = "unstable-pre-spec")] +impl Reference { + /// Creates a new `Reference` with the given event ID. + pub fn new(event_id: EventId) -> Self { + Self { event_id } + } +} + /// An annotation for an event. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct Annotation { /// The event that is being annotated. pub event_id: EventId, @@ -75,15 +93,31 @@ pub struct Annotation { pub key: String, } +impl Annotation { + /// Creates a new `Annotation` with the given event ID and key. + pub fn new(event_id: EventId, key: String) -> Self { + Self { event_id, key } + } +} + /// An event replacing another event. #[derive(Clone, Debug, Deserialize, Serialize)] #[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 event this event is replacing. pub event_id: EventId, } +#[cfg(feature = "unstable-pre-spec")] +impl Replacement { + /// Creates a new `Replacement` with the given event ID. + pub fn new(event_id: EventId) -> Self { + Self { event_id } + } +} + #[cfg(test)] mod tests { use matches::assert_matches; diff --git a/crates/ruma-events/src/room/server_acl.rs b/crates/ruma-events/src/room/server_acl.rs index d335f30a..69a60eee 100644 --- a/crates/ruma-events/src/room/server_acl.rs +++ b/crates/ruma-events/src/room/server_acl.rs @@ -10,6 +10,7 @@ pub type ServerAclEvent = StateEvent; /// The payload for `ServerAclEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.server_acl", kind = State)] pub struct ServerAclEventContent { /// True to allow server names that are IP address literals. False to deny. @@ -38,6 +39,14 @@ pub struct ServerAclEventContent { pub deny: Vec, } +impl ServerAclEventContent { + /// Creates a new `ServerAclEventContent` with the given IP literal allowance flag, allowed and + /// denied servers. + pub fn new(allow_ip_literals: bool, allow: Vec, deny: Vec) -> Self { + Self { allow_ip_literals, allow, deny } + } +} + #[cfg(test)] mod tests { use ruma_serde::Raw; diff --git a/crates/ruma-events/src/room/third_party_invite.rs b/crates/ruma-events/src/room/third_party_invite.rs index eeb7aecd..b5c00f32 100644 --- a/crates/ruma-events/src/room/third_party_invite.rs +++ b/crates/ruma-events/src/room/third_party_invite.rs @@ -14,6 +14,7 @@ pub type ThirdPartyInviteEvent = StateEvent; /// The payload for `ThirdPartyInviteEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.third_party_invite", kind = State)] pub struct ThirdPartyInviteEventContent { /// A user-readable string which represents the user who has been invited. @@ -42,8 +43,17 @@ pub struct ThirdPartyInviteEventContent { pub public_keys: Option>, } +impl ThirdPartyInviteEventContent { + /// Creates a new `ThirdPartyInviteEventContent` with the given display name, key validity url + /// and public key. + pub fn new(display_name: String, key_validity_url: String, public_key: String) -> Self { + Self { display_name, key_validity_url, public_key, public_keys } + } +} + /// A public key for signing a third party invite token. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct PublicKey { /// An optional URL which can be fetched to validate whether the key has been revoked. /// @@ -55,3 +65,10 @@ pub struct PublicKey { /// A Base64-encoded Ed25519 key with which the token must be signed. pub public_key: String, } + +impl PublicKey { + /// Creates a new `PublicKey` with the given base64-encoded ed25519 key. + pub fn new(public_key: String) -> Self { + Self { key_validity_url: None, public_key } + } +} diff --git a/crates/ruma-events/src/room/tombstone.rs b/crates/ruma-events/src/room/tombstone.rs index aedf7568..7818baa2 100644 --- a/crates/ruma-events/src/room/tombstone.rs +++ b/crates/ruma-events/src/room/tombstone.rs @@ -13,6 +13,7 @@ pub type TombstoneEvent = StateEvent; /// The payload for `TombstoneEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] #[ruma_event(type = "m.room.tombstone", kind = State)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct TombstoneEventContent { /// A server-defined message. /// @@ -24,3 +25,10 @@ pub struct TombstoneEventContent { /// The new room the client should be visiting. pub replacement_room: RoomId, } + +impl TombstoneEventContent { + /// Creates a new `TombstoneEventContent` with the given body and replacement room ID. + pub fn new(body: String, replacement_room: RoomId) -> Self { + Self { body, replacement_room } + } +} diff --git a/crates/ruma-events/src/room/topic.rs b/crates/ruma-events/src/room/topic.rs index a48702d3..9acbff66 100644 --- a/crates/ruma-events/src/room/topic.rs +++ b/crates/ruma-events/src/room/topic.rs @@ -10,8 +10,16 @@ pub type TopicEvent = StateEvent; /// The payload for `TopicEvent`. #[derive(Clone, Debug, Deserialize, Serialize, EventContent)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[ruma_event(type = "m.room.topic", kind = State)] pub struct TopicEventContent { /// The topic text. pub topic: String, } + +impl TopicEventContent { + /// Creates a new `TopicEventContent` with the given topic. + pub fn new(topic: String) -> Self { + Self { topic } + } +}