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