use serde::de::{Error, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::skip_serializing_none;
use std::fmt::{self, Formatter};
#[non_exhaustive]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct SBOMComponent {
#[serde(rename = "bom-ref")]
pub bom_ref: Option<String>,
#[serde(rename = "licenses")]
pub licenses: Option<Vec<crate::datadogV2::model::SBOMComponentLicense>>,
#[serde(rename = "name")]
pub name: String,
#[serde(rename = "properties")]
pub properties: Option<Vec<crate::datadogV2::model::SBOMComponentProperty>>,
#[serde(rename = "purl")]
pub purl: Option<String>,
#[serde(rename = "supplier")]
pub supplier: crate::datadogV2::model::SBOMComponentSupplier,
#[serde(rename = "type")]
pub type_: crate::datadogV2::model::SBOMComponentType,
#[serde(rename = "version")]
pub version: String,
#[serde(flatten)]
pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
#[serde(skip)]
#[serde(default)]
pub(crate) _unparsed: bool,
}
impl SBOMComponent {
pub fn new(
name: String,
supplier: crate::datadogV2::model::SBOMComponentSupplier,
type_: crate::datadogV2::model::SBOMComponentType,
version: String,
) -> SBOMComponent {
SBOMComponent {
bom_ref: None,
licenses: None,
name,
properties: None,
purl: None,
supplier,
type_,
version,
additional_properties: std::collections::BTreeMap::new(),
_unparsed: false,
}
}
pub fn bom_ref(mut self, value: String) -> Self {
self.bom_ref = Some(value);
self
}
pub fn licenses(mut self, value: Vec<crate::datadogV2::model::SBOMComponentLicense>) -> Self {
self.licenses = Some(value);
self
}
pub fn properties(
mut self,
value: Vec<crate::datadogV2::model::SBOMComponentProperty>,
) -> Self {
self.properties = Some(value);
self
}
pub fn purl(mut self, value: String) -> Self {
self.purl = 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 SBOMComponent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SBOMComponentVisitor;
impl<'a> Visitor<'a> for SBOMComponentVisitor {
type Value = SBOMComponent;
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 bom_ref: Option<String> = None;
let mut licenses: Option<Vec<crate::datadogV2::model::SBOMComponentLicense>> = None;
let mut name: Option<String> = None;
let mut properties: Option<Vec<crate::datadogV2::model::SBOMComponentProperty>> =
None;
let mut purl: Option<String> = None;
let mut supplier: Option<crate::datadogV2::model::SBOMComponentSupplier> = None;
let mut type_: Option<crate::datadogV2::model::SBOMComponentType> = None;
let mut version: Option<String> = 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() {
"bom-ref" => {
if v.is_null() {
continue;
}
bom_ref = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"licenses" => {
if v.is_null() {
continue;
}
licenses = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"name" => {
name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"properties" => {
if v.is_null() {
continue;
}
properties = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"purl" => {
if v.is_null() {
continue;
}
purl = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"supplier" => {
supplier = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"type" => {
type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
if let Some(ref _type_) = type_ {
match _type_ {
crate::datadogV2::model::SBOMComponentType::UnparsedObject(
_type_,
) => {
_unparsed = true;
}
_ => {}
}
}
}
"version" => {
version = 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 name = name.ok_or_else(|| M::Error::missing_field("name"))?;
let supplier = supplier.ok_or_else(|| M::Error::missing_field("supplier"))?;
let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
let version = version.ok_or_else(|| M::Error::missing_field("version"))?;
let content = SBOMComponent {
bom_ref,
licenses,
name,
properties,
purl,
supplier,
type_,
version,
additional_properties,
_unparsed,
};
Ok(content)
}
}
deserializer.deserialize_any(SBOMComponentVisitor)
}
}