#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Contributor {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub r#type: super::primitives::Code,
pub name: super::primitives::String,
pub contact: Vec<super::contact_detail::ContactDetail>,
}
impl super::super::codec::Json for Contributor {
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 let Some(v) = super::super::codec::Primitive::value_json(&self.r#type)? {
object.insert(std::string::String::from("type"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(&self.r#type)? {
object.insert(std::string::String::from("_type"), e);
}
if let Some(v) = super::super::codec::Primitive::value_json(&self.name)? {
object.insert(std::string::String::from("name"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(&self.name)? {
object.insert(std::string::String::from("_name"), e);
}
if !self.contact.is_empty() {
let mut items = Vec::with_capacity(self.contact.len());
for item in &self.contact {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("contact"),
super::super::codec::Value::Array(items),
);
}
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_type: Option<&super::super::codec::Value> = None;
let mut raw_type_element: Option<&super::super::codec::Value> = None;
let mut raw_name: Option<&super::super::codec::Value> = None;
let mut raw_name_element: Option<&super::super::codec::Value> = None;
let mut raw_contact: 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);
}
"type" => {
raw_type = Some(value);
}
"_type" => {
raw_type_element = Some(value);
}
"name" => {
raw_name = Some(value);
}
"_name" => {
raw_name_element = Some(value);
}
"contact" => {
raw_contact = 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 field_type = path.with("type", |path| {
super::super::codec::Primitive::from_json_parts(raw_type, raw_type_element, path)
})?;
let field_name = path.with("name", |path| {
super::super::codec::Primitive::from_json_parts(raw_name, raw_name_element, path)
})?;
let mut field_contact = Vec::new();
if let Some(raw) = raw_contact {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_contact.push(path.with_index("contact", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
Ok(Self {
id: field_id,
extension: field_extension,
r#type: field_type,
name: field_name,
contact: field_contact,
})
}
}
impl serde::Serialize for Contributor {
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_entry(&mut map, "_name", &self.name)?;
super::super::codec::element_entry(&mut map, "_type", &self.r#type)?;
if !self.contact.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "contact", &self.contact)?;
}
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(v) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
}
super::super::codec::value_entry(&mut map, "name", &self.name)?;
super::super::codec::value_entry(&mut map, "type", &self.r#type)?;
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for Contributor {
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("Contributor");
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)
}
}