use derivative::Derivative;
use log::Record;
use std::{fmt, io};
#[cfg(feature = "config_parsing")]
use serde::de;
#[cfg(feature = "config_parsing")]
use serde_value::Value;
#[cfg(feature = "config_parsing")]
use std::collections::BTreeMap;
#[cfg(feature = "config_parsing")]
use crate::config::Deserializable;
#[cfg(feature = "json_encoder")]
pub mod json;
#[cfg(feature = "pattern_encoder")]
pub mod pattern;
pub mod writer;
#[allow(dead_code)]
#[cfg(windows)]
const NEWLINE: &'static str = "\r\n";
#[allow(dead_code)]
#[cfg(not(windows))]
const NEWLINE: &str = "\n";
pub trait Encode: fmt::Debug + Send + Sync + 'static {
fn encode(&self, w: &mut dyn Write, record: &Record) -> anyhow::Result<()>;
}
#[cfg(feature = "config_parsing")]
impl Deserializable for dyn Encode {
fn name() -> &'static str {
"encoder"
}
}
#[cfg(feature = "config_parsing")]
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct EncoderConfig {
pub kind: String,
pub config: Value,
}
#[cfg(feature = "config_parsing")]
impl<'de> de::Deserialize<'de> for EncoderConfig {
fn deserialize<D>(d: D) -> Result<EncoderConfig, D::Error>
where
D: de::Deserializer<'de>,
{
let mut map = BTreeMap::<Value, Value>::deserialize(d)?;
let kind = match map.remove(&Value::String("kind".to_owned())) {
Some(kind) => kind.deserialize_into().map_err(|e| e.to_error())?,
None => "pattern".to_owned(),
};
Ok(EncoderConfig {
kind,
config: Value::Map(map),
})
}
}
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
}
#[derive(Derivative)]
#[derivative(Debug)]
#[derive(Clone, Eq, PartialEq, Hash, Default)]
pub struct Style {
pub text: Option<Color>,
pub background: Option<Color>,
pub intense: Option<bool>,
#[derivative(Debug = "ignore")]
_p: (),
}
impl Style {
pub fn new() -> Style {
Style::default()
}
pub fn text(&mut self, text: Color) -> &mut Style {
self.text = Some(text);
self
}
pub fn background(&mut self, background: Color) -> &mut Style {
self.background = Some(background);
self
}
pub fn intense(&mut self, intense: bool) -> &mut Style {
self.intense = Some(intense);
self
}
}
pub trait Write: io::Write {
#[allow(unused_variables)]
fn set_style(&mut self, style: &Style) -> io::Result<()> {
Ok(())
}
}
impl<'a, W: Write + ?Sized> Write for &'a mut W {
fn set_style(&mut self, style: &Style) -> io::Result<()> {
<W as Write>::set_style(*self, style)
}
}