From 647f56e0a17cedf848cf415f4ec82ca41639f61c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 18 Aug 2020 03:22:13 +0200 Subject: [PATCH] client-api: Make structs in sync_events non-exhaustive --- ruma-client-api/src/r0/sync/sync_events.rs | 90 +++++++++++++++++++++- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/ruma-client-api/src/r0/sync/sync_events.rs b/ruma-client-api/src/r0/sync/sync_events.rs index fea398c5..6b7260ca 100644 --- a/ruma-client-api/src/r0/sync/sync_events.rs +++ b/ruma-client-api/src/r0/sync/sync_events.rs @@ -160,6 +160,7 @@ impl<'a> From<&'a str> for Filter<'a> { /// Updates to rooms. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct Rooms { /// The rooms that the user has left or been banned from. #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] @@ -175,6 +176,11 @@ pub struct Rooms { } impl Rooms { + /// Creates an empty `Rooms`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there is no update in any room. pub fn is_empty(&self) -> bool { self.leave.is_empty() && self.join.is_empty() && self.invite.is_empty() @@ -182,7 +188,8 @@ impl Rooms { } /// Historical updates to left rooms. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct LeftRoom { /// The timeline of messages and state changes in the room up to the point when the user /// left. @@ -199,6 +206,11 @@ pub struct LeftRoom { } impl LeftRoom { + /// Creates an empty `LeftRoom`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are updates in the room. pub fn is_empty(&self) -> bool { self.timeline.is_empty() && self.state.is_empty() && self.account_data.is_empty() @@ -206,7 +218,8 @@ impl LeftRoom { } /// Updates to joined rooms. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct JoinedRoom { /// Information about the room which clients may need to correctly render it /// to users. @@ -238,6 +251,11 @@ pub struct JoinedRoom { } impl JoinedRoom { + /// Creates an empty `JoinedRoom`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no updates in the room. pub fn is_empty(&self) -> bool { self.summary.is_empty() @@ -251,6 +269,7 @@ impl JoinedRoom { /// unread notifications count #[derive(Clone, Copy, Default, Debug, Deserialize, Serialize)] +#[non_exhaustive] pub struct UnreadNotificationsCount { /// The number of unread notifications for this room with the highlight flag set. #[serde(skip_serializing_if = "Option::is_none")] @@ -262,6 +281,11 @@ pub struct UnreadNotificationsCount { } impl UnreadNotificationsCount { + /// Creates an empty `UnreadNotificationsCount`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no notification count updates. pub fn is_empty(&self) -> bool { self.highlight_count.is_none() && self.notification_count.is_none() @@ -270,6 +294,7 @@ impl UnreadNotificationsCount { /// Events in the room. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct Timeline { /// True if the number of events returned was limited by the `limit` on the filter. #[serde(default, skip_serializing_if = "ruma_serde::is_default")] @@ -286,6 +311,11 @@ pub struct Timeline { } impl Timeline { + /// Creates an empty `Timeline`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no timeline updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -294,6 +324,7 @@ impl Timeline { /// State events in the room. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct State { /// A list of state events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -301,6 +332,11 @@ pub struct State { } impl State { + /// Creates an empty `State`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no state updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -309,6 +345,7 @@ impl State { /// The private data that this user has attached to this room. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct AccountData { /// A list of events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -316,6 +353,11 @@ pub struct AccountData { } impl AccountData { + /// Creates an empty `AccountData`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no account data updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -324,6 +366,7 @@ impl AccountData { /// Ephemeral events not recorded in the timeline or state of the room. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct Ephemeral { /// A list of events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -331,6 +374,11 @@ pub struct Ephemeral { } impl Ephemeral { + /// Creates an empty `Ephemeral`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no ephemeral event updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -339,6 +387,7 @@ impl Ephemeral { /// Information about room for rendering to clients. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct RoomSummary { /// Users which can be used to generate a room name if the room does not have /// one. Required if room name or canonical aliases are not set or empty. @@ -359,6 +408,11 @@ pub struct RoomSummary { } impl RoomSummary { + /// Creates an empty `RoomSummary`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no room summary updates. pub fn is_empty(&self) -> bool { self.heroes.is_empty() @@ -368,7 +422,8 @@ impl RoomSummary { } /// Updates to the rooms that the user has been invited to. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct InvitedRoom { /// The state of a room that the user has been invited to. #[serde(default, skip_serializing_if = "InviteState::is_empty")] @@ -376,6 +431,11 @@ pub struct InvitedRoom { } impl InvitedRoom { + /// Creates an empty `InvitedRoom`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no updates to this room. pub fn is_empty(&self) -> bool { self.invite_state.is_empty() @@ -384,6 +444,7 @@ impl InvitedRoom { /// The state of a room that the user has been invited to. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct InviteState { /// A list of state events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -391,6 +452,11 @@ pub struct InviteState { } impl InviteState { + /// Creates an empty `InviteState`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no state updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -399,6 +465,7 @@ impl InviteState { /// Updates to the presence status of other users. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct Presence { /// A list of events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -406,6 +473,11 @@ pub struct Presence { } impl Presence { + /// Creates an empty `Presence`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no presence updates. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -414,6 +486,7 @@ impl Presence { /// Messages sent dirrectly between devices. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct ToDevice { /// A list of to-device events. #[serde(default, skip_serializing_if = "Vec::is_empty")] @@ -421,6 +494,11 @@ pub struct ToDevice { } impl ToDevice { + /// Creates an empty `ToDevice`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no to-device events. pub fn is_empty(&self) -> bool { self.events.is_empty() @@ -429,6 +507,7 @@ impl ToDevice { /// Information on E2E device udpates. #[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[non_exhaustive] pub struct DeviceLists { /// List of users who have updated their device identity keys or who now /// share an encrypted room with the client since the previous sync @@ -442,6 +521,11 @@ pub struct DeviceLists { } impl DeviceLists { + /// Creates an empty `DeviceLists`. + pub fn new() -> Self { + Default::default() + } + /// Returns true if there are no device list updates. pub fn is_empty(&self) -> bool { self.changed.is_empty() && self.left.is_empty()