diff options
| author | Austin Hellyer <[email protected]> | 2016-09-19 09:00:03 -0700 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-10-18 11:14:27 -0700 |
| commit | 8fc8c81403c3daa187ba96a7d488a64db21463bf (patch) | |
| tree | 81bc4890c28b08ce806f69084617066bce863c2d /src/utils/message_builder.rs | |
| download | serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.tar.xz serenity-8fc8c81403c3daa187ba96a7d488a64db21463bf.zip | |
Initial commit
Diffstat (limited to 'src/utils/message_builder.rs')
| -rw-r--r-- | src/utils/message_builder.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/utils/message_builder.rs b/src/utils/message_builder.rs new file mode 100644 index 0000000..a24fd2d --- /dev/null +++ b/src/utils/message_builder.rs @@ -0,0 +1,97 @@ +use std::default::Default; +use std::fmt; +use ::model::{ChannelId, Emoji, Mentionable, RoleId, UserId}; + +/// The Message Builder is an ergonomic utility to easily build a message, +/// by adding text and mentioning mentionable structs. +/// +/// The finalized value can be accessed via `.build()` or the inner value. +/// +/// # Examples +/// +/// Build a message, mentioning a user and an emoji: +/// +/// ```rust,ignore +/// use serenity::utils::MessageBuilder; +/// +/// let content = MessageBuilder::new() +/// .push("You sent a message, ") +/// .mention(user) +/// .push("! "); +/// .mention(emoji) +/// .build(); +/// ``` +pub struct MessageBuilder(pub String); + +impl MessageBuilder { + /// Creates a new, empty-content builder. + pub fn new() -> MessageBuilder { + MessageBuilder::default() + } + + /// Pulls the inner value out of the builder. This is equivilant to simply + /// retrieving the value. + pub fn build(self) -> String { + self.0 + } + + /// Mentions the channel in the built message. + pub fn channel<C: Into<ChannelId>>(mut self, channel: C) -> Self { + self.0.push_str(&format!("{}", channel.into())); + + self + } + + /// Uses and displays the given emoji in the built message. + pub fn emoji(mut self, emoji: Emoji) -> Self { + self.0.push_str(&format!("{}", emoji)); + + self + } + + /// Mentions something that implements the + /// [Mentionable](../model/trait.Mentionable.html) trait. + pub fn mention<M: Mentionable>(mut self, item: M) -> Self { + self.0.push_str(&item.mention()); + + self + } + + /// Pushes a string to the internal message content. + /// + /// Note that this does not mutate either the given data or the internal + /// message content in anyway prior to appending the given content to the + /// internal message. + pub fn push(mut self, content: &str) -> Self { + self.0.push_str(content); + + self + } + + + /// Mentions the role in the built message. + pub fn role<R: Into<RoleId>>(mut self, role: R) -> Self { + self.0.push_str(&format!("{}", role.into())); + + self + } + + /// Mentions the user in the built message. + pub fn user<U: Into<UserId>>(mut self, user: U) -> Self { + self.0.push_str(&format!("{}", user.into())); + + self + } +} + +impl fmt::Display for MessageBuilder { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +impl Default for MessageBuilder { + fn default() -> MessageBuilder { + MessageBuilder(String::default()) + } +} |