use ciborium::Value as CborValue;
use indexmap::IndexMap;
use vantage_types::Record;
pub mod cbor_diag;
pub mod json;
pub mod ndjson;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
#[default]
Table,
Json,
Ndjson,
CborDiag,
}
impl OutputFormat {
pub fn parse(s: &str) -> Option<Self> {
match s {
"table" => Some(Self::Table),
"json" => Some(Self::Json),
"ndjson" => Some(Self::Ndjson),
"cbor-diag" => Some(Self::CborDiag),
_ => None,
}
}
}
pub fn render_list(format: OutputFormat, records: &IndexMap<String, Record<CborValue>>) -> String {
match format {
OutputFormat::Table => String::new(),
OutputFormat::Json => json::write_list(records),
OutputFormat::Ndjson => ndjson::write_list(records),
OutputFormat::CborDiag => cbor_diag::write_list(records),
}
}
pub fn render_record(format: OutputFormat, id: &str, record: &Record<CborValue>) -> String {
match format {
OutputFormat::Table => String::new(),
OutputFormat::Json => json::write_record(id, record),
OutputFormat::Ndjson => ndjson::write_record(id, record),
OutputFormat::CborDiag => cbor_diag::write_record(id, record),
}
}
pub fn render_scalar(format: OutputFormat, label: &str, value: &CborValue) -> String {
match format {
OutputFormat::Table => String::new(),
OutputFormat::Json => json::write_scalar(label, value),
OutputFormat::Ndjson => ndjson::write_scalar(label, value),
OutputFormat::CborDiag => cbor_diag::write_scalar(label, value),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_known_formats() {
assert_eq!(OutputFormat::parse("table"), Some(OutputFormat::Table));
assert_eq!(OutputFormat::parse("json"), Some(OutputFormat::Json));
assert_eq!(OutputFormat::parse("ndjson"), Some(OutputFormat::Ndjson));
assert_eq!(
OutputFormat::parse("cbor-diag"),
Some(OutputFormat::CborDiag)
);
assert_eq!(OutputFormat::parse("yaml"), None);
}
}