diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
| commit | 9908999a6bae1585bb70b7814f13b49bf99b6c32 (patch) | |
| tree | d789f716400502ae0d124933f5c4d927867e7033 /src/builder | |
| parent | Fix some compilation feature targets, fix lints (diff) | |
| download | serenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.tar.xz serenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.zip | |
Slightly improve performance of builders
Builders would keep a `serde_json::Map<String, Value>`, which would
require re-creating owned strings for the same parameter multiple times
in some cases, depending on builder defaults and keying strategies.
This commit uses a `std::collections::HashMap<&'static str, Value>`
internally, and moves over values to a `serde_json::Map<String, Value>`
when it comes time to sending them to the appropriate `http` module
function.
This saves the number of heap-allocated string creations on most
builders, with specific performance increase on `builder::CreateMessage`
and `builder::CreateEmbed` & co.
Diffstat (limited to 'src/builder')
| -rw-r--r-- | src/builder/create_embed.rs | 82 | ||||
| -rw-r--r-- | src/builder/create_invite.rs | 18 | ||||
| -rw-r--r-- | src/builder/create_message.rs | 18 | ||||
| -rw-r--r-- | src/builder/edit_channel.rs | 20 | ||||
| -rw-r--r-- | src/builder/edit_guild.rs | 26 | ||||
| -rw-r--r-- | src/builder/edit_member.rs | 18 | ||||
| -rw-r--r-- | src/builder/edit_profile.rs | 20 | ||||
| -rw-r--r-- | src/builder/edit_role.rs | 62 | ||||
| -rw-r--r-- | src/builder/execute_webhook.rs | 23 | ||||
| -rw-r--r-- | src/builder/get_messages.rs | 12 |
10 files changed, 127 insertions, 172 deletions
diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index f197507..5475d4c 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -17,10 +17,12 @@ use chrono::{DateTime, TimeZone}; use serde_json::Value; +use std::collections::HashMap; use std::default::Default; use std::fmt::Display; use internal::prelude::*; use model::Embed; +use utils; #[cfg(feature = "utils")] use utils::Colour; @@ -37,7 +39,7 @@ use utils::Colour; /// [`Embed`]: ../model/struct.Embed.html /// [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds #[derive(Clone, Debug)] -pub struct CreateEmbed(pub Map<String, Value>); +pub struct CreateEmbed(pub HashMap<&'static str, Value>); impl CreateEmbed { /// Set the author of the embed. @@ -48,9 +50,9 @@ impl CreateEmbed { /// [`CreateEmbedAuthor`]: struct.CreateEmbedAuthor.html pub fn author<F>(mut self, f: F) -> Self where F: FnOnce(CreateEmbedAuthor) -> CreateEmbedAuthor { - let author = f(CreateEmbedAuthor::default()).0; + let map = utils::hashmap_to_json_map(f(CreateEmbedAuthor::default()).0); - self.0.insert("author".to_string(), Value::Object(author)); + self.0.insert("author", Value::Object(map)); CreateEmbed(self.0) } @@ -68,7 +70,7 @@ impl CreateEmbed { #[cfg(feature = "utils")] pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self { self.0.insert( - "color".to_string(), + "color", Value::Number(Number::from(u64::from(colour.into().0))), ); @@ -88,7 +90,7 @@ impl CreateEmbed { #[cfg(not(feature = "utils"))] pub fn colour(mut self, colour: u32) -> Self { self.0 - .insert("color".to_string(), Value::Number(Number::from(colour))); + .insert("color", Value::Number(Number::from(colour))); CreateEmbed(self.0) } @@ -98,7 +100,7 @@ impl CreateEmbed { /// **Note**: This can't be longer than 2048 characters. pub fn description<D: Display>(mut self, description: D) -> Self { self.0.insert( - "description".to_string(), + "description", Value::String(format!("{}", description)), ); @@ -117,12 +119,10 @@ impl CreateEmbed { /// [`CreateEmbedField`]: struct.CreateEmbedField.html pub fn field<F>(mut self, f: F) -> Self where F: FnOnce(CreateEmbedField) -> CreateEmbedField { - let field = f(CreateEmbedField::default()).0; + let map = utils::hashmap_to_json_map(f(CreateEmbedField::default()).0); { - let key = "fields".to_string(); - - let entry = self.0.remove(&key).unwrap_or_else(|| Value::Array(vec![])); + let entry = self.0.remove("fields").unwrap_or_else(|| Value::Array(vec![])); let mut arr = match entry { Value::Array(inner) => inner, _ => { @@ -134,9 +134,9 @@ impl CreateEmbed { return CreateEmbed(self.0); }, }; - arr.push(Value::Object(field)); + arr.push(Value::Object(map)); - self.0.insert("fields".to_string(), Value::Array(arr)); + self.0.insert("fields", Value::Array(arr)); } CreateEmbed(self.0) @@ -146,20 +146,18 @@ impl CreateEmbed { pub fn fields<It: IntoIterator<Item=CreateEmbedField>>(mut self, fields: It) -> Self { let fields = fields .into_iter() - .map(|m| Value::Object(m.0)) + .map(|m| Value::Object(utils::hashmap_to_json_map(m.0))) .collect::<Vec<Value>>(); { - let key = "fields".to_string(); - - let entry = self.0.remove(&key).unwrap_or_else(|| Value::Array(vec![])); + let entry = self.0.remove("fields").unwrap_or_else(|| Value::Array(vec![])); let mut arr = match entry { Value::Array(inner) => inner, _ => return CreateEmbed(self.0), }; arr.extend(fields); - self.0.insert("fields".to_string(), Value::Array(arr)); + self.0.insert("fields", Value::Array(arr)); } CreateEmbed(self.0) @@ -174,8 +172,9 @@ impl CreateEmbed { pub fn footer<F>(mut self, f: F) -> Self where F: FnOnce(CreateEmbedFooter) -> CreateEmbedFooter { let footer = f(CreateEmbedFooter::default()).0; + let map = utils::hashmap_to_json_map(footer); - self.0.insert("footer".to_string(), Value::Object(footer)); + self.0.insert("footer", Value::Object(map)); CreateEmbed(self.0) } @@ -186,7 +185,7 @@ impl CreateEmbed { "url": url.to_string() }); - self.0.insert("image".to_string(), image); + self.0.insert("image", image); CreateEmbed(self.0) } @@ -197,7 +196,7 @@ impl CreateEmbed { "url": url.to_string(), }); - self.0.insert("thumbnail".to_string(), thumbnail); + self.0.insert("thumbnail", thumbnail); CreateEmbed(self.0) } @@ -283,7 +282,7 @@ impl CreateEmbed { /// ``` pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self { self.0 - .insert("timestamp".to_string(), Value::String(timestamp.into().ts)); + .insert("timestamp", Value::String(timestamp.into().ts)); CreateEmbed(self.0) } @@ -291,7 +290,7 @@ impl CreateEmbed { /// Set the title of the embed. pub fn title<D: Display>(mut self, title: D) -> Self { self.0 - .insert("title".to_string(), Value::String(format!("{}", title))); + .insert("title", Value::String(format!("{}", title))); CreateEmbed(self.0) } @@ -299,7 +298,7 @@ impl CreateEmbed { /// Set the URL to direct to when clicking on the title. pub fn url(mut self, url: &str) -> Self { self.0 - .insert("url".to_string(), Value::String(url.to_string())); + .insert("url", Value::String(url.to_string())); CreateEmbed(self.0) } @@ -318,8 +317,8 @@ impl CreateEmbed { impl Default for CreateEmbed { /// Creates a builder with default values, setting the `type` to `rich`. fn default() -> CreateEmbed { - let mut map = Map::new(); - map.insert("type".to_string(), Value::String("rich".to_string())); + let mut map = HashMap::new(); + map.insert("type", Value::String("rich".to_string())); CreateEmbed(map) } @@ -391,29 +390,26 @@ impl From<Embed> for CreateEmbed { /// [`CreateEmbed::author`]: struct.CreateEmbed.html#method.author /// [`name`]: #method.name #[derive(Clone, Debug, Default)] -pub struct CreateEmbedAuthor(pub Map<String, Value>); +pub struct CreateEmbedAuthor(pub HashMap<&'static str, Value>); impl CreateEmbedAuthor { /// Set the URL of the author's icon. pub fn icon_url(mut self, icon_url: &str) -> Self { - self.0 - .insert("icon_url".to_string(), Value::String(icon_url.to_string())); + self.0.insert("icon_url", Value::String(icon_url.to_string())); self } /// Set the author's name. pub fn name(mut self, name: &str) -> Self { - self.0 - .insert("name".to_string(), Value::String(name.to_string())); + self.0.insert("name", Value::String(name.to_string())); self } /// Set the author's URL. pub fn url(mut self, url: &str) -> Self { - self.0 - .insert("url".to_string(), Value::String(url.to_string())); + self.0.insert("url", Value::String(url.to_string())); self } @@ -428,28 +424,26 @@ impl CreateEmbedAuthor { /// [`Embed`]: ../model/struct.Embed.html /// [`CreateEmbed::field`]: struct.CreateEmbed.html#method.field #[derive(Clone, Debug)] -pub struct CreateEmbedField(pub Map<String, Value>); +pub struct CreateEmbedField(pub HashMap<&'static str, Value>); impl CreateEmbedField { /// Set whether the field is inlined. Set to true by default. pub fn inline(mut self, inline: bool) -> Self { - self.0.insert("inline".to_string(), Value::Bool(inline)); + self.0.insert("inline", Value::Bool(inline)); self } /// Set the field's name. It can't be longer than 256 characters. pub fn name<D: Display>(mut self, name: D) -> Self { - self.0 - .insert("name".to_string(), Value::String(format!("{}", name))); + self.0.insert("name", Value::String(format!("{}", name))); self } /// Set the field's value. It can't be longer than 1024 characters. pub fn value<D: Display>(mut self, value: D) -> Self { - self.0 - .insert("value".to_string(), Value::String(format!("{}", value))); + self.0.insert("value", Value::String(format!("{}", value))); self } @@ -459,8 +453,8 @@ impl Default for CreateEmbedField { /// Creates a builder with default values, setting the value of `inline` to /// `true`. fn default() -> CreateEmbedField { - let mut map = Map::new(); - map.insert("inline".to_string(), Value::Bool(true)); + let mut map = HashMap::new(); + map.insert("inline", Value::Bool(true)); CreateEmbedField(map) } @@ -474,21 +468,19 @@ impl Default for CreateEmbedField { /// [`Embed`]: ../model/struct.Embed.html /// [`CreateEmbed::footer`]: struct.CreateEmbed.html#method.footer #[derive(Clone, Debug, Default)] -pub struct CreateEmbedFooter(pub Map<String, Value>); +pub struct CreateEmbedFooter(pub HashMap<&'static str, Value>); impl CreateEmbedFooter { /// Set the icon URL's value. This only supports HTTP(S). pub fn icon_url(mut self, icon_url: &str) -> Self { - self.0 - .insert("icon_url".to_string(), Value::String(icon_url.to_string())); + self.0.insert("icon_url", Value::String(icon_url.to_string())); self } /// Set the footer's text. pub fn text<D: Display>(mut self, text: D) -> Self { - self.0 - .insert("text".to_string(), Value::String(format!("{}", text))); + self.0.insert("text", Value::String(format!("{}", text))); self } diff --git a/src/builder/create_invite.rs b/src/builder/create_invite.rs index d719c17..87c4988 100644 --- a/src/builder/create_invite.rs +++ b/src/builder/create_invite.rs @@ -1,4 +1,5 @@ use serde_json::Value; +use std::collections::HashMap; use std::default::Default; use internal::prelude::*; @@ -58,7 +59,7 @@ use internal::prelude::*; /// [`GuildChannel::create_invite`]: ../model/struct.GuildChannel.html#method.create_invite /// [`RichInvite`]: ../model/struct.Invite.html #[derive(Clone, Debug)] -pub struct CreateInvite(pub JsonMap); +pub struct CreateInvite(pub HashMap<&'static str, Value>); impl CreateInvite { /// The duration that the invite will be valid for. @@ -89,8 +90,7 @@ impl CreateInvite { /// # } /// ``` pub fn max_age(mut self, max_age: u64) -> Self { - self.0 - .insert("max_age".to_string(), Value::Number(Number::from(max_age))); + self.0.insert("max_age", Value::Number(Number::from(max_age))); self } @@ -123,8 +123,7 @@ impl CreateInvite { /// # } /// ``` pub fn max_uses(mut self, max_uses: u64) -> Self { - self.0 - .insert("max_uses".to_string(), Value::Number(Number::from(max_uses))); + self.0.insert("max_uses", Value::Number(Number::from(max_uses))); self } @@ -155,8 +154,7 @@ impl CreateInvite { /// # } /// ``` pub fn temporary(mut self, temporary: bool) -> Self { - self.0 - .insert("temporary".to_string(), Value::Bool(temporary)); + self.0.insert("temporary", Value::Bool(temporary)); self } @@ -187,7 +185,7 @@ impl CreateInvite { /// # } /// ``` pub fn unique(mut self, unique: bool) -> Self { - self.0.insert("unique".to_string(), Value::Bool(unique)); + self.0.insert("unique", Value::Bool(unique)); self } @@ -206,8 +204,8 @@ impl Default for CreateInvite { /// let invite_builder = CreateInvite::default(); /// ``` fn default() -> CreateInvite { - let mut map = Map::new(); - map.insert("validate".to_string(), Value::Null); + let mut map = HashMap::new(); + map.insert("validate", Value::Null); CreateInvite(map) } diff --git a/src/builder/create_message.rs b/src/builder/create_message.rs index 821048b..5b3abdb 100644 --- a/src/builder/create_message.rs +++ b/src/builder/create_message.rs @@ -1,6 +1,8 @@ use super::CreateEmbed; use model::ReactionType; use internal::prelude::*; +use utils; +use std::collections::HashMap; use std::fmt::Display; /// A builder to specify the contents of an [`http::send_message`] request, @@ -39,15 +41,14 @@ use std::fmt::Display; /// [`embed`]: #method.embed /// [`http::send_message`]: ../http/fn.send_message.html #[derive(Clone, Debug)] -pub struct CreateMessage(pub Map<String, Value>, pub Option<Vec<ReactionType>>); +pub struct CreateMessage(pub HashMap<&'static str, Value>, pub Option<Vec<ReactionType>>); impl CreateMessage { /// Set the content of the message. /// /// **Note**: Message contents must be under 2000 unicode code points. pub fn content<D: Display>(mut self, content: D) -> Self { - self.0 - .insert("content".to_string(), Value::String(format!("{}", content))); + self.0.insert("content", Value::String(format!("{}", content))); CreateMessage(self.0, self.1) } @@ -55,9 +56,10 @@ impl CreateMessage { /// Set an embed for the message. pub fn embed<F>(mut self, f: F) -> Self where F: FnOnce(CreateEmbed) -> CreateEmbed { - let embed = Value::Object(f(CreateEmbed::default()).0); + let map = utils::hashmap_to_json_map(f(CreateEmbed::default()).0); + let embed = Value::Object(map); - self.0.insert("embed".to_string(), embed); + self.0.insert("embed", embed); CreateMessage(self.0, self.1) } @@ -68,7 +70,7 @@ impl CreateMessage { /// /// Defaults to `false`. pub fn tts(mut self, tts: bool) -> Self { - self.0.insert("tts".to_string(), Value::Bool(tts)); + self.0.insert("tts", Value::Bool(tts)); CreateMessage(self.0, self.1) } @@ -88,8 +90,8 @@ impl Default for CreateMessage { /// [`Message`]: ../model/struct.Message.html /// [`tts`]: #method.tts fn default() -> CreateMessage { - let mut map = Map::default(); - map.insert("tts".to_string(), Value::Bool(false)); + let mut map = HashMap::default(); + map.insert("tts", Value::Bool(false)); CreateMessage(map, None) } diff --git a/src/builder/edit_channel.rs b/src/builder/edit_channel.rs index 1ed1551..96455e2 100644 --- a/src/builder/edit_channel.rs +++ b/src/builder/edit_channel.rs @@ -1,4 +1,5 @@ use internal::prelude::*; +use std::collections::HashMap; /// A builder to edit a [`GuildChannel`] for use via [`GuildChannel::edit`] /// @@ -18,7 +19,7 @@ use internal::prelude::*; /// [`GuildChannel`]: ../model/struct.GuildChannel.html /// [`GuildChannel::edit`]: ../model/struct.GuildChannel.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditChannel(pub JsonMap); +pub struct EditChannel(pub HashMap<&'static str, Value>); impl EditChannel { /// The bitrate of the channel in bits. @@ -27,8 +28,7 @@ impl EditChannel { /// /// [voice]: ../model/enum.ChannelType.html#variant.Voice pub fn bitrate(mut self, bitrate: u64) -> Self { - self.0 - .insert("bitrate".to_string(), Value::Number(Number::from(bitrate))); + self.0.insert("bitrate", Value::Number(Number::from(bitrate))); self } @@ -37,16 +37,14 @@ impl EditChannel { /// /// Must be between 2 and 100 characters long. pub fn name(mut self, name: &str) -> Self { - self.0 - .insert("name".to_string(), Value::String(name.to_string())); + self.0.insert("name", Value::String(name.to_string())); self } /// The position of the channel in the channel list. pub fn position(mut self, position: u64) -> Self { - self.0 - .insert("position".to_string(), Value::Number(Number::from(position))); + self.0.insert("position", Value::Number(Number::from(position))); self } @@ -59,8 +57,7 @@ impl EditChannel { /// /// [text]: ../model/enum.ChannelType.html#variant.Text pub fn topic(mut self, topic: &str) -> Self { - self.0 - .insert("topic".to_string(), Value::String(topic.to_string())); + self.0.insert("topic", Value::String(topic.to_string())); self } @@ -71,10 +68,7 @@ impl EditChannel { /// /// [voice]: ../model/enum.ChannelType.html#variant.Voice pub fn user_limit(mut self, user_limit: u64) -> Self { - self.0.insert( - "user_limit".to_string(), - Value::Number(Number::from(user_limit)), - ); + self.0.insert("user_limit", Value::Number(Number::from(user_limit))); self } diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs index 0719305..45969fe 100644 --- a/src/builder/edit_guild.rs +++ b/src/builder/edit_guild.rs @@ -1,5 +1,6 @@ use internal::prelude::*; use model::{ChannelId, Region, UserId, VerificationLevel}; +use std::collections::HashMap; /// A builder to optionally edit certain fields of a [`Guild`]. This is meant /// for usage with [`Guild::edit`]. @@ -11,7 +12,7 @@ use model::{ChannelId, Region, UserId, VerificationLevel}; /// [`Guild`]: ../model/struct.Guild.html /// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html #[derive(Clone, Debug, Default)] -pub struct EditGuild(pub Map<String, Value>); +pub struct EditGuild(pub HashMap<&'static str, Value>); impl EditGuild { /// Set the "AFK voice channel" that users are to move to if they have been @@ -24,7 +25,7 @@ impl EditGuild { /// [`afk_timeout`]: #method.afk_timeout pub fn afk_channel<C: Into<ChannelId>>(mut self, channel: Option<C>) -> Self { self.0.insert( - "afk_channel_id".to_string(), + "afk_channel_id", match channel { Some(channel) => Value::Number(Number::from(channel.into().0)), None => Value::Null, @@ -40,7 +41,7 @@ impl EditGuild { /// [`afk_channel`]: #method.afk_channel pub fn afk_timeout(mut self, timeout: u64) -> Self { self.0.insert( - "afk_timeout".to_string(), + "afk_timeout", Value::Number(Number::from(timeout)), ); @@ -78,7 +79,7 @@ impl EditGuild { /// [`utils::read_image`]: ../utils/fn.read_image.html pub fn icon(mut self, icon: Option<&str>) -> Self { self.0.insert( - "icon".to_string(), + "icon", icon.map_or_else(|| Value::Null, |x| Value::String(x.to_string())), ); @@ -89,8 +90,7 @@ impl EditGuild { /// /// **Note**: Must be between (and including) 2-100 chracters. pub fn name(mut self, name: &str) -> Self { - self.0 - .insert("name".to_string(), Value::String(name.to_string())); + self.0.insert("name", Value::String(name.to_string())); self } @@ -99,10 +99,8 @@ impl EditGuild { /// /// **Note**: The current user must be the owner of the guild. pub fn owner<U: Into<UserId>>(mut self, user_id: U) -> Self { - self.0.insert( - "owner_id".to_string(), - Value::Number(Number::from(user_id.into().0)), - ); + let id = Value::Number(Number::from(user_id.into().0)); + self.0.insert("owner_id", id); self } @@ -134,8 +132,7 @@ impl EditGuild { /// /// [`Region::UsWest`]: ../model/enum.Region.html#variant.UsWest pub fn region(mut self, region: Region) -> Self { - self.0 - .insert("region".to_string(), Value::String(region.name().to_string())); + self.0.insert("region", Value::String(region.name().to_string())); self } @@ -149,8 +146,7 @@ impl EditGuild { /// [`features`]: ../model/struct.LiveGuild.html#structfield.features pub fn splash(mut self, splash: Option<&str>) -> Self { let splash = splash.map_or(Value::Null, |x| Value::String(x.to_string())); - - self.0.insert("splash".to_string(), splash); + self.0.insert("splash", splash); self } @@ -189,7 +185,7 @@ impl EditGuild { where V: Into<VerificationLevel> { let num = Value::Number(Number::from(verification_level.into().num())); - self.0.insert("verification_level".to_string(), num); + self.0.insert("verification_level", num); self } diff --git a/src/builder/edit_member.rs b/src/builder/edit_member.rs index 560d241..5a399f5 100644 --- a/src/builder/edit_member.rs +++ b/src/builder/edit_member.rs @@ -1,5 +1,6 @@ use model::{ChannelId, RoleId}; use internal::prelude::*; +use std::collections::HashMap; /// A builder which edits the properties of a [`Member`], to be used in /// conjunction with [`Member::edit`]. @@ -7,7 +8,7 @@ use internal::prelude::*; /// [`Member`]: ../model/struct.Member.html /// [`Member::edit`]: ../model/struct.Member.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditMember(pub JsonMap); +pub struct EditMember(pub HashMap<&'static str, Value>); impl EditMember { /// Whether to deafen the member. @@ -16,7 +17,7 @@ impl EditMember { /// /// [Deafen Members]: ../model/permissions/constant.DEAFEN_MEMBERS.html pub fn deafen(mut self, deafen: bool) -> Self { - self.0.insert("deaf".to_string(), Value::Bool(deafen)); + self.0.insert("deaf", Value::Bool(deafen)); self } @@ -27,7 +28,7 @@ impl EditMember { /// /// [Mute Members]: ../model/permissions/constant.MUTE_MEMBERS.html pub fn mute(mut self, mute: bool) -> Self { - self.0.insert("mute".to_string(), Value::Bool(mute)); + self.0.insert("mute", Value::Bool(mute)); self } @@ -39,8 +40,7 @@ impl EditMember { /// /// [Manage Nicknames]: ../model/permissions/constant.MANAGE_NICKNAMES.html pub fn nickname(mut self, nickname: &str) -> Self { - self.0 - .insert("nick".to_string(), Value::String(nickname.to_string())); + self.0.insert("nick", Value::String(nickname.to_string())); self } @@ -56,7 +56,7 @@ impl EditMember { .map(|x| Value::Number(Number::from(x.as_ref().0))) .collect(); - self.0.insert("roles".to_string(), Value::Array(role_ids)); + self.0.insert("roles", Value::Array(role_ids)); self } @@ -67,10 +67,8 @@ impl EditMember { /// /// [Move Members]: ../model/permissions/constant.MOVE_MEMBERS.html pub fn voice_channel<C: Into<ChannelId>>(mut self, channel_id: C) -> Self { - self.0.insert( - "channel_id".to_string(), - Value::Number(Number::from(channel_id.into().0)), - ); + let num = Value::Number(Number::from(channel_id.into().0)); + self.0.insert("channel_id", num); self } diff --git a/src/builder/edit_profile.rs b/src/builder/edit_profile.rs index ae0bbd5..648bfbf 100644 --- a/src/builder/edit_profile.rs +++ b/src/builder/edit_profile.rs @@ -1,11 +1,12 @@ use internal::prelude::*; +use std::collections::HashMap; /// A builder to edit the current user's settings, to be used in conjunction /// with [`CurrentUser::edit`]. /// /// [`CurrentUser::edit`]: ../model/struct.CurrentUser.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditProfile(pub JsonMap); +pub struct EditProfile(pub HashMap<&'static str, Value>); impl EditProfile { /// Sets the avatar of the current user. `None` can be passed to remove an @@ -44,8 +45,7 @@ impl EditProfile { /// [`utils::read_image`]: ../fn.read_image.html pub fn avatar(mut self, avatar: Option<&str>) -> Self { let avatar = avatar.map_or(Value::Null, |x| Value::String(x.to_string())); - - self.0.insert("avatar".to_string(), avatar); + self.0.insert("avatar", avatar); self } @@ -61,8 +61,7 @@ impl EditProfile { /// /// [provided]: #method.password pub fn email(mut self, email: &str) -> Self { - self.0 - .insert("email".to_string(), Value::String(email.to_string())); + self.0.insert("email", Value::String(email.to_string())); self } @@ -74,10 +73,7 @@ impl EditProfile { /// /// [provided]: #method.password pub fn new_password(mut self, new_password: &str) -> Self { - self.0.insert( - "new_password".to_string(), - Value::String(new_password.to_string()), - ); + self.0.insert("new_password", Value::String(new_password.to_string())); self } @@ -88,8 +84,7 @@ impl EditProfile { /// [modifying the password]: #method.new_password /// [modifying the associated email address]: #method.email pub fn password(mut self, password: &str) -> Self { - self.0 - .insert("password".to_string(), Value::String(password.to_string())); + self.0.insert("password", Value::String(password.to_string())); self } @@ -101,8 +96,7 @@ impl EditProfile { /// If there are no available discriminators with the requested username, /// an error will occur. pub fn username(mut self, username: &str) -> Self { - self.0 - .insert("username".to_string(), Value::String(username.to_string())); + self.0.insert("username", Value::String(username.to_string())); self } diff --git a/src/builder/edit_role.rs b/src/builder/edit_role.rs index 0ef1b35..0906329 100644 --- a/src/builder/edit_role.rs +++ b/src/builder/edit_role.rs @@ -1,6 +1,7 @@ use std::default::Default; use internal::prelude::*; use model::{permissions, Permissions, Role}; +use std::collections::HashMap; /// A builer to create or edit a [`Role`] for use via a number of model methods. /// @@ -39,48 +40,38 @@ use model::{permissions, Permissions, Role}; /// [`Role`]: ../model/struct.Role.html /// [`Role::edit`]: ../model/struct.Role.html#method.edit #[derive(Clone, Debug)] -pub struct EditRole(pub JsonMap); +pub struct EditRole(pub HashMap<&'static str, Value>); impl EditRole { /// Creates a new builder with the values of the given [`Role`]. /// /// [`Role`]: ../model/struct.Role.html pub fn new(role: &Role) -> Self { - let mut map = Map::new(); + let mut map = HashMap::new(); #[cfg(feature = "utils")] { - map.insert( - "color".to_string(), - Value::Number(Number::from(role.colour.0)), - ); + map.insert("color", Value::Number(Number::from(role.colour.0))); } #[cfg(not(feature = "utils"))] { - map.insert("color".to_string(), Value::Number(Number::from(role.colour))); + map.insert("color", Value::Number(Number::from(role.colour))); } - map.insert("hoist".to_string(), Value::Bool(role.hoist)); - map.insert("managed".to_string(), Value::Bool(role.managed)); - map.insert("mentionable".to_string(), Value::Bool(role.mentionable)); - map.insert("name".to_string(), Value::String(role.name.clone())); - map.insert( - "permissions".to_string(), - Value::Number(Number::from(role.permissions.bits())), - ); - map.insert( - "position".to_string(), - Value::Number(Number::from(role.position)), - ); + map.insert("hoist", Value::Bool(role.hoist)); + map.insert("managed", Value::Bool(role.managed)); + map.insert("mentionable", Value::Bool(role.mentionable)); + map.insert("name", Value::String(role.name.clone())); + map.insert("permissions",Value::Number(Number::from(role.permissions.bits()))); + map.insert("position", Value::Number(Number::from(role.position))); EditRole(map) } /// Sets the colour of the role. pub fn colour(mut self, colour: u64) -> Self { - self.0 - .insert("color".to_string(), Value::Number(Number::from(colour))); + self.0.insert("color", Value::Number(Number::from(colour))); self } @@ -88,15 +79,14 @@ impl EditRole { /// Whether or not to hoist the role above lower-positioned role in the user /// list. pub fn hoist(mut self, hoist: bool) -> Self { - self.0.insert("hoist".to_string(), Value::Bool(hoist)); + self.0.insert("hoist", Value::Bool(hoist)); self } /// Whether or not to make the role mentionable, notifying its users. pub fn mentionable(mut self, mentionable: bool) -> Self { - self.0 - .insert("mentionable".to_string(), Value::Bool(mentionable)); + self.0.insert("mentionable", Value::Bool(mentionable)); self } @@ -104,17 +94,14 @@ impl EditRole { /// The name of the role to set. pub fn name(mut self, name: &str) -> Self { self.0 - .insert("name".to_string(), Value::String(name.to_string())); + .insert("name", Value::String(name.to_string())); self } /// The set of permissions to assign the role. pub fn permissions(mut self, permissions: Permissions) -> Self { - self.0.insert( - "permissions".to_string(), - Value::Number(Number::from(permissions.bits())), - ); + self.0.insert("permissions", Value::Number(Number::from(permissions.bits()))); self } @@ -122,8 +109,7 @@ impl EditRole { /// The position to assign the role in the role list. This correlates to the /// role's position in the user list. pub fn position(mut self, position: u8) -> Self { - self.0 - .insert("position".to_string(), Value::Number(Number::from(position))); + self.0.insert("position", Value::Number(Number::from(position))); self } @@ -143,15 +129,15 @@ impl Default for EditRole { /// /// [general permissions set]: ../model/permissions/constant.PRESET_GENERAL.html fn default() -> EditRole { - let mut map = Map::new(); + let mut map = HashMap::new(); let permissions = Number::from(permissions::PRESET_GENERAL.bits()); - map.insert("color".to_string(), Value::Number(Number::from(10_070_709))); - map.insert("hoist".to_string(), Value::Bool(false)); - map.insert("mentionable".to_string(), Value::Bool(false)); - map.insert("name".to_string(), Value::String("new role".to_string())); - map.insert("permissions".to_string(), Value::Number(permissions)); - map.insert("position".to_string(), Value::Number(Number::from(1))); + map.insert("color", Value::Number(Number::from(10_070_709))); + map.insert("hoist", Value::Bool(false)); + map.insert("mentionable", Value::Bool(false)); + map.insert("name", Value::String("new role".to_string())); + map.insert("permissions", Value::Number(permissions)); + map.insert("position", Value::Number(Number::from(1))); EditRole(map) } diff --git a/src/builder/execute_webhook.rs b/src/builder/execute_webhook.rs index ba1668e..9e27a58 100644 --- a/src/builder/execute_webhook.rs +++ b/src/builder/execute_webhook.rs @@ -1,6 +1,6 @@ use serde_json::Value; +use std::collections::HashMap; use std::default::Default; -use internal::prelude::*; /// A builder to create the inner content of a [`Webhook`]'s execution. /// @@ -53,7 +53,7 @@ use internal::prelude::*; /// [`Webhook::execute`]: ../model/struct.Webhook.html#method.execute /// [`execute_webhook`]: ../http/fn.execute_webhook.html #[derive(Clone, Debug)] -pub struct ExecuteWebhook(pub JsonMap); +pub struct ExecuteWebhook(pub HashMap<&'static str, Value>); impl ExecuteWebhook { /// Override the default avatar of the webhook with an image URL. @@ -74,10 +74,7 @@ impl ExecuteWebhook { /// .content("Here's a webhook")); /// ``` pub fn avatar_url(mut self, avatar_url: &str) -> Self { - self.0.insert( - "avatar_url".to_string(), - Value::String(avatar_url.to_string()), - ); + self.0.insert("avatar_url", Value::String(avatar_url.to_string())); self } @@ -103,8 +100,7 @@ impl ExecuteWebhook { /// /// [`embeds`]: #method.embeds pub fn content(mut self, content: &str) -> Self { - self.0 - .insert("content".to_string(), Value::String(content.to_string())); + self.0.insert("content", Value::String(content.to_string())); self } @@ -123,7 +119,7 @@ impl ExecuteWebhook { /// [`Webhook::execute`]: ../model/struct.Webhook.html#method.execute /// [struct-level documentation]: #examples pub fn embeds(mut self, embeds: Vec<Value>) -> Self { - self.0.insert("embeds".to_string(), Value::Array(embeds)); + self.0.insert("embeds", Value::Array(embeds)); self } @@ -144,7 +140,7 @@ impl ExecuteWebhook { /// } /// ``` pub fn tts(mut self, tts: bool) -> Self { - self.0.insert("tts".to_string(), Value::Bool(tts)); + self.0.insert("tts", Value::Bool(tts)); self } @@ -165,8 +161,7 @@ impl ExecuteWebhook { /// } /// ``` pub fn username(mut self, username: &str) -> Self { - self.0 - .insert("username".to_string(), Value::String(username.to_string())); + self.0.insert("username", Value::String(username.to_string())); self } @@ -190,8 +185,8 @@ impl Default for ExecuteWebhook { /// [`Webhook`]: ../model/struct.Webhook.html /// [`tts`]: #method.tts fn default() -> ExecuteWebhook { - let mut map = Map::new(); - map.insert("tts".to_string(), Value::Bool(false)); + let mut map = HashMap::new(); + map.insert("tts", Value::Bool(false)); ExecuteWebhook(map) } diff --git a/src/builder/get_messages.rs b/src/builder/get_messages.rs index 71af9e5..e59584f 100644 --- a/src/builder/get_messages.rs +++ b/src/builder/get_messages.rs @@ -1,5 +1,5 @@ -use std::collections::BTreeMap; use model::MessageId; +use std::collections::HashMap; /// Builds a request for a request to the API to retrieve messages. /// @@ -50,13 +50,13 @@ use model::MessageId; /// /// [`GuildChannel::messages`]: ../model/struct.GuildChannel.html#method.messages #[derive(Clone, Debug, Default)] -pub struct GetMessages(pub BTreeMap<String, u64>); +pub struct GetMessages(pub HashMap<&'static str, u64>); impl GetMessages { /// Indicates to retrieve the messages after a specific message, given by /// its Id. pub fn after<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("after".to_string(), message_id.into().0); + self.0.insert("after", message_id.into().0); self } @@ -64,7 +64,7 @@ impl GetMessages { /// Indicates to retrieve the messages _around_ a specific message in either /// direction (before+after) the given message. pub fn around<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("around".to_string(), message_id.into().0); + self.0.insert("around", message_id.into().0); self } @@ -72,7 +72,7 @@ impl GetMessages { /// Indicates to retrieve the messages before a specific message, given by /// its Id. pub fn before<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("before".to_string(), message_id.into().0); + self.0.insert("before", message_id.into().0); self } @@ -86,7 +86,7 @@ impl GetMessages { /// reduced. pub fn limit(mut self, limit: u64) -> Self { self.0 - .insert("limit".to_string(), if limit > 100 { 100 } else { limit }); + .insert("limit", if limit > 100 { 100 } else { limit }); self } |