Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): add markdown format functions to util #2314

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion twilight-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ time = { default-features = false, features = ["formatting"], version = "0.3" }

[features]
builder = ["dep:twilight-model", "dep:twilight-validate"]
format = []
link = ["dep:twilight-model"]
permission-calculator = ["dep:twilight-model"]
snowflake = ["dep:twilight-model"]
full = ["builder", "link", "permission-calculator", "snowflake"]
full = ["builder", "format", "link", "permission-calculator", "snowflake"]

[package.metadata.docs.rs]
all-features = true
Expand Down
120 changes: 120 additions & 0 deletions twilight-util/src/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! Provides the Format trait for specifying formatting with Discord markdown for strings.

/// Format is a trait specifying formatting with Discord markdown for strings.
pub trait Format {
/// Returns the block quote formatting for a string.
#[must_use]
fn block_quote(self) -> Self;

/// Returns the bold formatting for a string.
#[must_use]
fn bold(self) -> Self;

/// Returns the codeblock formatting for a string.
#[must_use]
fn codeblock(self, language: &str) -> Self;

/// Returns the H1 formatting for a string.
#[must_use]
fn h1(self) -> Self;

/// Returns the H2 formatting for a string.
#[must_use]
fn h2(self) -> Self;

/// Returns the H3 formatting for a string.
#[must_use]
fn h3(self) -> Self;

/// Returns the inline code formatting for a string.
#[must_use]
fn inline_code(self) -> Self;

/// Returns the italic formatting for a string.
#[must_use]
fn italic(self) -> Self;

/// Returns the quote formatting for a string.
#[must_use]
fn line_quote(self) -> Self;

/// Returns the masked links formatting for a string.
///
/// This assumes `self` being the URL to be masked.
#[must_use]
fn masked_links(self, text: &str) -> Self;

/// Returns the relative timestamp formatting for a string.
#[must_use]
fn relative_timestamp(self) -> Self;

/// Returns the underline formatting for a string.
#[must_use]
fn underline(self) -> Self;

/// Returns the spoiler formatting for a string.
#[must_use]
fn spoiler(self) -> Self;

/// Returns the strikethrough formatting for a string.
#[must_use]
fn strikethrough(self) -> Self;
}

impl Format for String {
HTGAzureX1212 marked this conversation as resolved.
Show resolved Hide resolved
fn block_quote(self) -> Self {
format!(">>> {self}")
}

fn bold(self) -> Self {
format!("**{self}**")
}

fn codeblock(self, language: &str) -> Self {
format!("```{language}\n{self}```")
}

fn h1(self) -> Self {
format!("# {self}")
}

fn h2(self) -> Self {
format!("## {self}")
}

fn h3(self) -> Self {
format!("### {self}")
}

fn inline_code(self) -> Self {
format!("`{self}`")
}

fn italic(self) -> Self {
format!("*{self}*")
}

fn line_quote(self) -> Self {
format!("> {self}")
}

fn masked_links(self, text: &str) -> Self {
format!("[{text}]({self})")
}

fn relative_timestamp(self) -> Self {
format!("<t:{self}:R>")
}
HTGAzureX1212 marked this conversation as resolved.
Show resolved Hide resolved

fn underline(self) -> Self {
format!("__{self}__")
}

fn spoiler(self) -> Self {
format!("||{self}||")
}

fn strikethrough(self) -> Self {
format!("~~{self}~~")
}
}
3 changes: 3 additions & 0 deletions twilight-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#[cfg(feature = "builder")]
pub mod builder;

#[cfg(feature = "format")]
pub mod format;
HTGAzureX1212 marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "link")]
pub mod link;

Expand Down
Loading