datadog_api_client/datadogV2/model/
model_role_create_attributes.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/// Attributes of the created role.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct RoleCreateAttributes {
14    /// Creation time of the role.
15    #[serde(rename = "created_at")]
16    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
17    /// Time of last role modification.
18    #[serde(rename = "modified_at")]
19    pub modified_at: Option<chrono::DateTime<chrono::Utc>>,
20    /// Name of the role.
21    #[serde(rename = "name")]
22    pub name: String,
23    #[serde(flatten)]
24    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
25    #[serde(skip)]
26    #[serde(default)]
27    pub(crate) _unparsed: bool,
28}
29
30impl RoleCreateAttributes {
31    pub fn new(name: String) -> RoleCreateAttributes {
32        RoleCreateAttributes {
33            created_at: None,
34            modified_at: None,
35            name,
36            additional_properties: std::collections::BTreeMap::new(),
37            _unparsed: false,
38        }
39    }
40
41    pub fn created_at(mut self, value: chrono::DateTime<chrono::Utc>) -> Self {
42        self.created_at = Some(value);
43        self
44    }
45
46    pub fn modified_at(mut self, value: chrono::DateTime<chrono::Utc>) -> Self {
47        self.modified_at = Some(value);
48        self
49    }
50
51    pub fn additional_properties(
52        mut self,
53        value: std::collections::BTreeMap<String, serde_json::Value>,
54    ) -> Self {
55        self.additional_properties = value;
56        self
57    }
58}
59
60impl<'de> Deserialize<'de> for RoleCreateAttributes {
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62    where
63        D: Deserializer<'de>,
64    {
65        struct RoleCreateAttributesVisitor;
66        impl<'a> Visitor<'a> for RoleCreateAttributesVisitor {
67            type Value = RoleCreateAttributes;
68
69            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
70                f.write_str("a mapping")
71            }
72
73            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
74            where
75                M: MapAccess<'a>,
76            {
77                let mut created_at: Option<chrono::DateTime<chrono::Utc>> = None;
78                let mut modified_at: Option<chrono::DateTime<chrono::Utc>> = None;
79                let mut name: Option<String> = None;
80                let mut additional_properties: std::collections::BTreeMap<
81                    String,
82                    serde_json::Value,
83                > = std::collections::BTreeMap::new();
84                let mut _unparsed = false;
85
86                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
87                    match k.as_str() {
88                        "created_at" => {
89                            if v.is_null() {
90                                continue;
91                            }
92                            created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
93                        }
94                        "modified_at" => {
95                            if v.is_null() {
96                                continue;
97                            }
98                            modified_at =
99                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
100                        }
101                        "name" => {
102                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
103                        }
104                        &_ => {
105                            if let Ok(value) = serde_json::from_value(v.clone()) {
106                                additional_properties.insert(k, value);
107                            }
108                        }
109                    }
110                }
111                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
112
113                let content = RoleCreateAttributes {
114                    created_at,
115                    modified_at,
116                    name,
117                    additional_properties,
118                    _unparsed,
119                };
120
121                Ok(content)
122            }
123        }
124
125        deserializer.deserialize_any(RoleCreateAttributesVisitor)
126    }
127}