From b5bf9395c8cc3c73f34e89866104c4ee291fb3da Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 May 2025 12:21:41 -0500 Subject: [PATCH 1/2] refactor(writer): Simplify pub visibility --- src/fmt/writer/buffer.rs | 26 +++++++++++++------------- src/fmt/writer/mod.rs | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fmt/writer/buffer.rs b/src/fmt/writer/buffer.rs index c73b958..1ce21fb 100644 --- a/src/fmt/writer/buffer.rs +++ b/src/fmt/writer/buffer.rs @@ -3,13 +3,13 @@ use std::{io, sync::Mutex}; use crate::fmt::writer::WriteStyle; #[derive(Debug)] -pub(in crate::fmt::writer) struct BufferWriter { +pub(crate) struct BufferWriter { target: WritableTarget, write_style: WriteStyle, } impl BufferWriter { - pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { + pub(crate) fn stderr(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { target: if is_test { WritableTarget::PrintStderr @@ -20,7 +20,7 @@ impl BufferWriter { } } - pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { + pub(crate) fn stdout(is_test: bool, write_style: WriteStyle) -> Self { BufferWriter { target: if is_test { WritableTarget::PrintStdout @@ -31,7 +31,7 @@ impl BufferWriter { } } - pub(in crate::fmt::writer) fn pipe( + pub(crate) fn pipe( pipe: Box>, write_style: WriteStyle, ) -> Self { @@ -41,15 +41,15 @@ impl BufferWriter { } } - pub(in crate::fmt::writer) fn write_style(&self) -> WriteStyle { + pub(crate) fn write_style(&self) -> WriteStyle { self.write_style } - pub(in crate::fmt::writer) fn buffer(&self) -> Buffer { + pub(crate) fn buffer(&self) -> Buffer { Buffer(Vec::new()) } - pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> { + pub(crate) fn print(&self, buf: &Buffer) -> io::Result<()> { #![allow(clippy::print_stdout)] // enabled for tests only #![allow(clippy::print_stderr)] // enabled for tests only @@ -115,23 +115,23 @@ fn adapt(buf: &[u8], write_style: WriteStyle) -> io::Result> { Ok(adapted) } -pub(in crate::fmt) struct Buffer(Vec); +pub(crate) struct Buffer(Vec); impl Buffer { - pub(in crate::fmt) fn clear(&mut self) { + pub(crate) fn clear(&mut self) { self.0.clear(); } - pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result { + pub(crate) fn write(&mut self, buf: &[u8]) -> io::Result { self.0.extend(buf); Ok(buf.len()) } - pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> { + pub(crate) fn flush(&mut self) -> io::Result<()> { Ok(()) } - pub(in crate::fmt) fn as_bytes(&self) -> &[u8] { + pub(crate) fn as_bytes(&self) -> &[u8] { &self.0 } } @@ -145,7 +145,7 @@ impl std::fmt::Debug for Buffer { /// Log target, either `stdout`, `stderr` or a custom pipe. /// /// Same as `Target`, except the pipe is wrapped in a mutex for interior mutability. -pub(super) enum WritableTarget { +pub(crate) enum WritableTarget { /// Logs will be written to standard output. WriteStdout, /// Logs will be printed to standard output. diff --git a/src/fmt/writer/mod.rs b/src/fmt/writer/mod.rs index bb68cb5..33efd6a 100644 --- a/src/fmt/writer/mod.rs +++ b/src/fmt/writer/mod.rs @@ -4,7 +4,7 @@ mod target; use self::buffer::BufferWriter; use std::{io, mem, sync::Mutex}; -pub(super) use self::buffer::Buffer; +pub(crate) use self::buffer::Buffer; pub use target::Target; @@ -55,11 +55,11 @@ impl Writer { self.inner.write_style() } - pub(super) fn buffer(&self) -> Buffer { + pub(crate) fn buffer(&self) -> Buffer { self.inner.buffer() } - pub(super) fn print(&self, buf: &Buffer) -> io::Result<()> { + pub(crate) fn print(&self, buf: &Buffer) -> io::Result<()> { self.inner.print(buf) } } From d0061e36c06b56d0faf7d3329335a9a2f3b77bb3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 6 May 2025 12:28:37 -0500 Subject: [PATCH 2/2] refactor: Pull out writer as a top-level concept In thinking over #365 and looking over `Logger`, this is an orthogonal, top-level concept Maybe at a later point, we can consider if we should revisit how this is exposed in the API. --- src/fmt/mod.rs | 9 ++++----- src/lib.rs | 1 + src/logger.rs | 2 +- src/{fmt => }/writer/buffer.rs | 2 +- src/{fmt => }/writer/mod.rs | 5 +++-- src/{fmt => }/writer/target.rs | 0 6 files changed, 10 insertions(+), 9 deletions(-) rename src/{fmt => }/writer/buffer.rs (99%) rename src/{fmt => }/writer/mod.rs (98%) rename src/{fmt => }/writer/target.rs (100%) diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs index bd2a2b8..398dab7 100644 --- a/src/fmt/mod.rs +++ b/src/fmt/mod.rs @@ -71,7 +71,6 @@ use log::Record; mod humantime; #[cfg(feature = "kv")] mod kv; -pub(crate) mod writer; #[cfg(feature = "color")] pub use anstyle as style; @@ -80,10 +79,10 @@ pub use anstyle as style; pub use self::humantime::Timestamp; #[cfg(feature = "kv")] pub use self::kv::*; -pub use self::writer::Target; -pub use self::writer::WriteStyle; +pub use crate::writer::Target; +pub use crate::writer::WriteStyle; -use self::writer::{Buffer, Writer}; +use crate::writer::{Buffer, Writer}; /// Formatting precision of timestamps. /// @@ -650,7 +649,7 @@ mod tests { } fn formatter() -> Formatter { - let writer = writer::Builder::new() + let writer = crate::writer::Builder::new() .write_style(WriteStyle::Never) .build(); diff --git a/src/lib.rs b/src/lib.rs index e0afd1c..786d658 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -265,6 +265,7 @@ #![warn(clippy::print_stdout)] mod logger; +mod writer; pub mod fmt; diff --git a/src/logger.rs b/src/logger.rs index bd4d13b..1ecfcc8 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -3,8 +3,8 @@ use std::{borrow::Cow, cell::RefCell, env, io}; use log::{LevelFilter, Log, Metadata, Record, SetLoggerError}; use crate::fmt; -use crate::fmt::writer::{self, Writer}; use crate::fmt::{FormatFn, Formatter}; +use crate::writer::{self, Writer}; /// The default name for the environment variable to read filters from. pub const DEFAULT_FILTER_ENV: &str = "RUST_LOG"; diff --git a/src/fmt/writer/buffer.rs b/src/writer/buffer.rs similarity index 99% rename from src/fmt/writer/buffer.rs rename to src/writer/buffer.rs index 1ce21fb..f2661ee 100644 --- a/src/fmt/writer/buffer.rs +++ b/src/writer/buffer.rs @@ -1,6 +1,6 @@ use std::{io, sync::Mutex}; -use crate::fmt::writer::WriteStyle; +use crate::writer::WriteStyle; #[derive(Debug)] pub(crate) struct BufferWriter { diff --git a/src/fmt/writer/mod.rs b/src/writer/mod.rs similarity index 98% rename from src/fmt/writer/mod.rs rename to src/writer/mod.rs index 33efd6a..61e9255 100644 --- a/src/fmt/writer/mod.rs +++ b/src/writer/mod.rs @@ -1,10 +1,11 @@ mod buffer; mod target; -use self::buffer::BufferWriter; use std::{io, mem, sync::Mutex}; -pub(crate) use self::buffer::Buffer; +use buffer::BufferWriter; + +pub(crate) use buffer::Buffer; pub use target::Target; diff --git a/src/fmt/writer/target.rs b/src/writer/target.rs similarity index 100% rename from src/fmt/writer/target.rs rename to src/writer/target.rs