From ce929f2d0045560820f06d9c1b0768f2b401ff20 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 16 Apr 2023 02:07:06 +0000 Subject: refactor(quick): optimise arguments --- crates/germ/src/quick.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/germ/src/quick.rs b/crates/germ/src/quick.rs index 3ecf5a2..6bc5913 100644 --- a/crates/germ/src/quick.rs +++ b/crates/germ/src/quick.rs @@ -23,7 +23,10 @@ pub enum HeadingLevel { } #[must_use] -pub fn heading(text: &str, level: &HeadingLevel) -> String { +pub fn heading( + text: &(impl ToString + ?Sized), + level: &HeadingLevel, +) -> String { format!( "{} {}", match level { @@ -31,35 +34,42 @@ pub fn heading(text: &str, level: &HeadingLevel) -> String { HeadingLevel::Two => "##", HeadingLevel::Three => "###", }, - text + text.to_string() ) } #[must_use] -pub fn list_item(text: &str) -> String { format!("* {text}") } +pub fn list_item(text: &(impl ToString + ?Sized)) -> String { + format!("* {}", text.to_string()) +} #[must_use] -pub fn list_items(items: &[&str]) -> String { +pub fn list_items(items: &[&(impl ToString + ?Sized)]) -> String { items .iter() - .map(|item| list_item(item)) + .map(|item| list_item(&item.to_string())) .collect::>() .join("\n") } #[must_use] -pub fn link(text: &str, location: Option<&str>) -> String { +pub fn link(text: &(impl ToString + ?Sized), location: Option<&str>) -> String { format!( "=> {}{}", - text, + text.to_string(), location.map_or_else(String::new, |l| format!(" {l}")) ) } #[must_use] -pub fn block_quote(text: &str) -> String { format!("> {text}") } +pub fn block_quote(text: &(impl ToString + ?Sized)) -> String { + format!("> {}", text.to_string()) +} #[must_use] -pub fn preformatted_text(text: &str, alt_text: Option<&str>) -> String { - format!("```{}\n{}\n```", alt_text.unwrap_or(""), text) +pub fn preformatted_text( + text: &(impl ToString + ?Sized), + alt_text: Option<&str>, +) -> String { + format!("```{}\n{}\n```", alt_text.unwrap_or(""), text.to_string()) } -- cgit v1.2.3