mielin_cli/output/
mod.rs

1//! Output formatting module for MielinCTL
2
3use clap::ValueEnum;
4use comfy_table::Table;
5use serde::Serialize;
6
7/// Output format for command results
8#[derive(Debug, Clone, Copy, Default, ValueEnum, PartialEq, Eq)]
9pub enum OutputFormat {
10    /// Human-readable table format (default)
11    #[default]
12    Table,
13    /// JSON format for scripting
14    Json,
15    /// YAML format for configuration
16    Yaml,
17    /// Quiet mode - minimal output
18    Quiet,
19}
20
21/// Trait for types that can be displayed in multiple formats
22pub trait MultiFormatDisplay: Serialize {
23    fn to_table(&self) -> Table;
24    fn to_quiet(&self) -> String {
25        String::new()
26    }
27}
28
29/// Render output in the specified format
30pub 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}