#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Specimen {
pub id: Option<std::string::String>,
pub meta: Option<super::meta::Meta>,
pub implicit_rules: Option<super::primitives::Uri>,
pub language: Option<super::primitives::Code>,
pub text: Option<super::narrative::Narrative>,
pub contained: Vec<super::resource::Resource>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub identifier: Vec<super::identifier::Identifier>,
pub accession_identifier: Option<super::identifier::Identifier>,
pub status: Option<super::primitives::Code>,
pub r#type: Option<super::codeable_concept::CodeableConcept>,
pub subject: Option<super::reference::Reference>,
pub received_time: Option<super::primitives::DateTime>,
pub parent: Vec<super::reference::Reference>,
pub request: Vec<super::reference::Reference>,
pub collection: Option<SpecimenCollection>,
pub processing: Vec<SpecimenProcessing>,
pub container: Vec<SpecimenContainer>,
pub condition: Vec<super::codeable_concept::CodeableConcept>,
pub note: Vec<super::annotation::Annotation>,
}
impl super::super::codec::Json for Specimen {
fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
let mut object = super::super::codec::Object::new();
object.insert(
std::string::String::from("resourceType"),
super::super::codec::Value::String(std::string::String::from("Specimen")),
);
if let Some(v) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(v.clone()),
);
}
if let Some(item) = &self.meta {
object.insert(
std::string::String::from("meta"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.implicit_rules {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("implicitRules"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_implicitRules"), e);
}
}
if let Some(item) = &self.language {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("language"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_language"), e);
}
}
if let Some(item) = &self.text {
object.insert(
std::string::String::from("text"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.contained.is_empty() {
let mut items = Vec::with_capacity(self.contained.len());
for item in &self.contained {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("contained"),
super::super::codec::Value::Array(items),
);
}
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 !self.identifier.is_empty() {
let mut items = Vec::with_capacity(self.identifier.len());
for item in &self.identifier {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("identifier"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.accession_identifier {
object.insert(
std::string::String::from("accessionIdentifier"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.status {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("status"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_status"), e);
}
}
if let Some(item) = &self.r#type {
object.insert(
std::string::String::from("type"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.subject {
object.insert(
std::string::String::from("subject"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.received_time {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("receivedTime"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_receivedTime"), e);
}
}
if !self.parent.is_empty() {
let mut items = Vec::with_capacity(self.parent.len());
for item in &self.parent {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("parent"),
super::super::codec::Value::Array(items),
);
}
if !self.request.is_empty() {
let mut items = Vec::with_capacity(self.request.len());
for item in &self.request {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("request"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.collection {
object.insert(
std::string::String::from("collection"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.processing.is_empty() {
let mut items = Vec::with_capacity(self.processing.len());
for item in &self.processing {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("processing"),
super::super::codec::Value::Array(items),
);
}
if !self.container.is_empty() {
let mut items = Vec::with_capacity(self.container.len());
for item in &self.container {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("container"),
super::super::codec::Value::Array(items),
);
}
if !self.condition.is_empty() {
let mut items = Vec::with_capacity(self.condition.len());
for item in &self.condition {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("condition"),
super::super::codec::Value::Array(items),
);
}
if !self.note.is_empty() {
let mut items = Vec::with_capacity(self.note.len());
for item in &self.note {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("note"),
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_meta: Option<&super::super::codec::Value> = None;
let mut raw_implicit_rules: Option<&super::super::codec::Value> = None;
let mut raw_implicit_rules_element: Option<&super::super::codec::Value> = None;
let mut raw_language: Option<&super::super::codec::Value> = None;
let mut raw_language_element: Option<&super::super::codec::Value> = None;
let mut raw_text: Option<&super::super::codec::Value> = None;
let mut raw_contained: 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_identifier: Option<&super::super::codec::Value> = None;
let mut raw_accession_identifier: Option<&super::super::codec::Value> = None;
let mut raw_status: Option<&super::super::codec::Value> = None;
let mut raw_status_element: Option<&super::super::codec::Value> = None;
let mut raw_type: Option<&super::super::codec::Value> = None;
let mut raw_subject: Option<&super::super::codec::Value> = None;
let mut raw_received_time: Option<&super::super::codec::Value> = None;
let mut raw_received_time_element: Option<&super::super::codec::Value> = None;
let mut raw_parent: Option<&super::super::codec::Value> = None;
let mut raw_request: Option<&super::super::codec::Value> = None;
let mut raw_collection: Option<&super::super::codec::Value> = None;
let mut raw_processing: Option<&super::super::codec::Value> = None;
let mut raw_container: Option<&super::super::codec::Value> = None;
let mut raw_condition: Option<&super::super::codec::Value> = None;
let mut raw_note: Option<&super::super::codec::Value> = None;
for (key, value) in object {
match key.as_str() {
"resourceType" => {
if value.as_str() != Some("Specimen") {
return Err(path.error(super::super::codec::DecodeErrorKind::ResourceType));
}
}
"id" => {
raw_id = Some(value);
}
"meta" => {
raw_meta = Some(value);
}
"implicitRules" => {
raw_implicit_rules = Some(value);
}
"_implicitRules" => {
raw_implicit_rules_element = Some(value);
}
"language" => {
raw_language = Some(value);
}
"_language" => {
raw_language_element = Some(value);
}
"text" => {
raw_text = Some(value);
}
"contained" => {
raw_contained = Some(value);
}
"extension" => {
raw_extension = Some(value);
}
"modifierExtension" => {
raw_modifier_extension = Some(value);
}
"identifier" => {
raw_identifier = Some(value);
}
"accessionIdentifier" => {
raw_accession_identifier = Some(value);
}
"status" => {
raw_status = Some(value);
}
"_status" => {
raw_status_element = Some(value);
}
"type" => {
raw_type = Some(value);
}
"subject" => {
raw_subject = Some(value);
}
"receivedTime" => {
raw_received_time = Some(value);
}
"_receivedTime" => {
raw_received_time_element = Some(value);
}
"parent" => {
raw_parent = Some(value);
}
"request" => {
raw_request = Some(value);
}
"collection" => {
raw_collection = Some(value);
}
"processing" => {
raw_processing = Some(value);
}
"container" => {
raw_container = Some(value);
}
"condition" => {
raw_condition = Some(value);
}
"note" => {
raw_note = 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 field_meta = raw_meta
.map(|value| {
path.with("meta", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_implicit_rules = match (raw_implicit_rules, raw_implicit_rules_element) {
(None, None) => None,
(value, element) => Some(path.with("implicitRules", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_language = match (raw_language, raw_language_element) {
(None, None) => None,
(value, element) => Some(path.with("language", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_text = raw_text
.map(|value| {
path.with("text", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let mut field_contained = Vec::new();
if let Some(raw) = raw_contained {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_contained.push(path.with_index("contained", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
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 mut field_identifier = Vec::new();
if let Some(raw) = raw_identifier {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_identifier.push(path.with_index("identifier", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_accession_identifier = raw_accession_identifier
.map(|value| {
path.with("accessionIdentifier", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_status = match (raw_status, raw_status_element) {
(None, None) => None,
(value, element) => Some(path.with("status", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_type = raw_type
.map(|value| {
path.with("type", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_subject = raw_subject
.map(|value| {
path.with("subject", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_received_time = match (raw_received_time, raw_received_time_element) {
(None, None) => None,
(value, element) => Some(path.with("receivedTime", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let mut field_parent = Vec::new();
if let Some(raw) = raw_parent {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_parent.push(path.with_index("parent", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_request = Vec::new();
if let Some(raw) = raw_request {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_request.push(path.with_index("request", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_collection = raw_collection
.map(|value| {
path.with("collection", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let mut field_processing = Vec::new();
if let Some(raw) = raw_processing {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_processing.push(path.with_index("processing", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_container = Vec::new();
if let Some(raw) = raw_container {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_container.push(path.with_index("container", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_condition = Vec::new();
if let Some(raw) = raw_condition {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_condition.push(path.with_index("condition", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_note = Vec::new();
if let Some(raw) = raw_note {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_note.push(path.with_index("note", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
Ok(Self {
id: field_id,
meta: field_meta,
implicit_rules: field_implicit_rules,
language: field_language,
text: field_text,
contained: field_contained,
extension: field_extension,
modifier_extension: field_modifier_extension,
identifier: field_identifier,
accession_identifier: field_accession_identifier,
status: field_status,
r#type: field_type,
subject: field_subject,
received_time: field_received_time,
parent: field_parent,
request: field_request,
collection: field_collection,
processing: field_processing,
container: field_container,
condition: field_condition,
note: field_note,
})
}
}
impl serde::Serialize for Specimen {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(item) = &self.implicit_rules {
super::super::codec::element_entry(&mut map, "_implicitRules", item)?;
}
if let Some(item) = &self.language {
super::super::codec::element_entry(&mut map, "_language", item)?;
}
if let Some(item) = &self.received_time {
super::super::codec::element_entry(&mut map, "_receivedTime", item)?;
}
if let Some(item) = &self.status {
super::super::codec::element_entry(&mut map, "_status", item)?;
}
if let Some(item) = &self.accession_identifier {
serde::ser::SerializeMap::serialize_entry(&mut map, "accessionIdentifier", item)?;
}
if let Some(item) = &self.collection {
serde::ser::SerializeMap::serialize_entry(&mut map, "collection", item)?;
}
if !self.condition.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "condition", &self.condition)?;
}
if !self.contained.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "contained", &self.contained)?;
}
if !self.container.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "container", &self.container)?;
}
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)?;
}
if !self.identifier.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "identifier", &self.identifier)?;
}
if let Some(item) = &self.implicit_rules {
super::super::codec::value_entry(&mut map, "implicitRules", item)?;
}
if let Some(item) = &self.language {
super::super::codec::value_entry(&mut map, "language", item)?;
}
if let Some(item) = &self.meta {
serde::ser::SerializeMap::serialize_entry(&mut map, "meta", item)?;
}
if !self.modifier_extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"modifierExtension",
&self.modifier_extension,
)?;
}
if !self.note.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "note", &self.note)?;
}
if !self.parent.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "parent", &self.parent)?;
}
if !self.processing.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "processing", &self.processing)?;
}
if let Some(item) = &self.received_time {
super::super::codec::value_entry(&mut map, "receivedTime", item)?;
}
if !self.request.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "request", &self.request)?;
}
serde::ser::SerializeMap::serialize_entry(&mut map, "resourceType", "Specimen")?;
if let Some(item) = &self.status {
super::super::codec::value_entry(&mut map, "status", item)?;
}
if let Some(item) = &self.subject {
serde::ser::SerializeMap::serialize_entry(&mut map, "subject", item)?;
}
if let Some(item) = &self.text {
serde::ser::SerializeMap::serialize_entry(&mut map, "text", item)?;
}
if let Some(item) = &self.r#type {
serde::ser::SerializeMap::serialize_entry(&mut map, "type", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for Specimen {
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("Specimen");
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)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SpecimenCollection {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub collector: Option<super::reference::Reference>,
pub collected: Option<SpecimenCollectionCollected>,
pub duration: Option<super::duration::Duration>,
pub quantity: Option<super::quantity::Quantity>,
pub method: Option<super::codeable_concept::CodeableConcept>,
pub body_site: Option<super::codeable_concept::CodeableConcept>,
pub fasting_status: Option<SpecimenCollectionFastingStatus>,
}
impl super::super::codec::Json for SpecimenCollection {
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.collector {
object.insert(
std::string::String::from("collector"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.collected {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("collected{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_collected{suffix}"), element);
}
}
if let Some(item) = &self.duration {
object.insert(
std::string::String::from("duration"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.quantity {
object.insert(
std::string::String::from("quantity"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.method {
object.insert(
std::string::String::from("method"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.body_site {
object.insert(
std::string::String::from("bodySite"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.fasting_status {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("fastingStatus{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_fastingStatus{suffix}"), element);
}
}
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_collector: Option<&super::super::codec::Value> = None;
let mut raw_collected = super::super::codec::ChoiceSlot::default();
let mut raw_duration: Option<&super::super::codec::Value> = None;
let mut raw_quantity: Option<&super::super::codec::Value> = None;
let mut raw_method: Option<&super::super::codec::Value> = None;
let mut raw_body_site: Option<&super::super::codec::Value> = None;
let mut raw_fasting_status = super::super::codec::ChoiceSlot::default();
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);
}
"collector" => {
raw_collector = Some(value);
}
"collectedDateTime" => {
raw_collected.value("DateTime", value, path)?;
}
"_collectedDateTime" => {
raw_collected.element("DateTime", value, path)?;
}
"collectedPeriod" => {
raw_collected.value("Period", value, path)?;
}
"_collectedPeriod" => {
raw_collected.element("Period", value, path)?;
}
"duration" => {
raw_duration = Some(value);
}
"quantity" => {
raw_quantity = Some(value);
}
"method" => {
raw_method = Some(value);
}
"bodySite" => {
raw_body_site = Some(value);
}
"fastingStatusCodeableConcept" => {
raw_fasting_status.value("CodeableConcept", value, path)?;
}
"_fastingStatusCodeableConcept" => {
raw_fasting_status.element("CodeableConcept", value, path)?;
}
"fastingStatusDuration" => {
raw_fasting_status.value("Duration", value, path)?;
}
"_fastingStatusDuration" => {
raw_fasting_status.element("Duration", value, path)?;
}
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_collector = raw_collector
.map(|value| {
path.with("collector", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_collected = match raw_collected.suffix {
None => None,
Some(suffix) => Some(path.with("collected", |path| {
SpecimenCollectionCollected::from_json_parts(
suffix,
raw_collected.value,
raw_collected.element,
path,
)
})?),
};
let field_duration = raw_duration
.map(|value| {
path.with("duration", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_quantity = raw_quantity
.map(|value| {
path.with("quantity", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_method = raw_method
.map(|value| {
path.with("method", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_body_site = raw_body_site
.map(|value| {
path.with("bodySite", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_fasting_status = match raw_fasting_status.suffix {
None => None,
Some(suffix) => Some(path.with("fastingStatus", |path| {
SpecimenCollectionFastingStatus::from_json_parts(
suffix,
raw_fasting_status.value,
raw_fasting_status.element,
path,
)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
collector: field_collector,
collected: field_collected,
duration: field_duration,
quantity: field_quantity,
method: field_method,
body_site: field_body_site,
fasting_status: field_fasting_status,
})
}
}
impl serde::Serialize for SpecimenCollection {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(SpecimenCollectionCollected::DateTime(inner)) = &self.collected {
super::super::codec::element_entry(&mut map, "_collectedDateTime", inner)?;
}
if let Some(item) = &self.body_site {
serde::ser::SerializeMap::serialize_entry(&mut map, "bodySite", item)?;
}
match &self.collected {
Some(SpecimenCollectionCollected::DateTime(inner)) => {
super::super::codec::value_entry(&mut map, "collectedDateTime", inner)?;
}
Some(SpecimenCollectionCollected::Period(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"collectedPeriod",
inner.as_ref(),
)?;
}
None => {}
}
if let Some(item) = &self.collector {
serde::ser::SerializeMap::serialize_entry(&mut map, "collector", item)?;
}
if let Some(item) = &self.duration {
serde::ser::SerializeMap::serialize_entry(&mut map, "duration", item)?;
}
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
match &self.fasting_status {
Some(SpecimenCollectionFastingStatus::CodeableConcept(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"fastingStatusCodeableConcept",
inner.as_ref(),
)?;
}
Some(SpecimenCollectionFastingStatus::Duration(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"fastingStatusDuration",
inner.as_ref(),
)?;
}
None => {}
}
if let Some(v) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
}
if let Some(item) = &self.method {
serde::ser::SerializeMap::serialize_entry(&mut map, "method", item)?;
}
if !self.modifier_extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"modifierExtension",
&self.modifier_extension,
)?;
}
if let Some(item) = &self.quantity {
serde::ser::SerializeMap::serialize_entry(&mut map, "quantity", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for SpecimenCollection {
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("SpecimenCollection");
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)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpecimenCollectionCollected {
DateTime(super::primitives::DateTime),
Period(Box<super::period::Period>),
}
impl SpecimenCollectionCollected {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::DateTime(inner) => Ok((
"DateTime",
super::super::codec::Primitive::value_json(inner)?,
super::super::codec::Primitive::element_json(inner)?,
)),
Self::Period(inner) => Ok((
"Period",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"DateTime" => Ok(Self::DateTime(
super::super::codec::Primitive::from_json_parts(value, element, path)?,
)),
"Period" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Period(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpecimenCollectionFastingStatus {
CodeableConcept(Box<super::codeable_concept::CodeableConcept>),
Duration(Box<super::duration::Duration>),
}
impl SpecimenCollectionFastingStatus {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::CodeableConcept(inner) => Ok((
"CodeableConcept",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::Duration(inner) => Ok((
"Duration",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"CodeableConcept" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::CodeableConcept(Box::new(inner)))
}
"Duration" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Duration(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SpecimenContainer {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub identifier: Vec<super::identifier::Identifier>,
pub description: Option<super::primitives::String>,
pub r#type: Option<super::codeable_concept::CodeableConcept>,
pub capacity: Option<super::quantity::Quantity>,
pub specimen_quantity: Option<super::quantity::Quantity>,
pub additive: Option<SpecimenContainerAdditive>,
}
impl super::super::codec::Json for SpecimenContainer {
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 !self.identifier.is_empty() {
let mut items = Vec::with_capacity(self.identifier.len());
for item in &self.identifier {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("identifier"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.description {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("description"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_description"), e);
}
}
if let Some(item) = &self.r#type {
object.insert(
std::string::String::from("type"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.capacity {
object.insert(
std::string::String::from("capacity"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.specimen_quantity {
object.insert(
std::string::String::from("specimenQuantity"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.additive {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("additive{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_additive{suffix}"), element);
}
}
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_identifier: Option<&super::super::codec::Value> = None;
let mut raw_description: Option<&super::super::codec::Value> = None;
let mut raw_description_element: Option<&super::super::codec::Value> = None;
let mut raw_type: Option<&super::super::codec::Value> = None;
let mut raw_capacity: Option<&super::super::codec::Value> = None;
let mut raw_specimen_quantity: Option<&super::super::codec::Value> = None;
let mut raw_additive = super::super::codec::ChoiceSlot::default();
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);
}
"identifier" => {
raw_identifier = Some(value);
}
"description" => {
raw_description = Some(value);
}
"_description" => {
raw_description_element = Some(value);
}
"type" => {
raw_type = Some(value);
}
"capacity" => {
raw_capacity = Some(value);
}
"specimenQuantity" => {
raw_specimen_quantity = Some(value);
}
"additiveCodeableConcept" => {
raw_additive.value("CodeableConcept", value, path)?;
}
"_additiveCodeableConcept" => {
raw_additive.element("CodeableConcept", value, path)?;
}
"additiveReference" => {
raw_additive.value("Reference", value, path)?;
}
"_additiveReference" => {
raw_additive.element("Reference", value, path)?;
}
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 mut field_identifier = Vec::new();
if let Some(raw) = raw_identifier {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_identifier.push(path.with_index("identifier", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_description = match (raw_description, raw_description_element) {
(None, None) => None,
(value, element) => Some(path.with("description", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_type = raw_type
.map(|value| {
path.with("type", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_capacity = raw_capacity
.map(|value| {
path.with("capacity", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_specimen_quantity = raw_specimen_quantity
.map(|value| {
path.with("specimenQuantity", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_additive = match raw_additive.suffix {
None => None,
Some(suffix) => Some(path.with("additive", |path| {
SpecimenContainerAdditive::from_json_parts(
suffix,
raw_additive.value,
raw_additive.element,
path,
)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
identifier: field_identifier,
description: field_description,
r#type: field_type,
capacity: field_capacity,
specimen_quantity: field_specimen_quantity,
additive: field_additive,
})
}
}
impl serde::Serialize for SpecimenContainer {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(item) = &self.description {
super::super::codec::element_entry(&mut map, "_description", item)?;
}
match &self.additive {
Some(SpecimenContainerAdditive::CodeableConcept(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"additiveCodeableConcept",
inner.as_ref(),
)?;
}
Some(SpecimenContainerAdditive::Reference(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"additiveReference",
inner.as_ref(),
)?;
}
None => {}
}
if let Some(item) = &self.capacity {
serde::ser::SerializeMap::serialize_entry(&mut map, "capacity", item)?;
}
if let Some(item) = &self.description {
super::super::codec::value_entry(&mut map, "description", item)?;
}
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)?;
}
if !self.identifier.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "identifier", &self.identifier)?;
}
if !self.modifier_extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"modifierExtension",
&self.modifier_extension,
)?;
}
if let Some(item) = &self.specimen_quantity {
serde::ser::SerializeMap::serialize_entry(&mut map, "specimenQuantity", item)?;
}
if let Some(item) = &self.r#type {
serde::ser::SerializeMap::serialize_entry(&mut map, "type", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for SpecimenContainer {
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("SpecimenContainer");
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)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpecimenContainerAdditive {
CodeableConcept(Box<super::codeable_concept::CodeableConcept>),
Reference(Box<super::reference::Reference>),
}
impl SpecimenContainerAdditive {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::CodeableConcept(inner) => Ok((
"CodeableConcept",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::Reference(inner) => Ok((
"Reference",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"CodeableConcept" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::CodeableConcept(Box::new(inner)))
}
"Reference" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Reference(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SpecimenProcessing {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub description: Option<super::primitives::String>,
pub procedure: Option<super::codeable_concept::CodeableConcept>,
pub additive: Vec<super::reference::Reference>,
pub time: Option<SpecimenProcessingTime>,
}
impl super::super::codec::Json for SpecimenProcessing {
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.description {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("description"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_description"), e);
}
}
if let Some(item) = &self.procedure {
object.insert(
std::string::String::from("procedure"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.additive.is_empty() {
let mut items = Vec::with_capacity(self.additive.len());
for item in &self.additive {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("additive"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.time {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("time{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_time{suffix}"), element);
}
}
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_description: Option<&super::super::codec::Value> = None;
let mut raw_description_element: Option<&super::super::codec::Value> = None;
let mut raw_procedure: Option<&super::super::codec::Value> = None;
let mut raw_additive: Option<&super::super::codec::Value> = None;
let mut raw_time = super::super::codec::ChoiceSlot::default();
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);
}
"description" => {
raw_description = Some(value);
}
"_description" => {
raw_description_element = Some(value);
}
"procedure" => {
raw_procedure = Some(value);
}
"additive" => {
raw_additive = Some(value);
}
"timeDateTime" => {
raw_time.value("DateTime", value, path)?;
}
"_timeDateTime" => {
raw_time.element("DateTime", value, path)?;
}
"timePeriod" => {
raw_time.value("Period", value, path)?;
}
"_timePeriod" => {
raw_time.element("Period", value, path)?;
}
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_description = match (raw_description, raw_description_element) {
(None, None) => None,
(value, element) => Some(path.with("description", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_procedure = raw_procedure
.map(|value| {
path.with("procedure", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let mut field_additive = Vec::new();
if let Some(raw) = raw_additive {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_additive.push(path.with_index("additive", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_time = match raw_time.suffix {
None => None,
Some(suffix) => Some(path.with("time", |path| {
SpecimenProcessingTime::from_json_parts(
suffix,
raw_time.value,
raw_time.element,
path,
)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
description: field_description,
procedure: field_procedure,
additive: field_additive,
time: field_time,
})
}
}
impl serde::Serialize for SpecimenProcessing {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(item) = &self.description {
super::super::codec::element_entry(&mut map, "_description", item)?;
}
if let Some(SpecimenProcessingTime::DateTime(inner)) = &self.time {
super::super::codec::element_entry(&mut map, "_timeDateTime", inner)?;
}
if !self.additive.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "additive", &self.additive)?;
}
if let Some(item) = &self.description {
super::super::codec::value_entry(&mut map, "description", item)?;
}
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)?;
}
if !self.modifier_extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"modifierExtension",
&self.modifier_extension,
)?;
}
if let Some(item) = &self.procedure {
serde::ser::SerializeMap::serialize_entry(&mut map, "procedure", item)?;
}
match &self.time {
Some(SpecimenProcessingTime::DateTime(inner)) => {
super::super::codec::value_entry(&mut map, "timeDateTime", inner)?;
}
Some(SpecimenProcessingTime::Period(inner)) => {
serde::ser::SerializeMap::serialize_entry(&mut map, "timePeriod", inner.as_ref())?;
}
None => {}
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for SpecimenProcessing {
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("SpecimenProcessing");
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)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpecimenProcessingTime {
DateTime(super::primitives::DateTime),
Period(Box<super::period::Period>),
}
impl SpecimenProcessingTime {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::DateTime(inner) => Ok((
"DateTime",
super::super::codec::Primitive::value_json(inner)?,
super::super::codec::Primitive::element_json(inner)?,
)),
Self::Period(inner) => Ok((
"Period",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"DateTime" => Ok(Self::DateTime(
super::super::codec::Primitive::from_json_parts(value, element, path)?,
)),
"Period" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Period(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}