Skip to main content

asana_cli/output/
mod.rs

1//! Output helpers for rendering command results.
2
3pub mod project;
4pub mod task;
5
6use clap::ValueEnum;
7use std::fmt;
8use std::str::FromStr;
9
10/// Unified output format used by every asana-cli command.
11///
12/// `Table` adapts to single-record (vertical key/value) vs collection (horizontal
13/// rows) at the renderer layer.
14#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
15pub enum OutputFormat {
16    /// Human-readable table; renders vertically for single records.
17    #[default]
18    Table,
19    /// JSON representation suitable for scripting.
20    Json,
21    /// Comma separated value export.
22    Csv,
23    /// Markdown friendly tables.
24    Markdown,
25}
26
27impl fmt::Display for OutputFormat {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        f.write_str(match self {
30            Self::Table => "table",
31            Self::Json => "json",
32            Self::Csv => "csv",
33            Self::Markdown => "markdown",
34        })
35    }
36}
37
38impl FromStr for OutputFormat {
39    type Err = String;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        match s.to_ascii_lowercase().as_str() {
43            "table" => Ok(Self::Table),
44            "json" => Ok(Self::Json),
45            "csv" => Ok(Self::Csv),
46            "markdown" | "md" => Ok(Self::Markdown),
47            other => Err(format!(
48                "unsupported output format '{other}'; expected table, json, csv, or markdown"
49            )),
50        }
51    }
52}
53
54/// Backwards-compatible alias retained while task/project output modules transition.
55pub type TaskOutputFormat = OutputFormat;
56/// Backwards-compatible alias retained while task/project output modules transition.
57pub type ProjectOutputFormat = OutputFormat;