datadog_api_client/datadogV2/model/
model_component.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/// [Definition of a UI component in the app](<https://docs.datadoghq.com/service_management/app_builder/components/>)
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct Component {
14    /// Events to listen for on the UI component.
15    #[serde(rename = "events")]
16    pub events: Option<Vec<crate::datadogV2::model::AppBuilderEvent>>,
17    /// The ID of the UI component. This property is deprecated; use `name` to identify individual components instead.
18    #[serde(rename = "id", default, with = "::serde_with::rust::double_option")]
19    pub id: Option<Option<String>>,
20    /// A unique identifier for this UI component. This name is also visible in the app editor.
21    #[serde(rename = "name")]
22    pub name: String,
23    /// Properties of a UI component. Different component types can have their own additional unique properties. See the [components documentation](<https://docs.datadoghq.com/service_management/app_builder/components/>) for more detail on each component type and its properties.
24    #[serde(rename = "properties")]
25    pub properties: crate::datadogV2::model::ComponentProperties,
26    /// The UI component type.
27    #[serde(rename = "type")]
28    pub type_: crate::datadogV2::model::ComponentType,
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 Component {
37    pub fn new(
38        name: String,
39        properties: crate::datadogV2::model::ComponentProperties,
40        type_: crate::datadogV2::model::ComponentType,
41    ) -> Component {
42        Component {
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: Option<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 Component {
73    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74    where
75        D: Deserializer<'de>,
76    {
77        struct ComponentVisitor;
78        impl<'a> Visitor<'a> for ComponentVisitor {
79            type Value = Component;
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<Option<String>> = None;
91                let mut name: Option<String> = None;
92                let mut properties: Option<crate::datadogV2::model::ComponentProperties> = None;
93                let mut type_: Option<crate::datadogV2::model::ComponentType> = 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                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
110                        }
111                        "name" => {
112                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
113                        }
114                        "properties" => {
115                            properties = 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::ComponentType::UnparsedObject(
122                                        _type_,
123                                    ) => {
124                                        _unparsed = true;
125                                    }
126                                    _ => {}
127                                }
128                            }
129                        }
130                        &_ => {
131                            if let Ok(value) = serde_json::from_value(v.clone()) {
132                                additional_properties.insert(k, value);
133                            }
134                        }
135                    }
136                }
137                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
138                let properties = properties.ok_or_else(|| M::Error::missing_field("properties"))?;
139                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
140
141                let content = Component {
142                    events,
143                    id,
144                    name,
145                    properties,
146                    type_,
147                    additional_properties,
148                    _unparsed,
149                };
150
151                Ok(content)
152            }
153        }
154
155        deserializer.deserialize_any(ComponentVisitor)
156    }
157}