datadog_api_client/datadogV2/model/
model_case.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 Case {
14 #[serde(rename = "attributes")]
16 pub attributes: crate::datadogV2::model::CaseAttributes,
17 #[serde(rename = "id")]
19 pub id: String,
20 #[serde(rename = "relationships")]
22 pub relationships: Option<crate::datadogV2::model::CaseRelationships>,
23 #[serde(rename = "type")]
25 pub type_: crate::datadogV2::model::CaseResourceType,
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 Case {
34 pub fn new(
35 attributes: crate::datadogV2::model::CaseAttributes,
36 id: String,
37 type_: crate::datadogV2::model::CaseResourceType,
38 ) -> Case {
39 Case {
40 attributes,
41 id,
42 relationships: None,
43 type_,
44 additional_properties: std::collections::BTreeMap::new(),
45 _unparsed: false,
46 }
47 }
48
49 pub fn relationships(mut self, value: crate::datadogV2::model::CaseRelationships) -> Self {
50 self.relationships = Some(value);
51 self
52 }
53
54 pub fn additional_properties(
55 mut self,
56 value: std::collections::BTreeMap<String, serde_json::Value>,
57 ) -> Self {
58 self.additional_properties = value;
59 self
60 }
61}
62
63impl<'de> Deserialize<'de> for Case {
64 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
65 where
66 D: Deserializer<'de>,
67 {
68 struct CaseVisitor;
69 impl<'a> Visitor<'a> for CaseVisitor {
70 type Value = Case;
71
72 fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
73 f.write_str("a mapping")
74 }
75
76 fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
77 where
78 M: MapAccess<'a>,
79 {
80 let mut attributes: Option<crate::datadogV2::model::CaseAttributes> = None;
81 let mut id: Option<String> = None;
82 let mut relationships: Option<crate::datadogV2::model::CaseRelationships> = None;
83 let mut type_: Option<crate::datadogV2::model::CaseResourceType> = None;
84 let mut additional_properties: std::collections::BTreeMap<
85 String,
86 serde_json::Value,
87 > = std::collections::BTreeMap::new();
88 let mut _unparsed = false;
89
90 while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
91 match k.as_str() {
92 "attributes" => {
93 attributes = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
94 }
95 "id" => {
96 id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
97 }
98 "relationships" => {
99 if v.is_null() {
100 continue;
101 }
102 relationships =
103 Some(serde_json::from_value(v).map_err(M::Error::custom)?);
104 }
105 "type" => {
106 type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
107 if let Some(ref _type_) = type_ {
108 match _type_ {
109 crate::datadogV2::model::CaseResourceType::UnparsedObject(
110 _type_,
111 ) => {
112 _unparsed = true;
113 }
114 _ => {}
115 }
116 }
117 }
118 &_ => {
119 if let Ok(value) = serde_json::from_value(v.clone()) {
120 additional_properties.insert(k, value);
121 }
122 }
123 }
124 }
125 let attributes = attributes.ok_or_else(|| M::Error::missing_field("attributes"))?;
126 let id = id.ok_or_else(|| M::Error::missing_field("id"))?;
127 let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
128
129 let content = Case {
130 attributes,
131 id,
132 relationships,
133 type_,
134 additional_properties,
135 _unparsed,
136 };
137
138 Ok(content)
139 }
140 }
141
142 deserializer.deserialize_any(CaseVisitor)
143 }
144}