mod json;
pub use json::{JsonColorConfig, JsonFormatter};
use crate::types::Value;
pub trait Formatter: Send + Sync {
fn name(&self) -> &str;
fn can_format(&self, value: &Value) -> bool;
fn format(&self, value: &Value) -> Result<String, FormatError>;
}
#[derive(Debug)]
pub struct FormatError {
pub message: String,
}
impl std::fmt::Display for FormatError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Format error: {}", self.message)
}
}
impl std::error::Error for FormatError {}
pub struct TextFormatter;
impl Formatter for TextFormatter {
fn name(&self) -> &str {
"text"
}
fn can_format(&self, _value: &Value) -> bool {
true }
fn format(&self, value: &Value) -> Result<String, FormatError> {
String::from_utf8(value.data.clone()).map_err(|_| FormatError {
message: "Invalid UTF-8".to_string(),
})
}
}