Skip to content

Commit d418367

Browse files
committed
refactor(fmt): Reduce duplication in DefaultFormatWriter
1 parent 6169a5a commit d418367

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed

src/fmt/mod.rs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,9 @@ impl Builder {
231231
} else {
232232
Box::new(move |buf, record| {
233233
let fmt = DefaultFormatWriter {
234-
timestamp: built.default_format.timestamp,
235-
module_path: built.default_format.module_path,
236-
target: built.default_format.target,
237-
level: built.default_format.level,
238-
written_header_value: false,
239-
indent: built.default_format.indent,
240-
suffix: built.default_format.suffix,
241-
source_file: built.default_format.source_file,
242-
source_line_number: built.default_format.source_line_number,
243-
#[cfg(feature = "kv")]
244-
kv_format: built
245-
.default_format
246-
.kv_format
247-
.as_deref()
248-
.unwrap_or(&default_kv_format),
234+
format: &built.default_format,
249235
buf,
236+
written_header_value: false,
250237
};
251238

252239
fmt.write(record)
@@ -321,18 +308,9 @@ impl Default for DefaultFormat {
321308
///
322309
/// This format needs to work with any combination of crate features.
323310
struct DefaultFormatWriter<'a> {
324-
timestamp: Option<TimestampPrecision>,
325-
module_path: bool,
326-
target: bool,
327-
level: bool,
328-
source_file: bool,
329-
source_line_number: bool,
330-
written_header_value: bool,
331-
indent: Option<usize>,
311+
format: &'a DefaultFormat,
332312
buf: &'a mut Formatter,
333-
suffix: &'a str,
334-
#[cfg(feature = "kv")]
335-
kv_format: &'a KvFormatFn,
313+
written_header_value: bool,
336314
}
337315

338316
impl DefaultFormatWriter<'_> {
@@ -347,7 +325,7 @@ impl DefaultFormatWriter<'_> {
347325
self.write_args(record)?;
348326
#[cfg(feature = "kv")]
349327
self.write_kv(record)?;
350-
write!(self.buf, "{}", self.suffix)
328+
write!(self.buf, "{}", self.format.suffix)
351329
}
352330

353331
fn subtle_style(&self, text: &'static str) -> SubtleStyle {
@@ -383,7 +361,7 @@ impl DefaultFormatWriter<'_> {
383361
}
384362

385363
fn write_level(&mut self, record: &Record<'_>) -> io::Result<()> {
386-
if !self.level {
364+
if !self.format.level {
387365
return Ok(());
388366
}
389367

@@ -409,7 +387,7 @@ impl DefaultFormatWriter<'_> {
409387
#[cfg(feature = "humantime")]
410388
{
411389
use self::TimestampPrecision::{Micros, Millis, Nanos, Seconds};
412-
let ts = match self.timestamp {
390+
let ts = match self.format.timestamp {
413391
None => return Ok(()),
414392
Some(Seconds) => self.buf.timestamp_seconds(),
415393
Some(Millis) => self.buf.timestamp_millis(),
@@ -429,7 +407,7 @@ impl DefaultFormatWriter<'_> {
429407
}
430408

431409
fn write_module_path(&mut self, record: &Record<'_>) -> io::Result<()> {
432-
if !self.module_path {
410+
if !self.format.module_path {
433411
return Ok(());
434412
}
435413

@@ -441,12 +419,16 @@ impl DefaultFormatWriter<'_> {
441419
}
442420

443421
fn write_source_location(&mut self, record: &Record<'_>) -> io::Result<()> {
444-
if !self.source_file {
422+
if !self.format.source_file {
445423
return Ok(());
446424
}
447425

448426
if let Some(file_path) = record.file() {
449-
let line = self.source_line_number.then(|| record.line()).flatten();
427+
let line = self
428+
.format
429+
.source_line_number
430+
.then(|| record.line())
431+
.flatten();
450432
match line {
451433
Some(line) => self.write_header_value(format_args!("{file_path}:{line}")),
452434
None => self.write_header_value(file_path),
@@ -457,7 +439,7 @@ impl DefaultFormatWriter<'_> {
457439
}
458440

459441
fn write_target(&mut self, record: &Record<'_>) -> io::Result<()> {
460-
if !self.target {
442+
if !self.format.target {
461443
return Ok(());
462444
}
463445

@@ -477,7 +459,7 @@ impl DefaultFormatWriter<'_> {
477459
}
478460

479461
fn write_args(&mut self, record: &Record<'_>) -> io::Result<()> {
480-
match self.indent {
462+
match self.format.indent {
481463
// Fast path for no indentation
482464
None => write!(self.buf, "{}", record.args()),
483465

@@ -497,7 +479,7 @@ impl DefaultFormatWriter<'_> {
497479
write!(
498480
self.fmt.buf,
499481
"{}{:width$}",
500-
self.fmt.suffix,
482+
self.fmt.format.suffix,
501483
"",
502484
width = self.indent_count
503485
)?;
@@ -530,7 +512,11 @@ impl DefaultFormatWriter<'_> {
530512

531513
#[cfg(feature = "kv")]
532514
fn write_kv(&mut self, record: &Record<'_>) -> io::Result<()> {
533-
let format = self.kv_format;
515+
let format = self
516+
.format
517+
.kv_format
518+
.as_deref()
519+
.unwrap_or(&default_kv_format);
534520
format(self.buf, record.key_values())
535521
}
536522
}

0 commit comments

Comments
 (0)