From 74b6a4c9d8f4cd3b8b511eab06722608b091656b Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Tue, 29 Jun 2021 14:41:07 -0700 Subject: [PATCH] common: Make all pub enums non_exhaustive --- crates/ruma-common/src/authentication.rs | 11 +++++++++++ crates/ruma-common/src/presence.rs | 11 +++++++++++ crates/ruma-common/src/push.rs | 11 +++++++++++ .../src/push/condition/room_member_count_is.rs | 1 + crates/ruma-common/src/push/iter.rs | 2 ++ crates/ruma-common/src/receipt.rs | 11 +++++++++++ crates/ruma-common/src/thirdparty.rs | 11 +++++++++++ crates/ruma-common/src/to_device.rs | 1 + 8 files changed, 59 insertions(+) diff --git a/crates/ruma-common/src/authentication.rs b/crates/ruma-common/src/authentication.rs index 1bd2bafe..f980e77c 100644 --- a/crates/ruma-common/src/authentication.rs +++ b/crates/ruma-common/src/authentication.rs @@ -3,7 +3,11 @@ use ruma_serde::StringEnum; /// Access token types. +/// +/// 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 TokenType { /// Bearer token type Bearer, @@ -11,3 +15,10 @@ pub enum TokenType { #[doc(hidden)] _Custom(String), } + +impl TokenType { + /// Creates a string slice from this `TokenType`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-common/src/presence.rs b/crates/ruma-common/src/presence.rs index ccf2212c..8dec4d31 100644 --- a/crates/ruma-common/src/presence.rs +++ b/crates/ruma-common/src/presence.rs @@ -5,8 +5,12 @@ use ruma_serde::StringEnum; /// A description of a user's connectivity and availability for chat. +/// +/// 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 PresenceState { /// Disconnected from the service. Offline, @@ -32,3 +36,10 @@ impl Default for &'_ PresenceState { &PresenceState::Online } } + +impl PresenceState { + /// Creates a string slice from this `PresenceState`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-common/src/push.rs b/crates/ruma-common/src/push.rs index ec6304e4..33c1c736 100644 --- a/crates/ruma-common/src/push.rs +++ b/crates/ruma-common/src/push.rs @@ -411,9 +411,13 @@ impl PusherData { /// A special format that the homeserver should use when sending notifications to a Push Gateway. /// Currently, only "event_id_only" is supported as of [Push Gateway API r0.1.1][spec]. /// +/// 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()`. +/// /// [spec]: https://matrix.org/docs/spec/push_gateway/r0.1.1#homeserver-behaviour #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[ruma_enum(rename_all = "snake_case")] +#[non_exhaustive] pub enum PushFormat { /// Require the homeserver to only send a reduced set of fields in the push. EventIdOnly, @@ -422,6 +426,13 @@ pub enum PushFormat { _Custom(String), } +impl PushFormat { + /// Creates a string slice from this `PushFormat`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + #[cfg(test)] mod tests { use std::collections::BTreeMap; diff --git a/crates/ruma-common/src/push/condition/room_member_count_is.rs b/crates/ruma-common/src/push/condition/room_member_count_is.rs index 580b79a2..f88be8af 100644 --- a/crates/ruma-common/src/push/condition/room_member_count_is.rs +++ b/crates/ruma-common/src/push/condition/room_member_count_is.rs @@ -11,6 +11,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; /// /// Used by `RoomMemberCountIs`. Defaults to `==`. #[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[allow(clippy::exhaustive_enums)] pub enum ComparisonOperator { /// Equals Eq, diff --git a/crates/ruma-common/src/push/iter.rs b/crates/ruma-common/src/push/iter.rs index d1bdf4c3..d78501b0 100644 --- a/crates/ruma-common/src/push/iter.rs +++ b/crates/ruma-common/src/push/iter.rs @@ -7,6 +7,7 @@ use super::{ /// The kinds of push rules that are available. #[derive(Clone, Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum AnyPushRule { /// Rules that override all other kinds. Override(ConditionalPushRule), @@ -114,6 +115,7 @@ impl IntoIterator for Ruleset { /// Reference to any kind of push rule. #[derive(Clone, Copy, Debug)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub enum AnyPushRuleRef<'a> { /// Rules that override all other kinds. Override(&'a ConditionalPushRule), diff --git a/crates/ruma-common/src/receipt.rs b/crates/ruma-common/src/receipt.rs index f8874e2f..2e0fc00a 100644 --- a/crates/ruma-common/src/receipt.rs +++ b/crates/ruma-common/src/receipt.rs @@ -3,7 +3,11 @@ use ruma_serde::{OrdAsRefStr, PartialEqAsRefStr, PartialOrdAsRefStr, StringEnum}; /// The type of receipt. +/// +/// 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, PartialOrdAsRefStr, OrdAsRefStr, PartialEqAsRefStr, Eq, StringEnum)] +#[non_exhaustive] pub enum ReceiptType { /// m.read #[ruma_enum(rename = "m.read")] @@ -12,3 +16,10 @@ pub enum ReceiptType { #[doc(hidden)] _Custom(String), } + +impl ReceiptType { + /// Creates a string slice from this `ReceiptType`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} diff --git a/crates/ruma-common/src/thirdparty.rs b/crates/ruma-common/src/thirdparty.rs index 2e516c33..e3088841 100644 --- a/crates/ruma-common/src/thirdparty.rs +++ b/crates/ruma-common/src/thirdparty.rs @@ -211,8 +211,12 @@ impl User { } /// The medium of a third party identifier. +/// +/// 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 Medium { /// Email address identifier Email, @@ -224,6 +228,13 @@ pub enum Medium { _Custom(String), } +impl Medium { + /// Creates a string slice from this `Medium`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + /// An identifier external to Matrix. /// /// To create an instance of this type, first create a `ThirdPartyIdentifierInit` and convert it to diff --git a/crates/ruma-common/src/to_device.rs b/crates/ruma-common/src/to_device.rs index aa977eef..59aa6705 100644 --- a/crates/ruma-common/src/to_device.rs +++ b/crates/ruma-common/src/to_device.rs @@ -15,6 +15,7 @@ use serde::{ /// Represents one or all of a user's devices. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[allow(clippy::exhaustive_enums)] pub enum DeviceIdOrAllDevices { /// Represents a device Id for one of a user's devices. DeviceId(DeviceIdBox),