rocketmq_admin_core/cli/
formatters.rs1mod json_formatter;
20mod table_formatter;
21mod yaml_formatter;
22
23pub use json_formatter::JsonFormatter;
24use serde::Serialize;
25pub use table_formatter::TableFormatter;
26pub use yaml_formatter::YamlFormatter;
27
28#[derive(Debug, Clone, Copy, Default)]
30pub enum OutputFormat {
31 #[default]
32 Table,
33 Json,
34 Yaml,
35}
36
37impl From<&str> for OutputFormat {
38 fn from(s: &str) -> Self {
39 match s.to_lowercase().as_str() {
40 "json" => Self::Json,
41 "yaml" | "yml" => Self::Yaml,
42 _ => Self::Table,
43 }
44 }
45}
46
47pub trait Formatter {
49 fn format<T: Serialize>(&self, data: &T) -> String;
51}
52
53pub enum FormatterType {
55 Json(JsonFormatter),
56 Table(TableFormatter),
57 Yaml(YamlFormatter),
58}
59
60impl FormatterType {
61 pub fn format<T: Serialize>(&self, data: &T) -> String {
62 match self {
63 Self::Json(f) => f.format(data),
64 Self::Table(f) => f.format(data),
65 Self::Yaml(f) => f.format(data),
66 }
67 }
68}
69
70impl From<OutputFormat> for FormatterType {
71 fn from(format: OutputFormat) -> Self {
72 match format {
73 OutputFormat::Json => Self::Json(JsonFormatter),
74 OutputFormat::Table => Self::Table(TableFormatter),
75 OutputFormat::Yaml => Self::Yaml(YamlFormatter),
76 }
77 }
78}
79
80pub fn get_formatter(format: OutputFormat) -> FormatterType {
82 FormatterType::from(format)
83}