diff options
| author | acdenisSK <[email protected]> | 2017-07-27 06:42:48 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-07-27 07:30:23 +0200 |
| commit | 550030264952f0e0043b63f4582bb817ef8bbf37 (patch) | |
| tree | b921e2f78fd603a5ca671623083a32806fd16090 /src/model/channel | |
| parent | Use a consistent indentation style (diff) | |
| download | serenity-550030264952f0e0043b63f4582bb817ef8bbf37.tar.xz serenity-550030264952f0e0043b63f4582bb817ef8bbf37.zip | |
rustfmt
Diffstat (limited to 'src/model/channel')
| -rw-r--r-- | src/model/channel/attachment.rs | 12 | ||||
| -rw-r--r-- | src/model/channel/channel_id.rs | 167 | ||||
| -rw-r--r-- | src/model/channel/embed.rs | 28 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 86 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 140 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 104 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 111 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 80 | ||||
| -rw-r--r-- | src/model/channel/reaction.rs | 66 |
9 files changed, 413 insertions, 381 deletions
diff --git a/src/model/channel/attachment.rs b/src/model/channel/attachment.rs index 2238174..5d47469 100644 --- a/src/model/channel/attachment.rs +++ b/src/model/channel/attachment.rs @@ -1,9 +1,9 @@ -#[cfg(feature="model")] +#[cfg(feature = "model")] use hyper::Client as HyperClient; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::io::Read; -#[cfg(feature="model")] -use ::internal::prelude::*; +#[cfg(feature = "model")] +use internal::prelude::*; /// A file uploaded with a message. Not to be confused with [`Embed`]s. /// @@ -27,7 +27,7 @@ pub struct Attachment { pub width: Option<u64>, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Attachment { /// If this attachment is an image, then a tuple of the width and height /// in pixels is returned. @@ -90,7 +90,7 @@ impl Attachment { /// } /// /// fn on_ready(&self, _: Context, ready: Ready) { - /// println!("{} is connected!", ready.user.name); + /// println!("{} is connected!", ready.user.name); /// } /// } /// let token = env::var("DISCORD_TOKEN").expect("token in environment"); diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index f83062c..62d8820 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -1,18 +1,18 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; -use ::model::*; +use model::*; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::borrow::Cow; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::fmt::Write as FmtWrite; -#[cfg(feature="model")] -use ::builder::{CreateMessage, EditChannel, GetMessages}; -#[cfg(feature="cache")] -use ::CACHE; -#[cfg(feature="model")] -use ::http::{self, AttachmentType}; - -#[cfg(feature="model")] +#[cfg(feature = "model")] +use builder::{CreateMessage, EditChannel, GetMessages}; +#[cfg(feature = "cache")] +use CACHE; +#[cfg(feature = "model")] +use http::{self, AttachmentType}; + +#[cfg(feature = "model")] impl ChannelId { /// Broadcasts that the current user is typing to a channel for the next 5 /// seconds. @@ -35,9 +35,7 @@ impl ChannelId { /// /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] - pub fn broadcast_typing(&self) -> Result<()> { - http::broadcast_typing(self.0) - } + pub fn broadcast_typing(&self) -> Result<()> { http::broadcast_typing(self.0) } /// Creates a [permission overwrite][`PermissionOverwrite`] for either a /// single [`Member`] or [`Role`] within the channel. @@ -52,8 +50,7 @@ impl ChannelId { /// [`PermissionOverwrite`]: struct.PermissionOverwrite.html /// [`Role`]: struct.Role.html /// [Manage Channels]: permissions/constant.MANAGE_CHANNELS.html - pub fn create_permission(&self, target: &PermissionOverwrite) - -> Result<()> { + pub fn create_permission(&self, target: &PermissionOverwrite) -> Result<()> { let (id, kind) = match target.kind { PermissionOverwriteType::Member(id) => (id.0, "member"), PermissionOverwriteType::Role(id) => (id.0, "role"), @@ -82,16 +79,16 @@ impl ChannelId { /// [`Message::react`]: struct.Message.html#method.react /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html #[inline] - pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { http::create_reaction(self.0, message_id.into().0, &reaction_type.into()) } /// Deletes this channel, returning the channel on a successful deletion. #[inline] - pub fn delete(&self) -> Result<Channel> { - http::delete_channel(self.0) - } + pub fn delete(&self) -> Result<Channel> { http::delete_channel(self.0) } /// Deletes a [`Message`] given its Id. /// @@ -123,7 +120,8 @@ impl ChannelId { /// [`Channel::delete_messages`]: enum.Channel.html#method.delete_messages /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn delete_messages(&self, message_ids: &[MessageId]) -> Result<()> { - let ids = message_ids.into_iter() + let ids = message_ids + .into_iter() .map(|message_id| message_id.0) .collect::<Vec<u64>>(); @@ -153,8 +151,14 @@ impl ChannelId { /// /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html - pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn delete_reaction<M, R>(&self, + message_id: M, + user_id: Option<UserId>, + reaction_type: R) + -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { http::delete_reaction(self.0, message_id.into().0, user_id.map(|uid| uid.0), @@ -205,7 +209,9 @@ impl ChannelId { /// [`Message`]: struct.Message.html /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + M: Into<MessageId>, { let map = f(CreateMessage::default()).0; if let Some(content) = map.get("content") { @@ -220,15 +226,13 @@ impl ChannelId { } /// Search the cache for the channel with the Id. - #[cfg(feature="cache")] - pub fn find(&self) -> Option<Channel> { - CACHE.read().unwrap().channel(*self) - } + #[cfg(feature = "cache")] + pub fn find(&self) -> Option<Channel> { CACHE.read().unwrap().channel(*self) } /// Search the cache for the channel. If it can't be found, the channel is /// requested over REST. pub fn get(&self) -> Result<Channel> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { if let Some(channel) = CACHE.read().unwrap().channel(*self) { return Ok(channel); @@ -243,9 +247,7 @@ impl ChannelId { /// Requires the [Manage Channels] permission. /// [Manage Channels]: permissions/constant.MANAGE_CHANNELS.html #[inline] - pub fn invites(&self) -> Result<Vec<RichInvite>> { - http::get_channel_invites(self.0) - } + pub fn invites(&self) -> Result<Vec<RichInvite>> { http::get_channel_invites(self.0) } /// Gets a message from the channel. /// @@ -254,12 +256,11 @@ impl ChannelId { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn message<M: Into<MessageId>>(&self, message_id: M) -> Result<Message> { - http::get_message(self.0, message_id.into().0) - .map(|mut msg| { - msg.transform_content(); + http::get_message(self.0, message_id.into().0).map(|mut msg| { + msg.transform_content(); - msg - }) + msg + }) } /// Gets messages from the channel. @@ -271,7 +272,8 @@ impl ChannelId { /// [`Channel::messages`]: enum.Channel.html#method.messages /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html pub fn messages<F>(&self, f: F) -> Result<Vec<Message>> - where F: FnOnce(GetMessages) -> GetMessages { + where + F: FnOnce(GetMessages) -> GetMessages, { let mut map = f(GetMessages::default()).0; let mut query = format!("?limit={}", map.remove("limit").unwrap_or(50)); @@ -283,35 +285,35 @@ impl ChannelId { write!(query, "&before={}", before)?; } - http::get_messages(self.0, &query) - .map(|msgs| msgs - .into_iter() + http::get_messages(self.0, &query).map(|msgs| { + msgs.into_iter() .map(|mut msg| { msg.transform_content(); msg - }).collect::<Vec<Message>>()) + }) + .collect::<Vec<Message>>() + }) } /// Returns the name of whatever channel this id holds. - #[cfg(feature="model")] + #[cfg(feature = "model")] pub fn name(&self) -> Option<String> { use self::Channel::*; - Some(match match self.find() { Some(c) => c, None => return None, } { - Guild(channel) => { - channel.read().unwrap().name().to_string() - }, - Group(channel) => { - match channel.read().unwrap().name() { - Cow::Borrowed(name) => name.to_string(), - Cow::Owned(name) => name, - } - }, - Private(channel) => { - channel.read().unwrap().name() - } - }) + Some(match match self.find() { + Some(c) => c, + None => return None, + } { + Guild(channel) => channel.read().unwrap().name().to_string(), + Group(channel) => { + match channel.read().unwrap().name() { + Cow::Borrowed(name) => name.to_string(), + Cow::Owned(name) => name, + } + }, + Private(channel) => channel.read().unwrap().name(), + }) } /// Pins a [`Message`] to the channel. @@ -326,9 +328,7 @@ impl ChannelId { /// /// [`Message`]: struct.Message.html #[inline] - pub fn pins(&self) -> Result<Vec<Message>> { - http::get_pins(self.0) - } + pub fn pins(&self) -> Result<Vec<Message>> { http::get_pins(self.0) } /// Gets the list of [`User`]s who have reacted to a [`Message`] with a /// certain [`Emoji`]. @@ -343,11 +343,15 @@ impl ChannelId { /// [`User`]: struct.User.html /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html pub fn reaction_users<M, R, U>(&self, - message_id: M, - reaction_type: R, - limit: Option<u8>, - after: Option<U>) - -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { + message_id: M, + reaction_type: R, + limit: Option<u8>, + after: Option<U>) + -> Result<Vec<User>> + where + M: Into<MessageId>, + R: Into<ReactionType>, + U: Into<UserId>, { let limit = limit.map_or(50, |x| if x > 100 { 100 } else { x }); http::get_reaction_users(self.0, @@ -432,7 +436,9 @@ impl ChannelId { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + T: Into<AttachmentType<'a>>, { let mut map = f(CreateMessage::default()).0; if let Some(content) = map.get("content") { @@ -468,7 +474,8 @@ impl ChannelId { /// [`CreateMessage`]: ../builder/struct.CreateMessage.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html pub fn send_message<F>(&self, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + where + F: FnOnce(CreateMessage) -> CreateMessage, { let CreateMessage(map, reactions) = f(CreateMessage::default()); Message::check_content_length(&map)?; @@ -502,9 +509,7 @@ impl ChannelId { /// /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[inline] - pub fn webhooks(&self) -> Result<Vec<Webhook>> { - http::get_channel_webhooks(self.0) - } + pub fn webhooks(&self) -> Result<Vec<Webhook>> { http::get_channel_webhooks(self.0) } } impl From<Channel> for ChannelId { @@ -531,33 +536,23 @@ impl<'a> From<&'a Channel> for ChannelId { impl From<PrivateChannel> for ChannelId { /// Gets the Id of a private channel. - fn from(private_channel: PrivateChannel) -> ChannelId { - private_channel.id - } + fn from(private_channel: PrivateChannel) -> ChannelId { private_channel.id } } impl<'a> From<&'a PrivateChannel> for ChannelId { /// Gets the Id of a private channel. - fn from(private_channel: &PrivateChannel) -> ChannelId { - private_channel.id - } + fn from(private_channel: &PrivateChannel) -> ChannelId { private_channel.id } } impl From<GuildChannel> for ChannelId { /// Gets the Id of a guild channel. - fn from(public_channel: GuildChannel) -> ChannelId { - public_channel.id - } + fn from(public_channel: GuildChannel) -> ChannelId { public_channel.id } } impl<'a> From<&'a GuildChannel> for ChannelId { /// Gets the Id of a guild channel. - fn from(public_channel: &GuildChannel) -> ChannelId { - public_channel.id - } + fn from(public_channel: &GuildChannel) -> ChannelId { public_channel.id } } impl Display for ChannelId { - fn fmt(&self, f: &mut Formatter) -> FmtResult { - Display::fmt(&self.0, f) - } + fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.0, f) } } diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs index c0ac1e4..5aea7b0 100644 --- a/src/model/channel/embed.rs +++ b/src/model/channel/embed.rs @@ -1,9 +1,9 @@ -#[cfg(feature="utils")] -use ::utils::Colour; -#[cfg(feature="model")] -use ::internal::prelude::*; -#[cfg(feature="model")] -use ::builder::CreateEmbed; +#[cfg(feature = "utils")] +use utils::Colour; +#[cfg(feature = "model")] +use internal::prelude::*; +#[cfg(feature = "model")] +use builder::CreateEmbed; /// Represents a rich embed which allows using richer markdown, multiple fields /// and more. This was heavily inspired by [slack's attachments]. @@ -20,12 +20,12 @@ pub struct Embed { /// Information about the author of the embed. pub author: Option<EmbedAuthor>, /// The colour code of the embed. - #[cfg(feature="utils")] - #[serde(default, rename="color")] + #[cfg(feature = "utils")] + #[serde(default, rename = "color")] pub colour: Colour, /// The colour code of the embed. - #[cfg(not(feature="utils"))] - #[serde(default, rename="color")] + #[cfg(not(feature = "utils"))] + #[serde(default, rename = "color")] pub colour: u32, /// The description of the embed. /// @@ -42,7 +42,7 @@ pub struct Embed { pub image: Option<EmbedImage>, /// The type of the embed. For embeds not generated by Discord's backend, /// this will always be "rich". - #[serde(rename="type")] + #[serde(rename = "type")] pub kind: String, /// Provider information for the embed. /// @@ -63,7 +63,7 @@ pub struct Embed { pub video: Option<EmbedVideo>, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Embed { /// Creates a fake Embed, giving back a `serde_json` map. /// @@ -87,7 +87,9 @@ impl Embed { /// .inline(false))); /// ``` #[inline] - pub fn fake<F>(f: F) -> Value where F: FnOnce(CreateEmbed) -> CreateEmbed { + pub fn fake<F>(f: F) -> Value + where + F: FnOnce(CreateEmbed) -> CreateEmbed, { Value::Object(f(CreateEmbed::default()).0) } } diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 116b6c1..42c1249 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -1,14 +1,14 @@ use chrono::{DateTime, FixedOffset}; -use ::model::*; +use model::*; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::borrow::Cow; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::fmt::Write as FmtWrite; -#[cfg(feature="model")] -use ::builder::{CreateMessage, GetMessages}; -#[cfg(feature="model")] -use ::http::{self, AttachmentType}; +#[cfg(feature = "model")] +use builder::{CreateMessage, GetMessages}; +#[cfg(feature = "model")] +use http::{self, AttachmentType}; /// A group channel - potentially including other [`User`]s - separate from a /// [`Guild`]. @@ -18,7 +18,7 @@ use ::http::{self, AttachmentType}; #[derive(Clone, Debug, Deserialize)] pub struct Group { /// The Id of the group channel. - #[serde(rename="id")] + #[serde(rename = "id")] pub channel_id: ChannelId, /// The optional icon of the group channel. pub icon: Option<String>, @@ -31,11 +31,11 @@ pub struct Group { /// The Id of the group owner. pub owner_id: UserId, /// A map of the group's recipients. - #[serde(deserialize_with="deserialize_users")] + #[serde(deserialize_with = "deserialize_users")] pub recipients: HashMap<UserId, Arc<RwLock<User>>>, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Group { /// Adds the given user to the group. If the user is already in the group, /// then nothing is done. @@ -59,9 +59,7 @@ impl Group { /// Broadcasts that the current user is typing in the group. #[inline] - pub fn broadcast_typing(&self) -> Result<()> { - self.channel_id.broadcast_typing() - } + pub fn broadcast_typing(&self) -> Result<()> { self.channel_id.broadcast_typing() } /// React to a [`Message`] with a custom [`Emoji`] or unicode character. /// @@ -76,8 +74,10 @@ impl Group { /// [`Message::react`]: struct.Message.html#method.react /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html #[inline] - pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { self.channel_id.create_reaction(message_id, reaction_type) } @@ -119,9 +119,16 @@ impl Group { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] - pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { - self.channel_id.delete_reaction(message_id, user_id, reaction_type) + pub fn delete_reaction<M, R>(&self, + message_id: M, + user_id: Option<UserId>, + reaction_type: R) + -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { + self.channel_id + .delete_reaction(message_id, user_id, reaction_type) } /// Edits a [`Message`] in the channel given its Id. @@ -145,14 +152,17 @@ impl Group { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + M: Into<MessageId>, { self.channel_id.edit_message(message_id, f) } /// Returns the formatted URI of the group's icon if one exists. pub fn icon_url(&self) -> Option<String> { - self.icon.as_ref().map(|icon| - format!(cdn!("/channel-icons/{}/{}.webp"), self.channel_id, icon)) + self.icon + .as_ref() + .map(|icon| format!(cdn!("/channel-icons/{}/{}.webp"), self.channel_id, icon)) } /// Determines if the channel is NSFW. @@ -164,15 +174,11 @@ impl Group { /// /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html #[inline] - pub fn is_nsfw(&self) -> bool { - false - } + pub fn is_nsfw(&self) -> bool { false } /// Leaves the group. #[inline] - pub fn leave(&self) -> Result<Group> { - http::leave_group(self.channel_id.0) - } + pub fn leave(&self) -> Result<Group> { http::leave_group(self.channel_id.0) } /// Gets a message from the channel. /// @@ -191,7 +197,8 @@ impl Group { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn messages<F>(&self, f: F) -> Result<Vec<Message>> - where F: FnOnce(GetMessages) -> GetMessages { + where + F: FnOnce(GetMessages) -> GetMessages, { self.channel_id.messages(f) } @@ -214,15 +221,13 @@ impl Group { } Cow::Owned(name) - } + }, } } /// Retrieves the list of messages that have been pinned in the group. #[inline] - pub fn pins(&self) -> Result<Vec<Message>> { - self.channel_id.pins() - } + pub fn pins(&self) -> Result<Vec<Message>> { self.channel_id.pins() } /// Gets the list of [`User`]s who have reacted to a [`Message`] with a /// certain [`Emoji`]. @@ -242,8 +247,13 @@ impl Group { reaction_type: R, limit: Option<u8>, after: Option<U>) - -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { - self.channel_id.reaction_users(message_id, reaction_type, limit, after) + -> Result<Vec<User>> + where + M: Into<MessageId>, + R: Into<ReactionType>, + U: Into<UserId>, { + self.channel_id + .reaction_users(message_id, reaction_type, limit, after) } /// Removes a recipient from the group. If the recipient is already not in @@ -272,9 +282,7 @@ impl Group { /// [`ChannelId`]: ../model/struct.ChannelId.html /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong #[inline] - pub fn say(&self, content: &str) -> Result<Message> { - self.channel_id.say(content) - } + pub fn say(&self, content: &str) -> Result<Message> { self.channel_id.say(content) } /// Sends (a) file(s) along with optional message contents. /// @@ -296,7 +304,9 @@ impl Group { /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + T: Into<AttachmentType<'a>>, { self.channel_id.send_files(files, f) } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 678e601..250e112 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -1,20 +1,20 @@ use chrono::{DateTime, FixedOffset}; -use ::model::*; +use model::*; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::fmt::{Display, Formatter, Result as FmtResult}; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::mem; -#[cfg(feature="cache")] -use ::CACHE; -#[cfg(feature="model")] -use ::builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; -#[cfg(feature="model")] -use ::http::{self, AttachmentType}; -#[cfg(feature="cache")] -use ::internal::prelude::*; -#[cfg(all(feature="model", feature="utils"))] -use ::utils as serenity_utils; +#[cfg(feature = "cache")] +use CACHE; +#[cfg(feature = "model")] +use builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; +#[cfg(feature = "model")] +use http::{self, AttachmentType}; +#[cfg(feature = "cache")] +use internal::prelude::*; +#[cfg(all(feature = "model", feature = "utils"))] +use utils as serenity_utils; /// Represents a guild's text or voice channel. Some methods are available only /// for voice channels and some are only available for text channels. @@ -36,7 +36,7 @@ pub struct GuildChannel { /// incremented by one. pub guild_id: GuildId, /// The type of the channel. - #[serde(rename="type")] + #[serde(rename = "type")] pub kind: ChannelType, /// The Id of the last message sent in the channel. /// @@ -69,19 +69,16 @@ pub struct GuildChannel { /// Used to tell if the channel is not safe for work. /// Note however, it's recommended to use [`is_nsfw`] as it's gonna be more accurate. /// - /// [`is_nsfw`]: struct.GuildChannel.html#method.is_nsfw - + /// [`is_nsfw`]: struct.GuildChannel.html#method.is_nsfw // This field can or can not be present sometimes, but if it isn't // default to `false`. #[serde(default = "nsfw_false")] pub nsfw: bool, } -fn nsfw_false() -> bool { - false -} +fn nsfw_false() -> bool { false } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl GuildChannel { /// Broadcasts to the channel that the current user is typing. /// @@ -96,9 +93,7 @@ impl GuildChannel { /// /// [`ModelError::InvalidPermissions`]: enum.ModelError.html#variant.InvalidPermissions /// [Send Messages]: permissions/constant.SEND_MESSAGES.html - pub fn broadcast_typing(&self) -> Result<()> { - self.id.broadcast_typing() - } + pub fn broadcast_typing(&self) -> Result<()> { self.id.broadcast_typing() } /// Creates an invite leading to the given channel. /// @@ -110,8 +105,9 @@ impl GuildChannel { /// let invite = channel.create_invite(|i| i.max_uses(5)); /// ``` pub fn create_invite<F>(&self, f: F) -> Result<RichInvite> - where F: FnOnce(CreateInvite) -> CreateInvite { - #[cfg(feature="cache")] + where + F: FnOnce(CreateInvite) -> CreateInvite, { + #[cfg(feature = "cache")] { let req = permissions::CREATE_INVITE; @@ -235,7 +231,7 @@ impl GuildChannel { /// Deletes this channel, returning the channel on a successful deletion. pub fn delete(&self) -> Result<Channel> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_CHANNELS; @@ -285,8 +281,14 @@ impl GuildChannel { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] - pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn delete_reaction<M, R>(&self, + message_id: M, + user_id: Option<UserId>, + reaction_type: R) + -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { self.id.delete_reaction(message_id, user_id, reaction_type) } @@ -302,9 +304,10 @@ impl GuildChannel { /// channel.edit(|c| c.name("test").bitrate(86400)); /// ``` pub fn edit<F>(&mut self, f: F) -> Result<()> - where F: FnOnce(EditChannel) -> EditChannel { + where + F: FnOnce(EditChannel) -> EditChannel, { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_CHANNELS; @@ -315,8 +318,10 @@ impl GuildChannel { let mut map = Map::new(); map.insert("name".to_owned(), Value::String(self.name.clone())); - map.insert("position".to_owned(), Value::Number(Number::from(self.position))); - map.insert("type".to_owned(), Value::String(self.kind.name().to_owned())); + map.insert("position".to_owned(), + Value::Number(Number::from(self.position))); + map.insert("type".to_owned(), + Value::String(self.kind.name().to_owned())); let edited = f(EditChannel(map)).0; @@ -351,7 +356,9 @@ impl GuildChannel { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + M: Into<MessageId>, { self.id.edit_message(message_id, f) } @@ -359,19 +366,15 @@ impl GuildChannel { /// /// **Note**: Right now this performs a clone of the guild. This will be /// optimized in the future. - #[cfg(feature="cache")] - pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> { - CACHE.read().unwrap().guild(self.guild_id) - } + #[cfg(feature = "cache")] + pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> { CACHE.read().unwrap().guild(self.guild_id) } /// Gets all of the channel's invites. /// /// Requires the [Manage Channels] permission. /// [Manage Channels]: permissions/constant.MANAGE_CHANNELS.html #[inline] - pub fn invites(&self) -> Result<Vec<RichInvite>> { - self.id.invites() - } + pub fn invites(&self) -> Result<Vec<RichInvite>> { self.id.invites() } /// Determines if the channel is NSFW. /// @@ -383,7 +386,7 @@ impl GuildChannel { /// [`ChannelType::Text`]: enum.ChannelType.html#variant.Text /// [`ChannelType::Voice`]: enum.ChannelType.html#variant.Voice /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html - #[cfg(feature="utils")] + #[cfg(feature = "utils")] #[inline] pub fn is_nsfw(&self) -> bool { self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name)) @@ -409,14 +412,13 @@ impl GuildChannel { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn messages<F>(&self, f: F) -> Result<Vec<Message>> - where F: FnOnce(GetMessages) -> GetMessages { + where + F: FnOnce(GetMessages) -> GetMessages, { self.id.messages(f) } /// Returns the name of the guild channel. - pub fn name(&self) -> &str { - &self.name - } + pub fn name(&self) -> &str { &self.name } /// Calculates the permissions of a member. /// @@ -471,9 +473,11 @@ impl GuildChannel { /// }; /// /// let current_user_id = CACHE.read().unwrap().user.id; - /// let permissions = channel.read().unwrap().permissions_for(current_user_id).unwrap(); + /// let permissions = + /// channel.read().unwrap().permissions_for(current_user_id).unwrap(); /// - /// if !permissions.contains(permissions::ATTACH_FILES | permissions::SEND_MESSAGES) { + /// if !permissions.contains(permissions::ATTACH_FILES | + /// permissions::SEND_MESSAGES) { /// return; /// } /// @@ -486,7 +490,8 @@ impl GuildChannel { /// }, /// }; /// - /// let _ = msg.channel_id.send_files(vec![(&file, "cat.png")], |m| m.content("here's a cat")); + /// let _ = msg.channel_id.send_files(vec![(&file, "cat.png")], |m| + /// m.content("here's a cat")); /// } /// } /// @@ -506,7 +511,7 @@ impl GuildChannel { /// [`User`]: struct.User.html /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html - #[cfg(feature="cache")] + #[cfg(feature = "cache")] pub fn permissions_for<U: Into<UserId>>(&self, user_id: U) -> Result<Permissions> { self.guild() .ok_or_else(|| Error::Model(ModelError::GuildNotFound)) @@ -515,15 +520,11 @@ impl GuildChannel { /// Pins a [`Message`] to the channel. #[inline] - pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - self.id.pin(message_id) - } + pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { self.id.pin(message_id) } /// Gets all channel's pins. #[inline] - pub fn pins(&self) -> Result<Vec<Message>> { - self.id.pins() - } + pub fn pins(&self) -> Result<Vec<Message>> { self.id.pins() } /// Gets the list of [`User`]s who have reacted to a [`Message`] with a /// certain [`Emoji`]. @@ -542,8 +543,13 @@ impl GuildChannel { reaction_type: R, limit: Option<u8>, after: Option<U>) - -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { - self.id.reaction_users(message_id, reaction_type, limit, after) + -> Result<Vec<User>> + where + M: Into<MessageId>, + R: Into<ReactionType>, + U: Into<UserId>, { + self.id + .reaction_users(message_id, reaction_type, limit, after) } /// Sends a message with just the given message content in the channel. @@ -557,9 +563,7 @@ impl GuildChannel { /// [`ChannelId`]: struct.ChannelId.html /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong #[inline] - pub fn say(&self, content: &str) -> Result<Message> { - self.id.say(content) - } + pub fn say(&self, content: &str) -> Result<Message> { self.id.say(content) } /// Sends (a) file(s) along with optional message contents. /// @@ -581,7 +585,9 @@ impl GuildChannel { /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + T: Into<AttachmentType<'a>>, { self.id.send_files(files, f) } @@ -605,7 +611,7 @@ impl GuildChannel { /// [`Message`]: struct.Message.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html pub fn send_message<F: FnOnce(CreateMessage) -> CreateMessage>(&self, f: F) -> Result<Message> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::SEND_MESSAGES; @@ -634,15 +640,11 @@ impl GuildChannel { /// /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[inline] - pub fn webhooks(&self) -> Result<Vec<Webhook>> { - self.id.webhooks() - } + pub fn webhooks(&self) -> Result<Vec<Webhook>> { self.id.webhooks() } } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Display for GuildChannel { /// Formats the channel, creating a mention of it. - fn fmt(&self, f: &mut Formatter) -> FmtResult { - Display::fmt(&self.id.mention(), f) - } + fn fmt(&self, f: &mut Formatter) -> FmtResult { Display::fmt(&self.id.mention(), f) } } diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 0305271..8b479bc 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -1,19 +1,19 @@ use chrono::{DateTime, FixedOffset}; use serde_json::Value; -use ::model::*; +use model::*; -#[cfg(feature="cache")] +#[cfg(feature = "cache")] use std::fmt::Write; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::mem; -#[cfg(feature="model")] -use ::builder::{CreateEmbed, CreateMessage}; -#[cfg(feature="model")] -use ::constants; -#[cfg(feature="cache")] -use ::CACHE; -#[cfg(feature="model")] -use ::http; +#[cfg(feature = "model")] +use builder::{CreateEmbed, CreateMessage}; +#[cfg(feature = "model")] +use constants; +#[cfg(feature = "cache")] +use CACHE; +#[cfg(feature = "model")] +use http; /// A representation of a message over a guild's text channel, a group, or a /// private channel. @@ -38,7 +38,7 @@ pub struct Message { pub embeds: Vec<Embed>, /// Indicator of the type of message this is, i.e. whether it is a regular /// message or a system message. - #[serde(rename="type")] + #[serde(rename = "type")] pub kind: MessageType, /// Indicator of whether the message mentions everyone. pub mention_everyone: bool, @@ -67,7 +67,7 @@ pub struct Message { pub webhook_id: Option<WebhookId>, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Message { /// Retrieves the related channel located in the cache. /// @@ -109,17 +109,14 @@ impl Message { /// }); /// # } /// ``` - #[cfg(feature="cache")] + #[cfg(feature = "cache")] #[inline] - pub fn channel(&self) -> Option<Channel> { - CACHE.read().unwrap().channel(self.channel_id) - } + pub fn channel(&self) -> Option<Channel> { CACHE.read().unwrap().channel(self.channel_id) } - /// A util function for determining whether this message was sent by someone else, or the bot. - #[cfg(all(feature="cache", feature="utils"))] - pub fn is_own(&self) -> bool { - self.author.id == CACHE.read().unwrap().user.id - } + /// A util function for determining whether this message was sent by someone else, or the + /// bot. + #[cfg(all(feature = "cache", feature = "utils"))] + pub fn is_own(&self) -> bool { self.author.id == CACHE.read().unwrap().user.id } /// Deletes the message. /// @@ -136,7 +133,7 @@ impl Message { /// [`ModelError::InvalidUser`]: enum.ModelError.html#variant.InvalidUser /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn delete(&self) -> Result<()> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_MESSAGES; let is_author = self.author.id == CACHE.read().unwrap().user.id; @@ -164,7 +161,7 @@ impl Message { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn delete_reactions(&self) -> Result<()> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_MESSAGES; @@ -209,8 +206,9 @@ impl Message { /// [`CreateMessage`]: ../builder/struct.CreateMessage.html /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content pub fn edit<F>(&mut self, f: F) -> Result<()> - where F: FnOnce(CreateMessage) -> CreateMessage { - #[cfg(feature="cache")] + where + F: FnOnce(CreateMessage) -> CreateMessage, { + #[cfg(feature = "cache")] { if self.author.id != CACHE.read().unwrap().user.id { return Err(Error::Model(ModelError::InvalidUser)); @@ -246,7 +244,8 @@ impl Message { pub(crate) fn transform_content(&mut self) { match self.kind { MessageType::PinsAdd => { - self.content = format!("{} pinned a message to this channel. See all the pins.", self.author); + self.content = format!("{} pinned a message to this channel. See all the pins.", + self.author); }, MessageType::MemberJoin => { let sec = self.timestamp.timestamp() as usize; @@ -264,7 +263,7 @@ impl Message { /// Returns message content, but with user and role mentions replaced with /// names and everyone/here mentions cancelled. - #[cfg(feature="cache")] + #[cfg(feature = "cache")] pub fn content_safe(&self) -> String { let mut result = self.content.clone(); @@ -290,8 +289,9 @@ impl Message { } // And finally replace everyone and here mentions. - result.replace("@everyone", "@\u{200B}everyone") - .replace("@here", "@\u{200B}here") + result + .replace("@everyone", "@\u{200B}everyone") + .replace("@here", "@\u{200B}here") } /// Gets the list of [`User`]s who have reacted to a [`Message`] with a @@ -311,9 +311,16 @@ impl Message { /// [`User`]: struct.User.html /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] - pub fn reaction_users<R, U>(&self, reaction_type: R, limit: Option<u8>, after: Option<U>) - -> Result<Vec<User>> where R: Into<ReactionType>, U: Into<UserId> { - self.channel_id.reaction_users(self.id, reaction_type, limit, after) + pub fn reaction_users<R, U>(&self, + reaction_type: R, + limit: Option<u8>, + after: Option<U>) + -> Result<Vec<User>> + where + R: Into<ReactionType>, + U: Into<UserId>, { + self.channel_id + .reaction_users(self.id, reaction_type, limit, after) } /// Returns the associated `Guild` for the message if one is in the cache. @@ -324,9 +331,10 @@ impl Message { /// Requires the `cache` feature be enabled. /// /// [`guild_id`]: #method.guild_id - #[cfg(feature="cache")] + #[cfg(feature = "cache")] pub fn guild(&self) -> Option<Arc<RwLock<Guild>>> { - self.guild_id().and_then(|guild_id| CACHE.read().unwrap().guild(guild_id)) + self.guild_id() + .and_then(|guild_id| CACHE.read().unwrap().guild(guild_id)) } /// Retrieves the Id of the guild that the message was sent in, if sent in @@ -334,7 +342,7 @@ impl Message { /// /// Returns `None` if the channel data or guild data does not exist in the /// cache. - #[cfg(feature="cache")] + #[cfg(feature = "cache")] pub fn guild_id(&self) -> Option<GuildId> { match CACHE.read().unwrap().channel(self.channel_id) { Some(Channel::Guild(ch)) => Some(ch.read().unwrap().guild_id), @@ -343,7 +351,7 @@ impl Message { } /// True if message was sent using direct messages. - #[cfg(feature="cache")] + #[cfg(feature = "cache")] pub fn is_private(&self) -> bool { match CACHE.read().unwrap().channel(self.channel_id) { Some(Channel::Group(_)) | Some(Channel::Private(_)) => true, @@ -383,7 +391,7 @@ impl Message { /// [`ModelError::InvalidPermissions`]: enum.ModelError.html#variant.InvalidPermissions /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn pin(&self) -> Result<()> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_MESSAGES; @@ -410,7 +418,7 @@ impl Message { /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html /// [permissions]: permissions pub fn react<R: Into<ReactionType>>(&self, reaction_type: R) -> Result<()> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::ADD_REACTIONS; @@ -419,9 +427,7 @@ impl Message { } } - http::create_reaction(self.channel_id.0, - self.id.0, - &reaction_type.into()) + http::create_reaction(self.channel_id.0, self.id.0, &reaction_type.into()) } /// Replies to the user, mentioning them prior to the content in the form @@ -451,7 +457,7 @@ impl Message { return Err(Error::Model(ModelError::MessageTooLong(length_over))); } - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::SEND_MESSAGES; @@ -485,7 +491,7 @@ impl Message { /// [`ModelError::InvalidPermissions`]: enum.ModelError.html#variant.InvalidPermissions /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn unpin(&self) -> Result<()> { - #[cfg(feature="cache")] + #[cfg(feature = "cache")] { let req = permissions::MANAGE_MESSAGES; @@ -563,16 +569,12 @@ impl Message { impl From<Message> for MessageId { /// Gets the Id of a `Message`. - fn from(message: Message) -> MessageId { - message.id - } + fn from(message: Message) -> MessageId { message.id } } impl<'a> From<&'a Message> for MessageId { /// Gets the Id of a `Message`. - fn from(message: &Message) -> MessageId { - message.id - } + fn from(message: &Message) -> MessageId { message.id } } /// A representation of a reaction to a message. @@ -590,7 +592,7 @@ pub struct MessageReaction { /// Indicator of whether the current user has sent the type of reaction. pub me: bool, /// The type of reaction. - #[serde(rename="emoji")] + #[serde(rename = "emoji")] pub reaction_type: ReactionType, } diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 708e276..02be9d8 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -19,14 +19,14 @@ pub use self::reaction::*; use serde::de::Error as DeError; use serde_json; use super::utils::deserialize_u64; -use ::model::*; +use model::*; -#[cfg(feature="model")] +#[cfg(feature = "model")] use std::fmt::{Display, Formatter, Result as FmtResult}; -#[cfg(feature="model")] -use ::builder::{CreateMessage, GetMessages}; -#[cfg(feature="model")] -use ::http::AttachmentType; +#[cfg(feature = "model")] +use builder::{CreateMessage, GetMessages}; +#[cfg(feature = "model")] +use http::AttachmentType; /// A container for any channel. #[derive(Clone, Debug)] @@ -46,7 +46,7 @@ pub enum Channel { Private(Arc<RwLock<PrivateChannel>>), } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Channel { /// React to a [`Message`] with a custom [`Emoji`] or unicode character. /// @@ -61,8 +61,10 @@ impl Channel { /// [`Message::react`]: struct.Message.html#method.react /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html #[inline] - pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { self.id().create_reaction(message_id, reaction_type) } @@ -111,9 +113,16 @@ impl Channel { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] - pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { - self.id().delete_reaction(message_id, user_id, reaction_type) + pub fn delete_reaction<M, R>(&self, + message_id: M, + user_id: Option<UserId>, + reaction_type: R) + -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { + self.id() + .delete_reaction(message_id, user_id, reaction_type) } /// Edits a [`Message`] in the channel given its Id. @@ -137,7 +146,9 @@ impl Channel { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + M: Into<MessageId>, { self.id().edit_message(message_id, f) } @@ -146,7 +157,7 @@ impl Channel { /// Refer to [`utils::is_nsfw`] for more details. /// /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html - #[cfg(feature="utils")] + #[cfg(feature = "utils")] #[inline] pub fn is_nsfw(&self) -> bool { match *self { @@ -183,7 +194,8 @@ impl Channel { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn messages<F>(&self, f: F) -> Result<Vec<Message>> - where F: FnOnce(GetMessages) -> GetMessages { + where + F: FnOnce(GetMessages) -> GetMessages, { self.id().messages(f) } @@ -209,8 +221,13 @@ impl Channel { reaction_type: R, limit: Option<u8>, after: Option<U>) - -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { - self.id().reaction_users(message_id, reaction_type, limit, after) + -> Result<Vec<User>> + where + M: Into<MessageId>, + R: Into<ReactionType>, + U: Into<UserId>, { + self.id() + .reaction_users(message_id, reaction_type, limit, after) } /// Retrieves the Id of the inner [`Group`], [`GuildChannel`], or @@ -238,9 +255,7 @@ impl Channel { /// [`ChannelId`]: struct.ChannelId.html /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong #[inline] - pub fn say(&self, content: &str) -> Result<Message> { - self.id().say(content) - } + pub fn say(&self, content: &str) -> Result<Message> { self.id().say(content) } /// Sends (a) file(s) along with optional message contents. /// @@ -262,7 +277,9 @@ impl Channel { /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + T: Into<AttachmentType<'a>>, { self.id().send_files(files, f) } @@ -287,7 +304,8 @@ impl Channel { /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] pub fn send_message<F>(&self, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + where + F: FnOnce(CreateMessage) -> CreateMessage, { self.id().send_message(f) } @@ -313,21 +331,27 @@ impl<'de> Deserialize<'de> for Channel { }; match kind { - 0 | 2 => serde_json::from_value::<GuildChannel>(Value::Object(v)) - .map(|x| Channel::Guild(Arc::new(RwLock::new(x)))) - .map_err(DeError::custom), - 1 => serde_json::from_value::<PrivateChannel>(Value::Object(v)) - .map(|x| Channel::Private(Arc::new(RwLock::new(x)))) - .map_err(DeError::custom), - 3 => serde_json::from_value::<Group>(Value::Object(v)) - .map(|x| Channel::Group(Arc::new(RwLock::new(x)))) - .map_err(DeError::custom), + 0 | 2 => { + serde_json::from_value::<GuildChannel>(Value::Object(v)) + .map(|x| Channel::Guild(Arc::new(RwLock::new(x)))) + .map_err(DeError::custom) + }, + 1 => { + serde_json::from_value::<PrivateChannel>(Value::Object(v)) + .map(|x| Channel::Private(Arc::new(RwLock::new(x)))) + .map_err(DeError::custom) + }, + 3 => { + serde_json::from_value::<Group>(Value::Object(v)) + .map(|x| Channel::Group(Arc::new(RwLock::new(x)))) + .map_err(DeError::custom) + }, _ => Err(DeError::custom("Unknown channel type")), } } } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Display for Channel { /// Formats the channel into a "mentioned" string. /// @@ -344,12 +368,8 @@ impl Display for Channel { /// [`PrivateChannel`]: struct.PrivateChannel.html fn fmt(&self, f: &mut Formatter) -> FmtResult { match *self { - Channel::Group(ref group) => { - Display::fmt(&group.read().unwrap().name(), f) - }, - Channel::Guild(ref ch) => { - Display::fmt(&ch.read().unwrap().id.mention(), f) - }, + Channel::Group(ref group) => Display::fmt(&group.read().unwrap().name(), f), + Channel::Guild(ref ch) => Display::fmt(&ch.read().unwrap().id.mention(), f), Channel::Private(ref ch) => { let channel = ch.read().unwrap(); let recipient = channel.recipient.read().unwrap(); @@ -397,9 +417,9 @@ impl ChannelType { struct PermissionOverwriteData { allow: Permissions, deny: Permissions, - #[serde(deserialize_with="deserialize_u64")] + #[serde(deserialize_with = "deserialize_u64")] id: u64, - #[serde(rename="type")] + #[serde(rename = "type")] kind: String, } @@ -412,7 +432,8 @@ pub struct PermissionOverwrite { } impl<'de> Deserialize<'de> for PermissionOverwrite { - fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<PermissionOverwrite, D::Error> { + fn deserialize<D: Deserializer<'de>>(deserializer: D) + -> StdResult<PermissionOverwrite, D::Error> { let data = PermissionOverwriteData::deserialize(deserializer)?; let kind = match &data.kind[..] { @@ -422,10 +443,10 @@ impl<'de> Deserialize<'de> for PermissionOverwrite { }; Ok(PermissionOverwrite { - allow: data.allow, - deny: data.deny, - kind: kind, - }) + allow: data.allow, + deny: data.deny, + kind: kind, + }) } } diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index f6d79ed..893bb84 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -1,12 +1,12 @@ use chrono::{DateTime, FixedOffset}; use std::fmt::{Display, Formatter, Result as FmtResult}; use super::deserialize_single_recipient; -use ::model::*; +use model::*; -#[cfg(feature="model")] -use ::builder::{CreateMessage, GetMessages}; -#[cfg(feature="model")] -use ::http::AttachmentType; +#[cfg(feature = "model")] +use builder::{CreateMessage, GetMessages}; +#[cfg(feature = "model")] +use http::AttachmentType; /// A Direct Message text channel with another user. #[derive(Clone, Debug, Deserialize)] @@ -26,19 +26,17 @@ pub struct PrivateChannel { /// This should always be [`ChannelType::Private`]. /// /// [`ChannelType::Private`]: enum.ChannelType.html#variant.Private - #[serde(rename="type")] + #[serde(rename = "type")] pub kind: ChannelType, /// The recipient to the private channel. - #[serde(deserialize_with="deserialize_single_recipient", rename="recipients")] + #[serde(deserialize_with = "deserialize_single_recipient", rename = "recipients")] pub recipient: Arc<RwLock<User>>, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl PrivateChannel { /// Broadcasts that the current user is typing to the recipient. - pub fn broadcast_typing(&self) -> Result<()> { - self.id.broadcast_typing() - } + pub fn broadcast_typing(&self) -> Result<()> { self.id.broadcast_typing() } /// React to a [`Message`] with a custom [`Emoji`] or unicode character. /// @@ -52,8 +50,10 @@ impl PrivateChannel { /// [`Message`]: struct.Message.html /// [`Message::react`]: struct.Message.html#method.react /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html - pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { self.id.create_reaction(message_id, reaction_type) } @@ -61,9 +61,7 @@ impl PrivateChannel { /// and is equivalent to closing a private channel on the client, which can /// be re-opened. #[inline] - pub fn delete(&self) -> Result<Channel> { - self.id.delete() - } + pub fn delete(&self) -> Result<Channel> { self.id.delete() } /// Deletes all messages by Ids from the given vector in the channel. /// @@ -103,8 +101,14 @@ impl PrivateChannel { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] - pub fn delete_reaction<M, R>(&self, message_id: M, user_id: Option<UserId>, reaction_type: R) - -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + pub fn delete_reaction<M, R>(&self, + message_id: M, + user_id: Option<UserId>, + reaction_type: R) + -> Result<()> + where + M: Into<MessageId>, + R: Into<ReactionType>, { self.id.delete_reaction(message_id, user_id, reaction_type) } @@ -129,7 +133,9 @@ impl PrivateChannel { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content #[inline] pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + M: Into<MessageId>, { self.id.edit_message(message_id, f) } @@ -142,9 +148,7 @@ impl PrivateChannel { /// /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html #[inline] - pub fn is_nsfw(&self) -> bool { - false - } + pub fn is_nsfw(&self) -> bool { false } /// Gets a message from the channel. /// @@ -166,14 +170,13 @@ impl PrivateChannel { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn messages<F>(&self, f: F) -> Result<Vec<Message>> - where F: FnOnce(GetMessages) -> GetMessages { + where + F: FnOnce(GetMessages) -> GetMessages, { self.id.messages(f) } /// Returns "DM with $username#discriminator". - pub fn name(&self) -> String { - format!("DM with {}", self.recipient.read().unwrap().tag()) - } + pub fn name(&self) -> String { format!("DM with {}", self.recipient.read().unwrap().tag()) } /// Gets the list of [`User`]s who have reacted to a [`Message`] with a /// certain [`Emoji`]. @@ -193,24 +196,25 @@ impl PrivateChannel { reaction_type: R, limit: Option<u8>, after: Option<U>) - -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { - self.id.reaction_users(message_id, reaction_type, limit, after) + -> Result<Vec<User>> + where + M: Into<MessageId>, + R: Into<ReactionType>, + U: Into<UserId>, { + self.id + .reaction_users(message_id, reaction_type, limit, after) } /// Pins a [`Message`] to the channel. /// /// [`Message`]: struct.Message.html #[inline] - pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - self.id.pin(message_id) - } + pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { self.id.pin(message_id) } /// Retrieves the list of messages that have been pinned in the private /// channel. #[inline] - pub fn pins(&self) -> Result<Vec<Message>> { - self.id.pins() - } + pub fn pins(&self) -> Result<Vec<Message>> { self.id.pins() } /// Sends a message with just the given message content in the channel. /// @@ -223,9 +227,7 @@ impl PrivateChannel { /// [`ChannelId`]: ../model/struct.ChannelId.html /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong #[inline] - pub fn say(&self, content: &str) -> Result<Message> { - self.id.say(content) - } + pub fn say(&self, content: &str) -> Result<Message> { self.id.say(content) } /// Sends (a) file(s) along with optional message contents. /// @@ -247,7 +249,9 @@ impl PrivateChannel { /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { + where + F: FnOnce(CreateMessage) -> CreateMessage, + T: Into<AttachmentType<'a>>, { self.id.send_files(files, f) } diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs index e900fff..5ecb038 100644 --- a/src/model/channel/reaction.rs +++ b/src/model/channel/reaction.rs @@ -1,12 +1,12 @@ use serde::de::{Deserialize, Error as DeError, MapAccess, Visitor}; use std::fmt::{Display, Formatter, Result as FmtResult, Write as FmtWrite}; -use ::internal::prelude::*; -use ::model::*; +use internal::prelude::*; +use model::*; -#[cfg(feature="cache")] -use ::CACHE; -#[cfg(feature="model")] -use ::http; +#[cfg(feature = "cache")] +use CACHE; +#[cfg(feature = "model")] +use http; /// An emoji reaction to a message. #[derive(Clone, Debug, Deserialize)] @@ -28,7 +28,7 @@ pub struct Reaction { pub user_id: UserId, } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl Reaction { /// Deletes the reaction, but only if the current user is the user who made /// the reaction or has permission to. @@ -72,10 +72,7 @@ impl Reaction { Some(self.user_id.0) }}; - http::delete_reaction(self.channel_id.0, - self.message_id.0, - user_id, - &self.emoji) + http::delete_reaction(self.channel_id.0, self.message_id.0, user_id, &self.emoji) } /// Retrieves the list of [`User`]s who have reacted to a [`Message`] with a @@ -106,8 +103,9 @@ impl Reaction { limit: Option<u8>, after: Option<U>) -> Result<Vec<User>> - where R: Into<ReactionType>, - U: Into<UserId> { + where + R: Into<ReactionType>, + U: Into<UserId>, { http::get_reaction_users(self.channel_id.0, self.message_id.0, &reaction_type.into(), @@ -207,13 +205,13 @@ impl<'de> Deserialize<'de> for ReactionType { let name = name.ok_or_else(|| DeError::missing_field("name"))?; Ok(if let Some(id) = id { - ReactionType::Custom { - id: id, - name: name, - } - } else { - ReactionType::Unicode(name) - }) + ReactionType::Custom { + id: id, + name: name, + } + } else { + ReactionType::Unicode(name) + }) } } @@ -223,7 +221,7 @@ impl<'de> Deserialize<'de> for ReactionType { } } -#[cfg(any(feature="model", feature="http"))] +#[cfg(any(feature = "model", feature = "http"))] impl ReactionType { /// Creates a data-esque display of the type. This is not very useful for /// displaying, as the primary client can not render it, but can be useful @@ -233,15 +231,16 @@ impl ReactionType { /// likely little use for it. pub fn as_data(&self) -> String { match *self { - ReactionType::Custom { id, ref name } => { - format!("{}:{}", name, id) - }, + ReactionType::Custom { + id, + ref name, + } => format!("{}:{}", name, id), ReactionType::Unicode(ref unicode) => unicode.clone(), } } } -#[cfg(feature="model")] +#[cfg(feature = "model")] impl From<char> for ReactionType { /// Creates a `ReactionType` from a `char`. /// @@ -264,9 +263,7 @@ impl From<char> for ReactionType { /// # try_main().unwrap(); /// # } /// ``` - fn from(ch: char) -> ReactionType { - ReactionType::Unicode(ch.to_string()) - } + fn from(ch: char) -> ReactionType { ReactionType::Unicode(ch.to_string()) } } impl From<Emoji> for ReactionType { @@ -279,9 +276,7 @@ impl From<Emoji> for ReactionType { } impl From<String> for ReactionType { - fn from(unicode: String) -> ReactionType { - ReactionType::Unicode(unicode) - } + fn from(unicode: String) -> ReactionType { ReactionType::Unicode(unicode) } } impl<'a> From<&'a str> for ReactionType { @@ -301,9 +296,7 @@ impl<'a> From<&'a str> for ReactionType { /// /// foo("🍎"); /// ``` - fn from(unicode: &str) -> ReactionType { - ReactionType::Unicode(unicode.to_owned()) - } + fn from(unicode: &str) -> ReactionType { ReactionType::Unicode(unicode.to_owned()) } } impl Display for ReactionType { @@ -320,7 +313,10 @@ impl Display for ReactionType { /// [`ReactionType::Unicode`]: enum.ReactionType.html#variant.Unicode fn fmt(&self, f: &mut Formatter) -> FmtResult { match *self { - ReactionType::Custom { id, ref name } => { + ReactionType::Custom { + id, + ref name, + } => { f.write_char('<')?; f.write_char(':')?; f.write_str(name)?; |