below_dump/
transport.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::*;
16
17pub struct Transport {
18    opts: GeneralOpt,
19    fields: Vec<TransportField>,
20}
21
22impl Transport {
23    pub fn new(opts: &GeneralOpt, fields: Vec<TransportField>) -> Self {
24        Self {
25            opts: opts.to_owned(),
26            fields,
27        }
28    }
29}
30
31impl Dumper for Transport {
32    fn dump_model(
33        &self,
34        ctx: &CommonFieldContext,
35        model: &model::Model,
36        output: &mut dyn Write,
37        round: &mut usize,
38        comma_flag: bool,
39    ) -> Result<IterExecResult> {
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                    &model.network,
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                    &model.network,
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                    &model.network,
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, &model.network, self.opts.raw)
82            )?,
83            Some(OutputFormat::Json) => {
84                let json_output =
85                    print::dump_json(&self.fields, ctx, &model.network, self.opts.raw);
86                if comma_flag {
87                    write!(output, ",{}", json_output)?;
88                } else {
89                    write!(output, "{}", json_output)?;
90                }
91            }
92            Some(OutputFormat::OpenMetrics) => write!(
93                output,
94                "{}",
95                print::dump_openmetrics(&self.fields, ctx, &model.network)
96            )?,
97        };
98        *round += 1;
99
100        match &self.opts.output_format {
101            Some(OutputFormat::Json) | Some(OutputFormat::OpenMetrics) => (),
102            _ => writeln!(output)?,
103        }
104
105        Ok(IterExecResult::Success)
106    }
107}