pub fn bold(text: &str) -> String {
format!("**{}**", text)
}
pub fn italic(text: &str) -> String {
format!("*{}*", text)
}
pub fn underline(text: &str) -> String {
format!("__{}__", text)
}
pub fn strike(text: &str) -> String {
format!("~~{}~~", text)
}
pub fn spoiler(text: &str) -> String {
format!("||{}||", text)
}
pub fn code(text: &str) -> String {
format!("`{}`", text)
}
pub fn codeblock(text: &str, language: Option<&str>) -> String {
format!("```{}\n{}\n```", language.unwrap_or(""), text)
}
pub fn quote(text: &str) -> String {
text.lines().map(|l| format!("> {}", l)).collect::<Vec<_>>().join("\n")
}
pub fn quote_block(text: &str) -> String {
format!(">>> {}", text)
}
pub fn user_mention(user_id: u64) -> String {
format!("<@{}>", user_id)
}
pub fn role_mention(role_id: u64) -> String {
format!("<@&{}>", role_id)
}
pub fn channel_mention(channel_id: u64) -> String {
format!("<#{}>", channel_id)
}
pub fn everyone() -> &'static str {
"@everyone"
}
pub fn here() -> &'static str {
"@here"
}
pub fn custom_emoji(name: &str, id: u64, animated: bool) -> String {
if animated {
format!("<a:{}:{}>", name, id)
} else {
format!("<:{}:{}>", name, id)
}
}
pub fn escape(text: &str) -> String {
let mut out = String::with_capacity(text.len() + 8);
for ch in text.chars() {
match ch {
'\\' | '*' | '_' | '~' | '|' | '`' | '>' | '[' | ']' => {
out.push('\\');
out.push(ch);
}
_ => out.push(ch),
}
}
out
}
pub fn bold_italic(text: &str) -> String {
format!("***{}***", text)
}
pub fn underline_bold(text: &str) -> String {
format!("__**{}**__", text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bold() {
assert_eq!(bold("hi"), "**hi**");
}
#[test]
fn test_italic() {
assert_eq!(italic("hi"), "*hi*");
}
#[test]
fn test_code() {
assert_eq!(code("x"), "`x`");
}
#[test]
fn test_spoiler() {
assert_eq!(spoiler("hi"), "||hi||");
}
#[test]
fn test_codeblock_no_lang() {
assert_eq!(codeblock("x", None), "```\nx\n```");
}
#[test]
fn test_codeblock_lang() {
assert_eq!(codeblock("x", Some("rust")), "```rust\nx\n```");
}
#[test]
fn test_quote() {
assert_eq!(quote("a\nb"), "> a\n> b");
}
#[test]
fn test_escape() {
assert_eq!(escape("**bold**"), "\\*\\*bold\\*\\*");
}
#[test]
fn test_user_mention() {
assert_eq!(user_mention(123), "<@123>");
}
#[test]
fn test_channel_mention() {
assert_eq!(channel_mention(456), "<#456>");
}
#[test]
fn test_custom_emoji() {
assert_eq!(custom_emoji("wave", 789, false), "<:wave:789>");
}
#[test]
fn test_custom_emoji_animated() {
assert_eq!(custom_emoji("dance", 789, true), "<a:dance:789>");
}
}