diff --git a/twilight-util/Cargo.toml b/twilight-util/Cargo.toml index 5386bf7b767..54f3a18bed5 100644 --- a/twilight-util/Cargo.toml +++ b/twilight-util/Cargo.toml @@ -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 diff --git a/twilight-util/src/format.rs b/twilight-util/src/format.rs new file mode 100644 index 00000000000..f45746f3d26 --- /dev/null +++ b/twilight-util/src/format.rs @@ -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 { + 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!("") + } + + fn underline(self) -> Self { + format!("__{self}__") + } + + fn spoiler(self) -> Self { + format!("||{self}||") + } + + fn strikethrough(self) -> Self { + format!("~~{self}~~") + } +} diff --git a/twilight-util/src/lib.rs b/twilight-util/src/lib.rs index 073acfc62f7..e8077d07d4d 100644 --- a/twilight-util/src/lib.rs +++ b/twilight-util/src/lib.rs @@ -15,6 +15,9 @@ #[cfg(feature = "builder")] pub mod builder; +#[cfg(feature = "format")] +pub mod format; + #[cfg(feature = "link")] pub mod link;