From 646764dc194ad3ea78cd5d871b100cb58eb67d12 Mon Sep 17 00:00:00 2001 From: Vilgot Fredenberg Date: Thu, 20 Jan 2022 00:15:59 +0100 Subject: [PATCH] refactor(http)!: impl `Display` directly on `RequestReactionType` (#1457) --- http/src/request/channel/reaction/mod.rs | 66 ++++++++---------------- http/src/routing/route_display.rs | 16 +++--- 2 files changed, 30 insertions(+), 52 deletions(-) diff --git a/http/src/request/channel/reaction/mod.rs b/http/src/request/channel/reaction/mod.rs index 9a812cec9d0..fe6fcb308af 100644 --- a/http/src/request/channel/reaction/mod.rs +++ b/http/src/request/channel/reaction/mod.rs @@ -34,35 +34,6 @@ pub enum RequestReactionType<'a> { }, } -impl<'a> RequestReactionType<'a> { - /// Create a display formatter for a reaction type resulting in a format - /// acceptable for use in URLs. - /// - /// # Examples - /// - /// Format the transgender flag for use in a URL: - /// - /// ``` - /// use twilight_http::request::channel::reaction::RequestReactionType; - /// - /// let reaction = RequestReactionType::Unicode { - /// name: "🏳️‍⚧️", - /// }; - /// - /// // Retrieve the display formatter. - /// let display = reaction.display(); - /// - /// // And now format it into a percent-encoded string and then check it. - /// assert_eq!( - /// "%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%E2%9A%A7%EF%B8%8F", - /// display.to_string(), - /// ); - /// ``` - pub const fn display(&'a self) -> RequestReactionTypeDisplay<'a> { - RequestReactionTypeDisplay(self) - } -} - /// Format a [`RequestReactionType`] into a format acceptable for use in URLs. /// /// # Examples @@ -78,18 +49,26 @@ impl<'a> RequestReactionType<'a> { /// name: Some("rarity"), /// }; /// -/// // Retrieve the display formatter. -/// let display = reaction.display(); +/// assert_eq!("rarity:123", reaction.to_string()); +/// ``` +/// +/// Format the transgeneder flag for use in a URL: /// -/// // And now format it into an acceptable string and then check it. -/// assert_eq!("rarity:123", display.to_string()); /// ``` -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct RequestReactionTypeDisplay<'a>(&'a RequestReactionType<'a>); - -impl Display for RequestReactionTypeDisplay<'_> { +/// use twilight_http::request::channel::reaction::RequestReactionType; +/// +/// let reaction = RequestReactionType::Unicode { +/// name: "🏳️‍⚧️", +/// }; +/// +/// assert_eq!( +/// "%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%E2%9A%A7%EF%B8%8F", +/// reaction.to_string(), +/// ); +/// ``` +impl Display for RequestReactionType<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - match self.0 { + match self { RequestReactionType::Custom { id, name } => { if let Some(name) = name { f.write_str(name)?; @@ -114,7 +93,7 @@ mod tests { // only be enabled on a module level. #![allow(clippy::non_ascii_literal)] - use super::{RequestReactionType, RequestReactionTypeDisplay}; + use super::RequestReactionType; use static_assertions::{assert_fields, assert_impl_all}; use std::{ fmt::{Debug, Display}, @@ -124,8 +103,7 @@ mod tests { assert_fields!(RequestReactionType::Custom: id, name); assert_fields!(RequestReactionType::Unicode: name); - assert_impl_all!(RequestReactionTypeDisplay<'_>: Clone, Copy, Debug, Display, Eq, PartialEq, Send, Sync); - assert_impl_all!(RequestReactionType<'_>: Clone, Copy, Debug, Eq, Hash, PartialEq, Send, Sync); + assert_impl_all!(RequestReactionType<'_>: Clone, Copy, Debug, Display, Eq, Hash, PartialEq, Send, Sync); #[test] fn test_display_custom_with_name() { @@ -134,7 +112,7 @@ mod tests { name: Some("foo"), }; - assert_eq!("foo:123", reaction.display().to_string()); + assert_eq!("foo:123", reaction.to_string()); } #[test] @@ -144,7 +122,7 @@ mod tests { name: None, }; - assert_eq!("e:123", reaction.display().to_string()); + assert_eq!("e:123", reaction.to_string()); } /// Test that unicode reactions format with percent encoding. @@ -158,7 +136,7 @@ mod tests { assert_eq!( "%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%F0%9F%8C%88", - reaction.display().to_string() + reaction.to_string() ); } } diff --git a/http/src/routing/route_display.rs b/http/src/routing/route_display.rs index bc916cd7a9a..3d4a2b963c6 100644 --- a/http/src/routing/route_display.rs +++ b/http/src/routing/route_display.rs @@ -251,7 +251,7 @@ impl Display for RouteDisplay<'_> { f.write_str("/messages/")?; Display::fmt(message_id, f)?; f.write_str("/reactions/")?; - Display::fmt(&emoji.display(), f)?; + Display::fmt(&emoji, f)?; f.write_str("/@me") } @@ -439,7 +439,7 @@ impl Display for RouteDisplay<'_> { Display::fmt(message_id, f)?; f.write_str("/reactions/")?; - Display::fmt(&emoji.display(), f) + Display::fmt(&emoji, f) } Route::DeleteMessage { channel_id, @@ -490,7 +490,7 @@ impl Display for RouteDisplay<'_> { f.write_str("/messages/")?; Display::fmt(message_id, f)?; f.write_str("/reactions/")?; - Display::fmt(&emoji.display(), f)?; + Display::fmt(&emoji, f)?; f.write_str("/")?; Display::fmt(user_id, f) @@ -1067,7 +1067,7 @@ impl Display for RouteDisplay<'_> { f.write_str("/messages/")?; Display::fmt(message_id, f)?; f.write_str("/reactions/")?; - Display::fmt(&emoji.display(), f)?; + Display::fmt(&emoji, f)?; f.write_str("?")?; if let Some(after) = after { @@ -1685,7 +1685,7 @@ mod tests { format!( "channels/{channel_id}/messages/{message_id}/reactions/{emoji}/@me", channel_id = CHANNEL_ID, - emoji = emoji.display(), + emoji = emoji, message_id = MESSAGE_ID ) ); @@ -1705,7 +1705,7 @@ mod tests { format!( "channels/{channel_id}/messages/{message_id}/reactions/{emoji}/@me", channel_id = CHANNEL_ID, - emoji = emoji.display(), + emoji = emoji, message_id = MESSAGE_ID ) ); @@ -2190,7 +2190,7 @@ mod tests { format!( "channels/{channel_id}/messages/{message_id}/reactions/{emoji}", channel_id = CHANNEL_ID, - emoji = emoji.display(), + emoji = emoji, message_id = MESSAGE_ID ) ); @@ -2305,7 +2305,7 @@ mod tests { format!( "channels/{channel_id}/messages/{message_id}/reactions/{emoji}/{user_id}", channel_id = CHANNEL_ID, - emoji = emoji.display(), + emoji = emoji, message_id = MESSAGE_ID, user_id = USER_ID )