datadog_api_client/datadogV2/model/
model_layer.rs1use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct Layer {
14    #[serde(rename = "attributes")]
16    pub attributes: Option<crate::datadogV2::model::LayerAttributes>,
17    #[serde(rename = "id")]
19    pub id: Option<String>,
20    #[serde(rename = "relationships")]
22    pub relationships: Option<crate::datadogV2::model::LayerRelationships>,
23    #[serde(rename = "type")]
25    pub type_: crate::datadogV2::model::LayerType,
26    #[serde(flatten)]
27    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
28    #[serde(skip)]
29    #[serde(default)]
30    pub(crate) _unparsed: bool,
31}
32
33impl Layer {
34    pub fn new(type_: crate::datadogV2::model::LayerType) -> Layer {
35        Layer {
36            attributes: None,
37            id: None,
38            relationships: None,
39            type_,
40            additional_properties: std::collections::BTreeMap::new(),
41            _unparsed: false,
42        }
43    }
44
45    pub fn attributes(mut self, value: crate::datadogV2::model::LayerAttributes) -> Self {
46        self.attributes = Some(value);
47        self
48    }
49
50    pub fn id(mut self, value: String) -> Self {
51        self.id = Some(value);
52        self
53    }
54
55    pub fn relationships(mut self, value: crate::datadogV2::model::LayerRelationships) -> Self {
56        self.relationships = Some(value);
57        self
58    }
59
60    pub fn additional_properties(
61        mut self,
62        value: std::collections::BTreeMap<String, serde_json::Value>,
63    ) -> Self {
64        self.additional_properties = value;
65        self
66    }
67}
68
69impl<'de> Deserialize<'de> for Layer {
70    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
71    where
72        D: Deserializer<'de>,
73    {
74        struct LayerVisitor;
75        impl<'a> Visitor<'a> for LayerVisitor {
76            type Value = Layer;
77
78            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
79                f.write_str("a mapping")
80            }
81
82            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
83            where
84                M: MapAccess<'a>,
85            {
86                let mut attributes: Option<crate::datadogV2::model::LayerAttributes> = None;
87                let mut id: Option<String> = None;
88                let mut relationships: Option<crate::datadogV2::model::LayerRelationships> = None;
89                let mut type_: Option<crate::datadogV2::model::LayerType> = None;
90                let mut additional_properties: std::collections::BTreeMap<
91                    String,
92                    serde_json::Value,
93                > = std::collections::BTreeMap::new();
94                let mut _unparsed = false;
95
96                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
97                    match k.as_str() {
98                        "attributes" => {
99                            if v.is_null() {
100                                continue;
101                            }
102                            attributes = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
103                        }
104                        "id" => {
105                            if v.is_null() {
106                                continue;
107                            }
108                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
109                        }
110                        "relationships" => {
111                            if v.is_null() {
112                                continue;
113                            }
114                            relationships =
115                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
116                        }
117                        "type" => {
118                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
119                            if let Some(ref _type_) = type_ {
120                                match _type_ {
121                                    crate::datadogV2::model::LayerType::UnparsedObject(_type_) => {
122                                        _unparsed = true;
123                                    }
124                                    _ => {}
125                                }
126                            }
127                        }
128                        &_ => {
129                            if let Ok(value) = serde_json::from_value(v.clone()) {
130                                additional_properties.insert(k, value);
131                            }
132                        }
133                    }
134                }
135                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
136
137                let content = Layer {
138                    attributes,
139                    id,
140                    relationships,
141                    type_,
142                    additional_properties,
143                    _unparsed,
144                };
145
146                Ok(content)
147            }
148        }
149
150        deserializer.deserialize_any(LayerVisitor)
151    }
152}