From c70f93a76778383b6a772b19650e2c1648fb0886 Mon Sep 17 00:00:00 2001 From: "HTGAzureX1212." <39023054+HTGAzureX1212@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:49:29 +0800 Subject: [PATCH 1/4] feat(util): add format to util --- twilight-util/Cargo.toml | 3 ++- twilight-util/src/format.rs | 39 +++++++++++++++++++++++++++++++++++++ twilight-util/src/lib.rs | 3 +++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 twilight-util/src/format.rs 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..fc15677f26d --- /dev/null +++ b/twilight-util/src/format.rs @@ -0,0 +1,39 @@ +pub trait Format { + fn bold(self) -> Self; + + fn inline_code(self) -> Self; + + fn italic(self) -> Self; + + fn relative_timestamp(self) -> Self; + + fn underline(self) -> Self; + + fn strikethrough(self) -> Self; +} + +impl Format for &str { + fn bold(self) -> Self { + concat!("**", self, "**") + } + + fn inline_code(self) -> Self { + concat!("`", self, "`") + } + + fn italic(self) -> Self { + concat!("*", self, "*") + } + + fn relative_timestamp(self) -> Self { + concat!("") + } + + fn underline(self) -> Self { + concat!("__", self, "__") + } + + fn strikethrough(self) -> Self { + concat!("~~", 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; From 9ec62be35c24080c62e455440dd58821375b6955 Mon Sep 17 00:00:00 2001 From: "HTGAzureX1212." <39023054+HTGAzureX1212@users.noreply.github.com> Date: Mon, 6 May 2024 08:35:48 +0800 Subject: [PATCH 2/4] add codeblock and spoiler format --- twilight-util/src/format.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/twilight-util/src/format.rs b/twilight-util/src/format.rs index fc15677f26d..58cb1e9fddd 100644 --- a/twilight-util/src/format.rs +++ b/twilight-util/src/format.rs @@ -1,39 +1,62 @@ +//! 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 bold formatting for a string. fn bold(self) -> Self; + /// Returns the codeblock formatting for a string. + fn codeblock(self, language: &str) -> Self; + + /// Returns the inline code formatting for a string. fn inline_code(self) -> Self; + /// Returns the italic formatting for a string. fn italic(self) -> Self; + /// Returns the relative timestamp formatting for a string. fn relative_timestamp(self) -> Self; + /// Returns the underline formatting for a string. fn underline(self) -> Self; + /// Returns the spoiler formatting for a string. + fn spoiler(self) -> Self; + + /// Returns the strikethrough formatting for a string. fn strikethrough(self) -> Self; } -impl Format for &str { +impl Format for String { fn bold(self) -> Self { - concat!("**", self, "**") + format!("**{self}**") + } + + fn codeblock(self, language: &str) -> Self { + format!("```{language}\n{self}```") } fn inline_code(self) -> Self { - concat!("`", self, "`") + format!("`{self}`") } fn italic(self) -> Self { - concat!("*", self, "*") + format!("*{self}*") } fn relative_timestamp(self) -> Self { - concat!("") + format!("") } fn underline(self) -> Self { - concat!("__", self, "__") + format!("__{self}__") + } + + fn spoiler(self) -> Self { + format!("||{self}||") } fn strikethrough(self) -> Self { - concat!("~~", self, "~~") + format!("~~{self}~~") } } From f7622bf03d9e673e6d5d87416eee14c8807e4637 Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Fri, 31 May 2024 13:30:56 +0800 Subject: [PATCH 3/4] add more formatting functions --- twilight-util/src/format.rs | 46 ++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/twilight-util/src/format.rs b/twilight-util/src/format.rs index 58cb1e9fddd..925ace52daf 100644 --- a/twilight-util/src/format.rs +++ b/twilight-util/src/format.rs @@ -1,19 +1,39 @@ //! Provides the Format trait for specifying formatting with Discord markdown for strings. -/// Format is a trait 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. + fn block_quote(self) -> Self; + /// Returns the bold formatting for a string. fn bold(self) -> Self; /// Returns the codeblock formatting for a string. fn codeblock(self, language: &str) -> Self; + /// Returns the H1 formatting for a string. + fn h1(self) -> Self; + + /// Returns the H2 formatting for a string. + fn h2(self) -> Self; + + /// Returns the H3 formatting for a string. + fn h3(self) -> Self; + /// Returns the inline code formatting for a string. fn inline_code(self) -> Self; /// Returns the italic formatting for a string. fn italic(self) -> Self; + /// Returns the quote formatting for a string. + fn line_quote(self) -> Self; + + /// Returns the masked links formatting for a string. + /// + /// This assumes `self` being the URL to be masked. + fn masked_links(self, text: &str) -> Self; + /// Returns the relative timestamp formatting for a string. fn relative_timestamp(self) -> Self; @@ -28,6 +48,10 @@ pub trait Format { } impl Format for String { + fn block_quote(self) -> Self { + format!(">>> {self}") + } + fn bold(self) -> Self { format!("**{self}**") } @@ -36,6 +60,18 @@ impl Format for String { 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}`") } @@ -44,6 +80,14 @@ impl Format for String { 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!("") } From 3a0fe8188aebab34c31590cc9feefb9426fd3559 Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Fri, 31 May 2024 13:32:22 +0800 Subject: [PATCH 4/4] add must_use --- twilight-util/src/format.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/twilight-util/src/format.rs b/twilight-util/src/format.rs index 925ace52daf..f45746f3d26 100644 --- a/twilight-util/src/format.rs +++ b/twilight-util/src/format.rs @@ -3,47 +3,61 @@ /// 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; }