#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProdCharacteristic {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub height: Option<super::quantity::Quantity>,
pub width: Option<super::quantity::Quantity>,
pub depth: Option<super::quantity::Quantity>,
pub weight: Option<super::quantity::Quantity>,
pub nominal_volume: Option<super::quantity::Quantity>,
pub external_diameter: Option<super::quantity::Quantity>,
pub shape: Option<super::primitives::String>,
pub color: Vec<super::primitives::String>,
pub imprint: Vec<super::primitives::String>,
pub image: Vec<super::attachment::Attachment>,
pub scoring: Option<super::codeable_concept::CodeableConcept>,
}
impl super::super::codec::Json for ProdCharacteristic {
fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
let mut object = super::super::codec::Object::new();
if let Some(v) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(v.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
if !self.modifier_extension.is_empty() {
let mut items = Vec::with_capacity(self.modifier_extension.len());
for item in &self.modifier_extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("modifierExtension"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.height {
object.insert(
std::string::String::from("height"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.width {
object.insert(
std::string::String::from("width"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.depth {
object.insert(
std::string::String::from("depth"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.weight {
object.insert(
std::string::String::from("weight"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.nominal_volume {
object.insert(
std::string::String::from("nominalVolume"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.external_diameter {
object.insert(
std::string::String::from("externalDiameter"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.shape {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("shape"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_shape"), e);
}
}
if !self.color.is_empty() {
let (values, elements) = super::super::codec::primitive_arrays(&self.color)?;
if let Some(values) = values {
object.insert(std::string::String::from("color"), values);
}
if let Some(elements) = elements {
object.insert(std::string::String::from("_color"), elements);
}
}
if !self.imprint.is_empty() {
let (values, elements) = super::super::codec::primitive_arrays(&self.imprint)?;
if let Some(values) = values {
object.insert(std::string::String::from("imprint"), values);
}
if let Some(elements) = elements {
object.insert(std::string::String::from("_imprint"), elements);
}
}
if !self.image.is_empty() {
let mut items = Vec::with_capacity(self.image.len());
for item in &self.image {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("image"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.scoring {
object.insert(
std::string::String::from("scoring"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
Ok(object)
}
fn from_json(
object: &super::super::codec::Object,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
let mut raw_id: Option<&super::super::codec::Value> = None;
let mut raw_extension: Option<&super::super::codec::Value> = None;
let mut raw_modifier_extension: Option<&super::super::codec::Value> = None;
let mut raw_height: Option<&super::super::codec::Value> = None;
let mut raw_width: Option<&super::super::codec::Value> = None;
let mut raw_depth: Option<&super::super::codec::Value> = None;
let mut raw_weight: Option<&super::super::codec::Value> = None;
let mut raw_nominal_volume: Option<&super::super::codec::Value> = None;
let mut raw_external_diameter: Option<&super::super::codec::Value> = None;
let mut raw_shape: Option<&super::super::codec::Value> = None;
let mut raw_shape_element: Option<&super::super::codec::Value> = None;
let mut raw_color: Option<&super::super::codec::Value> = None;
let mut raw_color_element: Option<&super::super::codec::Value> = None;
let mut raw_imprint: Option<&super::super::codec::Value> = None;
let mut raw_imprint_element: Option<&super::super::codec::Value> = None;
let mut raw_image: Option<&super::super::codec::Value> = None;
let mut raw_scoring: Option<&super::super::codec::Value> = None;
for (key, value) in object {
match key.as_str() {
"id" => {
raw_id = Some(value);
}
"extension" => {
raw_extension = Some(value);
}
"modifierExtension" => {
raw_modifier_extension = Some(value);
}
"height" => {
raw_height = Some(value);
}
"width" => {
raw_width = Some(value);
}
"depth" => {
raw_depth = Some(value);
}
"weight" => {
raw_weight = Some(value);
}
"nominalVolume" => {
raw_nominal_volume = Some(value);
}
"externalDiameter" => {
raw_external_diameter = Some(value);
}
"shape" => {
raw_shape = Some(value);
}
"_shape" => {
raw_shape_element = Some(value);
}
"color" => {
raw_color = Some(value);
}
"_color" => {
raw_color_element = Some(value);
}
"imprint" => {
raw_imprint = Some(value);
}
"_imprint" => {
raw_imprint_element = Some(value);
}
"image" => {
raw_image = Some(value);
}
"scoring" => {
raw_scoring = Some(value);
}
other => {
return path.with(other, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
let field_id = raw_id
.map(|value| {
path.with("id", |path| {
let value = super::super::codec::expect_single(value, path)?;
super::primitives::checked_string(
super::super::codec::expect_string(value, path)?,
path,
)
})
})
.transpose()?;
let mut field_extension = Vec::new();
if let Some(raw) = raw_extension {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_modifier_extension = Vec::new();
if let Some(raw) = raw_modifier_extension {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_modifier_extension.push(path.with_index(
"modifierExtension",
index,
|path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
},
)?);
}
}
let field_height = raw_height
.map(|value| {
path.with("height", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_width = raw_width
.map(|value| {
path.with("width", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_depth = raw_depth
.map(|value| {
path.with("depth", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_weight = raw_weight
.map(|value| {
path.with("weight", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_nominal_volume = raw_nominal_volume
.map(|value| {
path.with("nominalVolume", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_external_diameter = raw_external_diameter
.map(|value| {
path.with("externalDiameter", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_shape = match (raw_shape, raw_shape_element) {
(None, None) => None,
(value, element) => Some(path.with("shape", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let mut field_color = Vec::new();
for (index, (value, element)) in
super::super::codec::pair_arrays(raw_color, raw_color_element, path)?
.into_iter()
.enumerate()
{
field_color.push(path.with_index("color", index, |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?);
}
let mut field_imprint = Vec::new();
for (index, (value, element)) in
super::super::codec::pair_arrays(raw_imprint, raw_imprint_element, path)?
.into_iter()
.enumerate()
{
field_imprint.push(path.with_index("imprint", index, |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?);
}
let mut field_image = Vec::new();
if let Some(raw) = raw_image {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_image.push(path.with_index("image", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_scoring = raw_scoring
.map(|value| {
path.with("scoring", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
height: field_height,
width: field_width,
depth: field_depth,
weight: field_weight,
nominal_volume: field_nominal_volume,
external_diameter: field_external_diameter,
shape: field_shape,
color: field_color,
imprint: field_imprint,
image: field_image,
scoring: field_scoring,
})
}
}
impl serde::Serialize for ProdCharacteristic {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
super::super::codec::element_list_entry(&mut map, "_color", &self.color)?;
super::super::codec::element_list_entry(&mut map, "_imprint", &self.imprint)?;
if let Some(item) = &self.shape {
super::super::codec::element_entry(&mut map, "_shape", item)?;
}
super::super::codec::value_list_entry(&mut map, "color", &self.color)?;
if let Some(item) = &self.depth {
serde::ser::SerializeMap::serialize_entry(&mut map, "depth", item)?;
}
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(item) = &self.external_diameter {
serde::ser::SerializeMap::serialize_entry(&mut map, "externalDiameter", item)?;
}
if let Some(item) = &self.height {
serde::ser::SerializeMap::serialize_entry(&mut map, "height", item)?;
}
if let Some(v) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
}
if !self.image.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "image", &self.image)?;
}
super::super::codec::value_list_entry(&mut map, "imprint", &self.imprint)?;
if !self.modifier_extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"modifierExtension",
&self.modifier_extension,
)?;
}
if let Some(item) = &self.nominal_volume {
serde::ser::SerializeMap::serialize_entry(&mut map, "nominalVolume", item)?;
}
if let Some(item) = &self.scoring {
serde::ser::SerializeMap::serialize_entry(&mut map, "scoring", item)?;
}
if let Some(item) = &self.shape {
super::super::codec::value_entry(&mut map, "shape", item)?;
}
if let Some(item) = &self.weight {
serde::ser::SerializeMap::serialize_entry(&mut map, "weight", item)?;
}
if let Some(item) = &self.width {
serde::ser::SerializeMap::serialize_entry(&mut map, "width", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for ProdCharacteristic {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
let mut path = super::super::codec::Path::root("ProdCharacteristic");
let object =
super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
}
}