aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-06-04 12:36:08 -0700
committerZeyla Hellyer <[email protected]>2017-06-04 14:38:24 -0700
commit8360f329eae1751a8a413a6f6838486f3a0bba01 (patch)
treeb0970a0314d6f2cd1edb6ccbd2b46e3d6abafa18 /src
parentAdd CurrentUser::default_avatar_url (diff)
downloadserenity-8360f329eae1751a8a413a6f6838486f3a0bba01.tar.xz
serenity-8360f329eae1751a8a413a6f6838486f3a0bba01.zip
Move user avatar method logic out
The logic for the `User` and `CurrentUser` avatar methods (`avatar_url`, `default_avatar_url`, `static_avatar_url`) were duplicated, so move the logic out and have each method simply call the related function.
Diffstat (limited to 'src')
-rw-r--r--src/model/user.rs60
1 files changed, 34 insertions, 26 deletions
diff --git a/src/model/user.rs b/src/model/user.rs
index d3f64f1..5d0d3b6 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -59,24 +59,16 @@ impl CurrentUser {
/// None => println!("{} does not have an avatar set.", user.name)
/// }
/// ```
+ #[inline]
pub fn avatar_url(&self) -> Option<String> {
- self.avatar.as_ref()
- .map(|av| {
- let ext = if av.starts_with("a_") {
- "gif"
- } else {
- "webp"
- };
-
- format!(cdn!("/avatars/{}/{}.{}?size=1024"), self.id.0, av, ext)
- })
+ avatar_url(self.id, self.avatar.as_ref())
}
/// Returns the formatted URL to the user's default avatar URL.
///
/// This will produce a PNG URL.
pub fn default_avatar_url(&self) -> String {
- cdn!("/embed/avatars/{}.png", self.discriminator % 5u16).to_owned()
+ default_avatar_url(self.discriminator)
}
/// Alias of [`tag`].
@@ -169,9 +161,9 @@ impl CurrentUser {
/// None => println!("Could not get static avatar for {}.", user.name)
/// }
/// ```
+ #[inline]
pub fn static_avatar_url(&self) -> Option<String> {
- self.avatar.as_ref()
- .map(|av| format!(cdn!("/avatars/{}/{}.webp?size=1024"), self.id.0, av))
+ static_avatar_url(self.id, self.avatar.as_ref())
}
/// Returns the invite url for the bot with the given permissions.
@@ -357,17 +349,9 @@ impl User {
/// Returns the formatted URL of the user's icon, if one exists.
///
/// This will produce a WEBP image URL, or GIF if the user has a GIF avatar.
+ #[inline]
pub fn avatar_url(&self) -> Option<String> {
- self.avatar.as_ref()
- .map(|av| {
- let ext = if av.starts_with("a_") {
- "gif"
- } else {
- "webp"
- };
-
- format!(cdn!("/avatars/{}/{}.{}?size=1024"), self.id.0, av, ext)
- })
+ avatar_url(self.id, self.avatar.as_ref())
}
/// Creates a direct message channel between the [current user] and the
@@ -397,8 +381,9 @@ impl User {
/// Returns the formatted URL to the user's default avatar URL.
///
/// This will produce a PNG URL.
+ #[inline]
pub fn default_avatar_url(&self) -> String {
- cdn!("/embed/avatars/{}.png", self.discriminator % 5u16).to_owned()
+ default_avatar_url(self.discriminator)
}
/// Sends a message to a user through a direct message channel. This is a
@@ -611,9 +596,9 @@ impl User {
/// Returns a static formatted URL of the user's icon, if one exists.
///
/// This will always produce a WEBP image URL.
+ #[inline]
pub fn static_avatar_url(&self) -> Option<String> {
- self.avatar.as_ref()
- .map(|av| format!(cdn!("/avatars/{}/{}.webp?size=1024"), self.id.0, av))
+ static_avatar_url(self.id, self.avatar.as_ref())
}
/// Returns the "tag" for the user.
@@ -734,6 +719,29 @@ impl fmt::Display for UserId {
}
#[cfg(feature="model")]
+fn avatar_url(user_id: UserId, hash: Option<&String>) -> Option<String> {
+ hash.map(|hash| {
+ let ext = if hash.starts_with("a_") {
+ "gif"
+ } else {
+ "webp"
+ };
+
+ cdn!("/avatars/{}/{}.{}?size=1024", user_id.0, hash, ext)
+ })
+}
+
+#[cfg(feature="model")]
+fn default_avatar_url(discriminator: u16) -> String {
+ cdn!("/embed/avatars/{}.png", discriminator % 5u16)
+}
+
+#[cfg(feature="model")]
+fn static_avatar_url(user_id: UserId, hash: Option<&String>) -> Option<String> {
+ hash.map(|hash| cdn!("/avatars/{}/{}.webp?size=1024", user_id, hash))
+}
+
+#[cfg(feature="model")]
fn tag(name: &str, discriminator: u16) -> String {
// 32: max length of username
// 1: `#`