diff --git a/Cargo.toml b/Cargo.toml index 1db3b353..c9cef627 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/call.rs b/src/call.rs index 35df497c..e916b08b 100644 --- a/src/call.rs +++ b/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", - } -} diff --git a/src/call/hangup.rs b/src/call/hangup.rs index 205fd988..8c50f2d4 100644 --- a/src/call/hangup.rs +++ b/src/call/hangup.rs @@ -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", - } -} diff --git a/src/key/verification.rs b/src/key/verification.rs index fb119ee5..8173029a 100644 --- a/src/key/verification.rs +++ b/src/key/verification.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 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", - } -} diff --git a/src/macros.rs b/src/macros.rs index d9b95a15..ae776800 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 { - 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 { diff --git a/src/presence.rs b/src/presence.rs index 8d721945..4af7473e 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -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; diff --git a/src/room/guest_access.rs b/src/room/guest_access.rs index fe1c451c..e8d0d370 100644 --- a/src/room/guest_access.rs +++ b/src/room/guest_access.rs @@ -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", - } -} diff --git a/src/room/history_visibility.rs b/src/room/history_visibility.rs index 1627886c..c12b72d9 100644 --- a/src/room/history_visibility.rs +++ b/src/room/history_visibility.rs @@ -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", - } -} diff --git a/src/room/join_rules.rs b/src/room/join_rules.rs index ddf530fe..57eb3ffb 100644 --- a/src/room/join_rules.rs +++ b/src/room/join_rules.rs @@ -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", - } -} diff --git a/src/room/member.rs b/src/room/member.rs index 514b6139..c9105281 100644 --- a/src/room/member.rs +++ b/src/room/member.rs @@ -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 { diff --git a/src/room/message/feedback.rs b/src/room/message/feedback.rs index 199dab74..df303a2e 100644 --- a/src/room/message/feedback.rs +++ b/src/room/message/feedback.rs @@ -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", - } -} diff --git a/src/room_key_request.rs b/src/room_key_request.rs index a261ab6b..491c80d4 100644 --- a/src/room_key_request.rs +++ b/src/room_key_request.rs @@ -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 {