diff --git a/src/utils/message_builder.rs b/src/utils/message_builder.rs index a5b976f4bdd..ac9440d1950 100644 --- a/src/utils/message_builder.rs +++ b/src/utils/message_builder.rs @@ -189,9 +189,10 @@ impl MessageBuilder { /// /// assert_eq!(message.push("ing").0, "testing"); /// ``` - pub fn push>(mut self, content: T) -> Self { - let content = content.into(); - self.0.push_str(&content.to_string()); + pub fn push(mut self, content: D) -> Self { + let mut s = "".to_string(); + let _ = write!(&mut s, "{}", content); + self.0.push_str(&s); self } @@ -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(mut self, content: D, language: Option) -> 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 @@ -275,9 +282,13 @@ impl MessageBuilder { /// /// assert_eq!(content, expected); /// ``` - pub fn push_mono(mut self, content: &str) -> Self { + pub fn push_mono(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 @@ -304,36 +315,52 @@ impl MessageBuilder { /// /// assert_eq!(content, expected); /// ``` - pub fn push_italic(mut self, content: &str) -> Self { + pub fn push_italic(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(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(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(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 @@ -352,7 +379,7 @@ impl MessageBuilder { /// /// assert_eq!(content, "hello\nworld"); /// ``` - pub fn push_line>(mut self, content: T) -> Self { + pub fn push_line(mut self, content: D) -> Self { self = self.push(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_mono(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_italic(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_bold(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_underline(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_strike(content); self.0.push('\n'); @@ -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>(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(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(mut self, content: D, language: Option) -> 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(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(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(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(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(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 @@ -550,7 +610,7 @@ impl MessageBuilder { /// /// assert_eq!(content, "Hello @\u{200B}everyone\nHow are you?"); /// ``` - pub fn push_line_safe>(mut self, content: T) -> Self { + pub fn push_line_safe(mut self, content: D) -> Self { self = self.push_safe(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_mono_safe(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_italic_safe(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_bold_safe(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_underline_safe(content); self.0.push('\n'); @@ -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(mut self, content: D) -> Self { self = self.push_strike_safe(content); self.0.push('\n'); @@ -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 Add for Content { type Output = Content; @@ -884,8 +950,8 @@ impl From for Content { fn from(cm: ContentModifier) -> Content { cm.to_content() } } -impl From for Content { - fn from(stringer: T) -> Content { +impl Content { + pub fn from(stringer: T) -> Content { Content { italic: false, bold: false,