1use clap::ValueEnum;
4use comfy_table::Table;
5use serde::Serialize;
6
7#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
9pub enum OutputFormat {
10 #[default]
12 Table,
13 Json,
15 Yaml,
17 Quiet,
19}
20
21pub trait MultiFormatDisplay: Serialize {
23 fn to_table(&self) -> Table;
24 fn to_quiet(&self) -> String {
25 String::new()
26 }
27}
28
29pub fn render_output<T: MultiFormatDisplay>(
31 data: &T,
32 format: OutputFormat,
33) -> anyhow::Result<String> {
34 match format {
35 OutputFormat::Table => Ok(data.to_table().to_string()),
36 OutputFormat::Json => Ok(serde_json::to_string_pretty(data)?),
37 OutputFormat::Yaml => Ok(serde_yaml::to_string(data)?),
38 OutputFormat::Quiet => Ok(data.to_quiet()),
39 }
40}