From 7e1f50578c1fa952689527524d651155b30cd2f1 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 15 Oct 2024 12:26:54 +0800 Subject: [PATCH] try avoid conflict Signed-off-by: tison --- src/append/rolling_file/append.rs | 3 ++- src/append/stdio.rs | 5 +++-- src/encoder/mod.rs | 16 +++++++++++++--- src/layout/mod.rs | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/append/rolling_file/append.rs b/src/append/rolling_file/append.rs index 90f7afc..f160e1e 100644 --- a/src/append/rolling_file/append.rs +++ b/src/append/rolling_file/append.rs @@ -16,6 +16,7 @@ use log::Record; use crate::append::rolling_file::non_blocking::NonBlocking; use crate::append::Append; +use crate::encoder::IntoEncoder; use crate::layout::TextLayout; use crate::Encoder; @@ -39,7 +40,7 @@ impl RollingFile { } /// Sets the encoder used to format log records as bytes. - pub fn with_encoder(mut self, encoder: impl Into) -> Self { + pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self { self.encoder = encoder.into(); self } diff --git a/src/append/stdio.rs b/src/append/stdio.rs index 3c80f24..bf61a25 100644 --- a/src/append/stdio.rs +++ b/src/append/stdio.rs @@ -15,6 +15,7 @@ use std::io::Write; use crate::append::Append; +use crate::encoder::IntoEncoder; use crate::layout::TextLayout; use crate::Encoder; @@ -34,7 +35,7 @@ impl Default for Stdout { impl Stdout { /// Creates a new `Stdout` appender with the given encoder. - pub fn with_encoder(mut self, encoder: impl Into) -> Self { + pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self { self.encoder = encoder.into(); self } @@ -69,7 +70,7 @@ impl Default for Stderr { impl Stderr { /// Creates a new `Stderr` appender with the given encoder. - pub fn with_encoder(mut self, encoder: impl Into) -> Self { + pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self { self.encoder = encoder.into(); self } diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index 261b545..a6229a8 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -39,9 +39,19 @@ impl Encoder { } } -impl> From for Encoder { - fn from(layout: L) -> Self { - let layout = layout.into(); +pub trait IntoEncoder { + fn into(self) -> Encoder; +} + +impl> IntoEncoder for L { + fn into(self) -> Encoder { + self.into() + } +} + +impl> IntoEncoder for L { + fn into(self) -> Encoder { + let layout = self.into(); Encoder::Custom(CustomEncoder::new(move |record| { Ok(layout.format(record)?.into_bytes()) })) diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 66d1d64..b86808c 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -14,6 +14,7 @@ //! Describe how to format a log record. +use crate::Encoder; pub use custom::CustomLayout; #[cfg(feature = "json")] pub use json::JsonLayout; @@ -22,7 +23,6 @@ pub use kv::KvDisplay; pub use text::LevelColor; pub use text::TextLayout; - mod custom; #[cfg(feature = "json")] mod json; @@ -48,3 +48,23 @@ impl Layout { } } } + +pub trait IntoLayout { + fn into(self) -> Layout; +} + +impl> IntoLayout for L { + fn into(self) -> Layout { + self.into() + } +} + +impl> IntoLayout for L { + fn into(self) -> Layout { + let encoder = self.into(); + Layout::Custom(CustomLayout::new(move |record| { + let bytes = encoder.format(record)?; + Ok(String::from_utf8_lossy(&bytes).to_string()) + })) + } +}