use log::Record;
use log4rs::encode::{Encode, Write};

use crate::NEWLINE;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct JsonStrEncoder(());

impl JsonStrEncoder {
    /// Returns a new `JsonStrEncoder` with a default configuration.
    pub fn new() -> Self {
        Self::default()
    }
}

impl Encode for JsonStrEncoder {
    fn encode(&self, w: &mut dyn Write, record: &Record) -> anyhow::Result<()> {
        let message = record.args();
        w.write_fmt(message.clone())?;
        w.write_all(NEWLINE.as_bytes())?;
        Ok(())
    }
}
