datadog_api_client/datadogV2/model/
model_component_grid.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/// A grid component. The grid component is the root canvas for an app and contains all other components.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct ComponentGrid {
14    /// Events to listen for on the grid component.
15    #[serde(rename = "events")]
16    pub events: Option<Vec<crate::datadogV2::model::AppBuilderEvent>>,
17    /// The ID of the grid component. This property is deprecated; use `name` to identify individual components instead.
18    #[serde(rename = "id")]
19    pub id: Option<String>,
20    /// A unique identifier for this grid component. This name is also visible in the app editor.
21    #[serde(rename = "name")]
22    pub name: String,
23    /// Properties of a grid component.
24    #[serde(rename = "properties")]
25    pub properties: crate::datadogV2::model::ComponentGridProperties,
26    /// The grid component type.
27    #[serde(rename = "type")]
28    pub type_: crate::datadogV2::model::ComponentGridType,
29    #[serde(flatten)]
30    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
31    #[serde(skip)]
32    #[serde(default)]
33    pub(crate) _unparsed: bool,
34}
35
36impl ComponentGrid {
37    pub fn new(
38        name: String,
39        properties: crate::datadogV2::model::ComponentGridProperties,
40        type_: crate::datadogV2::model::ComponentGridType,
41    ) -> ComponentGrid {
42        ComponentGrid {
43            events: None,
44            id: None,
45            name,
46            properties,
47            type_,
48            additional_properties: std::collections::BTreeMap::new(),
49            _unparsed: false,
50        }
51    }
52
53    pub fn events(mut self, value: Vec<crate::datadogV2::model::AppBuilderEvent>) -> Self {
54        self.events = Some(value);
55        self
56    }
57
58    pub fn id(mut self, value: String) -> Self {
59        self.id = Some(value);
60        self
61    }
62
63    pub fn additional_properties(
64        mut self,
65        value: std::collections::BTreeMap<String, serde_json::Value>,
66    ) -> Self {
67        self.additional_properties = value;
68        self
69    }
70}
71
72impl<'de> Deserialize<'de> for ComponentGrid {
73    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74    where
75        D: Deserializer<'de>,
76    {
77        struct ComponentGridVisitor;
78        impl<'a> Visitor<'a> for ComponentGridVisitor {
79            type Value = ComponentGrid;
80
81            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
82                f.write_str("a mapping")
83            }
84
85            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
86            where
87                M: MapAccess<'a>,
88            {
89                let mut events: Option<Vec<crate::datadogV2::model::AppBuilderEvent>> = None;
90                let mut id: Option<String> = None;
91                let mut name: Option<String> = None;
92                let mut properties: Option<crate::datadogV2::model::ComponentGridProperties> = None;
93                let mut type_: Option<crate::datadogV2::model::ComponentGridType> = None;
94                let mut additional_properties: std::collections::BTreeMap<
95                    String,
96                    serde_json::Value,
97                > = std::collections::BTreeMap::new();
98                let mut _unparsed = false;
99
100                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
101                    match k.as_str() {
102                        "events" => {
103                            if v.is_null() {
104                                continue;
105                            }
106                            events = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
107                        }
108                        "id" => {
109                            if v.is_null() {
110                                continue;
111                            }
112                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
113                        }
114                        "name" => {
115                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
116                        }
117                        "properties" => {
118                            properties = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
119                        }
120                        "type" => {
121                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
122                            if let Some(ref _type_) = type_ {
123                                match _type_ {
124                                    crate::datadogV2::model::ComponentGridType::UnparsedObject(
125                                        _type_,
126                                    ) => {
127                                        _unparsed = true;
128                                    }
129                                    _ => {}
130                                }
131                            }
132                        }
133                        &_ => {
134                            if let Ok(value) = serde_json::from_value(v.clone()) {
135                                additional_properties.insert(k, value);
136                            }
137                        }
138                    }
139                }
140                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
141                let properties = properties.ok_or_else(|| M::Error::missing_field("properties"))?;
142                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
143
144                let content = ComponentGrid {
145                    events,
146                    id,
147                    name,
148                    properties,
149                    type_,
150                    additional_properties,
151                    _unparsed,
152                };
153
154                Ok(content)
155            }
156        }
157
158        deserializer.deserialize_any(ComponentGridVisitor)
159    }
160}