datadog_api_client/datadogV2/model/
model_user_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 user.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct UserCreateAttributes {
14    /// The email of the user.
15    #[serde(rename = "email")]
16    pub email: String,
17    /// The name of the user.
18    #[serde(rename = "name")]
19    pub name: Option<String>,
20    /// The title of the user.
21    #[serde(rename = "title")]
22    pub title: Option<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 UserCreateAttributes {
31    pub fn new(email: String) -> UserCreateAttributes {
32        UserCreateAttributes {
33            email,
34            name: None,
35            title: None,
36            additional_properties: std::collections::BTreeMap::new(),
37            _unparsed: false,
38        }
39    }
40
41    pub fn name(mut self, value: String) -> Self {
42        self.name = Some(value);
43        self
44    }
45
46    pub fn title(mut self, value: String) -> Self {
47        self.title = 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 UserCreateAttributes {
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62    where
63        D: Deserializer<'de>,
64    {
65        struct UserCreateAttributesVisitor;
66        impl<'a> Visitor<'a> for UserCreateAttributesVisitor {
67            type Value = UserCreateAttributes;
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 email: Option<String> = None;
78                let mut name: Option<String> = None;
79                let mut title: 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                        "email" => {
89                            email = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
90                        }
91                        "name" => {
92                            if v.is_null() {
93                                continue;
94                            }
95                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
96                        }
97                        "title" => {
98                            if v.is_null() {
99                                continue;
100                            }
101                            title = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
102                        }
103                        &_ => {
104                            if let Ok(value) = serde_json::from_value(v.clone()) {
105                                additional_properties.insert(k, value);
106                            }
107                        }
108                    }
109                }
110                let email = email.ok_or_else(|| M::Error::missing_field("email"))?;
111
112                let content = UserCreateAttributes {
113                    email,
114                    name,
115                    title,
116                    additional_properties,
117                    _unparsed,
118                };
119
120                Ok(content)
121            }
122        }
123
124        deserializer.deserialize_any(UserCreateAttributesVisitor)
125    }
126}