datadog_api_client/datadogV2/model/
model_metrics_aggregator.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.
4
5use serde::{Deserialize, Deserializer, Serialize, Serializer};
6
7#[non_exhaustive]
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum MetricsAggregator {
10    AVG,
11    MIN,
12    MAX,
13    SUM,
14    LAST,
15    PERCENTILE,
16    MEAN,
17    L2NORM,
18    AREA,
19    UnparsedObject(crate::datadog::UnparsedObject),
20}
21
22impl ToString for MetricsAggregator {
23    fn to_string(&self) -> String {
24        match self {
25            Self::AVG => String::from("avg"),
26            Self::MIN => String::from("min"),
27            Self::MAX => String::from("max"),
28            Self::SUM => String::from("sum"),
29            Self::LAST => String::from("last"),
30            Self::PERCENTILE => String::from("percentile"),
31            Self::MEAN => String::from("mean"),
32            Self::L2NORM => String::from("l2norm"),
33            Self::AREA => String::from("area"),
34            Self::UnparsedObject(v) => v.value.to_string(),
35        }
36    }
37}
38
39impl Serialize for MetricsAggregator {
40    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
41    where
42        S: Serializer,
43    {
44        match self {
45            Self::UnparsedObject(v) => v.serialize(serializer),
46            _ => serializer.serialize_str(self.to_string().as_str()),
47        }
48    }
49}
50
51impl<'de> Deserialize<'de> for MetricsAggregator {
52    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53    where
54        D: Deserializer<'de>,
55    {
56        let s: String = String::deserialize(deserializer)?;
57        Ok(match s.as_str() {
58            "avg" => Self::AVG,
59            "min" => Self::MIN,
60            "max" => Self::MAX,
61            "sum" => Self::SUM,
62            "last" => Self::LAST,
63            "percentile" => Self::PERCENTILE,
64            "mean" => Self::MEAN,
65            "l2norm" => Self::L2NORM,
66            "area" => Self::AREA,
67            _ => Self::UnparsedObject(crate::datadog::UnparsedObject {
68                value: serde_json::Value::String(s.into()),
69            }),
70        })
71    }
72}