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 AnnotationAttributes {
#[serde(rename = "author_id")]
pub author_id: String,
#[serde(rename = "color")]
pub color: crate::datadogV2::model::AnnotationColor,
#[serde(rename = "created_at")]
pub created_at: i64,
#[serde(rename = "description")]
pub description: String,
#[serialize_always]
#[serde(rename = "end_time")]
pub end_time: Option<i64>,
#[serde(rename = "modified_at")]
pub modified_at: i64,
#[serde(rename = "page_id")]
pub page_id: String,
#[serde(rename = "start_time")]
pub start_time: i64,
#[serde(rename = "type")]
pub type_: crate::datadogV2::model::AnnotationKind,
#[serde(rename = "widget_ids")]
pub widget_ids: Option<Vec<String>>,
#[serde(flatten)]
pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
#[serde(skip)]
#[serde(default)]
pub(crate) _unparsed: bool,
}
impl AnnotationAttributes {
pub fn new(
author_id: String,
color: crate::datadogV2::model::AnnotationColor,
created_at: i64,
description: String,
end_time: Option<i64>,
modified_at: i64,
page_id: String,
start_time: i64,
type_: crate::datadogV2::model::AnnotationKind,
) -> AnnotationAttributes {
AnnotationAttributes {
author_id,
color,
created_at,
description,
end_time,
modified_at,
page_id,
start_time,
type_,
widget_ids: None,
additional_properties: std::collections::BTreeMap::new(),
_unparsed: false,
}
}
pub fn widget_ids(mut self, value: Vec<String>) -> Self {
self.widget_ids = 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 AnnotationAttributes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AnnotationAttributesVisitor;
impl<'a> Visitor<'a> for AnnotationAttributesVisitor {
type Value = AnnotationAttributes;
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 author_id: Option<String> = None;
let mut color: Option<crate::datadogV2::model::AnnotationColor> = None;
let mut created_at: Option<i64> = None;
let mut description: Option<String> = None;
let mut end_time: Option<Option<i64>> = None;
let mut modified_at: Option<i64> = None;
let mut page_id: Option<String> = None;
let mut start_time: Option<i64> = None;
let mut type_: Option<crate::datadogV2::model::AnnotationKind> = None;
let mut widget_ids: Option<Vec<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() {
"author_id" => {
author_id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"color" => {
color = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
if let Some(ref _color) = color {
match _color {
crate::datadogV2::model::AnnotationColor::UnparsedObject(
_color,
) => {
_unparsed = true;
}
_ => {}
}
}
}
"created_at" => {
created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"description" => {
description =
Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"end_time" => {
end_time = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"modified_at" => {
modified_at =
Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"page_id" => {
page_id = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
}
"start_time" => {
start_time = 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::AnnotationKind::UnparsedObject(
_type_,
) => {
_unparsed = true;
}
_ => {}
}
}
}
"widget_ids" => {
if v.is_null() {
continue;
}
widget_ids = 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 author_id = author_id.ok_or_else(|| M::Error::missing_field("author_id"))?;
let color = color.ok_or_else(|| M::Error::missing_field("color"))?;
let created_at = created_at.ok_or_else(|| M::Error::missing_field("created_at"))?;
let description =
description.ok_or_else(|| M::Error::missing_field("description"))?;
let end_time = end_time.ok_or_else(|| M::Error::missing_field("end_time"))?;
let modified_at =
modified_at.ok_or_else(|| M::Error::missing_field("modified_at"))?;
let page_id = page_id.ok_or_else(|| M::Error::missing_field("page_id"))?;
let start_time = start_time.ok_or_else(|| M::Error::missing_field("start_time"))?;
let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
let content = AnnotationAttributes {
author_id,
color,
created_at,
description,
end_time,
modified_at,
page_id,
start_time,
type_,
widget_ids,
additional_properties,
_unparsed,
};
Ok(content)
}
}
deserializer.deserialize_any(AnnotationAttributesVisitor)
}
}