Future-proof enums with a __Nonexhaustive variant.

This can be replaced with the #[non_exhaustive] compiler attribute once
it's stabilized.
This commit is contained in:
Jimmy Cuadra 2019-06-14 17:30:29 -07:00
parent add7ef0d8b
commit 12212789b3
13 changed files with 81 additions and 0 deletions

View File

@ -30,6 +30,12 @@ pub enum SessionDescriptionType {
/// An offer. /// An offer.
#[serde(rename = "offer")] #[serde(rename = "offer")]
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! { impl_enum! {

View File

@ -35,6 +35,12 @@ pub enum Reason {
/// Party did not answer in time. /// Party did not answer in time.
#[serde(rename = "invite_timeout")] #[serde(rename = "invite_timeout")]
InviteTimeout, 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! { impl_enum! {

View File

@ -603,6 +603,9 @@ impl<'de> Deserialize<'de> for Event {
Ok(Event::Custom(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::Receipt
| EventType::Tag | EventType::Tag
| EventType::Typing => Err(D::Error::custom("not a room event".to_string())), | 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::Sticker
| EventType::Tag | EventType::Tag
| EventType::Typing => Err(D::Error::custom("not a state event".to_string())), | EventType::Typing => Err(D::Error::custom("not a state event".to_string())),
EventType::__Nonexhaustive => {
panic!("__Nonexhaustive enum variant is not intended for use.")
}
} }
} }
} }

View File

@ -209,6 +209,9 @@ impl<'de> Deserialize<'de> for Event {
| EventType::Sticker => Err(D::Error::custom( | EventType::Sticker => Err(D::Error::custom(
"not exclusively a basic event".to_string(), "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 => { | EventType::Typing => {
Err(D::Error::custom("not exclusively a room event".to_string())) Err(D::Error::custom("not exclusively a room event".to_string()))
} }
EventType::__Nonexhaustive => {
panic!("__Nonexhaustive enum variant is not intended for use.")
}
} }
} }
} }

View File

@ -227,6 +227,11 @@ pub enum EventType {
/// Any event that is not part of the specification. /// Any event that is not part of the specification.
Custom(String), 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. /// A basic event.
@ -324,6 +329,9 @@ impl Display for EventType {
EventType::Tag => "m.tag", EventType::Tag => "m.tag",
EventType::Typing => "m.typing", EventType::Typing => "m.typing",
EventType::Custom(ref event_type) => event_type, EventType::Custom(ref event_type) => event_type,
EventType::__Nonexhaustive => {
panic!("__Nonexhaustive enum variant is not intended for use.")
}
}; };
write!(f, "{}", event_type_str) write!(f, "{}", event_type_str)

View File

@ -4,6 +4,7 @@ macro_rules! impl_enum {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> {
let variant = match *self { let variant = match *self {
$($name::$variant => $s,)* $($name::$variant => $s,)*
$name::__Nonexhaustive => panic!("__Nonexhaustive enum variant is not intended for use."),
}; };
write!(f, "{}", variant) write!(f, "{}", variant)

View File

@ -53,6 +53,12 @@ pub enum PresenceState {
/// Connected to the service but not available for chat. /// Connected to the service but not available for chat.
#[serde(rename = "unavailable")] #[serde(rename = "unavailable")]
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! { impl_enum! {

View File

@ -27,6 +27,12 @@ pub enum GuestAccess {
/// Guests are not allowed to join the room. /// Guests are not allowed to join the room.
#[serde(rename = "forbidden")] #[serde(rename = "forbidden")]
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! { impl_enum! {

View File

@ -39,6 +39,12 @@ pub enum HistoryVisibility {
/// participating homeserver with anyone, regardless of whether they have ever joined the room. /// participating homeserver with anyone, regardless of whether they have ever joined the room.
#[serde(rename = "world_readable")] #[serde(rename = "world_readable")]
WorldReadable, 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! { impl_enum! {

View File

@ -33,6 +33,12 @@ pub enum JoinRule {
/// Anyone can join the room without any prior action. /// Anyone can join the room without any prior action.
#[serde(rename = "public")] #[serde(rename = "public")]
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! { impl_enum! {

View File

@ -80,6 +80,12 @@ pub enum MembershipState {
/// The user has left. /// The user has left.
#[serde(rename = "leave")] #[serde(rename = "leave")]
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! { impl_enum! {

View File

@ -51,6 +51,12 @@ pub enum MessageType {
/// A video message. /// A video message.
#[serde(rename = "m.video")] #[serde(rename = "m.video")]
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. /// The payload of a message event.
@ -536,6 +542,9 @@ impl<'de> Deserialize<'de> for MessageEventContent {
Ok(MessageEventContent::Video(content)) Ok(MessageEventContent::Video(content))
} }
MessageType::__Nonexhaustive => Err(D::Error::custom(
"Attempted to deserialize __Nonexhaustive variant.",
)),
} }
} }
} }

View File

@ -32,6 +32,12 @@ pub enum FeedbackType {
/// Sent when a message has been observed by the end user. /// Sent when a message has been observed by the end user.
#[serde(rename = "read")] #[serde(rename = "read")]
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! { impl_enum! {