Skip to content

Commit

Permalink
Cast forth consistency across builders
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed May 19, 2019
1 parent c6ae140 commit 13fae29
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 78 deletions.
86 changes: 29 additions & 57 deletions src/builder/create_embed.rs
Expand Up @@ -19,10 +19,6 @@ use chrono::{DateTime, TimeZone};
use crate::internal::prelude::*;
use crate::model::channel::Embed;
use serde_json::{json, Value};
use std::{
default::Default,
fmt::Display
};
use crate::utils::{self, VecMap};

#[cfg(feature = "utils")]
Expand Down Expand Up @@ -111,31 +107,24 @@ impl CreateEmbed {
///
/// **Note**: This can't be longer than 2048 characters.
#[inline]
pub fn description<D: Display>(&mut self, description: D) -> &mut Self {
self._description(description.to_string());
pub fn description<D: ToString>(&mut self, description: D) -> &mut Self {
self.0.insert("description", Value::String(description.to_string()));
self
}

fn _description(&mut self, description: String) {
self.0.insert(
"description",
Value::String(description),
);
}

/// Set a field. Note that this will not overwrite other fields, and will
/// add to them.
///
/// **Note**: Maximum amount of characters you can put is 256 in a field
/// name and 1024 in a field value.
#[inline]
pub fn field<T, U>(&mut self, name: T, value: U, inline: bool) -> &mut Self
where T: Display, U: Display {
self._field(&name.to_string(), &value.to_string(), inline);
where T: ToString, U: ToString {
self._field(name.to_string(), value.to_string(), inline);
self
}

fn _field(&mut self, name: &str, value: &str, inline: bool) {
fn _field(&mut self, name: String, value: String, inline: bool) {
{
let entry = self.0
.entry("fields")
Expand All @@ -158,11 +147,12 @@ impl CreateEmbed {
/// [`field`]: #method.field
pub fn fields<T, U, It>(&mut self, fields: It) -> &mut Self
where It: IntoIterator<Item=(T, U, bool)>,
T: Display,
U: Display {
for field in fields {
self.field(field.0.to_string(), field.1.to_string(), field.2);
T: ToString,
U: ToString {
for (name, value, inline) in fields {
self.field(name, value, inline);
}

self
}

Expand All @@ -183,9 +173,9 @@ impl CreateEmbed {
self
}

fn url_object(&mut self, name: &'static str, url: &str) -> &mut Self {
fn url_object(&mut self, name: &'static str, url: String) -> &mut Self {
let obj = json!({
"url": url.to_string()
"url": url,
});

self.0.insert(name, obj);
Expand All @@ -194,26 +184,19 @@ impl CreateEmbed {

/// Set the image associated with the embed. This only supports HTTP(S).
#[inline]
pub fn image<S: AsRef<str>>(&mut self, url: S) -> &mut Self {
self._image(url.as_ref());
self
}
pub fn image<S: ToString>(&mut self, url: S) -> &mut Self {
self.url_object("image", url.to_string());

fn _image(&mut self, url: &str) {
self.url_object("image", url);
self
}

/// Set the thumbnail of the embed. This only supports HTTP(S).
#[inline]
pub fn thumbnail<S: AsRef<str>>(&mut self, url: S) -> &mut Self {
self._thumbnail(url.as_ref());
pub fn thumbnail<S: ToString>(&mut self, url: S) -> &mut Self {
self.url_object("thumbnail", url.to_string());
self
}

fn _thumbnail(&mut self, url: &str) {
self.url_object("thumbnail", url);
}

/// Set the timestamp.
///
/// You may pass a direct string:
Expand Down Expand Up @@ -328,24 +311,16 @@ impl CreateEmbed {

/// Set the title of the embed.
#[inline]
pub fn title<D: Display>(&mut self, title: D) -> &mut Self {
self._title(title.to_string());
pub fn title<D: ToString>(&mut self, title: D) -> &mut Self {
self.0.insert("title", Value::String(title.to_string()));
self
}

fn _title(&mut self, title: String) {
self.0.insert("title", Value::String(title));
}

/// Set the URL to direct to when clicking on the title.
#[inline]
pub fn url<S: AsRef<str>>(&mut self, url: S) -> &mut Self {
self._url(url.as_ref());
self
}

fn _url(&mut self, url: &str) {
pub fn url<S: ToString>(&mut self, url: S) -> &mut Self {
self.0.insert("url", Value::String(url.to_string()));
self
}

/// Same as calling [`image`] with "attachment://filename.(jpg, png)".
Expand All @@ -357,13 +332,10 @@ impl CreateEmbed {
///
/// [`image`]: #method.image
#[inline]
pub fn attachment<S: AsRef<str>>(&mut self, filename: S) -> &mut Self {
self._attachment(filename.as_ref());
self
}
pub fn attachment<S: ToString>(&mut self, filename: S) -> &mut Self {
self.image(format_args!("attachment://{}", filename));

fn _attachment(&mut self, filename: &str) {
self.image(&format!("attachment://{}", filename));
self
}
}

Expand Down Expand Up @@ -458,19 +430,19 @@ pub struct CreateEmbedAuthor(pub VecMap<&'static str, Value>);

impl CreateEmbedAuthor {
/// Set the URL of the author's icon.
pub fn icon_url(&mut self, icon_url: &str) -> &mut Self {
pub fn icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
self.0.insert("icon_url", Value::String(icon_url.to_string()));
self
}

/// Set the author's name.
pub fn name(&mut self, name: &str) -> &mut Self {
pub fn name<S: ToString>(&mut self, name: S) -> &mut Self {
self.0.insert("name", Value::String(name.to_string()));
self
}

/// Set the author's URL.
pub fn url(&mut self, url: &str) -> &mut Self {
pub fn url<S: ToString>(&mut self, url: S) -> &mut Self {
self.0.insert("url", Value::String(url.to_string()));
self
}
Expand All @@ -488,13 +460,13 @@ pub struct CreateEmbedFooter(pub VecMap<&'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) -> &mut Self {
pub fn icon_url<S: ToString>(&mut self, icon_url: S) -> &mut Self {
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) -> &mut Self {
pub fn text<S: ToString>(&mut self, text: S) -> &mut Self {
self.0.insert("text", Value::String(text.to_string()));
self
}
Expand Down
1 change: 0 additions & 1 deletion src/builder/create_invite.rs
@@ -1,7 +1,6 @@
use crate::internal::prelude::*;
use crate::utils::VecMap;
use serde_json::Value;
use std::default::Default;

/// A builder to create a [`RichInvite`] for use via [`GuildChannel::create_invite`].
///
Expand Down
5 changes: 2 additions & 3 deletions src/builder/create_message.rs
@@ -1,7 +1,6 @@
use crate::internal::prelude::*;
use crate::http::AttachmentType;
use crate::model::channel::ReactionType;
use std::fmt::Display;
use super::CreateEmbed;
use crate::utils::{self, VecMap};

Expand Down Expand Up @@ -59,8 +58,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: Message contents must be under 2000 unicode code points.
#[inline]
pub fn content<D: Display>(&mut self, content: D) -> &mut Self {
self._content(content.to_string());
pub fn content<D: ToString>(&mut self, content: D) -> &mut Self {
self.0.insert("content", Value::String(content.to_string()));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_channel.rs
Expand Up @@ -36,7 +36,7 @@ impl EditChannel {
/// The name of the channel.
///
/// Must be between 2 and 100 characters long.
pub fn name(&mut self, name: &str) -> &mut Self {
pub fn name<S: ToString>(&mut self, name: S) -> &mut Self {
self.0.insert("name", Value::String(name.to_string()));
self
}
Expand All @@ -54,7 +54,7 @@ impl EditChannel {
/// This is for [text] channels only.
///
/// [text]: ../model/channel/enum.ChannelType.html#variant.Text
pub fn topic(&mut self, topic: &str) -> &mut Self {
pub fn topic<S: ToString>(&mut self, topic: S) -> &mut Self {
self.0.insert("topic", Value::String(topic.to_string()));
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_guild.rs
Expand Up @@ -94,7 +94,7 @@ impl EditGuild {
/// Set the name of the guild.
///
/// **Note**: Must be between (and including) 2-100 chracters.
pub fn name(&mut self, name: &str) -> &mut Self {
pub fn name<S: ToString>(&mut self, name: S) -> &mut Self {
self.0.insert("name", Value::String(name.to_string()));
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_member.rs
Expand Up @@ -37,7 +37,7 @@ impl EditMember {
/// Requires the [Manage Nicknames] permission.
///
/// [Manage Nicknames]: ../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_NICKNAMES
pub fn nickname(&mut self, nickname: &str) -> &mut Self {
pub fn nickname<S: ToString>(&mut self, nickname: S) -> &mut Self {
self.0.insert("nick", Value::String(nickname.to_string()));
self
}
Expand Down
9 changes: 2 additions & 7 deletions src/builder/edit_message.rs
@@ -1,5 +1,4 @@
use crate::internal::prelude::*;
use std::fmt::Display;
use super::CreateEmbed;
use crate::utils::{self, VecMap};

Expand Down Expand Up @@ -38,15 +37,11 @@ impl EditMessage {
///
/// **Note**: Message contents must be under 2000 unicode code points.
#[inline]
pub fn content<D: Display>(&mut self, content: D) -> &mut Self {
self._content(content.to_string());
pub fn content<D: ToString>(&mut self, content: D) -> &mut Self {
self.0.insert("content", Value::String(content.to_string()));
self
}

fn _content(&mut self, content: String) {
self.0.insert("content", Value::String(content));
}

/// Set an embed for the message.
pub fn embed<F>(&mut self, f: F) -> &mut Self
where F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed {
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_profile.rs
Expand Up @@ -100,7 +100,7 @@ impl EditProfile {
/// and current discriminator, a new unique discriminator will be assigned.
/// If there are no available discriminators with the requested username,
/// an error will occur.
pub fn username(&mut self, username: &str) -> &mut Self {
pub fn username<S: ToString>(&mut self, username: S) -> &mut Self {
self.0.insert("username", Value::String(username.to_string()));
self
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_role.rs
Expand Up @@ -93,7 +93,7 @@ impl EditRole {
}

/// The name of the role to set.
pub fn name(&mut self, name: &str) -> &mut Self {
pub fn name<S: ToString>(&mut self, name: S) -> &mut Self {
self.0.insert("name", Value::String(name.to_string()));
self
}
Expand Down
7 changes: 3 additions & 4 deletions src/builder/execute_webhook.rs
@@ -1,5 +1,4 @@
use serde_json::Value;
use std::default::Default;
use crate::utils::VecMap;

/// A builder to create the inner content of a [`Webhook`]'s execution.
Expand Down Expand Up @@ -71,7 +70,7 @@ impl ExecuteWebhook {
/// w.avatar_url(avatar_url).content("Here's a webhook")
/// });
/// ```
pub fn avatar_url(&mut self, avatar_url: &str) -> &mut Self {
pub fn avatar_url<S: ToString>(&mut self, avatar_url: S) -> &mut Self {
self.0.insert("avatar_url", Value::String(avatar_url.to_string()));
self
}
Expand Down Expand Up @@ -102,7 +101,7 @@ impl ExecuteWebhook {
/// ```
///
/// [`embeds`]: #method.embeds
pub fn content(&mut self, content: &str) -> &mut Self {
pub fn content<S: ToString>(&mut self, content: S) -> &mut Self {
self.0.insert("content", Value::String(content.to_string()));
self
}
Expand Down Expand Up @@ -172,7 +171,7 @@ impl ExecuteWebhook {
/// println!("Err sending webhook: {:?}", why);
/// }
/// ```
pub fn username(&mut self, username: &str) -> &mut Self {
pub fn username<S: ToString>(&mut self, username: S) -> &mut Self {
self.0.insert("username", Value::String(username.to_string()));
self
}
Expand Down

0 comments on commit 13fae29

Please sign in to comment.