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