brk_interface/
output.rs

1use std::fmt;
2
3use serde::Serialize;
4use tabled::Tabled as TabledTabled;
5
6use crate::Format;
7
8#[derive(Debug, Serialize)]
9#[serde(untagged, rename_all = "lowercase")]
10pub enum Output {
11    Json(Value),
12    CSV(String),
13    TSV(String),
14    MD(String),
15}
16
17#[derive(Debug, Serialize, TabledTabled)]
18#[serde(untagged)]
19pub enum Value {
20    Matrix(Vec<Vec<serde_json::Value>>),
21    List(Vec<serde_json::Value>),
22    Single(serde_json::Value),
23}
24
25impl Output {
26    pub fn default(format: Option<Format>) -> Self {
27        match format {
28            Some(Format::CSV) => Output::CSV("".to_string()),
29            Some(Format::TSV) => Output::TSV("".to_string()),
30            _ => Output::Json(Value::Single(serde_json::Value::Null)),
31        }
32    }
33}
34
35impl fmt::Display for Output {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Self::Json(value) => write!(f, "{}", serde_json::to_string_pretty(value).unwrap()),
39            Self::CSV(string) => write!(f, "{string}"),
40            Self::TSV(string) => write!(f, "{string}"),
41            Self::MD(string) => write!(f, "{string}"),
42        }
43    }
44}