| 1 |
use std::io::{self, Write}; |
| 2 |
|
| 3 |
#[cfg(feature = "color")] |
| 4 |
use super::WriteStyle; |
| 5 |
use super::{Formatter, StyledValue}; |
| 6 |
#[cfg(feature = "color")] |
| 7 |
use anstyle::Style; |
| 8 |
use log::kv::{Error, Key, Source, Value, VisitSource}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
pub(crate) type KvFormatFn = dyn Fn(&mut Formatter, &dyn Source) -> io::Result<()> + Sync + Send; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
pub fn hidden_kv_format(_formatter: &mut Formatter, _fields: &dyn Source) -> io::Result<()> { |
| 24 |
Ok(()) |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
pub fn default_kv_format(formatter: &mut Formatter, fields: &dyn Source) -> io::Result<()> { |
| 37 |
fields |
| 38 |
.visit(&mut DefaultVisitSource(formatter)) |
| 39 |
.map_err(|e| io::Error::new(io::ErrorKind::Other, e)) |
| 40 |
} |
| 41 |
|
| 42 |
struct DefaultVisitSource<'a>(&'a mut Formatter); |
| 43 |
|
| 44 |
impl<'kvs> VisitSource<'kvs> for DefaultVisitSource<'_> { |
| 45 |
fn visit_pair(&mut self, key: Key<'_>, value: Value<'kvs>) -> Result<(), Error> { |
| 46 |
write!(self.0, " {}={}", self.style_key(key), value)?; |
| 47 |
Ok(()) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl DefaultVisitSource<'_> { |
| 52 |
fn style_key<'k>(&self, text: Key<'k>) -> StyledValue<Key<'k>> { |
| 53 |
#[cfg(feature = "color")] |
| 54 |
{ |
| 55 |
StyledValue { |
| 56 |
style: if self.0.write_style == WriteStyle::Never { |
| 57 |
Style::new() |
| 58 |
} else { |
| 59 |
Style::new().italic() |
| 60 |
}, |
| 61 |
value: text, |
| 62 |
} |
| 63 |
} |
| 64 |
#[cfg(not(feature = "color"))] |
| 65 |
{ |
| 66 |
text |
| 67 |
} |
| 68 |
} |
| 69 |
} |