1use model::SingleTcModel;
2
3use super::*;
4
5pub struct Tc {
6 opts: GeneralOpt,
7 fields: Vec<TcField>,
8}
9
10impl Tc {
11 pub fn new(opts: &GeneralOpt, fields: Vec<TcField>) -> Self {
12 Self {
13 opts: opts.to_owned(),
14 fields,
15 }
16 }
17}
18
19impl Dumper for Tc {
20 fn dump_model(
21 &self,
22 ctx: &CommonFieldContext,
23 model: &model::Model,
24 output: &mut dyn Write,
25 round: &mut usize,
26 comma_flag: bool,
27 ) -> Result<IterExecResult> {
28 let tcs: Vec<&SingleTcModel> = match &model.tc {
29 Some(tc_model) => tc_model.tc.iter().collect(),
30 None => Vec::new(),
31 };
32 if tcs.is_empty() {
33 return Ok(IterExecResult::Skip);
34 }
35
36 let mut json_output = json!([]);
37
38 tcs.into_iter()
39 .map(|tc| {
40 match self.opts.output_format {
41 Some(OutputFormat::Raw) | None => write!(
42 output,
43 "{}",
44 print::dump_raw(
45 &self.fields,
46 ctx,
47 tc,
48 *round,
49 self.opts.repeat_title,
50 self.opts.disable_title,
51 self.opts.raw
52 )
53 )?,
54 Some(OutputFormat::Csv) => write!(
55 output,
56 "{}",
57 print::dump_csv(
58 &self.fields,
59 ctx,
60 tc,
61 *round,
62 self.opts.disable_title,
63 self.opts.raw
64 )
65 )?,
66 Some(OutputFormat::Tsv) => write!(
67 output,
68 "{}",
69 print::dump_tsv(
70 &self.fields,
71 ctx,
72 tc,
73 *round,
74 self.opts.disable_title,
75 self.opts.raw
76 )
77 )?,
78 Some(OutputFormat::KeyVal) => write!(
79 output,
80 "{}",
81 print::dump_kv(&self.fields, ctx, tc, self.opts.raw)
82 )?,
83 Some(OutputFormat::Json) => {
84 let par = print::dump_json(&self.fields, ctx, tc, self.opts.raw);
85 json_output.as_array_mut().unwrap().push(par);
86 }
87 Some(OutputFormat::OpenMetrics) => {
88 write!(output, "{}", print::dump_openmetrics(&self.fields, ctx, tc))?
89 }
90 }
91 *round += 1;
92 Ok(())
93 })
94 .collect::<Result<Vec<_>>>()?;
95
96 match (self.opts.output_format, comma_flag) {
97 (Some(OutputFormat::Json), true) => write!(output, ",{}", json_output)?,
98 (Some(OutputFormat::Json), false) => write!(output, "{}", json_output)?,
99 (Some(OutputFormat::OpenMetrics), _) => (),
100 _ => writeln!(output)?,
101 };
102
103 Ok(IterExecResult::Success)
104 }
105}