diff options
| author | Zeyla Hellyer <[email protected]> | 2018-04-26 21:02:02 -0700 |
|---|---|---|
| committer | Ken Swenson <[email protected]> | 2018-11-06 20:30:49 -0500 |
| commit | 0de0a81be06268f8ec57c8d825cbc70355fe869a (patch) | |
| tree | 9cfeaf04035c5623c720a6252be9470a03ccbd8f /src/builder | |
| parent | Permit sending files through the `CreateMessage` builder. (diff) | |
| download | serenity-0de0a81be06268f8ec57c8d825cbc70355fe869a.tar.xz serenity-0de0a81be06268f8ec57c8d825cbc70355fe869a.zip | |
Make builders mutably borrowed
Change the builders so that they are now mutably borrowed, accepting
`&mut self` instead of `self`. Their methods now return `()` instead of
`Self`.
Upgrade path:
Change code such as the following:
```rust
channel.send_message(|m| m
.embed(|e| e
.description("test")
.title("title")));
```
to the following style:
```rust
channel.send_message(|mut m| {
m.embed(|mut e| {
e.description("test");
e.title("title");
e
});
m
});
```
Closes #159.
Diffstat (limited to 'src/builder')
| -rw-r--r-- | src/builder/create_embed.rs | 199 | ||||
| -rw-r--r-- | src/builder/create_invite.rs | 49 | ||||
| -rw-r--r-- | src/builder/create_message.rs | 56 | ||||
| -rw-r--r-- | src/builder/edit_channel.rs | 28 | ||||
| -rw-r--r-- | src/builder/edit_guild.rs | 74 | ||||
| -rw-r--r-- | src/builder/edit_member.rs | 28 | ||||
| -rw-r--r-- | src/builder/edit_message.rs | 16 | ||||
| -rw-r--r-- | src/builder/edit_profile.rs | 23 | ||||
| -rw-r--r-- | src/builder/edit_role.rs | 43 | ||||
| -rw-r--r-- | src/builder/execute_webhook.rs | 90 | ||||
| -rw-r--r-- | src/builder/get_messages.rs | 45 |
11 files changed, 312 insertions, 339 deletions
diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index 35172e1..56c3e56 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -49,13 +49,11 @@ impl CreateEmbed { /// information. /// /// [`CreateEmbedAuthor`]: struct.CreateEmbedAuthor.html - pub fn author<F>(mut self, f: F) -> Self + pub fn author<F>(&mut self, f: F) where F: FnOnce(CreateEmbedAuthor) -> CreateEmbedAuthor { let map = utils::vecmap_to_json_map(f(CreateEmbedAuthor::default()).0); self.0.insert("author", Value::Object(map)); - - self } /// Set the colour of the left-hand side of the embed. @@ -65,23 +63,23 @@ impl CreateEmbed { /// [`colour`]: #method.colour #[cfg(feature = "utils")] #[inline] - pub fn color<C: Into<Colour>>(self, colour: C) -> Self { self.colour(colour) } + pub fn color<C: Into<Colour>>(&mut self, colour: C) { + self.colour(colour); + } /// Set the colour of the left-hand side of the embed. #[cfg(feature = "utils")] #[inline] - pub fn colour<C: Into<Colour>>(self, colour: C) -> Self { - self._colour(colour.into()) + pub fn colour<C: Into<Colour>>(&mut self, colour: C) { + self._colour(colour.into()); } #[cfg(feature = "utils")] - fn _colour(mut self, colour: Colour) -> Self { + fn _colour(&mut self, colour: Colour) { self.0.insert( "color", Value::Number(Number::from(u64::from(colour.0))), ); - - self } /// Set the colour of the left-hand side of the embed. @@ -91,32 +89,29 @@ impl CreateEmbed { /// [`colour`]: #method.colour #[cfg(not(feature = "utils"))] #[inline] - pub fn color(self, colour: u32) -> Self { self.colour(colour) } + pub fn color(self, colour: u32) { + self.colour(colour); + } /// Set the colour of the left-hand side of the embed. #[cfg(not(feature = "utils"))] - pub fn colour(mut self, colour: u32) -> Self { - self.0 - .insert("color", Value::Number(Number::from(colour))); - - self + pub fn colour(&mut self, colour: u32) { + self.0.insert("color", Value::Number(Number::from(colour))); } /// Set the description of the embed. /// /// **Note**: This can't be longer than 2048 characters. #[inline] - pub fn description<D: Display>(self, description: D) -> Self { + pub fn description<D: Display>(&mut self, description: D) { self._description(description.to_string()) } - fn _description(mut self, description: String) -> Self { + fn _description(&mut self, description: String) { self.0.insert( "description", Value::String(description), ); - - self } /// Set a field. Note that this will not overwrite other fields, and will @@ -125,12 +120,12 @@ impl CreateEmbed { /// **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>(self, name: T, value: U, inline: bool) -> Self + pub fn field<T, U>(&mut self, name: T, value: U, inline: bool) where T: Display, U: Display { - self._field(&name.to_string(), &value.to_string(), inline) + self._field(&name.to_string(), &value.to_string(), inline); } - fn _field(mut self, name: &str, value: &str, inline: bool) -> Self { + fn _field(&mut self, name: &str, value: &str, inline: bool) { { let entry = self.0 .entry("fields") @@ -144,8 +139,6 @@ impl CreateEmbed { })); } } - - self } /// Adds multiple fields at once. @@ -153,15 +146,13 @@ impl CreateEmbed { /// This is sugar to reduce the need of calling [`field`] manually multiple times. /// /// [`field`]: #method.field - pub fn fields<T, U, It>(mut self, fields: It) -> Self + pub fn fields<T, U, It>(&mut self, fields: It) where It: IntoIterator<Item=(T, U, bool)>, T: Display, U: Display { for field in fields { - self = self.field(field.0.to_string(), field.1.to_string(), field.2); + self.field(field.0.to_string(), field.1.to_string(), field.2); } - - self } /// Set the footer of the embed. @@ -170,44 +161,40 @@ impl CreateEmbed { /// information. /// /// [`CreateEmbedFooter`]: struct.CreateEmbedFooter.html - pub fn footer<F>(mut self, f: F) -> Self + pub fn footer<F>(&mut self, f: F) where F: FnOnce(CreateEmbedFooter) -> CreateEmbedFooter { let footer = f(CreateEmbedFooter::default()).0; let map = utils::vecmap_to_json_map(footer); self.0.insert("footer", Value::Object(map)); - - self } - fn url_object(mut self, name: &'static str, url: &str) -> Self { + fn url_object(&mut self, name: &'static str, url: &str) { let obj = json!({ "url": url.to_string() }); self.0.insert(name, obj); - - self } /// Set the image associated with the embed. This only supports HTTP(S). #[inline] - pub fn image<S: AsRef<str>>(self, url: S) -> Self { - self._image(url.as_ref()) + pub fn image<S: AsRef<str>>(&mut self, url: S) { + self._image(url.as_ref()); } - fn _image(self, url: &str) -> Self { - self.url_object("image", url) + fn _image(&mut self, url: &str) { + self.url_object("image", url); } /// Set the thumbnail of the embed. This only supports HTTP(S). #[inline] - pub fn thumbnail<S: AsRef<str>>(self, url: S) -> Self { - self._thumbnail(url.as_ref()) + pub fn thumbnail<S: AsRef<str>>(&mut self, url: S) { + self._thumbnail(url.as_ref()); } - fn _thumbnail(self, url: &str) -> Self { - self.url_object("thumbnail", url) + fn _thumbnail(&mut self, url: &str) { + self.url_object("thumbnail", url); } /// Set the timestamp. @@ -235,10 +222,16 @@ impl CreateEmbed { /// impl EventHandler for Handler { /// fn message(&self, _: Context, msg: Message) { /// if msg.content == "~embed" { - /// let _ = msg.channel_id.send_message(|m| m - /// .embed(|e| e - /// .title("hello") - /// .timestamp("2004-06-08T16:04:23"))); + /// let _ = msg.channel_id.send_message(|mut m| { + /// m.embed(|mut e| { + /// e.title("hello"); + /// e.timestamp("2004-06-08T16:04:23"); + /// + /// e + /// }); + /// + /// m + /// }); /// } /// } /// } @@ -275,18 +268,25 @@ impl CreateEmbed { /// if let Some(channel) = channel_search { /// let user = member.user.read(); /// - /// let _ = channel.read().send_message(|m| m - /// .embed(|e| { - /// let mut e = e - /// .author(|a| a.icon_url(&user.face()).name(&user.name)) - /// .title("Member Join"); + /// let _ = channel.read().send_message(|mut m| { + /// m.embed(|mut e| { + /// e.author(|mut a| { + /// a.icon_url(&user.face()); + /// a.name(&user.name); + /// + /// a + /// }); + /// e.title("Member Join"); /// /// if let Some(ref joined_at) = member.joined_at { - /// e = e.timestamp(joined_at); + /// e.timestamp(joined_at); /// } /// /// e - /// })); + /// }); + /// + /// m + /// }); /// } /// } /// } @@ -297,38 +297,32 @@ impl CreateEmbed { /// client.start().unwrap(); /// ``` #[inline] - pub fn timestamp<T: Into<Timestamp>>(self, timestamp: T) -> Self { - self._timestamp(timestamp.into()) + pub fn timestamp<T: Into<Timestamp>>(&mut self, timestamp: T) { + self._timestamp(timestamp.into()); } - fn _timestamp(mut self, timestamp: Timestamp) -> Self { + fn _timestamp(&mut self, timestamp: Timestamp) { self.0.insert("timestamp", Value::String(timestamp.ts)); - - self } /// Set the title of the embed. #[inline] - pub fn title<D: Display>(self, title: D) -> Self { - self._title(title.to_string()) + pub fn title<D: Display>(&mut self, title: D) { + self._title(title.to_string()); } - fn _title(mut self, title: String) -> Self { + fn _title(&mut self, title: String) { self.0.insert("title", Value::String(title)); - - self } /// Set the URL to direct to when clicking on the title. #[inline] - pub fn url<S: AsRef<str>>(self, url: S) -> Self { - self._url(url.as_ref()) + pub fn url<S: AsRef<str>>(&mut self, url: S) { + self._url(url.as_ref()); } - fn _url(mut self, url: &str) -> Self { + fn _url(&mut self, url: &str) { self.0.insert("url", Value::String(url.to_string())); - - self } /// Same as calling [`image`] with "attachment://filename.(jpg, png)". @@ -340,12 +334,12 @@ impl CreateEmbed { /// /// [`image`]: #method.image #[inline] - pub fn attachment<S: AsRef<str>>(self, filename: S) -> Self { - self._attachment(filename.as_ref()) + pub fn attachment<S: AsRef<str>>(&mut self, filename: S) { + self._attachment(filename.as_ref()); } - fn _attachment(self, filename: &str) -> Self { - self.image(&format!("attachment://{}", filename)) + fn _attachment(&mut self, filename: &str) { + self.image(&format!("attachment://{}", filename)); } } @@ -363,19 +357,20 @@ impl From<Embed> for CreateEmbed { /// Converts the fields of an embed into the values for a new embed builder. /// /// Some values - such as Proxy URLs - are not preserved. - fn from(embed: Embed) -> CreateEmbed { - let mut b = CreateEmbed::default().colour(embed.colour); + fn from(embed: Embed) -> Self { + let mut b = CreateEmbed::default(); + b.colour(embed.colour); if let Some(author) = embed.author { - b = b.author(move |mut a| { - a = a.name(&author.name); + b.author(move |mut a| { + a.name(&author.name); if let Some(icon_url) = author.icon_url { - a = a.icon_url(&icon_url); + a.icon_url(&icon_url); } if let Some(url) = author.url { - a = a.url(&url); + a.url(&url); } a @@ -383,39 +378,39 @@ impl From<Embed> for CreateEmbed { } if let Some(description) = embed.description { - b = b.description(&description); + b.description(&description); } for field in embed.fields { - b = b.field(field.name, field.value, field.inline); + b.field(field.name, field.value, field.inline); } if let Some(image) = embed.image { - b = b.image(&image.url); + b.image(&image.url); } if let Some(timestamp) = embed.timestamp { - b = b.timestamp(timestamp); + b.timestamp(timestamp); } if let Some(thumbnail) = embed.thumbnail { - b = b.thumbnail(&thumbnail.url); + b.thumbnail(&thumbnail.url); } if let Some(url) = embed.url { - b = b.url(&url); + b.url(&url); } if let Some(title) = embed.title { - b = b.title(&title); + b.title(&title); } if let Some(footer) = embed.footer { - b = b.footer(move |mut f| { - f = f.text(&footer.text); + b.footer(move |mut f| { + f.text(&footer.text); if let Some(icon_url) = footer.icon_url { - f = f.icon_url(&icon_url); + f.icon_url(&icon_url); } f @@ -439,24 +434,18 @@ 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) -> Self { + pub fn icon_url(&mut self, icon_url: &str) { 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 { + pub fn name(&mut self, name: &str) { self.0.insert("name", Value::String(name.to_string())); - - self } /// Set the author's URL. - pub fn url(mut self, url: &str) -> Self { + pub fn url(&mut self, url: &str) { self.0.insert("url", Value::String(url.to_string())); - - self } } @@ -472,17 +461,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) -> Self { + pub fn icon_url(&mut self, icon_url: &str) { 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 { + pub fn text<D: Display>(&mut self, text: D) { self.0.insert("text", Value::String(text.to_string())); - - self } } @@ -493,15 +478,15 @@ pub struct Timestamp { impl From<String> for Timestamp { fn from(ts: String) -> Self { - Timestamp { - ts, + Self { + ts: ts, } } } impl<'a> From<&'a str> for Timestamp { fn from(ts: &'a str) -> Self { - Timestamp { + Self { ts: ts.to_string(), } } @@ -510,7 +495,7 @@ impl<'a> From<&'a str> for Timestamp { impl<'a, Tz: TimeZone> From<&'a DateTime<Tz>> for Timestamp where Tz::Offset: Display { fn from(dt: &'a DateTime<Tz>) -> Self { - Timestamp { + Self { ts: dt.to_rfc3339(), } } diff --git a/src/builder/create_invite.rs b/src/builder/create_invite.rs index c38924b..0b487b3 100644 --- a/src/builder/create_invite.rs +++ b/src/builder/create_invite.rs @@ -33,7 +33,14 @@ use utils::VecMap; /// /// let reader = channel.read(); /// -/// let invite = match reader.create_invite(|i| i.max_age(3600).max_uses(10)) { +/// let creation = reader.create_invite(|mut i| { +/// i.max_age(3600); +/// i.max_uses(10); +/// +/// i +/// }); +/// +/// let invite = match creation { /// Ok(invite) => invite, /// Err(why) => { /// println!("Err creating invite: {:?}", why); @@ -84,7 +91,11 @@ impl CreateInvite { /// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read(); /// # - /// let invite = channel.create_invite(|i| i.max_age(3600))?; + /// let invite = channel.create_invite(|mut i| { + /// i.max_age(3600); + /// + /// i + /// })?; /// # Ok(()) /// # } /// # @@ -92,10 +103,8 @@ impl CreateInvite { /// # try_main().unwrap(); /// # } /// ``` - pub fn max_age(mut self, max_age: u64) -> Self { + pub fn max_age(&mut self, max_age: u64) { self.0.insert("max_age", Value::Number(Number::from(max_age))); - - self } /// The number of uses that the invite will be valid for. @@ -117,7 +126,11 @@ impl CreateInvite { /// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read(); /// # - /// let invite = channel.create_invite(|i| i.max_uses(5))?; + /// let invite = channel.create_invite(|mut i| { + /// i.max_uses(5); + /// + /// i + /// })?; /// # Ok(()) /// # } /// # @@ -125,10 +138,8 @@ impl CreateInvite { /// # try_main().unwrap(); /// # } /// ``` - pub fn max_uses(mut self, max_uses: u64) -> Self { + pub fn max_uses(&mut self, max_uses: u64) { self.0.insert("max_uses", Value::Number(Number::from(max_uses))); - - self } /// Whether an invite grants a temporary membership. @@ -148,7 +159,11 @@ impl CreateInvite { /// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read(); /// # - /// let invite = channel.create_invite(|i| i.temporary(true))?; + /// let invite = channel.create_invite(|mut i| { + /// i.temporary(true); + /// + /// i + /// })?; /// # Ok(()) /// # } /// # @@ -156,10 +171,8 @@ impl CreateInvite { /// # try_main().unwrap(); /// # } /// ``` - pub fn temporary(mut self, temporary: bool) -> Self { + pub fn temporary(&mut self, temporary: bool) { self.0.insert("temporary", Value::Bool(temporary)); - - self } /// Whether or not to try to reuse a similar invite. @@ -179,7 +192,11 @@ impl CreateInvite { /// # let channel = CACHE.read().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read(); /// # - /// let invite = channel.create_invite(|i| i.unique(true))?; + /// let invite = channel.create_invite(|mut i| { + /// i.unique(true); + /// + /// i + /// })?; /// # Ok(()) /// # } /// # @@ -187,10 +204,8 @@ impl CreateInvite { /// # try_main().unwrap(); /// # } /// ``` - pub fn unique(mut self, unique: bool) -> Self { + pub fn unique(&mut self, unique: bool) { self.0.insert("unique", Value::Bool(unique)); - - self } } diff --git a/src/builder/create_message.rs b/src/builder/create_message.rs index 000ad09..aa1a23c 100644 --- a/src/builder/create_message.rs +++ b/src/builder/create_message.rs @@ -27,12 +27,19 @@ use utils::{self, VecMap}; /// /// let channel_id = ChannelId(7); /// -/// let _ = channel_id.send_message(|m| m -/// .content("test") -/// .tts(true) -/// .embed(|e| e -/// .title("This is an embed") -/// .description("With a description"))); +/// let _ = channel_id.send_message(|mut m| { +/// m.content("test"); +/// m.tts(true); +/// +/// m.embed(|mut e| { +/// e.title("This is an embed"); +/// e.description("With a description"); +/// +/// e +/// }); +/// +/// m +/// }); /// ``` /// /// [`ChannelId::say`]: ../model/id/struct.ChannelId.html#method.say @@ -48,25 +55,20 @@ impl<'a> CreateMessage<'a> { /// /// **Note**: Message contents must be under 2000 unicode code points. #[inline] - pub fn content<D: Display>(self, content: D) -> Self { - self._content(content.to_string()) + pub fn content<D: Display>(&mut self, content: D) { + self._content(content.to_string()); } - fn _content(mut self, content: String) -> Self { + fn _content(&mut self, content: String) { self.0.insert("content", Value::String(content)); - - self } /// Set an embed for the message. - pub fn embed<F>(mut self, f: F) -> Self - where F: FnOnce(CreateEmbed) -> CreateEmbed { + pub fn embed<F: FnOnce(CreateEmbed) -> CreateEmbed>(&mut self, f: F) { let map = utils::vecmap_to_json_map(f(CreateEmbed::default()).0); let embed = Value::Object(map); self.0.insert("embed", embed); - - self } /// Set whether the message is text-to-speech. @@ -74,46 +76,36 @@ impl<'a> CreateMessage<'a> { /// Think carefully before setting this to `true`. /// /// Defaults to `false`. - pub fn tts(mut self, tts: bool) -> Self { + pub fn tts(&mut self, tts: bool) { self.0.insert("tts", Value::Bool(tts)); - - self } /// Adds a list of reactions to create after the message's sent. #[inline] - pub fn reactions<R: Into<ReactionType>, It: IntoIterator<Item=R>>(self, reactions: It) -> Self { - self._reactions(reactions.into_iter().map(Into::into).collect()) + pub fn reactions<R: Into<ReactionType>, It: IntoIterator<Item=R>>(&mut self, reactions: It) { + self._reactions(reactions.into_iter().map(Into::into).collect()); } - fn _reactions(mut self, reactions: Vec<ReactionType>) -> Self { + fn _reactions(&mut self, reactions: Vec<ReactionType>) { self.1 = Some(reactions); - - self } /// Appends a file to the message. - pub fn add_file<T: Into<AttachmentType<'a>>>(mut self, file: T) -> Self { + pub fn add_file<T: Into<AttachmentType<'a>>>(&mut self, file: T) { self.2.push(file.into()); - - self } /// Appends a list of files to the message. - pub fn add_files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(mut self, files: It) -> Self { + pub fn add_files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(&mut self, files: It) { self.2.extend(files.into_iter().map(|f| f.into())); - - self } /// Sets a list of files to include in the message. /// /// Calling this multiple times will overwrite the file list. /// To append files, call `add_file` or `add_files` instead. - pub fn files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(mut self, files: It) -> Self { + pub fn files<T: Into<AttachmentType<'a>>, It: IntoIterator<Item=T>>(&mut self, files: It) { self.2 = files.into_iter().map(|f| f.into()).collect(); - - self } } diff --git a/src/builder/edit_channel.rs b/src/builder/edit_channel.rs index e1b62a7..cd0b137 100644 --- a/src/builder/edit_channel.rs +++ b/src/builder/edit_channel.rs @@ -28,26 +28,20 @@ impl EditChannel { /// This is for [voice] channels only. /// /// [voice]: ../model/channel/enum.ChannelType.html#variant.Voice - pub fn bitrate(mut self, bitrate: u64) -> Self { + pub fn bitrate(&mut self, bitrate: u64) { self.0.insert("bitrate", Value::Number(Number::from(bitrate))); - - self } /// The name of the channel. /// /// Must be between 2 and 100 characters long. - pub fn name(mut self, name: &str) -> Self { + pub fn name(&mut self, name: &str) { 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 { + pub fn position(&mut self, position: u64) { self.0.insert("position", Value::Number(Number::from(position))); - - self } /// The topic of the channel. Can be empty. @@ -57,10 +51,8 @@ impl EditChannel { /// This is for [text] channels only. /// /// [text]: ../model/channel/enum.ChannelType.html#variant.Text - pub fn topic(mut self, topic: &str) -> Self { + pub fn topic(&mut self, topic: &str) { self.0.insert("topic", Value::String(topic.to_string())); - - self } /// The number of users that may be in the channel simultaneously. @@ -68,10 +60,8 @@ impl EditChannel { /// This is for [voice] channels only. /// /// [voice]: ../model/channel/enum.ChannelType.html#variant.Voice - pub fn user_limit(mut self, user_limit: u64) -> Self { + pub fn user_limit(&mut self, user_limit: u64) { self.0.insert("user_limit", Value::Number(Number::from(user_limit))); - - self } /// The parent category of the channel. @@ -81,16 +71,14 @@ impl EditChannel { /// [text]: ../model/channel/enum.ChannelType.html#variant.Text /// [voice]: ../model/channel/enum.ChannelType.html#variant.Voice #[inline] - pub fn category<C: Into<Option<ChannelId>>>(self, category: C) -> Self { - self._category(category.into()) + pub fn category<C: Into<Option<ChannelId>>>(&mut self, category: C) { + self._category(category.into()); } - fn _category(mut self, category: Option<ChannelId>) -> Self { + fn _category(&mut self, category: Option<ChannelId>) { self.0.insert("parent_id", match category { Some(c) => Value::Number(Number::from(c.0)), None => Value::Null }); - - self } } diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs index c9083a2..13ec226 100644 --- a/src/builder/edit_guild.rs +++ b/src/builder/edit_guild.rs @@ -24,11 +24,11 @@ impl EditGuild { /// /// [`afk_timeout`]: #method.afk_timeout #[inline] - pub fn afk_channel<C: Into<ChannelId>>(self, channel: Option<C>) -> Self { - self._afk_channel(channel.map(Into::into)) + pub fn afk_channel<C: Into<ChannelId>>(&mut self, channel: Option<C>) { + self._afk_channel(channel.map(Into::into)); } - fn _afk_channel(mut self, channel: Option<ChannelId>) -> Self { + fn _afk_channel(&mut self, channel: Option<ChannelId>) { self.0.insert( "afk_channel_id", match channel { @@ -36,21 +36,17 @@ impl EditGuild { None => Value::Null, }, ); - - self } /// Set the amount of time a user is to be moved to the AFK channel - /// configured via [`afk_channel`] - after being AFK. /// /// [`afk_channel`]: #method.afk_channel - pub fn afk_timeout(mut self, timeout: u64) -> Self { + pub fn afk_timeout(&mut self, timeout: u64) { self.0.insert( "afk_timeout", Value::Number(Number::from(timeout)), ); - - self } /// Set the icon of the guild. Pass `None` to remove the icon. @@ -72,7 +68,11 @@ impl EditGuild { /// /// let base64_icon = utils::read_image("./guild_icon.png")?; /// - /// guild.edit(|g| g.icon(Some(&base64_icon)))?; + /// guild.edit(|mut g| { + /// g.icon(Some(&base64_icon)); + /// + /// g + /// })?; /// # Ok(()) /// # } /// # @@ -82,37 +82,31 @@ impl EditGuild { /// ``` /// /// [`utils::read_image`]: ../utils/fn.read_image.html - pub fn icon(mut self, icon: Option<&str>) -> Self { + pub fn icon(&mut self, icon: Option<&str>) { self.0.insert( "icon", icon.map_or_else(|| Value::Null, |x| Value::String(x.to_string())), ); - - self } /// Set the name of the guild. /// - /// **Note**: Must be between (and including) 2-100 characters. - pub fn name(mut self, name: &str) -> Self { + /// **Note**: Must be between (and including) 2-100 chracters. + pub fn name(&mut self, name: &str) { self.0.insert("name", Value::String(name.to_string())); - - self } /// Transfers the ownership of the guild to another user by Id. /// /// **Note**: The current user must be the owner of the guild. #[inline] - pub fn owner<U: Into<UserId>>(self, user_id: U) -> Self { - self._owner(user_id.into()) + pub fn owner<U: Into<UserId>>(&mut self, user_id: U) { + self._owner(user_id.into()); } - fn _owner(mut self, user_id: UserId) -> Self { + fn _owner(&mut self, user_id: UserId) { let id = Value::Number(Number::from(user_id.0)); self.0.insert("owner_id", id); - - self } /// Set the voice region of the server. @@ -131,7 +125,11 @@ impl EditGuild { /// /// // assuming a `guild` has already been bound /// - /// guild.edit(|g| g.region(Region::UsWest))?; + /// guild.edit(|mut g| { + /// g.region(Region::UsWest); + /// + /// g + /// })?; /// # Ok(()) /// # } /// # @@ -141,10 +139,8 @@ impl EditGuild { /// ``` /// /// [`Region::UsWest`]: ../model/guild/enum.Region.html#variant.UsWest - pub fn region(mut self, region: Region) -> Self { + pub fn region(&mut self, region: Region) { self.0.insert("region", Value::String(region.name().to_string())); - - self } /// Set the splash image of the guild on the invitation page. @@ -153,11 +149,9 @@ impl EditGuild { /// You can check this through a guild's [`features`] list. /// /// [`features`]: ../model/guild/struct.Guild.html#structfield.features - pub fn splash(mut self, splash: Option<&str>) -> Self { + pub fn splash(&mut self, splash: Option<&str>) { let splash = splash.map_or(Value::Null, |x| Value::String(x.to_string())); self.0.insert("splash", splash); - - self } /// Set the verification level of the guild. This can restrict what a @@ -176,14 +170,26 @@ impl EditGuild { /// /// // assuming a `guild` has already been bound /// - /// if let Err(why) = guild.edit(|g| g.verification_level(VerificationLevel::High)) { + /// let edit = guild.edit(|mut g| { + /// g.verification_level(VerificationLevel::High); + /// + /// g + /// }); + /// + /// if let Err(why) = edit { /// println!("Error setting verification level: {:?}", why); /// } /// /// // additionally, you may pass in just an integer of the verification /// // level /// - /// if let Err(why) = guild.edit(|g| g.verification_level(3)) { + /// let edit = guild.edit(|mut g| { + /// g.verification_level(3); + /// + /// g + /// }); + /// + /// if let Err(why) = edit { /// println!("Error setting verification level: {:?}", why); /// } /// ``` @@ -191,15 +197,13 @@ impl EditGuild { /// [`VerificationLevel`]: ../model/guild/enum.VerificationLevel.html /// [`VerificationLevel::High`]: ../model/guild/enum.VerificationLevel.html#variant.High #[inline] - pub fn verification_level<V>(self, verification_level: V) -> Self + pub fn verification_level<V>(&mut self, verification_level: V) where V: Into<VerificationLevel> { - self._verification_level(verification_level.into()) + self._verification_level(verification_level.into()); } - fn _verification_level(mut self, verification_level: VerificationLevel) -> Self { + fn _verification_level(&mut self, verification_level: VerificationLevel) { let num = Value::Number(Number::from(verification_level.num())); self.0.insert("verification_level", num); - - self } } diff --git a/src/builder/edit_member.rs b/src/builder/edit_member.rs index 0fba010..2b6674b 100644 --- a/src/builder/edit_member.rs +++ b/src/builder/edit_member.rs @@ -16,10 +16,8 @@ impl EditMember { /// Requires the [Deafen Members] permission. /// /// [Deafen Members]: ../model/permissions/struct.Permissions.html#associatedconstant.DEAFEN_MEMBERS - pub fn deafen(mut self, deafen: bool) -> Self { + pub fn deafen(&mut self, deafen: bool) { self.0.insert("deaf", Value::Bool(deafen)); - - self } /// Whether to mute the member. @@ -27,10 +25,8 @@ impl EditMember { /// Requires the [Mute Members] permission. /// /// [Mute Members]: ../model/permissions/struct.Permissions.html#associatedconstant.MUTE_MEMBERS - pub fn mute(mut self, mute: bool) -> Self { + pub fn mute(&mut self, mute: bool) { self.0.insert("mute", Value::Bool(mute)); - - self } /// Changes the member's nickname. Pass an empty string to reset the @@ -39,10 +35,8 @@ impl EditMember { /// Requires the [Manage Nicknames] permission. /// /// [Manage Nicknames]: ../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_NICKNAMES - pub fn nickname(mut self, nickname: &str) -> Self { + pub fn nickname(&mut self, nickname: &str) { self.0.insert("nick", Value::String(nickname.to_string())); - - self } /// Set the list of roles that the member should have. @@ -50,19 +44,17 @@ impl EditMember { /// Requires the [Manage Roles] permission to modify. /// /// [Manage Roles]: ../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_ROLES - pub fn roles<T: AsRef<RoleId>, It: IntoIterator<Item=T>>(self, roles: It) -> Self { - let roles = roles + pub fn roles<T: AsRef<RoleId>, It: IntoIterator<Item=T>>(&mut self, roles: It) { + let role_ids = roles .into_iter() .map(|x| Value::Number(Number::from(x.as_ref().0))) .collect(); - self._roles(roles) + self._roles(role_ids); } - fn _roles(mut self, roles: Vec<Value>) -> Self { + fn _roles(&mut self, roles: Vec<Value>) { self.0.insert("roles", Value::Array(roles)); - - self } /// The Id of the voice channel to move the member to. @@ -71,14 +63,12 @@ impl EditMember { /// /// [Move Members]: ../model/permissions/struct.Permissions.html#associatedconstant.MOVE_MEMBERS #[inline] - pub fn voice_channel<C: Into<ChannelId>>(self, channel_id: C) -> Self { + pub fn voice_channel<C: Into<ChannelId>>(&mut self, channel_id: C) { self._voice_channel(channel_id.into()) } - fn _voice_channel(mut self, channel_id: ChannelId) -> Self { + fn _voice_channel(&mut self, channel_id: ChannelId) { let num = Value::Number(Number::from(channel_id.0)); self.0.insert("channel_id", num); - - self } } diff --git a/src/builder/edit_message.rs b/src/builder/edit_message.rs index b796804..74d7123 100644 --- a/src/builder/edit_message.rs +++ b/src/builder/edit_message.rs @@ -14,8 +14,11 @@ use utils::{self, VecMap}; /// # /// # let mut message = ChannelId(7).message(MessageId(8)).unwrap(); /// # +/// let _ = message.edit(|mut m| { +/// m.content("hello"); /// -/// let _ = message.edit(|m| m.content("hello")); +/// m +/// }); /// ``` /// /// [`Message`]: ../model/channel/struct.Message.html @@ -27,24 +30,19 @@ impl EditMessage { /// /// **Note**: Message contents must be under 2000 unicode code points. #[inline] - pub fn content<D: Display>(self, content: D) -> Self { + pub fn content<D: Display>(&mut self, content: D) { self._content(content.to_string()) } - fn _content(mut self, content: String) -> Self { + fn _content(&mut self, content: String) { self.0.insert("content", Value::String(content)); - - self } /// Set an embed for the message. - pub fn embed<F>(mut self, f: F) -> Self - where F: FnOnce(CreateEmbed) -> CreateEmbed { + pub fn embed<F: FnOnce(CreateEmbed) -> CreateEmbed>(&mut self, f: F) { let map = utils::vecmap_to_json_map(f(CreateEmbed::default()).0); let embed = Value::Object(map); self.0.insert("embed", embed); - - self } } diff --git a/src/builder/edit_profile.rs b/src/builder/edit_profile.rs index 5bc0b92..2efe77c 100644 --- a/src/builder/edit_profile.rs +++ b/src/builder/edit_profile.rs @@ -34,8 +34,10 @@ impl EditProfile { /// let base64 = utils::read_image("./my_image.jpg") /// .expect("Failed to read image"); /// - /// let _ = context.edit_profile(|profile| { - /// profile.avatar(Some(&base64)) + /// let _ = context.edit_profile(|mut profile| { + /// profile.avatar(Some(&base64)); + /// + /// profile /// }); /// # } /// } @@ -46,11 +48,10 @@ impl EditProfile { /// ``` /// /// [`utils::read_image`]: ../utils/fn.read_image.html - pub fn avatar(mut self, avatar: Option<&str>) -> Self { + pub fn avatar(&mut self, avatar: Option<&str>) { let avatar = avatar.map_or(Value::Null, |x| Value::String(x.to_string())); - self.0.insert("avatar", avatar); - self + self.0.insert("avatar", avatar); } /// Modifies the current user's email address. @@ -75,10 +76,8 @@ impl EditProfile { /// [provided]. /// /// [provided]: #method.password - pub fn new_password(mut self, new_password: &str) -> Self { + pub fn new_password(&mut self, new_password: &str) { self.0.insert("new_password", Value::String(new_password.to_string())); - - self } /// Used for providing the current password as verification when @@ -86,10 +85,8 @@ impl EditProfile { /// /// [modifying the password]: #method.new_password /// [modifying the associated email address]: #method.email - pub fn password(mut self, password: &str) -> Self { + pub fn password(&mut self, password: &str) { self.0.insert("password", Value::String(password.to_string())); - - self } /// Modifies the current user's username. @@ -98,9 +95,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) -> Self { + pub fn username(&mut self, username: &str) { 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 bf0b489..c23f010 100644 --- a/src/builder/edit_role.rs +++ b/src/builder/edit_role.rs @@ -28,10 +28,13 @@ use utils::VecMap; /// # /// // assuming a `channel_id` and `guild_id` has been bound /// -/// let role = guild_id.create_role(|r| r -/// .hoist(true) -/// .mentionable(true) -/// .name("a test role")); +/// let role = guild_id.create_role(|mut r| { +/// r.hoist(true); +/// r.mentionable(true); +/// r.name("a test role"); +/// +/// r +/// }); /// ``` /// /// [`PartialGuild::create_role`]: ../model/guild/struct.PartialGuild.html#method.create_role @@ -72,47 +75,37 @@ impl EditRole { } /// Sets the colour of the role. - pub fn colour(mut self, colour: u64) -> Self { + pub fn colour(&mut self, colour: u64) { self.0.insert("color", Value::Number(Number::from(colour))); - - self } /// Whether or not to hoist the role above lower-positioned role in the user /// list. - pub fn hoist(mut self, hoist: bool) -> Self { + pub fn hoist(&mut self, hoist: bool) { 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 { + pub fn mentionable(&mut self, mentionable: bool) { self.0.insert("mentionable", Value::Bool(mentionable)); - - self } /// The name of the role to set. - pub fn name(mut self, name: &str) -> Self { - self.0 - .insert("name", Value::String(name.to_string())); - - self + pub fn name(&mut self, name: &str) { + self.0.insert("name", Value::String(name.to_string())); } /// The set of permissions to assign the role. - pub fn permissions(mut self, permissions: Permissions) -> Self { - self.0.insert("permissions", Value::Number(Number::from(permissions.bits()))); - - self + pub fn permissions(&mut self, permissions: Permissions) { + self.0.insert( + "permissions", + Value::Number(Number::from(permissions.bits())), + ); } /// 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 { + pub fn position(&mut self, position: u8) { self.0.insert("position", Value::Number(Number::from(position))); - - self } } diff --git a/src/builder/execute_webhook.rs b/src/builder/execute_webhook.rs index 780d102..821c125 100644 --- a/src/builder/execute_webhook.rs +++ b/src/builder/execute_webhook.rs @@ -26,21 +26,30 @@ use utils::VecMap; /// let webhook = http::get_webhook_with_token(id, token) /// .expect("valid webhook"); /// -/// let website = Embed::fake(|e| e -/// .title("The Rust Language Website") -/// .description("Rust is a systems programming language.") -/// .colour(Colour::from_rgb(222, 165, 132))); +/// let website = Embed::fake(|mut e| { +/// e.title("The Rust Language Website"); +/// e.description("Rust is a systems programming language."); +/// e.colour(Colour::from_rgb(222, 165, 132)); /// -/// let resources = Embed::fake(|e| e -/// .title("Rust Resources") -/// .description("A few resources to help with learning Rust") -/// .colour(0xDEA584) -/// .field("The Rust Book", "A comprehensive resource for Rust.", false) -/// .field("Rust by Example", "A collection of Rust examples", false)); +/// e +/// }); /// -/// let _ = webhook.execute(false, |w| w -/// .content("Here's some information on Rust:") -/// .embeds(vec![website, resources])); +/// let resources = Embed::fake(|mut e| { +/// e.title("Rust Resources"); +/// e.description("A few resources to help with learning Rust"); +/// e.colour(0xDEA584); +/// e.field("The Rust Book", "A comprehensive resource for Rust.", false); +/// e.field("Rust by Example", "A collection of Rust examples", false); +/// +/// e +/// }); +/// +/// let _ = webhook.execute(false, |mut w| { +/// w.content("Here's some information on Rust:"); +/// w.embeds(vec![website, resources]); +/// +/// w +/// }); /// ``` /// /// [`Webhook`]: ../model/webhook/struct.Webhook.html @@ -63,14 +72,15 @@ impl ExecuteWebhook { /// # /// let avatar_url = "https://i.imgur.com/KTs6whd.jpg"; /// - /// let _ = webhook.execute(false, |w| w - /// .avatar_url(avatar_url) - /// .content("Here's a webhook")); + /// let _ = webhook.execute(false, |mut w| { + /// w.avatar_url(avatar_url); + /// w.content("Here's a webhook"); + /// + /// w + /// }); /// ``` - pub fn avatar_url(mut self, avatar_url: &str) -> Self { + pub fn avatar_url(&mut self, avatar_url: &str) { self.0.insert("avatar_url", Value::String(avatar_url.to_string())); - - self } /// Set the content of the message. @@ -87,16 +97,20 @@ impl ExecuteWebhook { /// # /// # let webhook = rest::get_webhook_with_token(0, "").unwrap(); /// # - /// if let Err(why) = webhook.execute(false, |w| w.content("foo")) { + /// let execution = webhook.execute(false, |mut w| { + /// w.content("foo"); + /// + /// w + /// }); + /// + /// if let Err(why) = execution { /// println!("Err sending webhook: {:?}", why); /// } /// ``` /// /// [`embeds`]: #method.embeds - pub fn content(mut self, content: &str) -> Self { + pub fn content(&mut self, content: &str) { self.0.insert("content", Value::String(content.to_string())); - - self } /// Set the embeds associated with the message. @@ -112,10 +126,8 @@ impl ExecuteWebhook { /// [`Embed::fake`]: ../model/channel/struct.Embed.html#method.fake /// [`Webhook::execute`]: ../model/webhook/struct.Webhook.html#method.execute /// [struct-level documentation]: #examples - pub fn embeds(mut self, embeds: Vec<Value>) -> Self { + pub fn embeds(&mut self, embeds: Vec<Value>) { self.0.insert("embeds", Value::Array(embeds)); - - self } /// Whether the message is a text-to-speech message. @@ -129,14 +141,19 @@ impl ExecuteWebhook { /// # /// # let webhook = rest::get_webhook_with_token(0, "").unwrap(); /// # - /// if let Err(why) = webhook.execute(false, |w| w.content("hello").tts(true)) { + /// let execution = webhook.execute(false, |mut w| { + /// w.content("hello"); + /// w.tts(true); + /// + /// w + /// }); + /// + /// if let Err(why) = execution { /// println!("Err sending webhook: {:?}", why); /// } /// ``` - pub fn tts(mut self, tts: bool) -> Self { + pub fn tts(&mut self, tts: bool) { self.0.insert("tts", Value::Bool(tts)); - - self } /// Override the default username of the webhook. @@ -150,14 +167,19 @@ impl ExecuteWebhook { /// # /// # let webhook = rest::get_webhook_with_token(0, "").unwrap(); /// # - /// if let Err(why) = webhook.execute(false, |w| w.content("hello").username("hakase")) { + /// let execution = webhook.execute(false, |mut w| { + /// w.content("hello"); + /// w.username("hakase"); + /// + /// w + /// }); + /// + /// if let Err(why) = execution { /// println!("Err sending webhook: {:?}", why); /// } /// ``` - pub fn username(mut self, username: &str) -> Self { + pub fn username(&mut self, username: &str) { self.0.insert("username", Value::String(username.to_string())); - - self } } diff --git a/src/builder/get_messages.rs b/src/builder/get_messages.rs index 2d7ee87..644dc44 100644 --- a/src/builder/get_messages.rs +++ b/src/builder/get_messages.rs @@ -29,17 +29,17 @@ use utils::VecMap; /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box<Error>> { -/// use serenity::builder::GetMessages; /// use serenity::model::id::{ChannelId, MessageId}; /// -/// let retriever = GetMessages::default() -/// .after(MessageId(158339864557912064)) -/// .limit(25); -/// /// // you can then pass it into a function which retrieves messages: /// let channel_id = ChannelId(81384788765712384); /// -/// let _messages = channel_id.messages(|_| retriever)?; +/// let _messages = channel_id.messages(|mut retriever| { +/// retriever.after(MessageId(158339864557912064)); +/// retriever.limit(25); +/// +/// retriever +/// })?; /// # Ok(()) /// # } /// # @@ -56,40 +56,34 @@ impl GetMessages { /// Indicates to retrieve the messages after a specific message, given by /// its Id. #[inline] - pub fn after<M: Into<MessageId>>(self, message_id: M) -> Self { - self._after(message_id.into()) + pub fn after<M: Into<MessageId>>(&mut self, message_id: M) { + self._after(message_id.into()); } - fn _after(mut self, message_id: MessageId) -> Self { + fn _after(&mut self, message_id: MessageId) { self.0.insert("after", message_id.0); - - self } /// Indicates to retrieve the messages _around_ a specific message in either /// direction (before+after) the given message. #[inline] - pub fn around<M: Into<MessageId>>(self, message_id: M) -> Self { - self._around(message_id.into()) + pub fn around<M: Into<MessageId>>(&mut self, message_id: M) { + self._around(message_id.into()); } - fn _around(mut self, message_id: MessageId) -> Self { + fn _around(&mut self, message_id: MessageId) { self.0.insert("around", message_id.0); - - self } /// Indicates to retrieve the messages before a specific message, given by /// its Id. #[inline] - pub fn before<M: Into<MessageId>>(self, message_id: M) -> Self { - self._before(message_id.into()) + pub fn before<M: Into<MessageId>>(&mut self, message_id: M) { + self._before(message_id.into()); } - fn _before(mut self, message_id: MessageId) -> Self { + fn _before(&mut self, message_id: MessageId) { self.0.insert("before", message_id.0); - - self } /// The maximum number of messages to retrieve for the query. @@ -99,15 +93,12 @@ impl GetMessages { /// **Note**: This field is capped to 100 messages due to a Discord /// limitation. If an amount larger than 100 is supplied, it will be /// reduced. - pub fn limit(mut self, limit: u64) -> Self { - self.0 - .insert("limit", if limit > 100 { 100 } else { limit }); - - self + pub fn limit(&mut self, limit: u64) { + self.0.insert("limit", if limit > 100 { 100 } else { limit }); } /// This is a function that is here for completeness. You do not need to /// call this - except to clear previous calls to `after`, `around`, and /// `before` - as it is the default value. - pub fn most_recent(self) -> Self { self } + pub fn most_recent(&self) { } } |