From d34a27091906be2b0dd950b9b4db1d76a7c55723 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 1 Nov 2020 02:55:51 +0100 Subject: [PATCH] Allow custom values for more enums --- ruma-api/CHANGELOG.md | 1 - ruma-api/Cargo.toml | 1 - ruma-api/src/error.rs | 4 -- ruma-client-api/Cargo.toml | 1 - .../src/r0/account/request_openid_token.rs | 8 ++-- .../src/r0/capabilities/get_capabilities.rs | 10 ++-- ruma-client-api/src/r0/filter.rs | 14 +++--- .../src/r0/membership/get_member_events.rs | 11 +++-- ruma-client-api/src/r0/push.rs | 29 ++++------- .../src/r0/receipt/create_receipt.rs | 18 ++----- ruma-client-api/src/r0/room.rs | 10 ++-- ruma-client-api/src/r0/room/create_room.rs | 10 ++-- .../src/r0/search/search_events.rs | 33 ++++++++----- ruma-client-api/src/r0/session/login.rs | 2 +- ruma-client-api/src/r0/sync/sync_events.rs | 12 +++-- ruma-client/examples/message_log.rs | 6 ++- ruma-client/src/lib.rs | 6 +-- ruma-common/Cargo.toml | 1 - ruma-common/src/presence.rs | 18 ++++--- ruma-common/src/push.rs | 9 ++-- ruma-common/src/thirdparty.rs | 9 ++-- ruma-events/Cargo.toml | 1 - ruma-events/src/call.rs | 11 +++-- ruma-events/src/call/hangup.rs | 11 +++-- ruma-events/src/event_type.rs | 1 - ruma-events/src/key/verification.rs | 48 ++++++++++--------- ruma-events/src/key/verification/cancel.rs | 1 - ruma-events/src/policy/rule.rs | 27 +++-------- ruma-events/src/room/guest_access.rs | 11 +++-- ruma-events/src/room/history_visibility.rs | 11 +++-- ruma-events/src/room/join_rules.rs | 11 +++-- ruma-events/src/room/member.rs | 15 +++--- ruma-events/src/room/message.rs | 17 ++++--- ruma-events/src/room/message/feedback.rs | 11 +++-- ruma-events/src/room_key_request.rs | 14 +++--- .../src/query/get_profile_information/v1.rs | 13 +++-- ruma-identifiers/Cargo.toml | 1 - ruma-push-gateway-api/Cargo.toml | 1 - .../src/send_event_notification/v1.rs | 12 ++--- ruma-signatures/Cargo.toml | 1 + ruma-signatures/src/lib.rs | 15 ++---- 41 files changed, 224 insertions(+), 222 deletions(-) diff --git a/ruma-api/CHANGELOG.md b/ruma-api/CHANGELOG.md index d64aa4bb..8d993eac 100644 --- a/ruma-api/CHANGELOG.md +++ b/ruma-api/CHANGELOG.md @@ -2,7 +2,6 @@ Breaking changes: -* Update strum dependency to 0.19 * The `EndpointError` trait now requires `std::error::Error`. This allows integrating `EndpointError`s in the common rust error ecosystem like `thiserror` and `anyhow`. * The `Endpoint` trait has been replaced by two new traits that each capture a subset of its diff --git a/ruma-api/Cargo.toml b/ruma-api/Cargo.toml index 796880ea..361fe5cd 100644 --- a/ruma-api/Cargo.toml +++ b/ruma-api/Cargo.toml @@ -24,7 +24,6 @@ ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde = { version = "1.0.114", features = ["derive"] } serde_json = "1.0.57" -strum = "0.19.2" thiserror = "1.0.20" [dev-dependencies] diff --git a/ruma-api/src/error.rs b/ruma-api/src/error.rs index 3aaa5cb4..fd29148b 100644 --- a/ruma-api/src/error.rs +++ b/ruma-api/src/error.rs @@ -201,10 +201,6 @@ pub enum DeserializationError { #[error("{0}")] Ident(#[from] ruma_identifiers::Error), - /// Path segment deserialization failed. - #[error("{0}")] - Strum(#[from] strum::ParseError), - /// Header value deserialization failed. #[error("{0}")] Header(#[from] http::header::ToStrError), diff --git a/ruma-client-api/Cargo.toml b/ruma-client-api/Cargo.toml index d0381bb2..f1d43e41 100644 --- a/ruma-client-api/Cargo.toml +++ b/ruma-client-api/Cargo.toml @@ -29,7 +29,6 @@ ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde = { version = "1.0.114", features = ["derive"] } serde_json = "1.0.57" -strum = { version = "0.19.2", features = ["derive"] } [dev-dependencies] maplit = "1.0.2" diff --git a/ruma-client-api/src/r0/account/request_openid_token.rs b/ruma-client-api/src/r0/account/request_openid_token.rs index 349d6c66..cc616355 100644 --- a/ruma-client-api/src/r0/account/request_openid_token.rs +++ b/ruma-client-api/src/r0/account/request_openid_token.rs @@ -3,8 +3,8 @@ use std::time::Duration; use ruma_api::ruma_api; +use ruma_common::StringEnum; use ruma_identifiers::{ServerNameBox, UserId}; -use serde::{Deserialize, Serialize}; ruma_api! { metadata: { @@ -61,9 +61,11 @@ impl Response { } /// Access token types. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, StringEnum)] pub enum TokenType { /// Bearer token type Bearer, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-client-api/src/r0/capabilities/get_capabilities.rs b/ruma-client-api/src/r0/capabilities/get_capabilities.rs index f8569370..94967050 100644 --- a/ruma-client-api/src/r0/capabilities/get_capabilities.rs +++ b/ruma-client-api/src/r0/capabilities/get_capabilities.rs @@ -2,6 +2,7 @@ use maplit::btreemap; use ruma_api::ruma_api; +use ruma_common::StringEnum; use ruma_identifiers::RoomVersionId; use serde::{Deserialize, Serialize}; use serde_json::Value as JsonValue; @@ -150,14 +151,15 @@ impl Default for RoomVersionsCapability { } /// The stability of a room version -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "lowercase")] pub enum RoomVersionStability { /// Support for the given version is stable. - #[serde(rename = "stable")] Stable, /// Support for the given version is unstable. - #[serde(rename = "unstable")] Unstable, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-client-api/src/r0/filter.rs b/ruma-client-api/src/r0/filter.rs index 4bdaffdb..6be07657 100644 --- a/ruma-client-api/src/r0/filter.rs +++ b/ruma-client-api/src/r0/filter.rs @@ -10,20 +10,22 @@ pub use lazy_load::LazyLoadOptions; pub use url::UrlFilter; use js_int::UInt; -use ruma_common::Outgoing; +use ruma_common::{Outgoing, StringEnum}; use ruma_identifiers::{RoomId, UserId}; -use serde::{Deserialize, Serialize}; +use serde::Serialize; /// Format to use for returned events. -#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum EventFormat { /// Client format, as described in the Client API. Client, /// Raw events from federation. Federation, + + #[doc(hidden)] + _Custom(String), } impl Default for EventFormat { @@ -292,7 +294,7 @@ impl IncomingFilter { } /// A filter definition -#[derive(Clone, Copy, Debug, Default, Outgoing, Serialize)] +#[derive(Clone, Debug, Default, Outgoing, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[incoming_derive(Clone, Default, Serialize)] pub struct FilterDefinition<'a> { diff --git a/ruma-client-api/src/r0/membership/get_member_events.rs b/ruma-client-api/src/r0/membership/get_member_events.rs index d82806d4..ac43b408 100644 --- a/ruma-client-api/src/r0/membership/get_member_events.rs +++ b/ruma-client-api/src/r0/membership/get_member_events.rs @@ -1,10 +1,9 @@ //! [GET /_matrix/client/r0/rooms/{roomId}/members](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-members) use ruma_api::ruma_api; -use ruma_common::Raw; +use ruma_common::{Raw, StringEnum}; use ruma_events::room::member::MemberEvent; use ruma_identifiers::RoomId; -use serde::{Deserialize, Serialize}; ruma_api! { metadata: { @@ -64,9 +63,8 @@ impl Response { } /// The kind of membership events to filter for. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "lowercase")] +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "lowercase")] pub enum MembershipEventFilter { /// The user has joined. Join, @@ -79,6 +77,9 @@ pub enum MembershipEventFilter { /// The user has been banned. Ban, + + #[doc(hidden)] + _Custom(String), } #[cfg(test)] diff --git a/ruma-client-api/src/r0/push.rs b/ruma-client-api/src/r0/push.rs index 76d5bc96..574ee21d 100644 --- a/ruma-client-api/src/r0/push.rs +++ b/ruma-client-api/src/r0/push.rs @@ -1,10 +1,7 @@ //! Endpoints for push notifications. -use std::convert::TryFrom; - -use ruma_common::push::PusherData; +use ruma_common::{push::PusherData, StringEnum}; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; pub mod delete_pushrule; pub mod get_notifications; @@ -20,12 +17,8 @@ pub mod set_pushrule_actions; pub mod set_pushrule_enabled; /// The kinds of push rules that are available -#[derive( - Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Display, EnumString, -)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum RuleKind { /// User-configured rules that override all other kinds Override, @@ -41,14 +34,9 @@ pub enum RuleKind { /// Content-specific rules Content, -} -impl TryFrom<&'_ str> for RuleKind { - type Error = strum::ParseError; - - fn try_from(s: &str) -> Result { - s.parse() - } + #[doc(hidden)] + _Custom(String), } /// Defines a pusher @@ -82,12 +70,15 @@ pub struct Pusher { } /// Which kind a pusher is -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum PusherKind { /// A pusher that sends HTTP pokes. Http, /// A pusher that emails the user with unread notifications. Email, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-client-api/src/r0/receipt/create_receipt.rs b/ruma-client-api/src/r0/receipt/create_receipt.rs index d239fb2a..3e12c68b 100644 --- a/ruma-client-api/src/r0/receipt/create_receipt.rs +++ b/ruma-client-api/src/r0/receipt/create_receipt.rs @@ -1,10 +1,8 @@ //! [POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}](https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-rooms-roomid-receipt-receipttype-eventid) -use std::convert::TryFrom; - use ruma_api::ruma_api; +use ruma_common::{AsRefStr, DisplayAsRefStr, FromString}; use ruma_identifiers::{EventId, RoomId}; -use strum::{Display, EnumString}; ruma_api! { metadata: { @@ -51,18 +49,12 @@ impl Response { } /// The type of receipt. -#[derive(Clone, Copy, Debug, Display, EnumString)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, AsRefStr, DisplayAsRefStr, FromString)] pub enum ReceiptType { /// m.read - #[strum(serialize = "m.read")] + #[ruma_enum(rename = "m.read")] Read, -} -impl TryFrom<&'_ str> for ReceiptType { - type Error = strum::ParseError; - - fn try_from(s: &str) -> Result { - s.parse() - } + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-client-api/src/r0/room.rs b/ruma-client-api/src/r0/room.rs index ce527c75..cddfecdc 100644 --- a/ruma-client-api/src/r0/room.rs +++ b/ruma-client-api/src/r0/room.rs @@ -5,18 +5,20 @@ pub mod get_room_event; pub mod report_content; pub mod upgrade_room; -use serde::{Deserialize, Serialize}; +use ruma_common::StringEnum; /// Whether or not a newly created room will be listed in the room directory. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum Visibility { /// Indicates that the room will be shown in the published room list. Public, /// Indicates that the room will not be shown in the published room list. Private, + + #[doc(hidden)] + _Custom(String), } impl Default for Visibility { diff --git a/ruma-client-api/src/r0/room/create_room.rs b/ruma-client-api/src/r0/room/create_room.rs index d7ce9314..131463be 100644 --- a/ruma-client-api/src/r0/room/create_room.rs +++ b/ruma-client-api/src/r0/room/create_room.rs @@ -2,7 +2,7 @@ use assign::assign; use ruma_api::ruma_api; -use ruma_common::Raw; +use ruma_common::{Raw, StringEnum}; use ruma_events::{ room::{ create::{CreateEventContent, PreviousRoom}, @@ -160,9 +160,8 @@ impl Default for CreationContent { } /// A convenience parameter for setting a few default state events. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum RoomPreset { /// `join_rules` is set to `invite` and `history_visibility` is set to `shared`. PrivateChat, @@ -172,4 +171,7 @@ pub enum RoomPreset { /// Same as `PrivateChat`, but all initial invitees get the same power level as the creator. TrustedPrivateChat, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-client-api/src/r0/search/search_events.rs b/ruma-client-api/src/r0/search/search_events.rs index d1048216..16187a96 100644 --- a/ruma-client-api/src/r0/search/search_events.rs +++ b/ruma-client-api/src/r0/search/search_events.rs @@ -4,7 +4,7 @@ use std::collections::BTreeMap; use js_int::{uint, UInt}; use ruma_api::ruma_api; -use ruma_common::{Outgoing, Raw}; +use ruma_common::{Outgoing, Raw, StringEnum}; use ruma_events::{AnyRoomEvent, AnyStateEvent}; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; @@ -71,7 +71,7 @@ impl Categories<'_> { } /// Criteria for searching a category of events. -#[derive(Clone, Copy, Debug, Outgoing, Serialize)] +#[derive(Clone, Debug, Outgoing, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct Criteria<'a> { /// The string to search events for. @@ -216,7 +216,7 @@ impl EventContextResult { } /// A grouping for partioning the result set. -#[derive(Clone, Copy, Default, Debug, Deserialize, Serialize)] +#[derive(Clone, Default, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct Grouping { /// The key within events to use for this grouping. @@ -236,15 +236,17 @@ impl Grouping { } /// The key within events to use for this grouping. -#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum GroupingKey { /// `room_id` RoomId, /// `sender` Sender, + + #[doc(hidden)] + _Custom(String), } /// Requests that the server partitions the result set based on the provided list of keys. @@ -270,26 +272,28 @@ impl Groupings<'_> { } /// The keys to search for. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, StringEnum)] pub enum SearchKeys { /// content.body - #[serde(rename = "content.body")] + #[ruma_enum(rename = "content.body")] ContentBody, /// content.name - #[serde(rename = "content.name")] + #[ruma_enum(rename = "content.name")] ContentName, /// content.topic - #[serde(rename = "content.topic")] + #[ruma_enum(rename = "content.topic")] ContentTopic, + + #[doc(hidden)] + _Custom(String), } /// The order in which to search for results. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, StringEnum)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[ruma_enum(rename_all = "snake_case")] pub enum OrderBy { /// Prioritize recent events. Recent, @@ -297,6 +301,9 @@ pub enum OrderBy { /// Prioritize events by a numerical ranking of how closely they matched the search /// criteria. Rank, + + #[doc(hidden)] + _Custom(String), } /// Categories of events that can be searched for. diff --git a/ruma-client-api/src/r0/session/login.rs b/ruma-client-api/src/r0/session/login.rs index 25ad4526..cba9a37d 100644 --- a/ruma-client-api/src/r0/session/login.rs +++ b/ruma-client-api/src/r0/session/login.rs @@ -78,7 +78,7 @@ impl Response { } /// Identification information for the user. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Outgoing, Serialize)] +#[derive(Clone, Debug, PartialEq, Eq, Outgoing, Serialize)] #[serde(from = "user_serde::IncomingUserInfo", into = "user_serde::UserInfo")] pub enum UserInfo<'a> { /// Either a fully qualified Matrix user ID, or just the localpart (as part of the 'identifier' diff --git a/ruma-client-api/src/r0/sync/sync_events.rs b/ruma-client-api/src/r0/sync/sync_events.rs index 7b473c08..66f4a149 100644 --- a/ruma-client-api/src/r0/sync/sync_events.rs +++ b/ruma-client-api/src/r0/sync/sync_events.rs @@ -29,7 +29,7 @@ ruma_api! { /// A filter represented either as its full JSON definition or the ID of a saved filter. #[serde(skip_serializing_if = "Option::is_none")] #[ruma_api(query)] - pub filter: Option>, + pub filter: Option<&'a Filter<'a>>, /// A point in time to continue a sync from. /// @@ -45,9 +45,11 @@ ruma_api! { pub full_state: bool, /// Controls whether the client is automatically marked as online by polling this API. + /// + /// Defaults to `PresenceState::Online`. #[serde(default, skip_serializing_if = "ruma_serde::is_default")] #[ruma_api(query)] - pub set_presence: PresenceState, + pub set_presence: &'a PresenceState, /// The maximum time to poll in milliseconds before returning this request. #[serde( @@ -117,7 +119,7 @@ impl Response { } /// A filter represented either as its full JSON definition or the ID of a saved filter. -#[derive(Clone, Copy, Debug, Outgoing, Serialize)] +#[derive(Clone, Debug, Outgoing, Serialize)] #[allow(clippy::large_enum_variant)] #[serde(untagged)] pub enum Filter<'a> { @@ -539,10 +541,10 @@ mod tests { #[test] fn serialize_all_params() { let req: http::Request> = Request { - filter: Some(Filter::FilterId("66696p746572")), + filter: Some(&Filter::FilterId("66696p746572")), since: Some("s72594_4483_1934"), full_state: true, - set_presence: PresenceState::Offline, + set_presence: &PresenceState::Offline, timeout: Some(Duration::from_millis(30000)), } .try_into_http_request("https://homeserver.tld", Some("auth_tok")) diff --git a/ruma-client/examples/message_log.rs b/ruma-client/examples/message_log.rs index b8707045..e36aa2e6 100644 --- a/ruma-client/examples/message_log.rs +++ b/ruma-client/examples/message_log.rs @@ -18,16 +18,18 @@ async fn log_messages(homeserver_url: Uri, username: &str, password: &str) -> an client.log_in(username, password, None, None).await?; + // FIXME: Possibly promotable when replacing `.into()` if `ignore_all` is made const. + let filter = FilterDefinition::ignore_all().into(); let initial_sync_response = client .request(assign!(sync_events::Request::new(), { - filter: Some(FilterDefinition::ignore_all().into()), + filter: Some(&filter), })) .await?; let mut sync_stream = Box::pin(client.sync( None, initial_sync_response.next_batch, - PresenceState::Online, + &PresenceState::Online, Some(Duration::from_secs(30)), )); diff --git a/ruma-client/src/lib.rs b/ruma-client/src/lib.rs index a6d8ab24..39cae0c9 100644 --- a/ruma-client/src/lib.rs +++ b/ruma-client/src/lib.rs @@ -54,7 +54,7 @@ //! let mut sync_stream = Box::pin(client.sync( //! None, //! next_batch_token, -//! PresenceState::Online, +//! &PresenceState::Online, //! Some(Duration::from_secs(30)), //! )); //! while let Some(response) = sync_stream.try_next().await? { @@ -281,9 +281,9 @@ impl Client { /// Convenience method that represents repeated calls to the sync_events endpoint as a stream. pub fn sync<'a>( &self, - filter: Option>, + filter: Option<&'a SyncFilter<'a>>, since: String, - set_presence: ruma_common::presence::PresenceState, + set_presence: &'a ruma_common::presence::PresenceState, timeout: Option, ) -> impl Stream>> + TryStream> diff --git a/ruma-common/Cargo.toml b/ruma-common/Cargo.toml index 7d96b40c..ed50afcc 100644 --- a/ruma-common/Cargo.toml +++ b/ruma-common/Cargo.toml @@ -17,7 +17,6 @@ ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde = { version = "1.0.114", features = ["derive"] } serde_json = { version = "1.0.57", features = ["raw_value"] } -strum = { version = "0.19.2", features = ["derive"] } [dev-dependencies] matches = "0.1.8" diff --git a/ruma-common/src/presence.rs b/ruma-common/src/presence.rs index 9953a576..009441f6 100644 --- a/ruma-common/src/presence.rs +++ b/ruma-common/src/presence.rs @@ -2,14 +2,11 @@ //! //! [presence]: https://matrix.org/docs/spec/client_server/r0.6.1#id62 -use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; +use crate::StringEnum; /// A description of a user's connectivity and availability for chat. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum PresenceState { /// Disconnected from the service. Offline, @@ -19,6 +16,9 @@ pub enum PresenceState { /// Connected to the service but not available for chat. Unavailable, + + #[doc(hidden)] + _Custom(String), } impl Default for PresenceState { @@ -26,3 +26,9 @@ impl Default for PresenceState { Self::Online } } + +impl Default for &'_ PresenceState { + fn default() -> Self { + &PresenceState::Online + } +} diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index cb75ab1e..f8c6b67c 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -2,6 +2,7 @@ //! //! [push]: https://matrix.org/docs/spec/client_server/r0.6.1#id89 +use ruma_common::StringEnum; use serde::{Deserialize, Serialize}; mod action; @@ -237,10 +238,12 @@ impl PusherData { /// Currently, only "event_id_only" is supported as of [Push Gateway API r0.1.1][spec]. /// /// [spec]: https://matrix.org/docs/spec/push_gateway/r0.1.1#homeserver-behaviour -#[derive(Clone, Copy, Debug, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum PushFormat { /// Require the homeserver to only send a reduced set of fields in the push. EventIdOnly, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-common/src/thirdparty.rs b/ruma-common/src/thirdparty.rs index d42e6d4a..21eabcf1 100644 --- a/ruma-common/src/thirdparty.rs +++ b/ruma-common/src/thirdparty.rs @@ -4,6 +4,7 @@ use std::collections::BTreeMap; +use ruma_common::StringEnum; use ruma_identifiers::{RoomAliasId, UserId}; use serde::{Deserialize, Serialize}; @@ -190,13 +191,15 @@ impl User { } /// The medium of a third party identifier. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "lowercase")] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "lowercase")] pub enum Medium { /// Email address identifier Email, /// Phone number identifier MSISDN, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/Cargo.toml b/ruma-events/Cargo.toml index c7ffabe9..decbdb5d 100644 --- a/ruma-events/Cargo.toml +++ b/ruma-events/Cargo.toml @@ -21,7 +21,6 @@ ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde = { version = "1.0.114", features = ["derive"] } serde_json = { version = "1.0.57", features = ["raw_value"] } -strum = { version = "0.19.2", features = ["derive"] } [dev-dependencies] maplit = "1.0.2" diff --git a/ruma-events/src/call.rs b/ruma-events/src/call.rs index 5a50fe03..8ce95bc9 100644 --- a/ruma-events/src/call.rs +++ b/ruma-events/src/call.rs @@ -2,8 +2,8 @@ //! //! This module also contains types shared by events in its child namespaces. +use ruma_common::StringEnum; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; pub mod answer; pub mod candidates; @@ -30,14 +30,15 @@ impl SessionDescription { } /// The type of VoIP session description. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum SessionDescriptionType { /// An answer. Answer, /// An offer. Offer, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/call/hangup.rs b/ruma-events/src/call/hangup.rs index b651944b..f93555b8 100644 --- a/ruma-events/src/call/hangup.rs +++ b/ruma-events/src/call/hangup.rs @@ -1,9 +1,9 @@ //! Types for the *m.call.hangup* event. use js_int::UInt; +use ruma_common::StringEnum; use ruma_events_macros::MessageEventContent; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::MessageEvent; @@ -31,14 +31,15 @@ pub struct HangupEventContent { /// 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, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum Reason { /// ICE negotiation failure. IceFailed, /// Party did not answer in time. InviteTimeout, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/event_type.rs b/ruma-events/src/event_type.rs index f5afb87d..be7ca3db 100644 --- a/ruma-events/src/event_type.rs +++ b/ruma-events/src/event_type.rs @@ -7,7 +7,6 @@ use ruma_common::StringEnum; // FIXME: Add `m.foo.bar` or `m.foo_bar` as a naming scheme in StringEnum and remove most rename // attributes. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, StringEnum)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum EventType { /// m.call.answer #[ruma_enum(rename = "m.call.answer")] diff --git a/ruma-events/src/key/verification.rs b/ruma-events/src/key/verification.rs index c5d9b38b..b4decf27 100644 --- a/ruma-events/src/key/verification.rs +++ b/ruma-events/src/key/verification.rs @@ -2,8 +2,7 @@ //! //! This module also contains types shared by events in its child namespaces. -use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; +use ruma_common::StringEnum; pub mod accept; pub mod cancel; @@ -13,62 +12,67 @@ pub mod request; pub mod start; /// A hash algorithm. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum HashAlgorithm { /// The SHA256 hash algorithm. Sha256, + + #[doc(hidden)] + _Custom(String), } /// A key agreement protocol. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "kebab-case")] -#[strum(serialize_all = "kebab-case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "kebab-case")] pub enum KeyAgreementProtocol { /// The [Curve25519](https://cr.yp.to/ecdh.html) key agreement protocol. Curve25519, /// The Curve25519 key agreement protocol with check for public keys. Curve25519HkdfSha256, + + #[doc(hidden)] + _Custom(String), } /// A message authentication code algorithm. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "kebab-case")] -#[strum(serialize_all = "kebab-case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "kebab-case")] pub enum MessageAuthenticationCode { /// The HKDF-HMAC-SHA256 MAC. HkdfHmacSha256, /// The HMAC-SHA256 MAC. HmacSha256, + + #[doc(hidden)] + _Custom(String), } /// A Short Authentication String method. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Serialize, Deserialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum ShortAuthenticationString { /// The decimal method. Decimal, /// The emoji method. Emoji, + + #[doc(hidden)] + _Custom(String), } /// A Short Authentication String (SAS) verification method. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, PartialEq, StringEnum)] pub enum VerificationMethod { /// The *m.sas.v1* verification method. - #[serde(rename = "m.sas.v1")] - #[strum(serialize = "m.sas.v1")] + #[ruma_enum(rename = "m.sas.v1")] MSasV1, + + #[doc(hidden)] + _Custom(String), } #[cfg(test)] diff --git a/ruma-events/src/key/verification/cancel.rs b/ruma-events/src/key/verification/cancel.rs index e729b845..0c2ba8fe 100644 --- a/ruma-events/src/key/verification/cancel.rs +++ b/ruma-events/src/key/verification/cancel.rs @@ -36,7 +36,6 @@ pub struct CancelEventContent { /// obtained through `.as_str()`. // FIXME: Add `m.foo_bar` as a naming scheme in StringEnum and remove rename attributes. #[derive(Clone, Debug, PartialEq, StringEnum)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum CancelCode { /// The user cancelled the verification. #[ruma_enum(rename = "m.user")] diff --git a/ruma-events/src/policy/rule.rs b/ruma-events/src/policy/rule.rs index fe7f3c39..8e63e952 100644 --- a/ruma-events/src/policy/rule.rs +++ b/ruma-events/src/policy/rule.rs @@ -1,7 +1,6 @@ //! Modules and types for events in the *m.policy.rule* namespace. -use std::fmt::{Display, Formatter, Result as FmtResult}; - +use ruma_common::StringEnum; use serde::{Deserialize, Serialize}; pub mod room; @@ -30,31 +29,19 @@ impl PolicyRuleEventContent { } /// Rules recommendations -#[derive(Clone, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, StringEnum)] pub enum Recommendation { /// Entities affected by the rule should be banned from participation where possible. - #[serde(rename = "m.ban")] + #[ruma_enum(rename = "m.ban")] Ban, + + #[doc(hidden)] + _Custom(String), } impl Recommendation { /// Creates a string slice from this `Recommendation`. pub fn as_str(&self) -> &str { - match *self { - Recommendation::Ban => "m.ban", - } - } -} - -impl Display for Recommendation { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - f.write_str(self.as_str()) - } -} - -impl From for String { - fn from(recommendation: Recommendation) -> String { - recommendation.to_string() + self.as_ref() } } diff --git a/ruma-events/src/room/guest_access.rs b/ruma-events/src/room/guest_access.rs index 74873bc1..09758a06 100644 --- a/ruma-events/src/room/guest_access.rs +++ b/ruma-events/src/room/guest_access.rs @@ -1,8 +1,8 @@ //! Types for the *m.room.guest_access* event. +use ruma_common::StringEnum; use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::StateEvent; @@ -29,14 +29,15 @@ impl GuestAccessEventContent { } /// A policy for guest user access to a room. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum GuestAccess { /// Guests are allowed to join the room. CanJoin, /// Guests are not allowed to join the room. Forbidden, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/room/history_visibility.rs b/ruma-events/src/room/history_visibility.rs index ea3c5222..1e410ff1 100644 --- a/ruma-events/src/room/history_visibility.rs +++ b/ruma-events/src/room/history_visibility.rs @@ -1,8 +1,8 @@ //! Types for the *m.room.history_visibility* event. +use ruma_common::StringEnum; use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::StateEvent; @@ -28,10 +28,8 @@ impl HistoryVisibilityEventContent { } /// Who can see a room's history. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_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 @@ -50,4 +48,7 @@ pub enum HistoryVisibility { /// All events while this is the `HistoryVisibility` value may be shared by any /// participating homeserver with anyone, regardless of whether they have ever joined the room. WorldReadable, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/room/join_rules.rs b/ruma-events/src/room/join_rules.rs index 73247cc8..6a725738 100644 --- a/ruma-events/src/room/join_rules.rs +++ b/ruma-events/src/room/join_rules.rs @@ -1,8 +1,8 @@ //! Types for the *m.room.join_rules* event. +use ruma_common::StringEnum; use ruma_events_macros::StateEventContent; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::StateEvent; @@ -27,10 +27,8 @@ impl JoinRulesEventContent { } /// The rule used for users wishing to join this room. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "lowercase")] -#[strum(serialize_all = "lowercase")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_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. @@ -44,4 +42,7 @@ pub enum JoinRule { /// Anyone can join the room without any prior action. Public, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/room/member.rs b/ruma-events/src/room/member.rs index 9a8bda96..424927b9 100644 --- a/ruma-events/src/room/member.rs +++ b/ruma-events/src/room/member.rs @@ -2,10 +2,10 @@ use std::collections::BTreeMap; +use ruma_common::StringEnum; use ruma_events_macros::StateEventContent; use ruma_identifiers::{ServerKeyId, ServerNameBox, UserId}; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::{StateEvent, StrippedStateEvent, SyncStateEvent}; @@ -64,10 +64,8 @@ pub struct MemberEventContent { } /// The membership state of a user. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "lowercase")] -#[strum(serialize_all = "lowercase")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "lowercase")] pub enum MembershipState { /// The user is banned. Ban, @@ -83,6 +81,9 @@ pub enum MembershipState { /// The user has left. Leave, + + #[doc(hidden)] + _Custom(String), } /// Information about a third party invitation. @@ -187,7 +188,7 @@ fn membership_change( } }; - match (prev_content.membership, &content.membership) { + match (&prev_content.membership, &content.membership) { (St::Invite, St::Invite) | (St::Leave, St::Leave) | (St::Ban, St::Ban) => Ch::None, (St::Invite, St::Join) | (St::Leave, St::Join) => Ch::Joined, (St::Invite, St::Leave) => { @@ -213,7 +214,7 @@ fn membership_change( (St::Join, St::Ban) => Ch::KickedAndBanned, (St::Leave, St::Invite) => Ch::Invited, (St::Ban, St::Leave) => Ch::Unbanned, - (St::Knock, _) | (_, St::Knock) => Ch::NotImplemented, + _ => Ch::NotImplemented, } } diff --git a/ruma-events/src/room/message.rs b/ruma-events/src/room/message.rs index 178f8188..7144abcf 100644 --- a/ruma-events/src/room/message.rs +++ b/ruma-events/src/room/message.rs @@ -287,24 +287,28 @@ pub struct ServerNoticeMessageEventContent { } /// Types of server notices. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, StringEnum)] pub enum ServerNoticeType { /// The server has exceeded some limit which requires the server administrator to intervene. - #[serde(rename = "m.server_notice.usage_limit_reached")] + #[ruma_enum(rename = "m.server_notice.usage_limit_reached")] UsageLimitReached, + + #[doc(hidden)] + _Custom(String), } /// Types of usage limits. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum LimitType { /// The server's number of active users in the last 30 days has exceeded the maximum. /// /// New connections are being refused by the server. What defines "active" is left as an /// implementation detail, however servers are encouraged to treat syncing users as "active". MonthlyActiveUser, + + #[doc(hidden)] + _Custom(String), } /// The format for the formatted representation of a message body. @@ -313,7 +317,6 @@ pub enum LimitType { /// available as a documented variant here, use its string representation, /// obtained through `.as_str()`. #[derive(Clone, Debug, StringEnum)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum MessageFormat { /// HTML. #[ruma_enum(rename = "org.matrix.custom.html")] diff --git a/ruma-events/src/room/message/feedback.rs b/ruma-events/src/room/message/feedback.rs index f9d23924..a97b711f 100644 --- a/ruma-events/src/room/message/feedback.rs +++ b/ruma-events/src/room/message/feedback.rs @@ -1,9 +1,9 @@ //! Types for the *m.room.message.feedback* event. +use ruma_common::StringEnum; use ruma_events_macros::MessageEventContent; use ruma_identifiers::EventId; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::MessageEvent; @@ -34,14 +34,15 @@ impl FeedbackEventContent { } /// A type of feedback. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum FeedbackType { /// Sent when a message is received. Delivered, /// Sent when a message has been observed by the end user. Read, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-events/src/room_key_request.rs b/ruma-events/src/room_key_request.rs index 2c7b387a..a0c06eb1 100644 --- a/ruma-events/src/room_key_request.rs +++ b/ruma-events/src/room_key_request.rs @@ -1,9 +1,9 @@ //! Types for the *m.room_key_request* event. +use ruma_common::StringEnum; use ruma_events_macros::BasicEventContent; use ruma_identifiers::{DeviceIdBox, EventEncryptionAlgorithm, RoomId}; use serde::{Deserialize, Serialize}; -use strum::{Display, EnumString}; use crate::BasicEvent; @@ -35,18 +35,18 @@ pub struct RoomKeyRequestEventContent { } /// A new key request or a cancellation of a previous request. -#[derive(Clone, Copy, Debug, PartialEq, Display, EnumString, Deserialize, Serialize)] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum Action { /// Request a key. Request, /// Cancel a request for a key. - #[serde(rename = "request_cancellation")] - #[strum(serialize = "request_cancellation")] + #[ruma_enum(rename = "request_cancellation")] CancelRequest, + + #[doc(hidden)] + _Custom(String), } /// Information about a requested key. diff --git a/ruma-federation-api/src/query/get_profile_information/v1.rs b/ruma-federation-api/src/query/get_profile_information/v1.rs index a3339722..f5d0ea91 100644 --- a/ruma-federation-api/src/query/get_profile_information/v1.rs +++ b/ruma-federation-api/src/query/get_profile_information/v1.rs @@ -1,8 +1,8 @@ //! [GET /_matrix/federation/v1/query/profile](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-query-profile) use ruma_api::ruma_api; +use ruma_common::StringEnum; use ruma_identifiers::UserId; -use serde::{Deserialize, Serialize}; ruma_api! { metadata: { @@ -22,7 +22,7 @@ ruma_api! { /// Profile field to query. #[serde(skip_serializing_if = "Option::is_none")] #[ruma_api(query)] - pub field: Option, + pub field: Option<&'a ProfileField>, } #[derive(Default)] @@ -52,13 +52,16 @@ impl Response { } /// Profile fields to specify in query. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] pub enum ProfileField { /// Display name of the user. - #[serde(rename = "displayname")] + #[ruma_enum(rename = "displayname")] DisplayName, /// Avatar URL for the user's avatar. - #[serde(rename = "avatar_url")] + #[ruma_enum(rename = "avatar_url")] AvatarUrl, + + #[doc(hidden)] + _Custom(String), } diff --git a/ruma-identifiers/Cargo.toml b/ruma-identifiers/Cargo.toml index eeef7c23..4bcf0e9d 100644 --- a/ruma-identifiers/Cargo.toml +++ b/ruma-identifiers/Cargo.toml @@ -32,7 +32,6 @@ ruma-identifiers-validation = { version = "0.1.1", path = "../ruma-identifiers-v ruma-serde = { version = "0.2.3", path = "../ruma-serde" } # Renamed so we can have a serde feature. serde1 = { package = "serde", version = "1.0.114", optional = true, features = ["derive"] } -strum = { version = "0.19.2", features = ["derive"] } [dev-dependencies] matches = "0.1.8" diff --git a/ruma-push-gateway-api/Cargo.toml b/ruma-push-gateway-api/Cargo.toml index aa9f7809..f4c9490c 100644 --- a/ruma-push-gateway-api/Cargo.toml +++ b/ruma-push-gateway-api/Cargo.toml @@ -19,7 +19,6 @@ ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde = { version = "1.0.114", features = ["derive"] } serde_json = "1.0.57" -strum = { version = "0.19.2", features = ["derive"] } [features] unstable-exhaustive-types = [] diff --git a/ruma-push-gateway-api/src/send_event_notification/v1.rs b/ruma-push-gateway-api/src/send_event_notification/v1.rs index 147b501e..d644a9a1 100644 --- a/ruma-push-gateway-api/src/send_event_notification/v1.rs +++ b/ruma-push-gateway-api/src/send_event_notification/v1.rs @@ -4,14 +4,13 @@ use js_int::UInt; use ruma_api::ruma_api; use ruma_common::{ push::{PusherData, Tweak}, - Outgoing, + Outgoing, StringEnum, }; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use serde::{Deserialize, Serialize}; use serde_json::value::RawValue as RawJsonValue; use std::time::SystemTime; -use strum::{Display, EnumString}; ruma_api! { metadata: { @@ -137,16 +136,17 @@ impl<'a> Notification<'a> { /// /// This may be used by push gateways to deliver less time-sensitive /// notifications in a way that will preserve battery power on mobile devices. -#[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, PartialEq)] -#[serde(rename_all = "snake_case")] -#[strum(serialize_all = "snake_case")] -#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +#[derive(Clone, Debug, PartialEq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] pub enum NotificationPriority { /// A high priority notification High, /// A low priority notification Low, + + #[doc(hidden)] + _Custom(String), } impl Default for NotificationPriority { diff --git a/ruma-signatures/Cargo.toml b/ruma-signatures/Cargo.toml index 1dc33748..089b20d6 100644 --- a/ruma-signatures/Cargo.toml +++ b/ruma-signatures/Cargo.toml @@ -15,6 +15,7 @@ version = "0.6.0-dev.1" [dependencies] base64 = "0.12.3" ring = "0.16.15" +ruma-common = { version = "0.2.0", path = "../ruma-common" } ruma-identifiers = { version = "0.17.4", path = "../ruma-identifiers" } ruma-serde = { version = "0.2.3", path = "../ruma-serde" } serde_json = "1.0.57" diff --git a/ruma-signatures/src/lib.rs b/ruma-signatures/src/lib.rs index 7996114e..23c587cf 100644 --- a/ruma-signatures/src/lib.rs +++ b/ruma-signatures/src/lib.rs @@ -49,6 +49,8 @@ use std::{ fmt::{Display, Formatter, Result as FmtResult}, }; +use ruma_common::{AsRefStr, DisplayAsRefStr}; + pub use functions::{ canonical_json, content_hash, hash_and_sign_event, redact, reference_hash, sign_json, verify_event, verify_json, @@ -114,22 +116,13 @@ impl From for Error { } /// The algorithm used for signing data. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, AsRefStr, DisplayAsRefStr)] +#[ruma_enum(rename_all = "snake_case")] pub enum Algorithm { /// The Ed25519 digital signature algorithm. Ed25519, } -impl Display for Algorithm { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - let name = match *self { - Self::Ed25519 => "ed25519", - }; - - write!(f, "{}", name) - } -} - /// An error when trying to extract the algorithm and version from a key identifier. #[derive(Clone, Debug, PartialEq)] enum SplitError<'a> {