Replace impl_enum with strum::{EnumString,Display}
Replace the impl_enum! macro, which automatically derives instances of std::fmt::Display and std::string::FromStr, with derive macros from the strum crate (Display and EnumString resp.). Closes #90.
This commit is contained in:
		
							parent
							
								
									d083127297
								
							
						
					
					
						commit
						d84de004c8
					
				| @ -20,6 +20,7 @@ ruma-identifiers = "0.16.1" | ||||
| ruma-serde = "0.2.1" | ||||
| serde = { version = "1.0.110", features = ["derive"] } | ||||
| serde_json = { version = "1.0.53", features = ["raw_value"] } | ||||
| strum = { version = "0.18.0", features = ["derive"] } | ||||
| 
 | ||||
| [dev-dependencies] | ||||
| maplit = "1.0.2" | ||||
|  | ||||
							
								
								
									
										11
									
								
								src/call.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/call.rs
									
									
									
									
									
								
							| @ -3,6 +3,7 @@ | ||||
| //! This module also contains types shared by events in its child namespaces.
 | ||||
| 
 | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| pub mod answer; | ||||
| pub mod candidates; | ||||
| @ -21,9 +22,10 @@ pub struct SessionDescription { | ||||
| } | ||||
| 
 | ||||
| /// The type of VoIP session description.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[non_exhaustive] | ||||
| #[serde(rename_all = "lowercase")] | ||||
| #[strum(serialize_all = "lowercase")] | ||||
| pub enum SessionDescriptionType { | ||||
|     /// An answer.
 | ||||
|     Answer, | ||||
| @ -31,10 +33,3 @@ pub enum SessionDescriptionType { | ||||
|     /// An offer.
 | ||||
|     Offer, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     SessionDescriptionType { | ||||
|         Answer => "answer", | ||||
|         Offer => "offer", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| use js_int::UInt; | ||||
| use ruma_events_macros::ruma_event; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// Sent by either party to signal their termination of the call. This can be sent either once
 | ||||
| @ -29,8 +30,9 @@ ruma_event! { | ||||
| /// This should not be provided when the user naturally ends or rejects the call. When there was an
 | ||||
| /// error in the call negotiation, this should be `ice_failed` for when ICE negotiation fails or
 | ||||
| /// `invite_timeout` for when the other party did not answer in time.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[serde(rename_all = "snake_case")] | ||||
| #[strum(serialize_all = "snake_case")] | ||||
| pub enum Reason { | ||||
|     /// ICE negotiation failure.
 | ||||
|     IceFailed, | ||||
| @ -38,10 +40,3 @@ pub enum Reason { | ||||
|     /// Party did not answer in time.
 | ||||
|     InviteTimeout, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     Reason { | ||||
|         IceFailed => "ice_failed", | ||||
|         InviteTimeout => "invite_timeout", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| //! This module also contains types shared by events in its child namespaces.
 | ||||
| 
 | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| pub mod accept; | ||||
| pub mod cancel; | ||||
| @ -12,76 +13,51 @@ pub mod request; | ||||
| pub mod start; | ||||
| 
 | ||||
| /// A hash algorithm.
 | ||||
| #[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)] | ||||
| pub enum HashAlgorithm { | ||||
|     /// The SHA256 hash algorithm.
 | ||||
|     #[serde(rename = "sha256")] | ||||
|     #[strum(serialize = "sha256")] | ||||
|     Sha256, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     HashAlgorithm { | ||||
|         Sha256 => "sha256", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// A key agreement protocol.
 | ||||
| #[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)] | ||||
| pub enum KeyAgreementProtocol { | ||||
|     /// The [Curve25519](https://cr.yp.to/ecdh.html) key agreement protocol.
 | ||||
|     #[serde(rename = "curve25519")] | ||||
|     #[strum(serialize = "curve25519")] | ||||
|     Curve25519, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     KeyAgreementProtocol { | ||||
|         Curve25519 => "curve25519", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// A message authentication code algorithm.
 | ||||
| #[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)] | ||||
| pub enum MessageAuthenticationCode { | ||||
|     /// The HKDF-HMAC-SHA256 MAC.
 | ||||
|     #[serde(rename = "hkdf-hmac-sha256")] | ||||
|     #[strum(serialize = "hkdf-hmac-sha256")] | ||||
|     HkdfHmacSha256, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     MessageAuthenticationCode { | ||||
|         HkdfHmacSha256 => "hkdf-hmac-sha256", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// A Short Authentication String method.
 | ||||
| #[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)] | ||||
| pub enum ShortAuthenticationString { | ||||
|     /// The decimal method.
 | ||||
|     #[serde(rename = "decimal")] | ||||
|     #[strum(serialize = "decimal")] | ||||
|     Decimal, | ||||
| 
 | ||||
|     /// The emoji method.
 | ||||
|     #[serde(rename = "emoji")] | ||||
|     #[strum(serialize = "emoji")] | ||||
|     Emoji, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     ShortAuthenticationString { | ||||
|         Decimal => "decimal", | ||||
|         Emoji => "emoji", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// A Short Authentication String (SAS) verification method.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| pub enum VerificationMethod { | ||||
|     /// The *m.sas.v1* verification method.
 | ||||
|     #[serde(rename = "m.sas.v1")] | ||||
|     #[strum(serialize = "m.sas.v1")] | ||||
|     MSasV1, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     VerificationMethod { | ||||
|         MSasV1 => "m.sas.v1", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,28 +1,3 @@ | ||||
| macro_rules! impl_enum { | ||||
|     ($name:ident { $($variant:ident => $s:expr,)+ }) => { | ||||
|         impl ::std::fmt::Display for $name { | ||||
|             fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { | ||||
|                 let variant = match *self { | ||||
|                     $($name::$variant => $s,)* | ||||
|                 }; | ||||
| 
 | ||||
|                 write!(f, "{}", variant) | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         impl ::std::str::FromStr for $name { | ||||
|             type Err = $crate::FromStrError; | ||||
| 
 | ||||
|             fn from_str(s: &str) -> Result<Self, Self::Err> { | ||||
|                 match s { | ||||
|                     $($s => Ok($name::$variant),)* | ||||
|                     _ => Err($crate::FromStrError), | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| macro_rules! impl_event { | ||||
|     ($name:ident, $content_name:ident, $event_type:path) => { | ||||
|         impl crate::Event for $name { | ||||
|  | ||||
| @ -4,6 +4,7 @@ use js_int::UInt; | ||||
| use ruma_events_macros::ruma_event; | ||||
| use ruma_identifiers::UserId; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// Informs the client of a user's presence state change.
 | ||||
| @ -42,29 +43,24 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// A description of a user's connectivity and availability for chat.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| pub enum PresenceState { | ||||
|     /// Disconnected from the service.
 | ||||
|     #[serde(rename = "offline")] | ||||
|     #[strum(serialize = "offline")] | ||||
|     Offline, | ||||
| 
 | ||||
|     /// Connected to the service.
 | ||||
|     #[serde(rename = "online")] | ||||
|     #[strum(serialize = "online")] | ||||
|     Online, | ||||
| 
 | ||||
|     /// Connected to the service but not available for chat.
 | ||||
|     #[serde(rename = "unavailable")] | ||||
|     #[strum(serialize = "unavailable")] | ||||
|     Unavailable, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     PresenceState { | ||||
|         Offline => "offline", | ||||
|         Online => "online", | ||||
|         Unavailable => "unavailable", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use std::convert::TryFrom; | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| 
 | ||||
| use ruma_events_macros::ruma_event; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// Controls whether guest users are allowed to join rooms.
 | ||||
| @ -19,9 +20,10 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// A policy for guest user access to a room.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[non_exhaustive] | ||||
| #[serde(rename_all = "snake_case")] | ||||
| #[strum(serialize_all = "snake_case")] | ||||
| pub enum GuestAccess { | ||||
|     /// Guests are allowed to join the room.
 | ||||
|     CanJoin, | ||||
| @ -29,10 +31,3 @@ pub enum GuestAccess { | ||||
|     /// Guests are not allowed to join the room.
 | ||||
|     Forbidden, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     GuestAccess { | ||||
|         CanJoin => "can_join", | ||||
|         Forbidden => "forbidden", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| 
 | ||||
| use ruma_events_macros::ruma_event; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// This event controls whether a member of a room can see the events that happened in a room
 | ||||
| @ -17,8 +18,9 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// Who can see a room's history.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[serde(rename_all = "snake_case")] | ||||
| #[strum(serialize_all = "snake_case")] | ||||
| pub enum HistoryVisibility { | ||||
|     /// Previous events are accessible to newly joined members from the point they were invited
 | ||||
|     /// onwards. Events stop being accessible when the member's state changes to something other
 | ||||
| @ -38,12 +40,3 @@ pub enum HistoryVisibility { | ||||
|     /// participating homeserver with anyone, regardless of whether they have ever joined the room.
 | ||||
|     WorldReadable, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     HistoryVisibility { | ||||
|         Invited => "invited", | ||||
|         Joined => "joined", | ||||
|         Shared => "shared", | ||||
|         WorldReadable => "world_readable", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -2,6 +2,7 @@ | ||||
| 
 | ||||
| use ruma_events_macros::ruma_event; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// Describes how users are allowed to join the room.
 | ||||
| @ -16,8 +17,9 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// The rule used for users wishing to join this room.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[serde(rename_all = "lowercase")] | ||||
| #[strum(serialize_all = "lowercase")] | ||||
| pub enum JoinRule { | ||||
|     /// A user who wishes to join the room must first receive an invite to the room from someone
 | ||||
|     /// already inside of the room.
 | ||||
| @ -32,12 +34,3 @@ pub enum JoinRule { | ||||
|     /// Anyone can join the room without any prior action.
 | ||||
|     Public, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     JoinRule { | ||||
|         Invite => "invite", | ||||
|         Knock => "knock", | ||||
|         Private => "private", | ||||
|         Public => "public", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -5,6 +5,7 @@ use std::collections::BTreeMap; | ||||
| use ruma_events_macros::ruma_event; | ||||
| use ruma_identifiers::UserId; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// The current membership state of a user in the room.
 | ||||
| @ -63,8 +64,9 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// The membership state of a user.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[serde(rename_all = "lowercase")] | ||||
| #[strum(serialize_all = "lowercase")] | ||||
| pub enum MembershipState { | ||||
|     /// The user is banned.
 | ||||
|     Ban, | ||||
| @ -82,16 +84,6 @@ pub enum MembershipState { | ||||
|     Leave, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     MembershipState { | ||||
|         Ban => "ban", | ||||
|         Invite => "invite", | ||||
|         Join => "join", | ||||
|         Knock => "knock", | ||||
|         Leave => "leave", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// Information about a third party invitation.
 | ||||
| #[derive(Clone, Debug, Deserialize, Serialize)] | ||||
| pub struct ThirdPartyInvite { | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| use ruma_events_macros::ruma_event; | ||||
| use ruma_identifiers::EventId; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| ruma_event! { | ||||
|     /// An acknowledgement of a message.
 | ||||
| @ -24,8 +25,9 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// A type of feedback.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| #[serde(rename_all = "lowercase")] | ||||
| #[strum(serialize_all = "lowercase")] | ||||
| pub enum FeedbackType { | ||||
|     /// Sent when a message is received.
 | ||||
|     Delivered, | ||||
| @ -33,10 +35,3 @@ pub enum FeedbackType { | ||||
|     /// Sent when a message has been observed by the end user.
 | ||||
|     Read, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     FeedbackType { | ||||
|         Delivered => "delivered", | ||||
|         Read => "read", | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| use ruma_events_macros::ruma_event; | ||||
| use ruma_identifiers::{DeviceId, RoomId}; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use strum::{Display, EnumString}; | ||||
| 
 | ||||
| use super::Algorithm; | ||||
| 
 | ||||
| @ -35,24 +36,19 @@ ruma_event! { | ||||
| } | ||||
| 
 | ||||
| /// A new key request or a cancellation of a previous request.
 | ||||
| #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] | ||||
| #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)] | ||||
| pub enum Action { | ||||
|     /// Request a key.
 | ||||
|     #[serde(rename = "request")] | ||||
|     #[strum(serialize = "request")] | ||||
|     Request, | ||||
| 
 | ||||
|     /// Cancel a request for a key.
 | ||||
|     #[serde(rename = "request_cancellation")] | ||||
|     #[strum(serialize = "request_cancellation")] | ||||
|     CancelRequest, | ||||
| } | ||||
| 
 | ||||
| impl_enum! { | ||||
|     Action { | ||||
|         Request => "request", | ||||
|         CancelRequest => "cancel_request", | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /// Information about a requested key.
 | ||||
| #[derive(Clone, Debug, Deserialize, Serialize)] | ||||
| pub struct RequestedKeyInfo { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user