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/8] 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/8] 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/8] 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/8] 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; } From c7ce2d9a001d5473198a64da8ba6a9aad6f43ce4 Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Thu, 26 Dec 2024 09:47:42 +0800 Subject: [PATCH 5/8] remove redundant relative timestamp and rename --- twilight-util/src/{format.rs => fmt.rs} | 8 -------- twilight-util/src/lib.rs | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) rename twilight-util/src/{format.rs => fmt.rs} (92%) diff --git a/twilight-util/src/format.rs b/twilight-util/src/fmt.rs similarity index 92% rename from twilight-util/src/format.rs rename to twilight-util/src/fmt.rs index f45746f3d26..0c87f79c543 100644 --- a/twilight-util/src/format.rs +++ b/twilight-util/src/fmt.rs @@ -44,10 +44,6 @@ pub trait Format { #[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; @@ -102,10 +98,6 @@ impl Format for String { format!("[{text}]({self})") } - fn relative_timestamp(self) -> Self { - format!("") - } - fn underline(self) -> Self { format!("__{self}__") } diff --git a/twilight-util/src/lib.rs b/twilight-util/src/lib.rs index e8077d07d4d..bbe66cf2877 100644 --- a/twilight-util/src/lib.rs +++ b/twilight-util/src/lib.rs @@ -16,7 +16,7 @@ pub mod builder; #[cfg(feature = "format")] -pub mod format; +pub mod fmt; #[cfg(feature = "link")] pub mod link; From e4a46595d68409234ab2f2e669102c50be85d1b1 Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Thu, 26 Dec 2024 10:22:09 +0800 Subject: [PATCH 6/8] update implementation --- twilight-util/src/fmt.rs | 184 +++++++++++++++++++++++++++------------ 1 file changed, 130 insertions(+), 54 deletions(-) diff --git a/twilight-util/src/fmt.rs b/twilight-util/src/fmt.rs index 0c87f79c543..388ff020e5a 100644 --- a/twilight-util/src/fmt.rs +++ b/twilight-util/src/fmt.rs @@ -1,112 +1,188 @@ //! Provides the Format trait for specifying formatting with Discord markdown for strings. +use std::fmt::{Display, Formatter, Result as FmtResult}; + /// Format is a trait specifying formatting with Discord markdown for strings. -pub trait Format { - /// Returns the block quote formatting for a string. +pub trait MarkdownFormat<'a, T> { + /// Returns the block quote formatting. #[must_use] - fn block_quote(self) -> Self; + fn block_quote(self) -> MarkdownDisplay<'a, T>; - /// Returns the bold formatting for a string. + /// Returns the bold formatting. #[must_use] - fn bold(self) -> Self; + fn bold(self) -> MarkdownDisplay<'a, T>; - /// Returns the codeblock formatting for a string. + /// Returns the codeblock formatting. #[must_use] - fn codeblock(self, language: &str) -> Self; + fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a, T>; - /// Returns the H1 formatting for a string. + /// Returns the H1 formatting. #[must_use] - fn h1(self) -> Self; + fn h1(self) -> MarkdownDisplay<'a, T>; - /// Returns the H2 formatting for a string. + /// Returns the H2 formatting. #[must_use] - fn h2(self) -> Self; + fn h2(self) -> MarkdownDisplay<'a, T>; - /// Returns the H3 formatting for a string. + /// Returns the H3 formatting. #[must_use] - fn h3(self) -> Self; + fn h3(self) -> MarkdownDisplay<'a, T>; - /// Returns the inline code formatting for a string. + /// Returns the inline code formatting. #[must_use] - fn inline_code(self) -> Self; + fn inline_code(self) -> MarkdownDisplay<'a, T>; - /// Returns the italic formatting for a string. + /// Returns the italic formatting. #[must_use] - fn italic(self) -> Self; + fn italic(self) -> MarkdownDisplay<'a, T>; - /// Returns the quote formatting for a string. + /// Returns the quote formatting. #[must_use] - fn line_quote(self) -> Self; + fn line_quote(self) -> MarkdownDisplay<'a, T>; - /// Returns the masked links formatting for a string. + /// Returns the masked links formatting. /// /// This assumes `self` being the URL to be masked. #[must_use] - fn masked_links(self, text: &str) -> Self; + fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a, T>; - /// Returns the underline formatting for a string. + /// Returns the underline formatting. #[must_use] - fn underline(self) -> Self; + fn underline(self) -> MarkdownDisplay<'a, T>; - /// Returns the spoiler formatting for a string. + /// Returns the spoiler formatting. #[must_use] - fn spoiler(self) -> Self; + fn spoiler(self) -> MarkdownDisplay<'a, T>; - /// Returns the strikethrough formatting for a string. + /// Returns the strikethrough formatting. #[must_use] - fn strikethrough(self) -> Self; + fn strikethrough(self) -> MarkdownDisplay<'a, T>; } -impl Format for String { - fn block_quote(self) -> Self { - format!(">>> {self}") +impl<'a, T> MarkdownFormat<'a, T> for T { + fn block_quote(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::BlockQuote) + } + + fn bold(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Bold) } - fn bold(self) -> Self { - format!("**{self}**") + fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Codeblock { language }) } - fn codeblock(self, language: &str) -> Self { - format!("```{language}\n{self}```") + fn h1(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::H1) } - fn h1(self) -> Self { - format!("# {self}") + fn h2(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::H2) } - fn h2(self) -> Self { - format!("## {self}") + fn h3(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::H3) } - fn h3(self) -> Self { - format!("### {self}") + fn inline_code(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::InlineCode) } - fn inline_code(self) -> Self { - format!("`{self}`") + fn italic(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Italic) } - fn italic(self) -> Self { - format!("*{self}*") + fn line_quote(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::LineQuote) } - fn line_quote(self) -> Self { - format!("> {self}") + fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::MaskedLink { mask }) } - fn masked_links(self, text: &str) -> Self { - format!("[{text}]({self})") + fn underline(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Underline) } - fn underline(self) -> Self { - format!("__{self}__") + fn spoiler(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Spoiler) } - fn spoiler(self) -> Self { - format!("||{self}||") + fn strikethrough(self) -> MarkdownDisplay<'a, T> { + MarkdownDisplay::new(self, MarkdownFlavour::Strikethrough) } +} + +/// Formatter to display some content with markdown formatting +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct MarkdownDisplay<'a, T> { + content: T, + flavour: MarkdownFlavour<'a>, +} - fn strikethrough(self) -> Self { - format!("~~{self}~~") +impl<'a, T> MarkdownDisplay<'a, T> { + pub(self) fn new(content: T, flavour: MarkdownFlavour<'a>) -> Self { + Self { content, flavour } } } + +impl<'a, T: Display> Display for MarkdownDisplay<'a, T> { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + match self.flavour { + MarkdownFlavour::BlockQuote => f.write_str(">>> ")?, + MarkdownFlavour::Bold => f.write_str("**")?, + MarkdownFlavour::Codeblock { language } => { + f.write_str("```")?; + if let Some(language) = language { + f.write_str(language)?; + } + f.write_str("\n")?; + } + MarkdownFlavour::H1 => f.write_str("# ")?, + MarkdownFlavour::H2 => f.write_str("## ")?, + MarkdownFlavour::H3 => f.write_str("### ")?, + MarkdownFlavour::InlineCode => f.write_str("`")?, + MarkdownFlavour::Italic => f.write_str("*")?, + MarkdownFlavour::LineQuote => f.write_str("> ")?, + MarkdownFlavour::MaskedLink { mask } => { + f.write_str("[")?; + f.write_str(mask)?; + f.write_str("](")?; + } + MarkdownFlavour::Underline => f.write_str("__")?, + MarkdownFlavour::Spoiler => f.write_str("||")?, + MarkdownFlavour::Strikethrough => f.write_str("~~")?, + }; + + Display::fmt(&self.content, f)?; + + match self.flavour { + MarkdownFlavour::Bold => f.write_str("**"), + MarkdownFlavour::Codeblock { language } => f.write_str("```"), + MarkdownFlavour::InlineCode => f.write_str("`"), + MarkdownFlavour::Italic => f.write_str("*"), + MarkdownFlavour::MaskedLink { mask } => f.write_str(")"), + MarkdownFlavour::Underline => f.write_str("__"), + MarkdownFlavour::Spoiler => f.write_str("||"), + MarkdownFlavour::Strikethrough => f.write_str("~~"), + _ => Ok(()), + } + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(self) enum MarkdownFlavour<'a> { + BlockQuote, + Bold, + Codeblock { language: Option<&'a str> }, + H1, + H2, + H3, + InlineCode, + Italic, + LineQuote, + MaskedLink { mask: &'a str }, + Underline, + Spoiler, + Strikethrough, +} From c6f97d44a7706ac4c69c81cf421a1ec9e4a4fb8f Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Thu, 26 Dec 2024 10:47:42 +0800 Subject: [PATCH 7/8] restrict to str only --- twilight-util/src/fmt.rs | 70 ++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/twilight-util/src/fmt.rs b/twilight-util/src/fmt.rs index 388ff020e5a..8c1955f9393 100644 --- a/twilight-util/src/fmt.rs +++ b/twilight-util/src/fmt.rs @@ -3,130 +3,130 @@ use std::fmt::{Display, Formatter, Result as FmtResult}; /// Format is a trait specifying formatting with Discord markdown for strings. -pub trait MarkdownFormat<'a, T> { +pub trait MarkdownFormat<'a> { /// Returns the block quote formatting. #[must_use] - fn block_quote(self) -> MarkdownDisplay<'a, T>; + fn block_quote(self) -> MarkdownDisplay<'a>; /// Returns the bold formatting. #[must_use] - fn bold(self) -> MarkdownDisplay<'a, T>; + fn bold(self) -> MarkdownDisplay<'a>; /// Returns the codeblock formatting. #[must_use] - fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a, T>; + fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a>; /// Returns the H1 formatting. #[must_use] - fn h1(self) -> MarkdownDisplay<'a, T>; + fn h1(self) -> MarkdownDisplay<'a>; /// Returns the H2 formatting. #[must_use] - fn h2(self) -> MarkdownDisplay<'a, T>; + fn h2(self) -> MarkdownDisplay<'a>; /// Returns the H3 formatting. #[must_use] - fn h3(self) -> MarkdownDisplay<'a, T>; + fn h3(self) -> MarkdownDisplay<'a>; /// Returns the inline code formatting. #[must_use] - fn inline_code(self) -> MarkdownDisplay<'a, T>; + fn inline_code(self) -> MarkdownDisplay<'a>; /// Returns the italic formatting. #[must_use] - fn italic(self) -> MarkdownDisplay<'a, T>; + fn italic(self) -> MarkdownDisplay<'a>; /// Returns the quote formatting. #[must_use] - fn line_quote(self) -> MarkdownDisplay<'a, T>; + fn line_quote(self) -> MarkdownDisplay<'a>; /// Returns the masked links formatting. /// /// This assumes `self` being the URL to be masked. #[must_use] - fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a, T>; + fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a>; /// Returns the underline formatting. #[must_use] - fn underline(self) -> MarkdownDisplay<'a, T>; + fn underline(self) -> MarkdownDisplay<'a>; /// Returns the spoiler formatting. #[must_use] - fn spoiler(self) -> MarkdownDisplay<'a, T>; + fn spoiler(self) -> MarkdownDisplay<'a>; /// Returns the strikethrough formatting. #[must_use] - fn strikethrough(self) -> MarkdownDisplay<'a, T>; + fn strikethrough(self) -> MarkdownDisplay<'a>; } -impl<'a, T> MarkdownFormat<'a, T> for T { - fn block_quote(self) -> MarkdownDisplay<'a, T> { +impl<'a> MarkdownFormat<'a> for &'a str { + fn block_quote(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::BlockQuote) } - fn bold(self) -> MarkdownDisplay<'a, T> { + fn bold(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Bold) } - fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a, T> { + fn codeblock(self, language: Option<&'a str>) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Codeblock { language }) } - fn h1(self) -> MarkdownDisplay<'a, T> { + fn h1(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::H1) } - fn h2(self) -> MarkdownDisplay<'a, T> { + fn h2(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::H2) } - fn h3(self) -> MarkdownDisplay<'a, T> { + fn h3(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::H3) } - fn inline_code(self) -> MarkdownDisplay<'a, T> { + fn inline_code(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::InlineCode) } - fn italic(self) -> MarkdownDisplay<'a, T> { + fn italic(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Italic) } - fn line_quote(self) -> MarkdownDisplay<'a, T> { + fn line_quote(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::LineQuote) } - fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a, T> { + fn masked_link(self, mask: &'a str) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::MaskedLink { mask }) } - fn underline(self) -> MarkdownDisplay<'a, T> { + fn underline(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Underline) } - fn spoiler(self) -> MarkdownDisplay<'a, T> { + fn spoiler(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Spoiler) } - fn strikethrough(self) -> MarkdownDisplay<'a, T> { + fn strikethrough(self) -> MarkdownDisplay<'a> { MarkdownDisplay::new(self, MarkdownFlavour::Strikethrough) } } /// Formatter to display some content with markdown formatting #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub struct MarkdownDisplay<'a, T> { - content: T, +pub struct MarkdownDisplay<'a> { + content: &'a str, flavour: MarkdownFlavour<'a>, } -impl<'a, T> MarkdownDisplay<'a, T> { - pub(self) fn new(content: T, flavour: MarkdownFlavour<'a>) -> Self { +impl<'a> MarkdownDisplay<'a> { + pub(self) fn new(content: &'a str, flavour: MarkdownFlavour<'a>) -> Self { Self { content, flavour } } } -impl<'a, T: Display> Display for MarkdownDisplay<'a, T> { +impl<'a> Display for MarkdownDisplay<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self.flavour { MarkdownFlavour::BlockQuote => f.write_str(">>> ")?, @@ -158,10 +158,10 @@ impl<'a, T: Display> Display for MarkdownDisplay<'a, T> { match self.flavour { MarkdownFlavour::Bold => f.write_str("**"), - MarkdownFlavour::Codeblock { language } => f.write_str("```"), + MarkdownFlavour::Codeblock { .. } => f.write_str("```"), MarkdownFlavour::InlineCode => f.write_str("`"), MarkdownFlavour::Italic => f.write_str("*"), - MarkdownFlavour::MaskedLink { mask } => f.write_str(")"), + MarkdownFlavour::MaskedLink { .. } => f.write_str(")"), MarkdownFlavour::Underline => f.write_str("__"), MarkdownFlavour::Spoiler => f.write_str("||"), MarkdownFlavour::Strikethrough => f.write_str("~~"), From 957a13599be834288c15c640109f3a56d5cd1d0f Mon Sep 17 00:00:00 2001 From: HTGAzureX1212 <39023054+HTGAzureX1212@users.noreply.github.com> Date: Thu, 26 Dec 2024 10:52:54 +0800 Subject: [PATCH 8/8] appease clippy --- twilight-util/src/fmt.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twilight-util/src/fmt.rs b/twilight-util/src/fmt.rs index 8c1955f9393..b1c85973b98 100644 --- a/twilight-util/src/fmt.rs +++ b/twilight-util/src/fmt.rs @@ -121,12 +121,12 @@ pub struct MarkdownDisplay<'a> { } impl<'a> MarkdownDisplay<'a> { - pub(self) fn new(content: &'a str, flavour: MarkdownFlavour<'a>) -> Self { + const fn new(content: &'a str, flavour: MarkdownFlavour<'a>) -> Self { Self { content, flavour } } } -impl<'a> Display for MarkdownDisplay<'a> { +impl Display for MarkdownDisplay<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self.flavour { MarkdownFlavour::BlockQuote => f.write_str(">>> ")?, @@ -171,7 +171,7 @@ impl<'a> Display for MarkdownDisplay<'a> { } #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(self) enum MarkdownFlavour<'a> { +enum MarkdownFlavour<'a> { BlockQuote, Bold, Codeblock { language: Option<&'a str> },