mod json_formatter;
mod table_formatter;
mod yaml_formatter;
pub use json_formatter::JsonFormatter;
use serde::Serialize;
pub use table_formatter::TableFormatter;
pub use yaml_formatter::YamlFormatter;
#[derive(Debug, Clone, Copy, Default)]
pub enum OutputFormat {
#[default]
Table,
Json,
Yaml,
}
impl From<&str> for OutputFormat {
fn from(s: &str) -> Self {
match s.to_lowercase().as_str() {
"json" => Self::Json,
"yaml" | "yml" => Self::Yaml,
_ => Self::Table,
}
}
}
pub trait Formatter {
fn format<T: Serialize>(&self, data: &T) -> String;
}
pub enum FormatterType {
Json(JsonFormatter),
Table(TableFormatter),
Yaml(YamlFormatter),
}
impl FormatterType {
pub fn format<T: Serialize>(&self, data: &T) -> String {
match self {
Self::Json(f) => f.format(data),
Self::Table(f) => f.format(data),
Self::Yaml(f) => f.format(data),
}
}
}
impl From<OutputFormat> for FormatterType {
fn from(format: OutputFormat) -> Self {
match format {
OutputFormat::Json => Self::Json(JsonFormatter),
OutputFormat::Table => Self::Table(TableFormatter),
OutputFormat::Yaml => Self::Yaml(YamlFormatter),
}
}
}
pub fn get_formatter(format: OutputFormat) -> FormatterType {
FormatterType::from(format)
}