From ab3d48b5765584fca713c03f1037b71c2bb8ac85 Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Fri, 2 Jul 2021 13:15:01 -0700 Subject: [PATCH] client-api: Make all pub enums non_exhaustive --- crates/ruma-api/src/error.rs | 1 + crates/ruma-api/src/lib.rs | 2 + crates/ruma-client-api/src/error.rs | 1 + crates/ruma-client-api/src/r0/account.rs | 11 ++++++ .../src/r0/account/register.rs | 2 + crates/ruma-client-api/src/r0/backup.rs | 1 + crates/ruma-client-api/src/r0/capabilities.rs | 13 ++++++- crates/ruma-client-api/src/r0/filter.rs | 11 ++++++ .../src/r0/filter/lazy_load.rs | 1 + crates/ruma-client-api/src/r0/filter/url.rs | 1 + .../src/r0/media/get_content_thumbnail.rs | 39 ++++++++++++------- .../src/r0/membership/get_member_events.rs | 11 ++++++ .../src/r0/message/get_message_events.rs | 1 + crates/ruma-client-api/src/r0/push.rs | 22 +++++++++++ crates/ruma-client-api/src/r0/room.rs | 11 ++++++ .../src/r0/room/create_room.rs | 11 ++++++ .../src/r0/search/search_events.rs | 23 +++++++++++ .../ruma-client-api/src/r0/session/login.rs | 2 + .../src/r0/sync/sync_events.rs | 3 +- .../src/r0/typing/create_typing_event.rs | 1 + crates/ruma-client-api/src/r0/uiaa.rs | 1 + 21 files changed, 153 insertions(+), 16 deletions(-) diff --git a/crates/ruma-api/src/error.rs b/crates/ruma-api/src/error.rs index 0dd2505a..4c060c26 100644 --- a/crates/ruma-api/src/error.rs +++ b/crates/ruma-api/src/error.rs @@ -189,6 +189,7 @@ impl StdError for ResponseDeserializationError {} /// An error was reported by the server (HTTP status code 4xx or 5xx) #[derive(Debug)] +#[allow(clippy::exhaustive_enums)] pub enum ServerError { /// An error that is expected to happen under certain circumstances and /// that has a well-defined structure diff --git a/crates/ruma-api/src/lib.rs b/crates/ruma-api/src/lib.rs index 155d7fc6..1ae3cbb2 100644 --- a/crates/ruma-api/src/lib.rs +++ b/crates/ruma-api/src/lib.rs @@ -215,6 +215,7 @@ use error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError}; /// An enum to control whether an access token should be added to outgoing requests #[derive(Clone, Copy, Debug)] +#[allow(clippy::exhaustive_enums)] pub enum SendAccessToken<'a> { /// Add the given access token to the request only if the `METADATA` on the request requires it IfRequired(&'a str), @@ -371,6 +372,7 @@ pub trait IncomingNonAuthRequest: IncomingRequest {} /// Authentication scheme used by the endpoint. #[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[allow(clippy::exhaustive_enums)] pub enum AuthScheme { /// No authentication is performed. None, diff --git a/crates/ruma-client-api/src/error.rs b/crates/ruma-client-api/src/error.rs index 9c8bc6ac..f12cf218 100644 --- a/crates/ruma-client-api/src/error.rs +++ b/crates/ruma-client-api/src/error.rs @@ -17,6 +17,7 @@ mod kind_serde; /// An enum for the error kind. Items may contain additional information. #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub enum ErrorKind { /// M_FORBIDDEN Forbidden, diff --git a/crates/ruma-client-api/src/r0/account.rs b/crates/ruma-client-api/src/r0/account.rs index ddfba44a..bdd737f8 100644 --- a/crates/ruma-client-api/src/r0/account.rs +++ b/crates/ruma-client-api/src/r0/account.rs @@ -40,8 +40,12 @@ impl<'a> IdentityServerInfo<'a> { } /// Possible values for deleting or unbinding 3PIDs. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, StringEnum)] #[ruma_enum(rename_all = "kebab-case")] +#[non_exhaustive] pub enum ThirdPartyIdRemovalStatus { /// Either the homeserver couldn't determine the right identity server to contact, or the /// identity server refused the operation. @@ -53,3 +57,10 @@ pub enum ThirdPartyIdRemovalStatus { #[doc(hidden)] _Custom(String), } + +impl ThirdPartyIdRemovalStatus { + /// Creates a string slice from this `ThirdPartyIdRemovalStatus`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-client-api/src/r0/account/register.rs b/crates/ruma-client-api/src/r0/account/register.rs index a69e898d..1641296d 100644 --- a/crates/ruma-client-api/src/r0/account/register.rs +++ b/crates/ruma-client-api/src/r0/account/register.rs @@ -111,6 +111,7 @@ impl Response { /// The kind of account being registered. #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum RegistrationKind { /// A guest account /// @@ -129,6 +130,7 @@ impl Default for RegistrationKind { /// The login type. #[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum LoginType { /// An appservice-specific login type #[serde(rename = "m.login.application_service")] diff --git a/crates/ruma-client-api/src/r0/backup.rs b/crates/ruma-client-api/src/r0/backup.rs index 13a72023..8a79e11a 100644 --- a/crates/ruma-client-api/src/r0/backup.rs +++ b/crates/ruma-client-api/src/r0/backup.rs @@ -41,6 +41,7 @@ impl RoomKeyBackup { /// The algorithm used for storing backups. #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(tag = "algorithm", content = "auth_data")] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum BackupAlgorithm { /// `m.megolm_backup.v1.curve25519-aes-sha2` backup algorithm. #[serde(rename = "m.megolm_backup.v1.curve25519-aes-sha2")] diff --git a/crates/ruma-client-api/src/r0/capabilities.rs b/crates/ruma-client-api/src/r0/capabilities.rs index f34abfb6..61400877 100644 --- a/crates/ruma-client-api/src/r0/capabilities.rs +++ b/crates/ruma-client-api/src/r0/capabilities.rs @@ -161,9 +161,13 @@ impl Default for RoomVersionsCapability { } } -/// The stability of a room version +/// The stability of a room version. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "lowercase")] +#[non_exhaustive] pub enum RoomVersionStability { /// Support for the given version is stable. Stable, @@ -175,6 +179,13 @@ pub enum RoomVersionStability { _Custom(String), } +impl RoomVersionStability { + /// Creates a string slice from this `RoomVersionStability`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + #[cfg(test)] mod tests { use std::borrow::Cow; diff --git a/crates/ruma-client-api/src/r0/filter.rs b/crates/ruma-client-api/src/r0/filter.rs index 585d8452..5f99658c 100644 --- a/crates/ruma-client-api/src/r0/filter.rs +++ b/crates/ruma-client-api/src/r0/filter.rs @@ -15,8 +15,12 @@ use ruma_serde::{Outgoing, StringEnum}; use serde::Serialize; /// Format to use for returned events. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum EventFormat { /// Client format, as described in the Client API. Client, @@ -28,6 +32,13 @@ pub enum EventFormat { _Custom(String), } +impl EventFormat { + /// Creates a string slice from this `EventFormat`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + impl Default for EventFormat { fn default() -> Self { Self::Client diff --git a/crates/ruma-client-api/src/r0/filter/lazy_load.rs b/crates/ruma-client-api/src/r0/filter/lazy_load.rs index 934e8182..6402d705 100644 --- a/crates/ruma-client-api/src/r0/filter/lazy_load.rs +++ b/crates/ruma-client-api/src/r0/filter/lazy_load.rs @@ -6,6 +6,7 @@ use serde::{ser::SerializeStruct as _, Deserialize, Serialize, Serializer}; /// [lazy-loading]: https://matrix.org/docs/spec/client_server/r0.6.0#lazy-loading-room-members #[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize)] #[serde(from = "LazyLoadJsonRepr")] +#[allow(clippy::exhaustive_enums)] pub enum LazyLoadOptions { /// Disables lazy-loading of membership events. Disabled, diff --git a/crates/ruma-client-api/src/r0/filter/url.rs b/crates/ruma-client-api/src/r0/filter/url.rs index 3def41cf..a6d0b21b 100644 --- a/crates/ruma-client-api/src/r0/filter/url.rs +++ b/crates/ruma-client-api/src/r0/filter/url.rs @@ -5,6 +5,7 @@ use serde::{ /// Options for filtering based on the presence of a URL. #[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[allow(clippy::exhaustive_enums)] pub enum UrlFilter { /// Includes only events with a url key in their content. EventsWithUrl, diff --git a/crates/ruma-client-api/src/r0/media/get_content_thumbnail.rs b/crates/ruma-client-api/src/r0/media/get_content_thumbnail.rs index 0ac94fac..b29fafa9 100644 --- a/crates/ruma-client-api/src/r0/media/get_content_thumbnail.rs +++ b/crates/ruma-client-api/src/r0/media/get_content_thumbnail.rs @@ -5,20 +5,6 @@ use ruma_api::ruma_api; use ruma_identifiers::{Error, MxcUri, ServerName}; use ruma_serde::StringEnum; -/// The desired resizing method. -#[derive(Clone, Debug, StringEnum)] -#[ruma_enum(rename_all = "snake_case")] -pub enum Method { - /// Crop the original to produce the requested image dimensions. - Crop, - - /// Maintain the original aspect ratio of the source image. - Scale, - - #[doc(hidden)] - _Custom(String), -} - ruma_api! { metadata: { description: "Get a thumbnail of content from the media store.", @@ -98,3 +84,28 @@ impl Response { Self { file, content_type: None } } } + +/// The desired resizing method. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. +#[derive(Clone, Debug, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] +pub enum Method { + /// Crop the original to produce the requested image dimensions. + Crop, + + /// Maintain the original aspect ratio of the source image. + Scale, + + #[doc(hidden)] + _Custom(String), +} + +impl Method { + /// Creates a string slice from this `Method`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-client-api/src/r0/membership/get_member_events.rs b/crates/ruma-client-api/src/r0/membership/get_member_events.rs index a9911b42..84db2fb9 100644 --- a/crates/ruma-client-api/src/r0/membership/get_member_events.rs +++ b/crates/ruma-client-api/src/r0/membership/get_member_events.rs @@ -63,8 +63,12 @@ impl Response { } /// The kind of membership events to filter for. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "lowercase")] +#[non_exhaustive] pub enum MembershipEventFilter { /// The user has joined. Join, @@ -82,6 +86,13 @@ pub enum MembershipEventFilter { _Custom(String), } +impl MembershipEventFilter { + /// Creates a string slice from this `MembershipEventFilter`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + #[cfg(all(test, feature = "server"))] mod tests { use matches::assert_matches; diff --git a/crates/ruma-client-api/src/r0/message/get_message_events.rs b/crates/ruma-client-api/src/r0/message/get_message_events.rs index 6d9f9c13..08a41d80 100644 --- a/crates/ruma-client-api/src/r0/message/get_message_events.rs +++ b/crates/ruma-client-api/src/r0/message/get_message_events.rs @@ -121,6 +121,7 @@ fn is_default_limit(val: &UInt) -> bool { /// The direction to return events from. #[derive(Clone, Debug, Deserialize, Serialize)] +#[allow(clippy::exhaustive_enums)] pub enum Direction { /// Return events backwards in time from the requested `from` token. #[serde(rename = "b")] diff --git a/crates/ruma-client-api/src/r0/push.rs b/crates/ruma-client-api/src/r0/push.rs index a00e3142..3a7f921d 100644 --- a/crates/ruma-client-api/src/r0/push.rs +++ b/crates/ruma-client-api/src/r0/push.rs @@ -159,8 +159,12 @@ impl TryFrom for ConditionalPushRule { } /// The kinds of push rules that are available. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum RuleKind { /// User-configured rules that override all other kinds. Override, @@ -181,9 +185,20 @@ pub enum RuleKind { _Custom(String), } +impl RuleKind { + /// Creates a string slice from this `RuleKind`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + /// Which kind a pusher is. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum PusherKind { /// A pusher that sends HTTP pokes. Http, @@ -194,3 +209,10 @@ pub enum PusherKind { #[doc(hidden)] _Custom(String), } + +impl PusherKind { + /// Creates a string slice from this `PusherKind`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-client-api/src/r0/room.rs b/crates/ruma-client-api/src/r0/room.rs index f12edb0c..314330b9 100644 --- a/crates/ruma-client-api/src/r0/room.rs +++ b/crates/ruma-client-api/src/r0/room.rs @@ -9,8 +9,12 @@ pub mod upgrade_room; use ruma_serde::StringEnum; /// Whether or not a newly created room will be listed in the room directory. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum Visibility { /// Indicates that the room will be shown in the published room list. Public, @@ -22,6 +26,13 @@ pub enum Visibility { _Custom(String), } +impl Visibility { + /// Creates a string slice from this `Visibility`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + impl Default for Visibility { fn default() -> Self { Self::Private diff --git a/crates/ruma-client-api/src/r0/room/create_room.rs b/crates/ruma-client-api/src/r0/room/create_room.rs index c09d09a2..3ef528ec 100644 --- a/crates/ruma-client-api/src/r0/room/create_room.rs +++ b/crates/ruma-client-api/src/r0/room/create_room.rs @@ -195,8 +195,12 @@ impl Default for CreationContent { } /// A convenience parameter for setting a few default state events. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum RoomPreset { /// `join_rules` is set to `invite` and `history_visibility` is set to `shared`. PrivateChat, @@ -210,3 +214,10 @@ pub enum RoomPreset { #[doc(hidden)] _Custom(String), } + +impl RoomPreset { + /// Creates a string slice from this `RoomPreset`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-client-api/src/r0/search/search_events.rs b/crates/ruma-client-api/src/r0/search/search_events.rs index f356306a..d9a9843f 100644 --- a/crates/ruma-client-api/src/r0/search/search_events.rs +++ b/crates/ruma-client-api/src/r0/search/search_events.rs @@ -236,8 +236,12 @@ impl Grouping { } /// The key within events to use for this grouping. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum GroupingKey { /// `room_id` RoomId, @@ -249,6 +253,13 @@ pub enum GroupingKey { _Custom(String), } +impl GroupingKey { + /// Creates a string slice from this `GroupingKey`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + /// Requests that the server partitions the result set based on the provided list of keys. #[derive(Clone, Default, Debug, Outgoing, Serialize)] #[incoming_derive(Default)] @@ -272,7 +283,11 @@ impl Groupings<'_> { } /// The keys to search for. +/// +/// This type can hold an arbitrary string. To check for formats that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[non_exhaustive] pub enum SearchKeys { /// content.body #[ruma_enum(rename = "content.body")] @@ -290,6 +305,13 @@ pub enum SearchKeys { _Custom(String), } +impl SearchKeys { + /// Creates a string slice from this `SearchKeys`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + /// The order in which to search for results. #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] @@ -465,6 +487,7 @@ impl UserProfile { /// Represents either a room or user ID for returning grouped search results. #[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)] +#[allow(clippy::exhaustive_enums)] pub enum RoomIdOrUserId { /// Represents a room ID. RoomId(RoomId), diff --git a/crates/ruma-client-api/src/r0/session/login.rs b/crates/ruma-client-api/src/r0/session/login.rs index c40c13e6..5a107af3 100644 --- a/crates/ruma-client-api/src/r0/session/login.rs +++ b/crates/ruma-client-api/src/r0/session/login.rs @@ -78,6 +78,7 @@ impl Response { /// Identification information for the user. #[derive(Clone, Debug, PartialEq, Eq, Outgoing, Serialize)] #[serde(from = "user_serde::IncomingUserIdentifier", into = "user_serde::UserIdentifier<'_>")] +#[allow(clippy::exhaustive_enums)] pub enum UserIdentifier<'a> { /// Either a fully qualified Matrix user ID, or just the localpart (as part of the 'identifier' /// field). @@ -106,6 +107,7 @@ pub enum UserIdentifier<'a> { /// The authentication mechanism. #[derive(Clone, Debug, PartialEq, Eq, Outgoing, Serialize)] #[serde(tag = "type")] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum LoginInfo<'a> { /// An identifier and password are supplied to authenticate. #[serde(rename = "m.login.password")] diff --git a/crates/ruma-client-api/src/r0/sync/sync_events.rs b/crates/ruma-client-api/src/r0/sync/sync_events.rs index 62c9fb05..5a24efbf 100644 --- a/crates/ruma-client-api/src/r0/sync/sync_events.rs +++ b/crates/ruma-client-api/src/r0/sync/sync_events.rs @@ -134,6 +134,7 @@ impl Response { /// A filter represented either as its full JSON definition or the ID of a saved filter. #[derive(Clone, Debug, Outgoing, Serialize)] #[allow(clippy::large_enum_variant)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[serde(untagged)] pub enum Filter<'a> { // The filter definition needs to be (de)serialized twice because it is a URL-encoded JSON @@ -146,8 +147,8 @@ pub enum Filter<'a> { // FilterDefinition is the first variant, JSON decoding is attempted first which is almost // functionally equivalent to looking at whether the first symbol is a '{' as the spec says. // (there are probably some corner cases like leading whitespace) - #[serde(with = "ruma_serde::json_string")] /// A complete filter definition serialized to JSON. + #[serde(with = "ruma_serde::json_string")] FilterDefinition(FilterDefinition<'a>), /// The ID of a filter saved on the server. diff --git a/crates/ruma-client-api/src/r0/typing/create_typing_event.rs b/crates/ruma-client-api/src/r0/typing/create_typing_event.rs index 9efae616..e436643f 100644 --- a/crates/ruma-client-api/src/r0/typing/create_typing_event.rs +++ b/crates/ruma-client-api/src/r0/typing/create_typing_event.rs @@ -53,6 +53,7 @@ impl Response { /// A mark for whether the user is typing within a length of time or not. #[derive(Clone, Copy, Debug, Serialize)] #[serde(into = "TypingInner")] +#[allow(clippy::exhaustive_enums)] pub enum Typing { /// Not typing. No, diff --git a/crates/ruma-client-api/src/r0/uiaa.rs b/crates/ruma-client-api/src/r0/uiaa.rs index 58b62166..aff80dd2 100644 --- a/crates/ruma-client-api/src/r0/uiaa.rs +++ b/crates/ruma-client-api/src/r0/uiaa.rs @@ -116,6 +116,7 @@ impl AuthFlow { /// Contains either a User-Interactive Authentication API response body or a Matrix error. #[derive(Clone, Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum UiaaResponse { /// User-Interactive Authentication API response AuthResponse(UiaaInfo),