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:
		
							parent
							
								
									add7ef0d8b
								
							
						
					
					
						commit
						12212789b3
					
				| @ -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! { | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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.") | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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.") | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
| @ -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) | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
| @ -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.", | ||||
|             )), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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! { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user