datadog_api_client/datadogV1/model/
model_widget_conditional_format.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9/// Define a conditional format for the widget.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct WidgetConditionalFormat {
14    /// Comparator to apply.
15    #[serde(rename = "comparator")]
16    pub comparator: crate::datadogV1::model::WidgetComparator,
17    /// Color palette to apply to the background, same values available as palette.
18    #[serde(rename = "custom_bg_color")]
19    pub custom_bg_color: Option<String>,
20    /// Color palette to apply to the foreground, same values available as palette.
21    #[serde(rename = "custom_fg_color")]
22    pub custom_fg_color: Option<String>,
23    /// True hides values.
24    #[serde(rename = "hide_value")]
25    pub hide_value: Option<bool>,
26    /// Displays an image as the background.
27    #[serde(rename = "image_url")]
28    pub image_url: Option<String>,
29    /// Metric from the request to correlate this conditional format with.
30    #[serde(rename = "metric")]
31    pub metric: Option<String>,
32    /// Color palette to apply.
33    #[serde(rename = "palette")]
34    pub palette: crate::datadogV1::model::WidgetPalette,
35    /// Defines the displayed timeframe.
36    #[serde(rename = "timeframe")]
37    pub timeframe: Option<String>,
38    /// Value for the comparator.
39    #[serde(rename = "value")]
40    pub value: f64,
41    #[serde(flatten)]
42    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
43    #[serde(skip)]
44    #[serde(default)]
45    pub(crate) _unparsed: bool,
46}
47
48impl WidgetConditionalFormat {
49    pub fn new(
50        comparator: crate::datadogV1::model::WidgetComparator,
51        palette: crate::datadogV1::model::WidgetPalette,
52        value: f64,
53    ) -> WidgetConditionalFormat {
54        WidgetConditionalFormat {
55            comparator,
56            custom_bg_color: None,
57            custom_fg_color: None,
58            hide_value: None,
59            image_url: None,
60            metric: None,
61            palette,
62            timeframe: None,
63            value,
64            additional_properties: std::collections::BTreeMap::new(),
65            _unparsed: false,
66        }
67    }
68
69    pub fn custom_bg_color(mut self, value: String) -> Self {
70        self.custom_bg_color = Some(value);
71        self
72    }
73
74    pub fn custom_fg_color(mut self, value: String) -> Self {
75        self.custom_fg_color = Some(value);
76        self
77    }
78
79    pub fn hide_value(mut self, value: bool) -> Self {
80        self.hide_value = Some(value);
81        self
82    }
83
84    pub fn image_url(mut self, value: String) -> Self {
85        self.image_url = Some(value);
86        self
87    }
88
89    pub fn metric(mut self, value: String) -> Self {
90        self.metric = Some(value);
91        self
92    }
93
94    pub fn timeframe(mut self, value: String) -> Self {
95        self.timeframe = Some(value);
96        self
97    }
98
99    pub fn additional_properties(
100        mut self,
101        value: std::collections::BTreeMap<String, serde_json::Value>,
102    ) -> Self {
103        self.additional_properties = value;
104        self
105    }
106}
107
108impl<'de> Deserialize<'de> for WidgetConditionalFormat {
109    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
110    where
111        D: Deserializer<'de>,
112    {
113        struct WidgetConditionalFormatVisitor;
114        impl<'a> Visitor<'a> for WidgetConditionalFormatVisitor {
115            type Value = WidgetConditionalFormat;
116
117            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
118                f.write_str("a mapping")
119            }
120
121            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
122            where
123                M: MapAccess<'a>,
124            {
125                let mut comparator: Option<crate::datadogV1::model::WidgetComparator> = None;
126                let mut custom_bg_color: Option<String> = None;
127                let mut custom_fg_color: Option<String> = None;
128                let mut hide_value: Option<bool> = None;
129                let mut image_url: Option<String> = None;
130                let mut metric: Option<String> = None;
131                let mut palette: Option<crate::datadogV1::model::WidgetPalette> = None;
132                let mut timeframe: Option<String> = None;
133                let mut value: Option<f64> = None;
134                let mut additional_properties: std::collections::BTreeMap<
135                    String,
136                    serde_json::Value,
137                > = std::collections::BTreeMap::new();
138                let mut _unparsed = false;
139
140                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
141                    match k.as_str() {
142                        "comparator" => {
143                            comparator = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
144                            if let Some(ref _comparator) = comparator {
145                                match _comparator {
146                                    crate::datadogV1::model::WidgetComparator::UnparsedObject(
147                                        _comparator,
148                                    ) => {
149                                        _unparsed = true;
150                                    }
151                                    _ => {}
152                                }
153                            }
154                        }
155                        "custom_bg_color" => {
156                            if v.is_null() {
157                                continue;
158                            }
159                            custom_bg_color =
160                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
161                        }
162                        "custom_fg_color" => {
163                            if v.is_null() {
164                                continue;
165                            }
166                            custom_fg_color =
167                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
168                        }
169                        "hide_value" => {
170                            if v.is_null() {
171                                continue;
172                            }
173                            hide_value = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
174                        }
175                        "image_url" => {
176                            if v.is_null() {
177                                continue;
178                            }
179                            image_url = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
180                        }
181                        "metric" => {
182                            if v.is_null() {
183                                continue;
184                            }
185                            metric = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
186                        }
187                        "palette" => {
188                            palette = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
189                            if let Some(ref _palette) = palette {
190                                match _palette {
191                                    crate::datadogV1::model::WidgetPalette::UnparsedObject(
192                                        _palette,
193                                    ) => {
194                                        _unparsed = true;
195                                    }
196                                    _ => {}
197                                }
198                            }
199                        }
200                        "timeframe" => {
201                            if v.is_null() {
202                                continue;
203                            }
204                            timeframe = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
205                        }
206                        "value" => {
207                            value = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
208                        }
209                        &_ => {
210                            if let Ok(value) = serde_json::from_value(v.clone()) {
211                                additional_properties.insert(k, value);
212                            }
213                        }
214                    }
215                }
216                let comparator = comparator.ok_or_else(|| M::Error::missing_field("comparator"))?;
217                let palette = palette.ok_or_else(|| M::Error::missing_field("palette"))?;
218                let value = value.ok_or_else(|| M::Error::missing_field("value"))?;
219
220                let content = WidgetConditionalFormat {
221                    comparator,
222                    custom_bg_color,
223                    custom_fg_color,
224                    hide_value,
225                    image_url,
226                    metric,
227                    palette,
228                    timeframe,
229                    value,
230                    additional_properties,
231                    _unparsed,
232                };
233
234                Ok(content)
235            }
236        }
237
238        deserializer.deserialize_any(WidgetConditionalFormatVisitor)
239    }
240}