Skip to content

Commit

Permalink
try avoid conflict
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 15, 2024
1 parent bfe8ff6 commit 7e1f505
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/append/rolling_file/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Encoder>) -> Self {
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
self
}
Expand Down
5 changes: 3 additions & 2 deletions src/append/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::io::Write;

use crate::append::Append;
use crate::encoder::IntoEncoder;
use crate::layout::TextLayout;
use crate::Encoder;

Expand All @@ -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<Encoder>) -> Self {
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
self
}
Expand Down Expand Up @@ -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<Encoder>) -> Self {
pub fn with_encoder(mut self, encoder: impl IntoEncoder) -> Self {
self.encoder = encoder.into();
self
}
Expand Down
16 changes: 13 additions & 3 deletions src/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ impl Encoder {
}
}

impl<L: Into<Layout>> From<L> for Encoder {
fn from(layout: L) -> Self {
let layout = layout.into();
pub trait IntoEncoder {
fn into(self) -> Encoder;
}

impl<L: Into<Encoder>> IntoEncoder for L {
fn into(self) -> Encoder {
self.into()
}
}

impl<L: Into<Layout>> IntoEncoder for L {
fn into(self) -> Encoder {
let layout = self.into();
Encoder::Custom(CustomEncoder::new(move |record| {
Ok(layout.format(record)?.into_bytes())
}))
Expand Down
22 changes: 21 additions & 1 deletion src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,6 @@ pub use kv::KvDisplay;
pub use text::LevelColor;
pub use text::TextLayout;


mod custom;
#[cfg(feature = "json")]
mod json;
Expand All @@ -48,3 +48,23 @@ impl Layout {
}
}
}

pub trait IntoLayout {
fn into(self) -> Layout;
}

impl<L: Into<Layout>> IntoLayout for L {
fn into(self) -> Layout {
self.into()
}
}

impl<L: Into<Encoder>> 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())
}))
}
}

0 comments on commit 7e1f505

Please sign in to comment.