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:
Jake Waksbaum 2020-05-28 23:50:44 -04:00
parent d083127297
commit d84de004c8
No known key found for this signature in database
GPG Key ID: 161DAF1F5CE43352
12 changed files with 43 additions and 141 deletions

View File

@ -20,6 +20,7 @@ ruma-identifiers = "0.16.1"
ruma-serde = "0.2.1" ruma-serde = "0.2.1"
serde = { version = "1.0.110", features = ["derive"] } serde = { version = "1.0.110", features = ["derive"] }
serde_json = { version = "1.0.53", features = ["raw_value"] } serde_json = { version = "1.0.53", features = ["raw_value"] }
strum = { version = "0.18.0", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
maplit = "1.0.2" maplit = "1.0.2"

View File

@ -3,6 +3,7 @@
//! This module also contains types shared by events in its child namespaces. //! This module also contains types shared by events in its child namespaces.
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
pub mod answer; pub mod answer;
pub mod candidates; pub mod candidates;
@ -21,9 +22,10 @@ pub struct SessionDescription {
} }
/// The type of VoIP session description. /// The type of VoIP session description.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)]
#[non_exhaustive] #[non_exhaustive]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum SessionDescriptionType { pub enum SessionDescriptionType {
/// An answer. /// An answer.
Answer, Answer,
@ -31,10 +33,3 @@ pub enum SessionDescriptionType {
/// An offer. /// An offer.
Offer, Offer,
} }
impl_enum! {
SessionDescriptionType {
Answer => "answer",
Offer => "offer",
}
}

View File

@ -3,6 +3,7 @@
use js_int::UInt; use js_int::UInt;
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// Sent by either party to signal their termination of the call. This can be sent either once /// 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 /// 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 /// 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. /// `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")] #[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum Reason { pub enum Reason {
/// ICE negotiation failure. /// ICE negotiation failure.
IceFailed, IceFailed,
@ -38,10 +40,3 @@ pub enum Reason {
/// Party did not answer in time. /// Party did not answer in time.
InviteTimeout, InviteTimeout,
} }
impl_enum! {
Reason {
IceFailed => "ice_failed",
InviteTimeout => "invite_timeout",
}
}

View File

@ -3,6 +3,7 @@
//! This module also contains types shared by events in its child namespaces. //! This module also contains types shared by events in its child namespaces.
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
pub mod accept; pub mod accept;
pub mod cancel; pub mod cancel;
@ -12,76 +13,51 @@ pub mod request;
pub mod start; pub mod start;
/// A hash algorithm. /// A hash algorithm.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)]
pub enum HashAlgorithm { pub enum HashAlgorithm {
/// The SHA256 hash algorithm. /// The SHA256 hash algorithm.
#[serde(rename = "sha256")] #[serde(rename = "sha256")]
#[strum(serialize = "sha256")]
Sha256, Sha256,
} }
impl_enum! {
HashAlgorithm {
Sha256 => "sha256",
}
}
/// A key agreement protocol. /// A key agreement protocol.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)]
pub enum KeyAgreementProtocol { pub enum KeyAgreementProtocol {
/// The [Curve25519](https://cr.yp.to/ecdh.html) key agreement protocol. /// The [Curve25519](https://cr.yp.to/ecdh.html) key agreement protocol.
#[serde(rename = "curve25519")] #[serde(rename = "curve25519")]
#[strum(serialize = "curve25519")]
Curve25519, Curve25519,
} }
impl_enum! {
KeyAgreementProtocol {
Curve25519 => "curve25519",
}
}
/// A message authentication code algorithm. /// A message authentication code algorithm.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)]
pub enum MessageAuthenticationCode { pub enum MessageAuthenticationCode {
/// The HKDF-HMAC-SHA256 MAC. /// The HKDF-HMAC-SHA256 MAC.
#[serde(rename = "hkdf-hmac-sha256")] #[serde(rename = "hkdf-hmac-sha256")]
#[strum(serialize = "hkdf-hmac-sha256")]
HkdfHmacSha256, HkdfHmacSha256,
} }
impl_enum! {
MessageAuthenticationCode {
HkdfHmacSha256 => "hkdf-hmac-sha256",
}
}
/// A Short Authentication String method. /// A Short Authentication String method.
#[derive(Clone, Copy, Debug, Serialize, PartialEq, Deserialize)] #[derive(Clone, Copy, Debug, Display, EnumString, Serialize, PartialEq, Deserialize)]
pub enum ShortAuthenticationString { pub enum ShortAuthenticationString {
/// The decimal method. /// The decimal method.
#[serde(rename = "decimal")] #[serde(rename = "decimal")]
#[strum(serialize = "decimal")]
Decimal, Decimal,
/// The emoji method. /// The emoji method.
#[serde(rename = "emoji")] #[serde(rename = "emoji")]
#[strum(serialize = "emoji")]
Emoji, Emoji,
} }
impl_enum! {
ShortAuthenticationString {
Decimal => "decimal",
Emoji => "emoji",
}
}
/// A Short Authentication String (SAS) verification method. /// 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 { pub enum VerificationMethod {
/// The *m.sas.v1* verification method. /// The *m.sas.v1* verification method.
#[serde(rename = "m.sas.v1")] #[serde(rename = "m.sas.v1")]
#[strum(serialize = "m.sas.v1")]
MSasV1, MSasV1,
} }
impl_enum! {
VerificationMethod {
MSasV1 => "m.sas.v1",
}
}

View File

