From f98bfbbdcd43e4f9f5c2448e93fd3584756e9014 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 3 Aug 2024 02:16:52 +0000 Subject: [PATCH] add string stream convenience util; add ?Sized bounds on log fmt functors Signed-off-by: Jason Volk --- src/core/log/fmt.rs | 8 ++++---- src/core/utils/string.rs | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/core/log/fmt.rs b/src/core/log/fmt.rs index 62944977..8766eb8d 100644 --- a/src/core/log/fmt.rs +++ b/src/core/log/fmt.rs @@ -5,7 +5,7 @@ use crate::Result; pub fn html(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()> where - S: Write, + S: Write + ?Sized, { let color = color::code_tag(level); let level = level.as_str().to_uppercase(); @@ -19,7 +19,7 @@ where pub fn markdown(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()> where - S: Write, + S: Write + ?Sized, { let level = level.as_str().to_uppercase(); writeln!(out, "`{level:>5}` `{span:^12}` `{msg}`")?; @@ -29,7 +29,7 @@ where pub fn markdown_table(out: &mut S, level: &Level, span: &str, msg: &str) -> Result<()> where - S: Write, + S: Write + ?Sized, { let level = level.as_str().to_uppercase(); writeln!(out, "| {level:>5} | {span:^12} | {msg} |")?; @@ -39,7 +39,7 @@ where pub fn markdown_table_head(out: &mut S) -> Result<()> where - S: Write, + S: Write + ?Sized, { write!(out, "| level | span | message |\n| ------: | :-----: | :------- |\n")?; diff --git a/src/core/utils/string.rs b/src/core/utils/string.rs index a597d198..85282b30 100644 --- a/src/core/utils/string.rs +++ b/src/core/utils/string.rs @@ -30,6 +30,16 @@ macro_rules! is_format { }; } +#[inline] +pub fn collect_stream(func: F) -> Result +where + F: FnOnce(&mut dyn std::fmt::Write) -> Result<()>, +{ + let mut out = String::new(); + func(&mut out)?; + Ok(out) +} + #[inline] #[must_use] pub fn camel_to_snake_string(s: &str) -> String {