Skip to content

Commit

Permalink
Resolve clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Mar 15, 2022
1 parent a88673f commit 111f437
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Expand Up @@ -214,7 +214,7 @@ impl Display for Error {
Error::Json(inner) => fmt::Display::fmt(&inner, f),
Error::Model(inner) => fmt::Display::fmt(&inner, f),
Error::Num(inner) => fmt::Display::fmt(&inner, f),
Error::Url(msg) => f.write_str(&msg),
Error::Url(msg) => f.write_str(msg),
#[cfg(feature = "simd-json")]
Error::SimdJson(inner) => fmt::Display::fmt(&inner, f),
#[cfg(feature = "client")]
Expand Down
2 changes: 1 addition & 1 deletion src/http/request.rs
Expand Up @@ -129,7 +129,7 @@ impl<'a> Request<'a> {

if let Some(multipart) = multipart {
// Setting multipart adds the content-length header
builder = builder.multipart(multipart.to_multipart_form(&client).await?);
builder = builder.multipart(multipart.to_multipart_form(client).await?);
} else {
headers.insert(
CONTENT_LENGTH,
Expand Down
2 changes: 1 addition & 1 deletion src/model/event.rs
Expand Up @@ -1796,7 +1796,7 @@ impl<'de> Deserialize<'de> for GatewayEvent {
.map_err(DeError::custom)?;
let payload = map
.remove("d")
.ok_or(Error::Decode("expected gateway event d", Value::from(map)))
.ok_or_else(|| Error::Decode("expected gateway event d", Value::from(map)))
.map_err(DeError::custom)?;

let x = match deserialize_event_with_type(kind.clone(), payload) {
Expand Down
11 changes: 4 additions & 7 deletions src/model/guild/member.rs
Expand Up @@ -195,13 +195,10 @@ impl Member {
let member = guild.members.get(&self.user.id)?;

for channel in guild.channels.values() {
match channel {
Channel::Guild(channel) => {
if guild.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel.clone());
}
},
_ => {},
if let Channel::Guild(channel) = channel {
if guild.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel.clone());
}
}
}

Expand Down
26 changes: 10 additions & 16 deletions src/model/guild/mod.rs
Expand Up @@ -294,13 +294,10 @@ impl Guild {
pub async fn default_channel(&self, uid: UserId) -> Option<&GuildChannel> {
let member = self.members.get(&uid)?;
for channel in self.channels.values() {
match channel {
Channel::Guild(ref channel) => {
if self.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel);
}
},
_ => {},
if let Channel::Guild(channel) = channel {
if self.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel);
}
}
}

Expand All @@ -315,15 +312,12 @@ impl Guild {
/// members, or both.
pub async fn default_channel_guaranteed(&self) -> Option<&GuildChannel> {
for channel in self.channels.values() {
match channel {
Channel::Guild(ref channel) => {
for member in self.members.values() {
if self.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel);
}
if let Channel::Guild(channel) = channel {
for member in self.members.values() {
if self.user_permissions_in(channel, member).ok()?.read_messages() {
return Some(channel);
}
},
_ => {},
}
}
}

Expand Down Expand Up @@ -2591,7 +2585,7 @@ impl<'de> Deserialize<'de> for Guild {
.map_err(DeError::custom)?;
let features = map
.remove("features")
.ok_or_else(|| Error::Other("expected guild features"))
.ok_or(Error::Other("expected guild features"))
.and_then(from_value::<Vec<String>>)
.map_err(DeError::custom)?;
let icon = match map.remove("icon") {
Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/partial_guild.rs
Expand Up @@ -1643,7 +1643,7 @@ impl<'de> Deserialize<'de> for PartialGuild {
.map_err(DeError::custom)?;
let features = map
.remove("features")
.ok_or_else(|| Error::Other("expected guild features"))
.ok_or(Error::Other("expected guild features"))
.and_then(from_value::<Vec<String>>)
.map_err(DeError::custom)?;
let icon = match map.remove("icon") {
Expand Down
157 changes: 154 additions & 3 deletions src/model/sticker/mod.rs
@@ -1,11 +1,162 @@
pub mod sticker;
#[cfg(feature = "model")]
use crate::model::prelude::*;
#[cfg(feature = "model")]
use crate::builder::EditSticker;
#[cfg(feature = "model")]
use crate::http::Http;
#[cfg(feature = "model")]
use crate::internal::prelude::*;
use crate::model::{
id::{GuildId, StickerId, StickerPackId},
user::User,
utils::{deserialize_comma_separated_string, serialize_comma_separated_string},
};

pub mod sticker_id;
pub mod sticker_item;
pub mod sticker_pack;

pub use self::{sticker::*, sticker_id::*, sticker_item::*, sticker_pack::*};
pub use self::{sticker_id::*, sticker_item::*, sticker_pack::*};

/// A sticker sent with a message.
///
/// Bots cannot send stickers.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Sticker {
/// The unique ID given to this sticker.
pub id: StickerId,
/// The unique ID of the pack the sticker is from.
pub pack_id: Option<StickerPackId>,
/// The name of the sticker.
pub name: String,
/// Description of the sticker
pub description: Option<String>,
/// For guild stickers, the Discord name of a unicode emoji representing the
/// sticker's expression. For standard stickers, a list of
/// related expressions.
#[serde(
serialize_with = "serialize_comma_separated_string",
deserialize_with = "deserialize_comma_separated_string"
)]
pub tags: Vec<String>,
/// The type of sticker.
#[serde(rename = "type")]
pub kind: StickerType,
/// The type of sticker format.
pub format_type: StickerFormatType,
/// Whether or not this guild sticker can be used, may be false due to loss
/// of Server Boosts.
#[serde(default)]
pub available: bool,
/// Id of the guild that owns this sticker.
pub guild_id: Option<GuildId>,
/// User that uploaded the sticker. This will be `None` if the current user
/// does not have the [Manage Emojis and Stickers] permission.
///
/// [Manage Emojis and Stickers]: crate::model::permissions::Permissions::MANAGE_EMOJIS_AND_STICKERS
pub user: Option<User>,
/// A sticker's sort order within a pack.
pub sort_value: Option<u64>,
}

#[cfg(feature = "model")]
use crate::model::prelude::*;
impl Sticker {
/// Deletes a [`Sticker`] by Id from the guild.
///
/// Requires the [Manage Emojis and Stickers] permission.
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission
/// to delete the sticker.
///
/// [Manage Emojis and Stickers]: crate::model::permissions::Permissions::MANAGE_EMOJIS_AND_STICKERS
#[inline]
pub async fn delete(&self, http: impl AsRef<Http>) -> Result<()> {
if let Some(guild_id) = self.guild_id {
guild_id.delete_sticker(&http, self.id).await
} else {
Err(Error::Model(ModelError::DeleteNitroSticker))
}
}

/// Edits a sticker, optionally setting its fields.
///
/// Requires the [Manage Emojis and Stickers] permission.
///
/// # Examples
///
/// Rename a sticker:
///
/// ```rust,ignore
/// guild.edit_sticker(&context, StickerId(7), |r| r.name("Bun bun meow"));
/// ```
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user lacks permission.
///
/// [`Error::Http`]: crate::error::Error::Http
/// [Manage Emojis and Stickers]: crate::model::permissions::Permissions::MANAGE_EMOJIS_AND_STICKERS
#[inline]
pub async fn edit<F>(&self, http: impl AsRef<Http>, f: F) -> Result<Sticker>
where
F: FnOnce(&mut EditSticker) -> &mut EditSticker,
{
if let Some(guild_id) = self.guild_id {
guild_id.edit_sticker(&http, self.id, f).await
} else {
Err(Error::Model(ModelError::DeleteNitroSticker))
}
}

/// Retrieves the URL to the sticker image.
///
/// **Note**: This will only be `None` if the format_type is unknown.
#[inline]
pub fn image_url(&self) -> Option<String> {
sticker_url(self.id, self.format_type)
}
}

/// Differentiates between sticker types.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum StickerType {
/// An official sticker in a pack, part of Nitro or in a removed purchasable
/// pack.
Standard = 1,
/// A sticker uploaded to a Boosted guild for the guild's members.
Guild = 2,
/// Unknown sticker type.
Unknown = !0,
}

enum_number!(StickerType {
Standard,
Guild
});

/// Differentiates between sticker formats.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum StickerFormatType {
/// A PNG format sticker.
Png = 1,
/// An APNG format animated sticker.
Apng = 2,
/// A LOTTIE format animated sticker.
Lottie = 3,
/// Unknown sticker format type.
Unknown = !0,
}

enum_number!(StickerFormatType {
Png,
Apng,
Lottie
});

#[cfg(feature = "model")]
fn sticker_url(sticker_id: StickerId, sticker_format_type: StickerFormatType) -> Option<String> {
Expand Down

0 comments on commit 111f437

Please sign in to comment.