use crate::LogEntry;
use dyn_clone::DynClone;
use dyn_fmt::AsStrFormatExt;
use regex::Regex;
use std::{fmt, hash};
use strfmt::strfmt;
pub trait FormatTrait: fmt::Display + DynClone + Send + Sync {
fn format(&self, log_entry: &LogEntry) -> String;
fn ft_fmt(&self, dt_fmt: String, fmt: String, log_entry: &LogEntry) -> String {
let dt = log_entry.timestamp.format(&dt_fmt).to_string();
strfmt!(
&fmt,
dt,
message => log_entry.message.clone(),
mod_path => log_entry.mod_path.clone(),
fn_name => log_entry.fn_name.clone(),
level => log_entry.level.as_str()
)
.unwrap()
}
}
dyn_clone::clone_trait_object!(FormatTrait);
impl fmt::Debug for dyn FormatTrait {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt(f)
}
}
#[cfg(test)]
mod tests {
use crate::*;
use strfmt::strfmt;
#[test]
fn strfmt_test() {
let fmt = "test: {text} - {text2}";
let text = "Some text";
let text2 = "Will this work?";
let output = strfmt!(fmt, text2, text).unwrap();
assert_eq!("test: Some text - Will this work?".to_string(), output);
}
#[test]
fn debug() {
let fmt = FormatType::Custom.create(Some(Box::new(SimpleFormatter::new())));
println!("fmt: {fmt}");
let sf: Box<dyn FormatTrait> = Box::new(SimpleFormatter::new());
println!("sf: {sf:?}");
assert_eq!(fmt.to_string(), sf.to_string());
}
}