datadog-api-client 0.34.0

Rust client for the Datadog API.
// 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};

/// A composite rule combining several predicates. Used as an alternative to `nodes` on a journey
/// step when several conditions must be matched together, in any order or in a specific order.
#[non_exhaustive]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct RUMOperationJourneyCompositeRule {
    /// The unique identifier of the composite rule. Generated by the server if omitted.
    #[serde(rename = "composite_rule_id")]
    pub composite_rule_id: Option<String>,
    /// A hash of the composite rule's configuration, computed by the server.
    #[serde(rename = "config_version")]
    pub config_version: Option<String>,
    /// The rule used to combine the composite rule's predicates. `all_of` requires every predicate
    /// to match, in any order. `in_order` requires every predicate to match in the given order.
    #[serde(rename = "kind")]
    pub kind: crate::datadogV2::model::RUMOperationJourneyCompositeRuleKind,
    /// The maximum time window, in milliseconds, in which all predicates must match.
    #[serde(rename = "max_window_ms")]
    pub max_window_ms: Option<i64>,
    /// The list of predicates that must be matched by RUM events.
    #[serde(rename = "predicates")]
    pub predicates: Vec<crate::datadogV2::model::RUMOperationJourneyPredicate>,
    #[serde(flatten)]
    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
    #[serde(skip)]
    #[serde(default)]
    pub(crate) _unparsed: bool,
}

impl RUMOperationJourneyCompositeRule {
    pub fn new(
        kind: crate::datadogV2::model::RUMOperationJourneyCompositeRuleKind,
        predicates: Vec<crate::datadogV2::model::RUMOperationJourneyPredicate>,
    ) -> RUMOperationJourneyCompositeRule {
        RUMOperationJourneyCompositeRule {
            composite_rule_id: None,
            config_version: None,
            kind,
            max_window_ms: None,
            predicates,
            additional_properties: std::collections::BTreeMap::new(),
            _unparsed: false,
        }
    }

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

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

    pub fn max_window_ms(mut self, value: i64) -> Self {
        self.max_window_ms = 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 RUMOperationJourneyCompositeRule {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct RUMOperationJourneyCompositeRuleVisitor;
        impl<'a> Visitor<'a> for RUMOperationJourneyCompositeRuleVisitor {
            type Value = RUMOperationJourneyCompositeRule;

            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 composite_rule_id: Option<String> = None;
                let mut config_version: Option<String> = None;
                let mut kind: Option<
                    crate::datadogV2::model::RUMOperationJourneyCompositeRuleKind,
                > = None;
                let mut max_window_ms: Option<i64> = None;
                let mut predicates: Option<
                    Vec<crate::datadogV2::model::RUMOperationJourneyPredicate>,
                > = 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() {
                        "composite_rule_id" => {
                            if v.is_null() {
                                continue;
                            }
                            composite_rule_id =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "config_version" => {
                            if v.is_null() {
                                continue;
                            }
                            config_version =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "kind" => {
                            kind = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                            if let Some(ref _kind) = kind {
                                match _kind {
                                    crate::datadogV2::model::RUMOperationJourneyCompositeRuleKind::UnparsedObject(_kind) => {
                                        _unparsed = true;
                                    },
                                    _ => {}
                                }
                            }
                        }
                        "max_window_ms" => {
                            if v.is_null() {
                                continue;
                            }
                            max_window_ms =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "predicates" => {
                            predicates = 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 kind = kind.ok_or_else(|| M::Error::missing_field("kind"))?;
                let predicates = predicates.ok_or_else(|| M::Error::missing_field("predicates"))?;

                let content = RUMOperationJourneyCompositeRule {
                    composite_rule_id,
                    config_version,
                    kind,
                    max_window_ms,
                    predicates,
                    additional_properties,
                    _unparsed,
                };

                Ok(content)
            }
        }

        deserializer.deserialize_any(RUMOperationJourneyCompositeRuleVisitor)
    }
}