below_dump/iface.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 model::SingleNetModelFieldId;
16
17use super::*;
18
19pub struct Iface {
20 opts: GeneralOpt,
21 select: Option<SingleNetModelFieldId>,
22 fields: Vec<IfaceField>,
23}
24
25impl Iface {
26 pub fn new(
27 opts: &GeneralOpt,
28 select: Option<SingleNetModelFieldId>,
29 fields: Vec<IfaceField>,
30 ) -> Self {
31 Self {
32 opts: opts.to_owned(),
33 select,
34 fields,
35 }
36 }
37}
38
39impl Dumper for Iface {
40 fn dump_model(
41 &self,
42 ctx: &CommonFieldContext,
43 model: &model::Model,
44 output: &mut dyn Write,
45 round: &mut usize,
46 comma_flag: bool,
47 ) -> Result<IterExecResult> {
48 let mut json_output = json!([]);
49
50 model
51 .network
52 .interfaces
53 .iter()
54 .filter(
55 #[allow(clippy::match_like_matches_macro)]
56 |(_, model)| match (self.select.as_ref(), self.opts.filter.as_ref()) {
57 (Some(field_id), Some(filter))
58 if !filter.is_match(
59 &model
60 .query(field_id)
61 .map_or("?".to_owned(), |v| v.to_string()),
62 ) =>
63 {
64 false
65 }
66 _ => true,
67 },
68 )
69 .map(|(_, model)| {
70 match self.opts.output_format {
71 Some(OutputFormat::Raw) | None => write!(
72 output,
73 "{}",
74 print::dump_raw(
75 &self.fields,
76 ctx,
77 model,
78 *round,
79 self.opts.repeat_title,
80 self.opts.disable_title,
81 self.opts.raw
82 )
83 )?,
84 Some(OutputFormat::Csv) => write!(
85 output,
86 "{}",
87 print::dump_csv(
88 &self.fields,
89 ctx,
90 model,
91 *round,
92 self.opts.disable_title,
93 self.opts.raw
94 )
95 )?,
96 Some(OutputFormat::Tsv) => write!(
97 output,
98 "{}",
99 print::dump_tsv(
100 &self.fields,
101 ctx,
102 model,
103 *round,
104 self.opts.disable_title,
105 self.opts.raw
106 )
107 )?,
108 Some(OutputFormat::KeyVal) => write!(
109 output,
110 "{}",
111 print::dump_kv(&self.fields, ctx, model, self.opts.raw)
112 )?,
113 Some(OutputFormat::Json) => {
114 let par = print::dump_json(&self.fields, ctx, model, self.opts.raw);
115 json_output.as_array_mut().unwrap().push(par);
116 }
117 Some(OutputFormat::OpenMetrics) => write!(
118 output,
119 "{}",
120 print::dump_openmetrics(&self.fields, ctx, model)
121 )?,
122 }
123 *round += 1;
124 Ok(())
125 })
126 .collect::<Result<Vec<_>>>()?;
127
128 match (self.opts.output_format, comma_flag) {
129 (Some(OutputFormat::Json), true) => write!(output, ",{}", json_output)?,
130 (Some(OutputFormat::Json), false) => write!(output, "{}", json_output)?,
131 (Some(OutputFormat::OpenMetrics), _) => (),
132 _ => writeln!(output)?,
133 };
134
135 Ok(IterExecResult::Success)
136 }
137}