datadog-api-client 0.33.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 security vulnerability affecting one or more components.
#[non_exhaustive]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct CycloneDXVulnerability {
    /// External advisory references for the vulnerability.
    #[serde(rename = "advisories")]
    pub advisories: Option<Vec<crate::datadogV2::model::CycloneDXVulnerabilityAdvisory>>,
    /// The components affected by this vulnerability. Must be non-empty. Each `ref` must match a `bom-ref` in `components`.
    #[serde(rename = "affects")]
    pub affects: Vec<crate::datadogV2::model::CycloneDXVulnerabilityAffects>,
    /// The exploitability analysis for the vulnerability. When `state` is set to `resolved`
    /// or `resolved_with_pedigree`, the vulnerability is closed in Datadog.
    /// Other state values are accepted but have no effect on the vulnerability status.
    #[serde(rename = "analysis")]
    pub analysis: Option<crate::datadogV2::model::CycloneDXVulnerabilityAnalysis>,
    /// CWE identifiers associated with the vulnerability.
    #[serde(rename = "cwes")]
    pub cwes: Option<Vec<i64>>,
    /// A short description of the vulnerability.
    #[serde(rename = "description")]
    pub description: Option<String>,
    /// Detailed information about the vulnerability.
    #[serde(rename = "detail")]
    pub detail: Option<String>,
    /// The vulnerability identifier (for example, a CVE ID).
    #[serde(rename = "id")]
    pub id: String,
    /// The severity ratings for the vulnerability. Must contain exactly one element.
    #[serde(rename = "ratings")]
    pub ratings: Vec<crate::datadogV2::model::CycloneDXVulnerabilityRating>,
    /// External reference identifiers for the vulnerability.
    #[serde(rename = "references")]
    pub references: Option<Vec<crate::datadogV2::model::CycloneDXVulnerabilityReference>>,
    #[serde(flatten)]
    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
    #[serde(skip)]
    #[serde(default)]
    pub(crate) _unparsed: bool,
}

impl CycloneDXVulnerability {
    pub fn new(
        affects: Vec<crate::datadogV2::model::CycloneDXVulnerabilityAffects>,
        id: String,
        ratings: Vec<crate::datadogV2::model::CycloneDXVulnerabilityRating>,
    ) -> CycloneDXVulnerability {
        CycloneDXVulnerability {
            advisories: None,
            affects,
            analysis: None,
            cwes: None,
            description: None,
            detail: None,
            id,
            ratings,
            references: None,
            additional_properties: std::collections::BTreeMap::new(),
            _unparsed: false,
        }
    }

    pub fn advisories(
        mut self,
        value: Vec<crate::datadogV2::model::CycloneDXVulnerabilityAdvisory>,
    ) -> Self {
        self.advisories = Some(value);
        self
    }

    pub fn analysis(
        mut self,
        value: crate::datadogV2::model::CycloneDXVulnerabilityAnalysis,
    ) -> Self {
        self.analysis = Some(value);
        self
    }

    pub fn cwes(mut self, value: Vec<i64>) -> Self {
        self.cwes = Some(value);
        self
    }

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

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

    pub fn references(
        mut self,
        value: Vec<crate::datadogV2::model::CycloneDXVulnerabilityReference>,
    ) -> Self {
        self.references = 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 CycloneDXVulnerability {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CycloneDXVulnerabilityVisitor;
        impl<'a> Visitor<'a> for CycloneDXVulnerabilityVisitor {
            type Value = CycloneDXVulnerability;

            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 advisories: Option<
                    Vec<crate::datadogV2::model::CycloneDXVulnerabilityAdvisory>,
                > = None;
                let mut affects: Option<
                    Vec<crate::datadogV2::model::CycloneDXVulnerabilityAffects>,
                > = None;
                let mut analysis: Option<crate::datadogV2::model::CycloneDXVulnerabilityAnalysis> =
                    None;
                let mut cwes: Option<Vec<i64>> = None;
                let mut description: Option<String> = None;
                let mut detail: Option<String> = None;
                let mut id: Option<String> = None;
                let mut ratings: Option<
                    Vec<crate::datadogV2::model::CycloneDXVulnerabilityRating>,
                > = None;
                let mut references: Option<
                    Vec<crate::datadogV2::model::CycloneDXVulnerabilityReference>,
                > = 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() {
                        "advisories" => {
                            if v.is_null() {
                                continue;
                            }
                            advisories = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "affects" => {
                            affects = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "analysis" => {
                            if v.is_null() {
                                continue;
                            }
                            analysis = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "cwes" => {
                            if v.is_null() {
                                continue;
                            }
                            cwes = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "description" => {
                            if v.is_null() {
                                continue;
                            }
                            description =
                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "detail" => {
                            if v.is_null() {
                                continue;
                            }
                            detail = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "id" => {
                            id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "ratings" => {
                            ratings = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
                        }
                        "references" => {
                            if v.is_null() {
                                continue;
                            }
                            references = 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 affects = affects.ok_or_else(|| M::Error::missing_field("affects"))?;
                let id = id.ok_or_else(|| M::Error::missing_field("id"))?;
                let ratings = ratings.ok_or_else(|| M::Error::missing_field("ratings"))?;

                let content = CycloneDXVulnerability {
                    advisories,
                    affects,
                    analysis,
                    cwes,
                    description,
                    detail,
                    id,
                    ratings,
                    references,
                    additional_properties,
                    _unparsed,
                };

                Ok(content)
            }
        }

        deserializer.deserialize_any(CycloneDXVulnerabilityVisitor)
    }
}