common: Make all pub enums non_exhaustive

This commit is contained in:
Devin Ragotzy 2021-06-29 14:41:07 -07:00 committed by Jonas Platte
parent 28e3d0f277
commit 74b6a4c9d8
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
8 changed files with 59 additions and 0 deletions

View File

@ -3,7 +3,11 @@
use ruma_serde::StringEnum; use ruma_serde::StringEnum;
/// Access token types. /// 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)] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[non_exhaustive]
pub enum TokenType { pub enum TokenType {
/// Bearer token type /// Bearer token type
Bearer, Bearer,
@ -11,3 +15,10 @@ pub enum TokenType {
#[doc(hidden)] #[doc(hidden)]
_Custom(String), _Custom(String),
} }
impl TokenType {
/// Creates a string slice from this `TokenType`.
pub fn as_str(&self) -> &str {
self.as_ref()
}
}

View File

@ -5,8 +5,12 @@
use ruma_serde::StringEnum; use ruma_serde::StringEnum;
/// A description of a user's connectivity and availability for chat. /// 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)] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "snake_case")] #[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PresenceState { pub enum PresenceState {
/// Disconnected from the service. /// Disconnected from the service.
Offline, Offline,
@ -32,3 +36,10 @@ impl Default for &'_ PresenceState {
&PresenceState::Online &PresenceState::Online
} }
} }
impl PresenceState {
/// Creates a string slice from this `PresenceState`.
pub fn as_str(&self) -> &str {
self.as_ref()
}
}

View File

@ -411,9 +411,13 @@ impl PusherData {
/// A special format that the homeserver should use when sending notifications to a Push Gateway. /// 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]. /// 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 /// [spec]: https://matrix.org/docs/spec/push_gateway/r0.1.1#homeserver-behaviour
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "snake_case")] #[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PushFormat { pub enum PushFormat {
/// Require the homeserver to only send a reduced set of fields in the push. /// Require the homeserver to only send a reduced set of fields in the push.
EventIdOnly, EventIdOnly,
@ -422,6 +426,13 @@ pub enum PushFormat {
_Custom(String), _Custom(String),
} }
impl PushFormat {
/// Creates a string slice from this `PushFormat`.
pub fn as_str(&self) -> &str {
self.as_ref()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::BTreeMap; use std::collections::BTreeMap;

View File

@ -11,6 +11,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// ///
/// Used by `RoomMemberCountIs`. Defaults to `==`. /// Used by `RoomMemberCountIs`. Defaults to `==`.
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum ComparisonOperator { pub enum ComparisonOperator {
/// Equals /// Equals
Eq, Eq,

View File

@ -7,6 +7,7 @@ use super::{
/// The kinds of push rules that are available. /// The kinds of push rules that are available.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum AnyPushRule { pub enum AnyPushRule {
/// Rules that override all other kinds. /// Rules that override all other kinds.
Override(ConditionalPushRule), Override(ConditionalPushRule),
@ -114,6 +115,7 @@ impl IntoIterator for Ruleset {
/// Reference to any kind of push rule. /// Reference to any kind of push rule.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub enum AnyPushRuleRef<'a> { pub enum AnyPushRuleRef<'a> {
/// Rules that override all other kinds. /// Rules that override all other kinds.
Override(&'a ConditionalPushRule), Override(&'a ConditionalPushRule),

View File

@ -3,7 +3,11 @@
use ruma_serde::{OrdAsRefStr, PartialEqAsRefStr, PartialOrdAsRefStr, StringEnum}; use ruma_serde::{OrdAsRefStr, PartialEqAsRefStr, PartialOrdAsRefStr, StringEnum};
/// The type of receipt. /// 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)] #[derive(Clone, Debug, PartialOrdAsRefStr, OrdAsRefStr, PartialEqAsRefStr, Eq, StringEnum)]
#[non_exhaustive]
pub enum ReceiptType { pub enum ReceiptType {
/// m.read /// m.read
#[ruma_enum(rename = "m.read")] #[ruma_enum(rename = "m.read")]
@ -12,3 +16,10 @@ pub enum ReceiptType {
#[doc(hidden)] #[doc(hidden)]
_Custom(String), _Custom(String),
} }
impl ReceiptType {
/// Creates a string slice from this `ReceiptType`.
pub fn as_str(&self) -> &str {
self.as_ref()
}
}

View File

@ -211,8 +211,12 @@ impl User {
} }
/// The medium of a third party identifier. /// 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)] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "lowercase")] #[ruma_enum(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Medium { pub enum Medium {
/// Email address identifier /// Email address identifier
Email, Email,
@ -224,6 +228,13 @@ pub enum Medium {
_Custom(String), _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. /// An identifier external to Matrix.
/// ///
/// To create an instance of this type, first create a `ThirdPartyIdentifierInit` and convert it to /// To create an instance of this type, first create a `ThirdPartyIdentifierInit` and convert it to

View File

@ -15,6 +15,7 @@ use serde::{
/// Represents one or all of a user's devices. /// Represents one or all of a user's devices.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::exhaustive_enums)]
pub enum DeviceIdOrAllDevices { pub enum DeviceIdOrAllDevices {
/// Represents a device Id for one of a user's devices. /// Represents a device Id for one of a user's devices.
DeviceId(DeviceIdBox), DeviceId(DeviceIdBox),