Skip to content

Commit

Permalink
refactor(http)!: impl Display directly on RequestReactionType (#1457
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vilgotf authored Jan 19, 2022
1 parent 7966668 commit 646764d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.
66 changes: 22 additions & 44 deletions http/src/request/channel/reaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)?;
Expand All @@ -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},
Expand All @@ -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() {
Expand All @@ -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]
Expand All @@ -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.
Expand All @@ -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()
);
}
}
16 changes: 8 additions & 8 deletions http/src/routing/route_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)
);
Expand All @@ -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
)
);
Expand Down Expand Up @@ -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
)
);
Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit 646764d

Please sign in to comment.