@ -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 { macro_rules! impl_event {
($name:ident, $content_name:ident, $event_type:path) => { ($name:ident, $content_name:ident, $event_type:path) => {
impl crate::Event for $name { impl crate::Event for $name {

View File

@ -4,6 +4,7 @@ use js_int::UInt;
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use ruma_identifiers::UserId; use ruma_identifiers::UserId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// Informs the client of a user's presence state change. /// 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. /// 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 { pub enum PresenceState {
/// Disconnected from the service. /// Disconnected from the service.
#[serde(rename = "offline")] #[serde(rename = "offline")]
#[strum(serialize = "offline")]
Offline, Offline,
/// Connected to the service. /// Connected to the service.
#[serde(rename = "online")] #[serde(rename = "online")]
#[strum(serialize = "online")]
Online, Online,
/// Connected to the service but not available for chat. /// Connected to the service but not available for chat.
#[serde(rename = "unavailable")] #[serde(rename = "unavailable")]
#[strum(serialize = "unavailable")]
Unavailable, Unavailable,
} }
impl_enum! {
PresenceState {
Offline => "offline",
Online => "online",
Unavailable => "unavailable",
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::convert::TryFrom; use std::convert::TryFrom;

View File

@ -2,6 +2,7 @@
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// Controls whether guest users are allowed to join rooms. /// Controls whether guest users are allowed to join rooms.
@ -19,9 +20,10 @@ ruma_event! {
} }
/// A policy for guest user access to a room. /// 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] #[non_exhaustive]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum GuestAccess { pub enum GuestAccess {
/// Guests are allowed to join the room. /// Guests are allowed to join the room.
CanJoin, CanJoin,
@ -29,10 +31,3 @@ pub enum GuestAccess {
/// Guests are not allowed to join the room. /// Guests are not allowed to join the room.
Forbidden, Forbidden,
} }
impl_enum! {
GuestAccess {
CanJoin => "can_join",
Forbidden => "forbidden",
}
}

View File

@ -2,6 +2,7 @@
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// This event controls whether a member of a room can see the events that happened in a room /// 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. /// 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")] #[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
pub enum HistoryVisibility { pub enum HistoryVisibility {
/// Previous events are accessible to newly joined members from the point they were invited /// 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 /// 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. /// participating homeserver with anyone, regardless of whether they have ever joined the room.
WorldReadable, WorldReadable,
} }
impl_enum! {
HistoryVisibility {
Invited => "invited",
Joined => "joined",
Shared => "shared",
WorldReadable => "world_readable",
}
}

View File

@ -2,6 +2,7 @@
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// Describes how users are allowed to join the room. /// 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. /// 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")] #[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum JoinRule { pub enum JoinRule {
/// A user who wishes to join the room must first receive an invite to the room from someone /// A user who wishes to join the room must first receive an invite to the room from someone
/// already inside of the room. /// already inside of the room.
@ -32,12 +34,3 @@ pub enum JoinRule {
/// Anyone can join the room without any prior action. /// Anyone can join the room without any prior action.
Public, Public,
} }
impl_enum! {
JoinRule {
Invite => "invite",
Knock => "knock",
Private => "private",
Public => "public",
}
}

View File

@ -5,6 +5,7 @@ use std::collections::BTreeMap;
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use ruma_identifiers::UserId; use ruma_identifiers::UserId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// The current membership state of a user in the room. /// The current membership state of a user in the room.
@ -63,8 +64,9 @@ ruma_event! {
} }
/// The membership state of a user. /// 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")] #[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum MembershipState { pub enum MembershipState {
/// The user is banned. /// The user is banned.
Ban, Ban,
@ -82,16 +84,6 @@ pub enum MembershipState {
Leave, Leave,
} }
impl_enum! {
MembershipState {
Ban => "ban",
Invite => "invite",
Join => "join",
Knock => "knock",
Leave => "leave",
}
}
/// Information about a third party invitation. /// Information about a third party invitation.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ThirdPartyInvite { pub struct ThirdPartyInvite {

View File

@ -3,6 +3,7 @@
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use ruma_identifiers::EventId; use ruma_identifiers::EventId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
ruma_event! { ruma_event! {
/// An acknowledgement of a message. /// An acknowledgement of a message.
@ -24,8 +25,9 @@ ruma_event! {
} }
/// A type of feedback. /// A type of feedback.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] #[derive(Clone, Copy, Debug, Display, EnumString, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum FeedbackType { pub enum FeedbackType {
/// Sent when a message is received. /// Sent when a message is received.
Delivered, Delivered,
@ -33,10 +35,3 @@ 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.
Read, Read,
} }
impl_enum! {
FeedbackType {
Delivered => "delivered",
Read => "read",
}
}

View File

@ -3,6 +3,7 @@
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
use ruma_identifiers::{DeviceId, RoomId}; use ruma_identifiers::{DeviceId, RoomId};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};
use super::Algorithm; use super::Algorithm;
@ -35,24 +36,19 @@ ruma_event! {
} }
/// A new key request or a cancellation of a previous request. /// 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 { pub enum Action {
/// Request a key. /// Request a key.
#[serde(rename = "request")] #[serde(rename = "request")]
#[strum(serialize = "request")]
Request, Request,
/// Cancel a request for a key. /// Cancel a request for a key.
#[serde(rename = "request_cancellation")] #[serde(rename = "request_cancellation")]
#[strum(serialize = "request_cancellation")]
CancelRequest, CancelRequest,
} }
impl_enum! {
Action {
Request => "request",
CancelRequest => "cancel_request",
}
}
/// Information about a requested key. /// Information about a requested key.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RequestedKeyInfo { pub struct RequestedKeyInfo {