use std::marker::PhantomData;
use crate::core::{LogEntry, LogLevel};
use crate::error::Result;
pub trait Adapter<T> {
fn convert(&self, external_log: &T) -> Result<LogEntry>;
fn configure(&mut self, options: AdapterOptions);
}
#[derive(Debug, Clone)]
pub struct AdapterOptions {
pub include_source: bool,
pub include_thread: bool,
pub include_stack_traces: bool,
pub context_extractor: Option<String>,
}
impl Default for AdapterOptions {
fn default() -> Self {
Self {
include_source: true,
include_thread: true,
include_stack_traces: true,
context_extractor: None,
}
}
}
pub struct StandardAdapter<T> {
options: AdapterOptions,
_phantom: PhantomData<T>,
}
impl<T> StandardAdapter<T> {
pub fn new() -> Self {
Self {
options: AdapterOptions::default(),
_phantom: PhantomData,
}
}
pub fn with_options(options: AdapterOptions) -> Self {
Self {
options,
_phantom: PhantomData,
}
}
}
impl<T> Default for StandardAdapter<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: AsRef<str>> Adapter<T> for StandardAdapter<T> {
fn convert(&self, external_log: &T) -> Result<LogEntry> {
let message = external_log.as_ref().to_string();
let entry = LogEntry::new(message, LogLevel::Info);
Ok(entry)
}
fn configure(&mut self, options: AdapterOptions) {
self.options = options;
}
}