Skip to content

Commit

Permalink
Also make the message builder use Display
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis committed Aug 4, 2017
1 parent 7e913b6 commit 05162aa
Showing 1 changed file with 119 additions and 53 deletions.
172 changes: 119 additions & 53 deletions src/utils/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ impl MessageBuilder {
///
/// assert_eq!(message.push("ing").0, "testing");
/// ```
pub fn push<T: Into<Content>>(mut self, content: T) -> Self {
let content = content.into();
self.0.push_str(&content.to_string());
pub fn push<D: fmt::Display>(mut self, content: D) -> Self {
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);

self
}
Expand Down Expand Up @@ -235,15 +236,21 @@ impl MessageBuilder {
///
/// assert_eq!(content, "```\nhello\n```");
/// ```
pub fn push_codeblock(mut self, content: &str, language: Option<&str>) -> Self {
pub fn push_codeblock<D: fmt::Display>(mut self, content: D, language: Option<D>) -> Self {
self.0.push_str("```");

if let Some(language) = language {
self.0.push_str(language);
let mut s = "".to_string();
let _ = write!(&mut s, "{}", language);
self.0.push_str(&s);
}

self.0.push('\n');
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push_str("\n```");

self
Expand Down Expand Up @@ -275,9 +282,13 @@ impl MessageBuilder {
///
/// assert_eq!(content, expected);
/// ```
pub fn push_mono(mut self, content: &str) -> Self {
pub fn push_mono<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push('`');
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push('`');

self
Expand All @@ -304,36 +315,52 @@ impl MessageBuilder {
///
/// assert_eq!(content, expected);
/// ```
pub fn push_italic(mut self, content: &str) -> Self {
pub fn push_italic<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push('_');
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push('_');

self
}

/// Pushes an inline bold text to the content.
pub fn push_bold(mut self, content: &str) -> Self {
pub fn push_bold<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("**");
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push_str("**");

self
}

/// Pushes an underlined inline text to the content.
pub fn push_underline(mut self, content: &str) -> Self {
pub fn push_underline<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("__");
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push_str("__");

self
}

/// Pushes a strikethrough inline text to the content.
pub fn push_strike(mut self, content: &str) -> Self {
pub fn push_strike<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("~~");
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
self.0.push_str(&s);
}
self.0.push_str("~~");

self
Expand All @@ -352,7 +379,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "hello\nworld");
/// ```
pub fn push_line<T: Into<Content>>(mut self, content: T) -> Self {
pub fn push_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push(content);
self.0.push('\n');

Expand All @@ -372,7 +399,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "`hello`\nworld");
/// ```
pub fn push_mono_line(mut self, content: &str) -> Self {
pub fn push_mono_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_mono(content);
self.0.push('\n');

Expand All @@ -392,7 +419,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "_hello_\nworld");
/// ```
pub fn push_italic_line(mut self, content: &str) -> Self {
pub fn push_italic_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_italic(content);
self.0.push('\n');

Expand All @@ -412,7 +439,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "**hello**\nworld");
/// ```
pub fn push_bold_line(mut self, content: &str) -> Self {
pub fn push_bold_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_bold(content);
self.0.push('\n');

Expand All @@ -432,7 +459,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "__hello__\nworld");
/// ```
pub fn push_underline_line(mut self, content: &str) -> Self {
pub fn push_underline_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_underline(content);
self.0.push('\n');

Expand All @@ -452,7 +479,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "~~hello~~\nworld");
/// ```
pub fn push_strike_line(mut self, content: &str) -> Self {
pub fn push_strike_line<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_strike(content);
self.0.push('\n');

Expand All @@ -461,75 +488,108 @@ impl MessageBuilder {

/// Pushes text to your message, but normalizing content - that means
/// ensuring that there's no unwanted formatting, mention spam etc.
pub fn push_safe<T: Into<Content>>(mut self, content: T) -> Self {
let mut content: Content = content.into();
content.inner = normalize(&content.inner)
.replace('*', "\\*")
.replace('`', "\\`")
.replace('_', "\\_");

self.0.push_str(&content.to_string());
pub fn push_safe<D: fmt::Display>(mut self, content: D) -> Self {
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s)
.replace('*', "\\=*")
.replace('`', "\\`")
.replace('_', "\\_");

self.0.push_str(&s);
}

self
}

/// Pushes a code-block to your message normalizing content.
pub fn push_codeblock_safe(mut self, content: &str, language: Option<&str>) -> Self {
let content = &normalize(content).replace("```", "'''");

pub fn push_codeblock_safe<D: fmt::Display>(mut self, content: D, language: Option<D>) -> Self {
self.0.push_str("```");

if let Some(language) = language {
self.0.push_str(language);
let mut s = "".to_string();
let _ = write!(&mut s, "{}", language);
self.0.push_str(&s);
}

self.0.push('\n');
self.0.push_str(content);
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace("```", "'''");
self.0.push_str(&s);
}
self.0.push_str("```");

self
}

/// Pushes an inline monospaced text to the content normalizing content.
pub fn push_mono_safe(mut self, content: &str) -> Self {
pub fn push_mono_safe<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push('`');
self.0.push_str(&normalize(content).replace('`', "'"));
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace('`', "'");
self.0.push_str(&s);
}
self.0.push('`');

self
}

/// Pushes an inline italicized text to the content normalizing content.
pub fn push_italic_safe(mut self, content: &str) -> Self {
pub fn push_italic_safe<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push('_');
self.0.push_str(&normalize(content).replace('_', " "));
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace('_', " ");
self.0.push_str(&s);
}
self.0.push('_');

self
}

/// Pushes an inline bold text to the content normalizing content.
pub fn push_bold_safe(mut self, content: &str) -> Self {
pub fn push_bold_safe<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("**");
self.0.push_str(&normalize(content).replace("**", " "));
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace("**", " ");
self.0.push_str(&s);
}
self.0.push_str("**");

self
}

/// Pushes an underlined inline text to the content normalizing content.
pub fn push_underline_safe(mut self, content: &str) -> Self {
pub fn push_underline_safe<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("__");
self.0.push_str(&normalize(content).replace("__", " "));
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace("__", " ");
self.0.push_str(&s);
}
self.0.push_str("__");

self
}

/// Pushes a strikethrough inline text to the content normalizing content.
pub fn push_strike_safe(mut self, content: &str) -> Self {
pub fn push_strike_safe<D: fmt::Display>(mut self, content: D) -> Self {
self.0.push_str("~~");
self.0.push_str(&normalize(content).replace("~~", " "));
{
let mut s = "".to_string();
let _ = write!(&mut s, "{}", content);
s = normalize(&s).replace("~~", " ");
self.0.push_str(&s);
}
self.0.push_str("~~");

self
Expand All @@ -550,7 +610,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "Hello @\u{200B}everyone\nHow are you?");
/// ```
pub fn push_line_safe<T: Into<Content>>(mut self, content: T) -> Self {
pub fn push_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_safe(content);
self.0.push('\n');

Expand All @@ -572,7 +632,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "`'hello @\u{200B}everyone'`\nworld");
/// ```
pub fn push_mono_line_safe(mut self, content: &str) -> Self {
pub fn push_mono_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_mono_safe(content);
self.0.push('\n');

Expand All @@ -594,7 +654,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "_@\u{200B}everyone_\nIsn't a mention.");
/// ```
pub fn push_italic_line_safe(mut self, content: &str) -> Self {
pub fn push_italic_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_italic_safe(content);
self.0.push('\n');

Expand All @@ -616,7 +676,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "**@\u{200B}everyone**\nIsn't a mention.");
/// ```
pub fn push_bold_line_safe(mut self, content: &str) -> Self {
pub fn push_bold_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_bold_safe(content);
self.0.push('\n');

Expand All @@ -638,7 +698,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "__@\u{200B}everyone__\nIsn't a mention.");
/// ```
pub fn push_underline_line_safe(mut self, content: &str) -> Self {
pub fn push_underline_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_underline_safe(content);
self.0.push('\n');

Expand All @@ -661,7 +721,7 @@ impl MessageBuilder {
///
/// assert_eq!(content, "~~@\u{200B}everyone~~\nIsn't a mention.");
/// ```
pub fn push_strike_line_safe(mut self, content: &str) -> Self {
pub fn push_strike_line_safe<D: fmt::Display>(mut self, content: D) -> Self {
self = self.push_strike_safe(content);
self.0.push('\n');

Expand Down Expand Up @@ -755,6 +815,12 @@ pub struct Content {
pub underline: bool,
}

impl fmt::Display for Content {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}

impl<T: ToString> Add<T> for Content {
type Output = Content;

Expand Down Expand Up @@ -884,8 +950,8 @@ impl From<ContentModifier> for Content {
fn from(cm: ContentModifier) -> Content { cm.to_content() }
}

impl<T: ToString> From<T> for Content {
fn from(stringer: T) -> Content {
impl Content {
pub fn from<T: ToString>(stringer: T) -> Content {
Content {
italic: false,
bold: false,
Expand Down

0 comments on commit 05162aa

Please sign in to comment.