From 12212789b35574c71c4b5a8261a5fda08c584419 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Fri, 14 Jun 2019 17:30:29 -0700 Subject: [PATCH] Future-proof enums with a __Nonexhaustive variant. This can be replaced with the #[non_exhaustive] compiler attribute once it's stabilized. --- src/call.rs | 6 ++++++ src/call/hangup.rs | 6 ++++++ src/collections/all.rs | 9 +++++++++ src/collections/only.rs | 6 ++++++ src/lib.rs | 8 ++++++++ src/macros.rs | 1 + src/presence.rs | 6 ++++++ src/room/guest_access.rs | 6 ++++++ src/room/history_visibility.rs | 6 ++++++ src/room/join_rules.rs | 6 ++++++ src/room/member.rs | 6 ++++++ src/room/message.rs | 9 +++++++++ src/room/message/feedback.rs | 6 ++++++ 13 files changed, 81 insertions(+) diff --git a/src/call.rs b/src/call.rs index 39738c92..4f34ce66 100644 --- a/src/call.rs +++ b/src/call.rs @@ -30,6 +30,12 @@ pub enum SessionDescriptionType { /// An offer. #[serde(rename = "offer")] Offer, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/call/hangup.rs b/src/call/hangup.rs index 23dcbe1f..74fccc97 100644 --- a/src/call/hangup.rs +++ b/src/call/hangup.rs @@ -35,6 +35,12 @@ pub enum Reason { /// Party did not answer in time. #[serde(rename = "invite_timeout")] InviteTimeout, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/collections/all.rs b/src/collections/all.rs index 529799ca..ebd6c5a3 100644 --- a/src/collections/all.rs +++ b/src/collections/all.rs @@ -603,6 +603,9 @@ impl<'de> Deserialize<'de> for Event { Ok(Event::Custom(event)) } } + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } } } } @@ -868,6 +871,9 @@ impl<'de> Deserialize<'de> for RoomEvent { | EventType::Receipt | EventType::Tag | EventType::Typing => Err(D::Error::custom("not a room event".to_string())), + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } } } } @@ -1059,6 +1065,9 @@ impl<'de> Deserialize<'de> for StateEvent { | EventType::Sticker | EventType::Tag | EventType::Typing => Err(D::Error::custom("not a state event".to_string())), + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } } } } diff --git a/src/collections/only.rs b/src/collections/only.rs index 943de2c1..47bfaf57 100644 --- a/src/collections/only.rs +++ b/src/collections/only.rs @@ -209,6 +209,9 @@ impl<'de> Deserialize<'de> for Event { | EventType::Sticker => Err(D::Error::custom( "not exclusively a basic event".to_string(), )), + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } } } } @@ -346,6 +349,9 @@ impl<'de> Deserialize<'de> for RoomEvent { | EventType::Typing => { Err(D::Error::custom("not exclusively a room event".to_string())) } + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } } } } diff --git a/src/lib.rs b/src/lib.rs index e31574a6..99bc6880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,6 +227,11 @@ pub enum EventType { /// Any event that is not part of the specification. Custom(String), + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + __Nonexhaustive, } /// A basic event. @@ -324,6 +329,9 @@ impl Display for EventType { EventType::Tag => "m.tag", EventType::Typing => "m.typing", EventType::Custom(ref event_type) => event_type, + EventType::__Nonexhaustive => { + panic!("__Nonexhaustive enum variant is not intended for use.") + } }; write!(f, "{}", event_type_str) diff --git a/src/macros.rs b/src/macros.rs index 6057dc4f..3fb5be23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -4,6 +4,7 @@ macro_rules! impl_enum { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { let variant = match *self { $($name::$variant => $s,)* + $name::__Nonexhaustive => panic!("__Nonexhaustive enum variant is not intended for use."), }; write!(f, "{}", variant) diff --git a/src/presence.rs b/src/presence.rs index be4a2f58..e43f5a99 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -53,6 +53,12 @@ pub enum PresenceState { /// Connected to the service but not available for chat. #[serde(rename = "unavailable")] Unavailable, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/room/guest_access.rs b/src/room/guest_access.rs index 28b26dae..65485171 100644 --- a/src/room/guest_access.rs +++ b/src/room/guest_access.rs @@ -27,6 +27,12 @@ pub enum GuestAccess { /// Guests are not allowed to join the room. #[serde(rename = "forbidden")] Forbidden, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/room/history_visibility.rs b/src/room/history_visibility.rs index f33aeb09..ba3dd0be 100644 --- a/src/room/history_visibility.rs +++ b/src/room/history_visibility.rs @@ -39,6 +39,12 @@ pub enum HistoryVisibility { /// participating homeserver with anyone, regardless of whether they have ever joined the room. #[serde(rename = "world_readable")] WorldReadable, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/room/join_rules.rs b/src/room/join_rules.rs index dfe1e04d..c34f992f 100644 --- a/src/room/join_rules.rs +++ b/src/room/join_rules.rs @@ -33,6 +33,12 @@ pub enum JoinRule { /// Anyone can join the room without any prior action. #[serde(rename = "public")] Public, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/room/member.rs b/src/room/member.rs index acaa998b..08e55fa1 100644 --- a/src/room/member.rs +++ b/src/room/member.rs @@ -80,6 +80,12 @@ pub enum MembershipState { /// The user has left. #[serde(rename = "leave")] Leave, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! { diff --git a/src/room/message.rs b/src/room/message.rs index 09783565..d6ccf59e 100644 --- a/src/room/message.rs +++ b/src/room/message.rs @@ -51,6 +51,12 @@ pub enum MessageType { /// A video message. #[serde(rename = "m.video")] Video, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } /// The payload of a message event. @@ -536,6 +542,9 @@ impl<'de> Deserialize<'de> for MessageEventContent { Ok(MessageEventContent::Video(content)) } + MessageType::__Nonexhaustive => Err(D::Error::custom( + "Attempted to deserialize __Nonexhaustive variant.", + )), } } } diff --git a/src/room/message/feedback.rs b/src/room/message/feedback.rs index 726f7273..f8fc0bbd 100644 --- a/src/room/message/feedback.rs +++ b/src/room/message/feedback.rs @@ -32,6 +32,12 @@ pub enum FeedbackType { /// Sent when a message has been observed by the end user. #[serde(rename = "read")] Read, + + /// Additional variants may be added in the future and will not be considered breaking changes + /// to `ruma-events`. + #[doc(hidden)] + #[serde(skip)] + __Nonexhaustive, } impl_enum! {