From 240c8bc4b883bfa0333ee70cb616c558898d7f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Mon, 30 May 2022 15:19:53 +0200 Subject: [PATCH] macros: Derive `as_str()` for types that derive StringEnum --- crates/ruma-client-api/src/account.rs | 7 ---- .../src/discovery/get_capabilities.rs | 7 ---- crates/ruma-client-api/src/filter.rs | 15 -------- .../src/media/get_content_thumbnail.rs | 7 ---- .../src/membership/get_member_events.rs | 7 ---- crates/ruma-client-api/src/push.rs | 14 -------- crates/ruma-client-api/src/room.rs | 7 ---- .../ruma-client-api/src/room/create_room.rs | 7 ---- .../src/search/search_events.rs | 14 -------- crates/ruma-common/src/authentication.rs | 7 ---- crates/ruma-common/src/directory.rs | 7 ---- crates/ruma-common/src/events/call/hangup.rs | 5 --- .../src/events/key/verification.rs | 35 ------------------- .../src/events/key/verification/cancel.rs | 7 ---- crates/ruma-common/src/events/policy/rule.rs | 7 ---- .../src/events/room/guest_access.rs | 7 ---- .../src/events/room/history_visibility.rs | 7 ---- crates/ruma-common/src/events/room/member.rs | 7 ---- crates/ruma-common/src/events/room/message.rs | 21 ----------- .../src/events/room/message/feedback.rs | 7 ---- .../src/events/room_key_request.rs | 7 ---- crates/ruma-common/src/presence.rs | 7 ---- crates/ruma-common/src/push.rs | 7 ---- crates/ruma-common/src/receipt.rs | 7 ---- crates/ruma-common/src/room.rs | 7 ---- crates/ruma-common/src/thirdparty.rs | 7 ---- .../src/query/get_profile_information.rs | 7 ---- crates/ruma-macros/src/lib.rs | 2 ++ .../src/send_event_notification.rs | 7 ---- 29 files changed, 2 insertions(+), 258 deletions(-) diff --git a/crates/ruma-client-api/src/account.rs b/crates/ruma-client-api/src/account.rs index 8680ae79..72d181ea 100644 --- a/crates/ruma-client-api/src/account.rs +++ b/crates/ruma-client-api/src/account.rs @@ -59,10 +59,3 @@ pub enum ThirdPartyIdRemovalStatus { #[doc(hidden)] _Custom(PrivOwnedStr), } - -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/discovery/get_capabilities.rs b/crates/ruma-client-api/src/discovery/get_capabilities.rs index f4b07b9e..9248ae35 100644 --- a/crates/ruma-client-api/src/discovery/get_capabilities.rs +++ b/crates/ruma-client-api/src/discovery/get_capabilities.rs @@ -212,13 +212,6 @@ pub enum RoomVersionStability { _Custom(PrivOwnedStr), } -impl RoomVersionStability { - /// Creates a string slice from this `RoomVersionStability`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Information about the `m.set_displayname` capability #[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-client-api/src/filter.rs b/crates/ruma-client-api/src/filter.rs index 0d6cc083..fad7ca72 100644 --- a/crates/ruma-client-api/src/filter.rs +++ b/crates/ruma-client-api/src/filter.rs @@ -33,13 +33,6 @@ pub enum EventFormat { _Custom(PrivOwnedStr), } -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 @@ -70,14 +63,6 @@ pub enum RelationType { _Custom(PrivOwnedStr), } -#[cfg(feature = "unstable-msc3440")] -impl RelationType { - /// Creates a string slice from this `RelationType`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Filters to be applied to room events. #[derive(Clone, Debug, Default, Incoming, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-client-api/src/media/get_content_thumbnail.rs b/crates/ruma-client-api/src/media/get_content_thumbnail.rs index 9b9a3d38..f1819b57 100644 --- a/crates/ruma-client-api/src/media/get_content_thumbnail.rs +++ b/crates/ruma-client-api/src/media/get_content_thumbnail.rs @@ -135,11 +135,4 @@ pub mod v3 { #[doc(hidden)] _Custom(PrivOwnedStr), } - - 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/membership/get_member_events.rs b/crates/ruma-client-api/src/membership/get_member_events.rs index 3479bacb..34a54176 100644 --- a/crates/ruma-client-api/src/membership/get_member_events.rs +++ b/crates/ruma-client-api/src/membership/get_member_events.rs @@ -100,13 +100,6 @@ pub mod v3 { _Custom(PrivOwnedStr), } - 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 assert_matches::assert_matches; diff --git a/crates/ruma-client-api/src/push.rs b/crates/ruma-client-api/src/push.rs index 4586fa14..b0271676 100644 --- a/crates/ruma-client-api/src/push.rs +++ b/crates/ruma-client-api/src/push.rs @@ -189,13 +189,6 @@ pub enum RuleKind { _Custom(PrivOwnedStr), } -impl RuleKind { - /// Creates a string slice from this `RuleKind`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Which kind a pusher is. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -211,10 +204,3 @@ pub enum PusherKind { #[doc(hidden)] _Custom(PrivOwnedStr), } - -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/room.rs b/crates/ruma-client-api/src/room.rs index cfefdd06..352156ec 100644 --- a/crates/ruma-client-api/src/room.rs +++ b/crates/ruma-client-api/src/room.rs @@ -26,13 +26,6 @@ pub enum Visibility { _Custom(PrivOwnedStr), } -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/room/create_room.rs b/crates/ruma-client-api/src/room/create_room.rs index 1e91af34..3a4f6159 100644 --- a/crates/ruma-client-api/src/room/create_room.rs +++ b/crates/ruma-client-api/src/room/create_room.rs @@ -203,11 +203,4 @@ pub mod v3 { #[doc(hidden)] _Custom(PrivOwnedStr), } - - 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/search/search_events.rs b/crates/ruma-client-api/src/search/search_events.rs index fc634087..bbfa698e 100644 --- a/crates/ruma-client-api/src/search/search_events.rs +++ b/crates/ruma-client-api/src/search/search_events.rs @@ -265,13 +265,6 @@ pub mod v3 { _Custom(PrivOwnedStr), } - 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, Incoming, Serialize)] #[incoming_derive(Default)] @@ -315,13 +308,6 @@ pub mod v3 { _Custom(PrivOwnedStr), } - 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. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] diff --git a/crates/ruma-common/src/authentication.rs b/crates/ruma-common/src/authentication.rs index b2189fd0..30ebf374 100644 --- a/crates/ruma-common/src/authentication.rs +++ b/crates/ruma-common/src/authentication.rs @@ -13,10 +13,3 @@ pub enum TokenType { #[doc(hidden)] _Custom(PrivOwnedStr), } - -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/directory.rs b/crates/ruma-common/src/directory.rs index baa2b067..c5bced55 100644 --- a/crates/ruma-common/src/directory.rs +++ b/crates/ruma-common/src/directory.rs @@ -248,13 +248,6 @@ pub enum PublicRoomJoinRule { _Custom(PrivOwnedStr), } -impl PublicRoomJoinRule { - /// Returns the string name of this `PublicRoomJoinRule`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - impl Default for PublicRoomJoinRule { fn default() -> Self { Self::Public diff --git a/crates/ruma-common/src/events/call/hangup.rs b/crates/ruma-common/src/events/call/hangup.rs index 1061d54f..478e1f84 100644 --- a/crates/ruma-common/src/events/call/hangup.rs +++ b/crates/ruma-common/src/events/call/hangup.rs @@ -126,11 +126,6 @@ pub enum Reason { } impl Reason { - /// Creates a string slice from this `Reason`. - pub fn as_str(&self) -> &str { - self.as_ref() - } - #[cfg(feature = "unstable-msc2746")] fn serialize_option_with_default( reason: &Option, diff --git a/crates/ruma-common/src/events/key/verification.rs b/crates/ruma-common/src/events/key/verification.rs index 8a41000f..026ba212 100644 --- a/crates/ruma-common/src/events/key/verification.rs +++ b/crates/ruma-common/src/events/key/verification.rs @@ -33,13 +33,6 @@ pub enum HashAlgorithm { _Custom(PrivOwnedStr), } -impl HashAlgorithm { - /// Creates a string slice from this `HashAlgorithm`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// A key agreement protocol. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -56,13 +49,6 @@ pub enum KeyAgreementProtocol { _Custom(PrivOwnedStr), } -impl KeyAgreementProtocol { - /// Creates a string slice from this `KeyAgreementProtocol`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// A message authentication code algorithm. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -79,13 +65,6 @@ pub enum MessageAuthenticationCode { _Custom(PrivOwnedStr), } -impl MessageAuthenticationCode { - /// Creates a string slice from this `MessageAuthenticationCode`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// A Short Authentication String method. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -102,13 +81,6 @@ pub enum ShortAuthenticationString { _Custom(PrivOwnedStr), } -impl ShortAuthenticationString { - /// Creates a string slice from this `ShortAuthenticationString`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// A relation which associates an `m.key.verification.request` with another key verification event. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] @@ -150,13 +122,6 @@ pub enum VerificationMethod { _Custom(PrivOwnedStr), } -impl VerificationMethod { - /// Creates a string slice from this `VerificationMethod`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - #[cfg(test)] mod tests { use serde_json::{from_value as from_json_value, json}; diff --git a/crates/ruma-common/src/events/key/verification/cancel.rs b/crates/ruma-common/src/events/key/verification/cancel.rs index 316c5ccd..1f37a518 100644 --- a/crates/ruma-common/src/events/key/verification/cancel.rs +++ b/crates/ruma-common/src/events/key/verification/cancel.rs @@ -127,13 +127,6 @@ pub enum CancelCode { _Custom(PrivOwnedStr), } -impl CancelCode { - /// Creates a string slice from this `CancelCode`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - #[cfg(test)] mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; diff --git a/crates/ruma-common/src/events/policy/rule.rs b/crates/ruma-common/src/events/policy/rule.rs index 5fa30a9b..959f9219 100644 --- a/crates/ruma-common/src/events/policy/rule.rs +++ b/crates/ruma-common/src/events/policy/rule.rs @@ -44,10 +44,3 @@ pub enum Recommendation { #[doc(hidden)] _Custom(PrivOwnedStr), } - -impl Recommendation { - /// Creates a string slice from this `Recommendation`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} diff --git a/crates/ruma-common/src/events/room/guest_access.rs b/crates/ruma-common/src/events/room/guest_access.rs index 9eaab67c..2d36d4bd 100644 --- a/crates/ruma-common/src/events/room/guest_access.rs +++ b/crates/ruma-common/src/events/room/guest_access.rs @@ -63,10 +63,3 @@ pub enum GuestAccess { #[doc(hidden)] _Custom(PrivOwnedStr), } - -impl GuestAccess { - /// Creates a string slice from this `GuestAccess`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} diff --git a/crates/ruma-common/src/events/room/history_visibility.rs b/crates/ruma-common/src/events/room/history_visibility.rs index 165d15f5..a5078dd4 100644 --- a/crates/ruma-common/src/events/room/history_visibility.rs +++ b/crates/ruma-common/src/events/room/history_visibility.rs @@ -79,10 +79,3 @@ pub enum HistoryVisibility { #[doc(hidden)] _Custom(PrivOwnedStr), } - -impl HistoryVisibility { - /// Creates a string slice from this `HistoryVisibility`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} diff --git a/crates/ruma-common/src/events/room/member.rs b/crates/ruma-common/src/events/room/member.rs index ecbeafa3..8dbe0a47 100644 --- a/crates/ruma-common/src/events/room/member.rs +++ b/crates/ruma-common/src/events/room/member.rs @@ -261,13 +261,6 @@ pub enum MembershipState { _Custom(PrivOwnedStr), } -impl MembershipState { - /// Creates a string slice from this `MembershipState`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Information about a third party invitation. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-common/src/events/room/message.rs b/crates/ruma-common/src/events/room/message.rs index 8f4bc6fe..15892af4 100644 --- a/crates/ruma-common/src/events/room/message.rs +++ b/crates/ruma-common/src/events/room/message.rs @@ -1484,13 +1484,6 @@ pub enum ServerNoticeType { _Custom(PrivOwnedStr), } -impl ServerNoticeType { - /// Creates a string slice from this `ServerNoticeType`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Types of usage limits. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -1507,13 +1500,6 @@ pub enum LimitType { _Custom(PrivOwnedStr), } -impl LimitType { - /// Creates a string slice from this `LimitType`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// The format for the formatted representation of a message body. #[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))] #[derive(Clone, Debug, PartialEq, Eq, StringEnum)] @@ -1527,13 +1513,6 @@ pub enum MessageFormat { _Custom(PrivOwnedStr), } -impl MessageFormat { - /// Creates a string slice from this `MessageFormat`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Common message event content fields for message types that have separate plain-text and /// formatted representations. #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/crates/ruma-common/src/events/room/message/feedback.rs b/crates/ruma-common/src/events/room/message/feedback.rs index 7f3d462b..853fb5f4 100644 --- a/crates/ruma-common/src/events/room/message/feedback.rs +++ b/crates/ruma-common/src/events/room/message/feedback.rs @@ -47,10 +47,3 @@ pub enum FeedbackType { #[doc(hidden)] _Custom(PrivOwnedStr), } - -impl FeedbackType { - /// Creates a string slice from this `FeedbackType`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} diff --git a/crates/ruma-common/src/events/room_key_request.rs b/crates/ruma-common/src/events/room_key_request.rs index 63c301df..d31761ff 100644 --- a/crates/ruma-common/src/events/room_key_request.rs +++ b/crates/ruma-common/src/events/room_key_request.rs @@ -63,13 +63,6 @@ pub enum Action { _Custom(PrivOwnedStr), } -impl Action { - /// Creates a string slice from this `Action`. - pub fn as_str(&self) -> &str { - self.as_ref() - } -} - /// Information about a requested key. #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] diff --git a/crates/ruma-common/src/presence.rs b/crates/ruma-common/src/presence.rs index 5b96f2b4..db1739f6 100644 --- a/crates/ruma-common/src/presence.rs +++ b/crates/ruma-common/src/presence.rs @@ -34,10 +34,3 @@ 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 8fdffbce..ddf92c3d 100644 --- a/crates/ruma-common/src/push.rs +++ b/crates/ruma-common/src/push.rs @@ -463,13 +463,6 @@ pub enum PushFormat { _Custom(PrivOwnedStr), } -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/receipt.rs b/crates/ruma-common/src/receipt.rs index a8c2300f..2b074298 100644 --- a/crates/ruma-common/src/receipt.rs +++ b/crates/ruma-common/src/receipt.rs @@ -17,10 +17,3 @@ pub enum ReceiptType { #[doc(hidden)] _Custom(PrivOwnedStr), } - -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/room.rs b/crates/ruma-common/src/room.rs index 4cec6769..e2c7d0b5 100644 --- a/crates/ruma-common/src/room.rs +++ b/crates/ruma-common/src/room.rs @@ -15,10 +15,3 @@ pub enum RoomType { #[doc(hidden)] _Custom(PrivOwnedStr), } - -impl RoomType { - /// Creates a string slice from this `RoomType`. - 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 b93dfed0..609d3c13 100644 --- a/crates/ruma-common/src/thirdparty.rs +++ b/crates/ruma-common/src/thirdparty.rs @@ -234,13 +234,6 @@ pub enum Medium { _Custom(PrivOwnedStr), } -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-federation-api/src/query/get_profile_information.rs b/crates/ruma-federation-api/src/query/get_profile_information.rs index 803a6501..025460ef 100644 --- a/crates/ruma-federation-api/src/query/get_profile_information.rs +++ b/crates/ruma-federation-api/src/query/get_profile_information.rs @@ -98,11 +98,4 @@ pub mod v1 { #[doc(hidden)] _Custom(PrivOwnedStr), } - - impl ProfileField { - /// Creates a string slice from this `ProfileField`. - pub fn as_str(&self) -> &str { - self.as_ref() - } - } } diff --git a/crates/ruma-macros/src/lib.rs b/crates/ruma-macros/src/lib.rs index 113b76e6..9f85c629 100644 --- a/crates/ruma-macros/src/lib.rs +++ b/crates/ruma-macros/src/lib.rs @@ -328,6 +328,7 @@ pub fn derive_string_enum(input: TokenStream) -> TokenStream { fn expand_all(input: ItemEnum) -> syn::Result { let as_ref_str_impl = expand_enum_as_ref_str(&input)?; let from_string_impl = expand_enum_from_string(&input)?; + let as_str_impl = expand_as_str_as_ref_str(&input.ident)?; let display_impl = expand_display_as_ref_str(&input.ident)?; let serialize_impl = expand_serialize_as_ref_str(&input.ident)?; let deserialize_impl = expand_deserialize_from_cow_str(&input.ident)?; @@ -335,6 +336,7 @@ pub fn derive_string_enum(input: TokenStream) -> TokenStream { Ok(quote! { #as_ref_str_impl #from_string_impl + #as_str_impl #display_impl #serialize_impl #deserialize_impl diff --git a/crates/ruma-push-gateway-api/src/send_event_notification.rs b/crates/ruma-push-gateway-api/src/send_event_notification.rs index 430a7766..d2a5dd46 100644 --- a/crates/ruma-push-gateway-api/src/send_event_notification.rs +++ b/crates/ruma-push-gateway-api/src/send_event_notification.rs @@ -162,13 +162,6 @@ pub mod v1 { _Custom(PrivOwnedStr), } - impl NotificationPriority { - /// Creates a string slice from this `NotificationPriority`. - pub fn as_str(&self) -> &str { - self.as_ref() - } - } - impl Default for NotificationPriority { fn default() -> Self { Self::High