datadog_api_client/datadogV2/model/
model_relation_meta.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/// Relation metadata.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct RelationMeta {
14    /// Relation creation time.
15    #[serde(rename = "createdAt")]
16    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
17    /// Relation defined by.
18    #[serde(rename = "definedBy")]
19    pub defined_by: Option<String>,
20    /// Relation modification time.
21    #[serde(rename = "modifiedAt")]
22    pub modified_at: Option<chrono::DateTime<chrono::Utc>>,
23    /// Relation source.
24    #[serde(rename = "source")]
25    pub source: Option<String>,
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 RelationMeta {
34    pub fn new() -> RelationMeta {
35        RelationMeta {
36            created_at: None,
37            defined_by: None,
38            modified_at: None,
39            source: None,
40            additional_properties: std::collections::BTreeMap::new(),
41            _unparsed: false,
42        }
43    }
44
45    pub fn created_at(mut self, value: chrono::DateTime<chrono::Utc>) -> Self {
46        self.created_at = Some(value);
47        self
48    }
49
50    pub fn defined_by(mut self, value: String) -> Self {
51        self.defined_by = Some(value);
52        self
53    }
54
55    pub fn modified_at(mut self, value: chrono::DateTime<chrono::Utc>) -> Self {
56        self.modified_at = Some(value);
57        self
58    }
59
60    pub fn source(mut self, value: String) -> Self {
61        self.source = Some(value);
62        self
63    }
64
65    pub fn additional_properties(
66        mut self,
67        value: std::collections::BTreeMap<String, serde_json::Value>,
68    ) -> Self {
69        self.additional_properties = value;
70        self
71    }
72}
73
74impl Default for RelationMeta {
75    fn default() -> Self {
76        Self::new()
77    }
78}
79
80impl<'de> Deserialize<'de> for RelationMeta {
81    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
82    where
83        D: Deserializer<'de>,
84    {
85        struct RelationMetaVisitor;
86        impl<'a> Visitor<'a> for RelationMetaVisitor {
87            type Value = RelationMeta;
88
89            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
90                f.write_str("a mapping")
91            }
92
93            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
94            where
95                M: MapAccess<'a>,
96            {
97                let mut created_at: Option<chrono::DateTime<chrono::Utc>> = None;
98                let mut defined_by: Option<String> = None;
99                let mut modified_at: Option<chrono::DateTime<chrono::Utc>> = None;
100                let mut source: Option<String> = None;
101                let mut additional_properties: std::collections::BTreeMap<
102                    String,
103                    serde_json::Value,
104                > = std::collections::BTreeMap::new();
105                let mut _unparsed = false;
106
107                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
108                    match k.as_str() {
109                        "createdAt" => {
110                            if v.is_null() {
111                                continue;
112                            }
113                            created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
114                        }
115                        "definedBy" => {
116                            if v.is_null() {
117                                continue;
118                            }
119                            defined_by = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
120                        }
121                        "modifiedAt" => {
122                            if v.is_null() {
123                                continue;
124                            }
125                            modified_at =
126                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
127                        }
128                        "source" => {
129                            if v.is_null() {
130                                continue;
131                            }
132                            source = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
133                        }
134                        &_ => {
135                            if let Ok(value) = serde_json::from_value(v.clone()) {
136                                additional_properties.insert(k, value);
137                            }
138                        }
139                    }
140                }
141
142                let content = RelationMeta {
143                    created_at,
144                    defined_by,
145                    modified_at,
146                    source,
147                    additional_properties,
148                    _unparsed,
149                };
150
151                Ok(content)
152            }
153        }
154
155        deserializer.deserialize_any(RelationMetaVisitor)
156    }
157}