brk_interface/
output.rs

1use crate::Format;
2
3#[derive(Debug)]
4pub enum Output {
5    Json(Value),
6    CSV(String),
7}
8
9impl Output {
10    #[allow(clippy::inherent_to_string)]
11    pub fn to_string(self) -> String {
12        match self {
13            Output::CSV(s) => s,
14            Output::Json(v) => unsafe { String::from_utf8_unchecked(v.to_vec()) },
15        }
16    }
17}
18
19#[derive(Debug)]
20pub enum Value {
21    Matrix(Vec<Vec<u8>>),
22    List(Vec<u8>),
23}
24
25impl Value {
26    pub fn to_vec(self) -> Vec<u8> {
27        match self {
28            Value::List(v) => v,
29            Self::Matrix(m) => {
30                let total_size = m.iter().map(|v| v.len()).sum::<usize>() + m.len() - 1 + 2;
31                let mut matrix = Vec::with_capacity(total_size);
32                matrix.push(b'[');
33
34                for (i, vec) in m.into_iter().enumerate() {
35                    if i > 0 {
36                        matrix.push(b',');
37                    }
38                    matrix.extend(vec);
39                }
40                matrix.push(b']');
41                matrix
42            }
43        }
44    }
45}
46
47impl Output {
48    pub fn default(format: Format) -> Self {
49        match format {
50            Format::CSV => Output::CSV("".to_string()),
51            Format::JSON => Output::Json(Value::List(b"[]".to_vec())),
52        }
53    }
54}