1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
use serde::de::{Error, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::skip_serializing_none;
use std::fmt::{self, Formatter};

/// RUM application attributes.
#[non_exhaustive]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct RUMApplicationAttributes {
    /// ID of the RUM application.
    #[serde(rename = "application_id")]
    pub application_id: String,
    /// Client token of the RUM application.
    #[serde(rename = "client_token")]
    pub client_token: String,
    /// Timestamp in ms of the creation date.
    #[serde(rename = "created_at")]
    pub created_at: i64,
    /// Handle of the creator user.
    #[serde(rename = "created_by_handle")]
    pub created_by_handle: String,
    /// Hash of the RUM application. Optional.
    #[serde(rename = "hash")]
    pub hash: Option<String>,
    /// Indicates if the RUM application is active.
    #[serde(rename = "is_active")]
    pub is_active: Option<bool>,
    /// Name of the RUM application.
    #[serde(rename = "name")]
    pub name: String,
    /// Org ID of the RUM application.
    #[serde(rename = "org_id")]
    pub org_id: i32,
    /// Type of the RUM application. Supported values are `browser`, `ios`, `android`, `react-native`, `flutter`, `roku`, `electron`, `unity`, `kotlin-multiplatform`.
    #[serde(rename = "type")]
    pub type_: String,
    /// Timestamp in ms of the last update date.
    #[serde(rename = "updated_at")]
    pub updated_at: i64,
    /// Handle of the updater user.
    #[serde(rename = "updated_by_handle")]
    pub updated_by_handle: String,
    #[serde(flatten)]
    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
    #[serde(skip)]
    #[serde(default)]
    pub(crate) _unparsed: bool,
}

impl RUMApplicationAttributes {
    pub fn new(
        application_id: String,
        client_token: String,
        created_at: i64,
        created_by_handle: String,
        name: String,
        org_id: i32,
        type_: String,
        updated_at: i64,
        updated_by_handle: String,
    ) -> RUMApplicationAttributes {
        RUMApplicationAttributes {
            application_id,
            client_token,
            created_at,
            created_by_handle,
            hash: None,
            is_active: None,
            name,
            org_id,
            type_,
            updated_at,
            updated_by_handle,
            additional_properties: std::collections::BTreeMap::new(),
            _unparsed: false,
        }
    }

    pub fn hash(mut self, value: String) -> Self {
        self.hash = Some(value);
        self
    }

    pub fn is_active(mut self, value: bool) -> Self {
        self.is_active = Some(value);
        self
    }

    pub fn additional_properties(
        mut self,
        value: std::collections::BTreeMap<String, serde_json::Value>,
    ) -> Self {
        self.additional_properties = value;
        self
    }
}

impl<'de> Deserialize<'de> for RUMApplicationAttributes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct RUMApplicationAttributesVisitor;
        impl<'a> Visitor<'a> for RUMApplicationAttributesVisitor {
            type Value = RUMApplicationAttributes;

            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
                f.write_str("a mapping")
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
            where
                M: MapAccess<'a>,
            {
                let mut application_id: Option<String> = None;
                let mut client_token: Option<String> = None;
                let mut created_at: Option<i64> = None;
                let mut created_by_handle: Option<String> = None;
                let mut hash: Option<String> = None;
                let mut is_active: Option<bool> = None;
                let mut name: Option<String> = None;
                let mut org_id: Option<i32> = None;
                let mut type_: Option<String> = None;
                let mut updated_at: Option<i64> = None;
                let mut updated_by_handle: Option<String> = None;
                let mut additional_properties: std::collections::BTreeMap<
                    String,
                    serde_json::Value,
                > = std::collections::BTreeMap::new();
                let mut _unparsed = false;

                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
                    match k.as_str() {
                        "application_id" => {
                            application_id =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "client_token" => {
                            client_token =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "created_at" => {
                            created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "created_by_handle" => {
                            created_by_handle =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "hash" => {
                            if v.is_null() {
                                continue;
                            }
                            hash = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "is_active" => {
                            if v.is_null() {
                                continue;
                            }
                            is_active = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "name" => {
                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "org_id" => {
                            org_id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "type" => {
                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "updated_at" => {
                            updated_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "updated_by_handle" => {
                            updated_by_handle =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        &_ => {
                            if let Ok(value) = serde_json::from_value(v.clone()) {
                                additional_properties.insert(k, value);
                            }
                        }
                    }
                }
                let application_id =
                    application_id.ok_or_else(|| M::Error::missing_field("application_id"))?;
                let client_token =
                    client_token.ok_or_else(|| M::Error::missing_field("client_token"))?;
                let created_at = created_at.ok_or_else(|| M::Error::missing_field("created_at"))?;
                let created_by_handle = created_by_handle
                    .ok_or_else(|| M::Error::missing_field("created_by_handle"))?;
                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
                let org_id = org_id.ok_or_else(|| M::Error::missing_field("org_id"))?;
                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
                let updated_at = updated_at.ok_or_else(|| M::Error::missing_field("updated_at"))?;
                let updated_by_handle = updated_by_handle
                    .ok_or_else(|| M::Error::missing_field("updated_by_handle"))?;

                let content = RUMApplicationAttributes {
                    application_id,
                    client_token,
                    created_at,
                    created_by_handle,
                    hash,
                    is_active,
                    name,
                    org_id,
                    type_,
                    updated_at,
                    updated_by_handle,
                    additional_properties,
                    _unparsed,
                };

                Ok(content)
            }
        }

        deserializer.deserialize_any(RUMApplicationAttributesVisitor)
    }
}