datadog_api_client/datadogV2/model/
model_permission.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 Permission {
14 #[serde(rename = "attributes")]
16 pub attributes: Option<crate::datadogV2::model::PermissionAttributes>,
17 #[serde(rename = "id")]
19 pub id: Option<String>,
20 #[serde(rename = "type")]
22 pub type_: crate::datadogV2::model::PermissionsType,
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 Permission {
31 pub fn new(type_: crate::datadogV2::model::PermissionsType) -> Permission {
32 Permission {
33 attributes: None,
34 id: None,
35 type_,
36 additional_properties: std::collections::BTreeMap::new(),
37 _unparsed: false,
38 }
39 }
40
41 pub fn attributes(mut self, value: crate::datadogV2::model::PermissionAttributes) -> Self {
42 self.attributes = Some(value);
43 self
44 }
45
46 pub fn id(mut self, value: String) -> Self {
47 self.id = 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 Permission {
61 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62 where
63 D: Deserializer<'de>,
64 {
65 struct PermissionVisitor;
66 impl<'a> Visitor<'a> for PermissionVisitor {
67 type Value = Permission;
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 attributes: Option<crate::datadogV2::model::PermissionAttributes> = None;
78 let mut id: Option<String> = None;
79 let mut type_: Option<crate::datadogV2::model::PermissionsType> = 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 "attributes" => {
89 if v.is_null() {
90 continue;
91 }
92 attributes = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
93 }
94 "id" => {
95 if v.is_null() {
96 continue;
97 }
98 id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
99 }
100 "type" => {
101 type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
102 if let Some(ref _type_) = type_ {
103 match _type_ {
104 crate::datadogV2::model::PermissionsType::UnparsedObject(
105 _type_,
106 ) => {
107 _unparsed = true;
108 }
109 _ => {}
110 }
111 }
112 }
113 &_ => {
114 if let Ok(value) = serde_json::from_value(v.clone()) {
115 additional_properties.insert(k, value);
116 }
117 }
118 }
119 }
120 let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
121
122 let content = Permission {
123 attributes,
124 id,
125 type_,
126 additional_properties,
127 _unparsed,
128 };
129
130 Ok(content)
131 }
132 }
133
134 deserializer.deserialize_any(PermissionVisitor)
135 }
136}