vantage_cli_util/output/
mod.rs1use ciborium::Value as CborValue;
10use indexmap::IndexMap;
11use vantage_types::Record;
12
13pub mod cbor_diag;
14pub mod json;
15pub mod ndjson;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
19pub enum OutputFormat {
20 #[default]
21 Table,
22 Json,
23 Ndjson,
24 CborDiag,
25}
26
27impl OutputFormat {
28 pub fn parse(s: &str) -> Option<Self> {
31 match s {
32 "table" => Some(Self::Table),
33 "json" => Some(Self::Json),
34 "ndjson" => Some(Self::Ndjson),
35 "cbor-diag" => Some(Self::CborDiag),
36 _ => None,
37 }
38 }
39}
40
41pub fn render_list(format: OutputFormat, records: &IndexMap<String, Record<CborValue>>) -> String {
48 match format {
49 OutputFormat::Table => String::new(),
50 OutputFormat::Json => json::write_list(records),
51 OutputFormat::Ndjson => ndjson::write_list(records),
52 OutputFormat::CborDiag => cbor_diag::write_list(records),
53 }
54}
55
56pub fn render_record(format: OutputFormat, id: &str, record: &Record<CborValue>) -> String {
58 match format {
59 OutputFormat::Table => String::new(),
60 OutputFormat::Json => json::write_record(id, record),
61 OutputFormat::Ndjson => ndjson::write_record(id, record),
62 OutputFormat::CborDiag => cbor_diag::write_record(id, record),
63 }
64}
65
66pub fn render_scalar(format: OutputFormat, label: &str, value: &CborValue) -> String {
68 match format {
69 OutputFormat::Table => String::new(),
70 OutputFormat::Json => json::write_scalar(label, value),
71 OutputFormat::Ndjson => ndjson::write_scalar(label, value),
72 OutputFormat::CborDiag => cbor_diag::write_scalar(label, value),
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn parse_known_formats() {
82 assert_eq!(OutputFormat::parse("table"), Some(OutputFormat::Table));
83 assert_eq!(OutputFormat::parse("json"), Some(OutputFormat::Json));
84 assert_eq!(OutputFormat::parse("ndjson"), Some(OutputFormat::Ndjson));
85 assert_eq!(
86 OutputFormat::parse("cbor-diag"),
87 Some(OutputFormat::CborDiag)
88 );
89 assert_eq!(OutputFormat::parse("yaml"), None);
90 }
91}