datadog_api_client/datadogV2/model/
model_validation_error_meta.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/// Describes additional metadata for validation errors, including field names and error messages.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct ValidationErrorMeta {
14    /// The field name that caused the error.
15    #[serde(rename = "field")]
16    pub field: Option<String>,
17    /// The ID of the component in which the error occurred.
18    #[serde(rename = "id")]
19    pub id: Option<String>,
20    /// The detailed error message.
21    #[serde(rename = "message")]
22    pub message: 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 ValidationErrorMeta {
31    pub fn new(message: String) -> ValidationErrorMeta {
32        ValidationErrorMeta {
33            field: None,
34            id: None,
35            message,
36            additional_properties: std::collections::BTreeMap::new(),
37            _unparsed: false,
38        }
39    }
40
41    pub fn field(mut self, value: String) -> Self {
42        self.field = 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 ValidationErrorMeta {
61    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
62    where
63        D: Deserializer<'de>,
64    {
65        struct ValidationErrorMetaVisitor;
66        impl<'a> Visitor<'a> for ValidationErrorMetaVisitor {
67            type Value = ValidationErrorMeta;
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 field: Option<String> = None;
78                let mut id: Option<String> = None;
79                let mut message: 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                        "field" => {
89                            if v.is_null() {
90                                continue;
91                            }
92                            field = 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                        "message" => {
101                            message = 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 message = message.ok_or_else(|| M::Error::missing_field("message"))?;
111
112                let content = ValidationErrorMeta {
113                    field,
114                    id,
115                    message,
116                    additional_properties,
117                    _unparsed,
118                };
119
120                Ok(content)
121            }
122        }
123
124        deserializer.deserialize_any(ValidationErrorMetaVisitor)
125    }
126}