#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Provenance {
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 target: Vec<super::reference::Reference>,
pub occurred: Option<ProvenanceOccurred>,
pub recorded: Option<super::primitives::Instant>,
pub policy: Vec<super::primitives::Uri>,
pub location: Option<super::reference::Reference>,
pub authorization: Vec<super::codeable_concept::CodeableConcept>,
pub why: Option<super::primitives::Markdown>,
pub activity: Option<super::codeable_concept::CodeableConcept>,
pub based_on: Vec<super::reference::Reference>,
pub patient: Option<super::reference::Reference>,
pub encounter: Option<super::reference::Reference>,
pub agent: Vec<ProvenanceAgent>,
pub entity: Vec<ProvenanceEntity>,
pub signature: Vec<super::signature::Signature>,
}
impl super::super::codec::Json for Provenance {
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("Provenance")),
);
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.target.is_empty() {
let mut items = Vec::with_capacity(self.target.len());
for item in &self.target {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("target"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.occurred {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("occurred{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_occurred{suffix}"), element);
}
}
if let Some(item) = &self.recorded {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("recorded"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_recorded"), e);
}
}
if !self.policy.is_empty() {
let (values, elements) = super::super::codec::primitive_arrays(&self.policy)?;
if let Some(values) = values {
object.insert(std::string::String::from("policy"), values);
}
if let Some(elements) = elements {
object.insert(std::string::String::from("_policy"), elements);
}
}
if let Some(item) = &self.location {
object.insert(
std::string::String::from("location"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.authorization.is_empty() {
let mut items = Vec::with_capacity(self.authorization.len());
for item in &self.authorization {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("authorization"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.why {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("why"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_why"), e);
}
}
if let Some(item) = &self.activity {
object.insert(
std::string::String::from("activity"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.based_on.is_empty() {
let mut items = Vec::with_capacity(self.based_on.len());
for item in &self.based_on {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("basedOn"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.patient {
object.insert(
std::string::String::from("patient"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.encounter {
object.insert(
std::string::String::from("encounter"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.agent.is_empty() {
let mut items = Vec::with_capacity(self.agent.len());
for item in &self.agent {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("agent"),
super::super::codec::Value::Array(items),
);
}
if !self.entity.is_empty() {
let mut items = Vec::with_capacity(self.entity.len());
for item in &self.entity {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("entity"),
super::super::codec::Value::Array(items),
);
}
if !self.signature.is_empty() {
let mut items = Vec::with_capacity(self.signature.len());
for item in &self.signature {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("signature"),
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_target: Option<&super::super::codec::Value> = None;
let mut raw_occurred = super::super::codec::ChoiceSlot::default();
let mut raw_recorded: Option<&super::super::codec::Value> = None;
let mut raw_recorded_element: Option<&super::super::codec::Value> = None;
let mut raw_policy: Option<&super::super::codec::Value> = None;
let mut raw_policy_element: Option<&super::super::codec::Value> = None;
let mut raw_location: Option<&super::super::codec::Value> = None;
let mut raw_authorization: Option<&super::super::codec::Value> = None;
let mut raw_why: Option<&super::super::codec::Value> = None;
let mut raw_why_element: Option<&super::super::codec::Value> = None;
let mut raw_activity: Option<&super::super::codec::Value> = None;
let mut raw_based_on: Option<&super::super::codec::Value> = None;
let mut raw_patient: Option<&super::super::codec::Value> = None;
let mut raw_encounter: Option<&super::super::codec::Value> = None;
let mut raw_agent: Option<&super::super::codec::Value> = None;
let mut raw_entity: Option<&super::super::codec::Value> = None;
let mut raw_signature: Option<&super::super::codec::Value> = None;
for (key, value) in object {
match key.as_str() {
"resourceType" => {
if value.as_str() != Some("Provenance") {
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);
}
"target" => {
raw_target = Some(value);
}
"occurredPeriod" => {
raw_occurred.value("Period", value, path)?;
}
"_occurredPeriod" => {
raw_occurred.element("Period", value, path)?;
}
"occurredDateTime" => {
raw_occurred.value("DateTime", value, path)?;
}
"_occurredDateTime" => {
raw_occurred.element("DateTime", value, path)?;
}
"recorded" => {
raw_recorded = Some(value);
}
"_recorded" => {
raw_recorded_element = Some(value);
}
"policy" => {
raw_policy = Some(value);
}
"_policy" => {
raw_policy_element = Some(value);
}
"location" => {
raw_location = Some(value);
}
"authorization" => {
raw_authorization = Some(value);
}
"why" => {
raw_why = Some(value);
}
"_why" => {
raw_why_element = Some(value);
}
"activity" => {
raw_activity = Some(value);
}
"basedOn" => {
raw_based_on = Some(value);
}
"patient" => {
raw_patient = Some(value);
}
"encounter" => {
raw_encounter = Some(value);
}
"agent" => {
raw_agent = Some(value);
}
"entity" => {
raw_entity = Some(value);
}
"signature" => {
raw_signature = 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_id(
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_target = Vec::new();
if let Some(raw) = raw_target {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_target.push(path.with_index("target", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_occurred = match raw_occurred.suffix {
None => None,
Some(suffix) => Some(path.with("occurred", |path| {
ProvenanceOccurred::from_json_parts(
suffix,
raw_occurred.value,
raw_occurred.element,
path,
)
})?),
};
let field_recorded = match (raw_recorded, raw_recorded_element) {
(None, None) => None,
(value, element) => Some(path.with("recorded", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let mut field_policy = Vec::new();
for (index, (value, element)) in
super::super::codec::pair_arrays(raw_policy, raw_policy_element, path)?
.into_iter()
.enumerate()
{
field_policy.push(path.with_index("policy", index, |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?);
}
let field_location = raw_location
.map(|value| {
path.with("location", |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_authorization = Vec::new();
if let Some(raw) = raw_authorization {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_authorization.push(path.with_index("authorization", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_why = match (raw_why, raw_why_element) {
(None, None) => None,
(value, element) => Some(path.with("why", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_activity = raw_activity
.map(|value| {
path.with("activity", |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_based_on = Vec::new();
if let Some(raw) = raw_based_on {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_based_on.push(path.with_index("basedOn", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_patient = raw_patient
.map(|value| {
path.with("patient", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_encounter = raw_encounter
.map(|value| {
path.with("encounter", |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_agent = Vec::new();
if let Some(raw) = raw_agent {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_agent.push(path.with_index("agent", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_entity = Vec::new();
if let Some(raw) = raw_entity {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_entity.push(path.with_index("entity", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_signature = Vec::new();
if let Some(raw) = raw_signature {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_signature.push(path.with_index("signature", 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,
target: field_target,
occurred: field_occurred,
recorded: field_recorded,
policy: field_policy,
location: field_location,
authorization: field_authorization,
why: field_why,
activity: field_activity,
based_on: field_based_on,
patient: field_patient,
encounter: field_encounter,
agent: field_agent,
entity: field_entity,
signature: field_signature,
})
}
}
impl serde::Serialize for Provenance {
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(ProvenanceOccurred::DateTime(inner)) = &self.occurred {
super::super::codec::element_entry(&mut map, "_occurredDateTime", inner)?;
}
super::super::codec::element_list_entry(&mut map, "_policy", &self.policy)?;
if let Some(item) = &self.recorded {
super::super::codec::element_entry(&mut map, "_recorded", item)?;
}
if let Some(item) = &self.why {
super::super::codec::element_entry(&mut map, "_why", item)?;
}
if let Some(item) = &self.activity {
serde::ser::SerializeMap::serialize_entry(&mut map, "activity", item)?;
}
if !self.agent.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "agent", &self.agent)?;
}
if !self.authorization.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"authorization",
&self.authorization,
)?;
}
if !self.based_on.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "basedOn", &self.based_on)?;
}
if !self.contained.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "contained", &self.contained)?;
}
if let Some(item) = &self.encounter {
serde::ser::SerializeMap::serialize_entry(&mut map, "encounter", item)?;
}
if !self.entity.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "entity", &self.entity)?;
}
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 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.location {
serde::ser::SerializeMap::serialize_entry(&mut map, "location", 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,
)?;
}
match &self.occurred {
Some(ProvenanceOccurred::DateTime(inner)) => {
super::super::codec::value_entry(&mut map, "occurredDateTime", inner)?;
}
Some(ProvenanceOccurred::Period(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"occurredPeriod",
inner.as_ref(),
)?;
}
None => {}
}
if let Some(item) = &self.patient {
serde::ser::SerializeMap::serialize_entry(&mut map, "patient", item)?;
}
super::super::codec::value_list_entry(&mut map, "policy", &self.policy)?;
if let Some(item) = &self.recorded {
super::super::codec::value_entry(&mut map, "recorded", item)?;
}
serde::ser::SerializeMap::serialize_entry(&mut map, "resourceType", "Provenance")?;
if !self.signature.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "signature", &self.signature)?;
}
if !self.target.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "target", &self.target)?;
}
if let Some(item) = &self.text {
serde::ser::SerializeMap::serialize_entry(&mut map, "text", item)?;
}
if let Some(item) = &self.why {
super::super::codec::value_entry(&mut map, "why", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for Provenance {
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("Provenance");
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 ProvenanceAgent {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub r#type: Option<super::codeable_concept::CodeableConcept>,
pub role: Vec<super::codeable_concept::CodeableConcept>,
pub who: super::reference::Reference,
pub on_behalf_of: Option<super::reference::Reference>,
}
impl super::super::codec::Json for ProvenanceAgent {
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.r#type {
object.insert(
std::string::String::from("type"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if !self.role.is_empty() {
let mut items = Vec::with_capacity(self.role.len());
for item in &self.role {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("role"),
super::super::codec::Value::Array(items),
);
}
object.insert(
std::string::String::from("who"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(&self.who)?),
);
if let Some(item) = &self.on_behalf_of {
object.insert(
std::string::String::from("onBehalfOf"),
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_type: Option<&super::super::codec::Value> = None;
let mut raw_role: Option<&super::super::codec::Value> = None;
let mut raw_who: Option<&super::super::codec::Value> = None;
let mut raw_on_behalf_of: 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);
}
"type" => {
raw_type = Some(value);
}
"role" => {
raw_role = Some(value);
}
"who" => {
raw_who = Some(value);
}
"onBehalfOf" => {
raw_on_behalf_of = 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_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 mut field_role = Vec::new();
if let Some(raw) = raw_role {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_role.push(path.with_index("role", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_who = path.with("who", |path| {
let value = raw_who
.ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})?;
let field_on_behalf_of = raw_on_behalf_of
.map(|value| {
path.with("onBehalfOf", |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,
r#type: field_type,
role: field_role,
who: field_who,
on_behalf_of: field_on_behalf_of,
})
}
}
impl serde::Serialize for ProvenanceAgent {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
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.on_behalf_of {
serde::ser::SerializeMap::serialize_entry(&mut map, "onBehalfOf", item)?;
}
if !self.role.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "role", &self.role)?;
}
if let Some(item) = &self.r#type {
serde::ser::SerializeMap::serialize_entry(&mut map, "type", item)?;
}
serde::ser::SerializeMap::serialize_entry(&mut map, "who", &self.who)?;
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for ProvenanceAgent {
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("ProvenanceAgent");
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 ProvenanceEntity {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub role: super::primitives::Code,
pub what: super::reference::Reference,
pub agent: Vec<ProvenanceAgent>,
}
impl super::super::codec::Json for ProvenanceEntity {
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(v) = super::super::codec::Primitive::value_json(&self.role)? {
object.insert(std::string::String::from("role"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(&self.role)? {
object.insert(std::string::String::from("_role"), e);
}
object.insert(
std::string::String::from("what"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(&self.what)?),
);
if !self.agent.is_empty() {
let mut items = Vec::with_capacity(self.agent.len());
for item in &self.agent {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("agent"),
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_modifier_extension: Option<&super::super::codec::Value> = None;
let mut raw_role: Option<&super::super::codec::Value> = None;
let mut raw_role_element: Option<&super::super::codec::Value> = None;
let mut raw_what: Option<&super::super::codec::Value> = None;
let mut raw_agent: 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);
}
"role" => {
raw_role = Some(value);
}
"_role" => {
raw_role_element = Some(value);
}
"what" => {
raw_what = Some(value);
}
"agent" => {
raw_agent = 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_role = path.with("role", |path| {
super::super::codec::Primitive::from_json_parts(raw_role, raw_role_element, path)
})?;
let field_what = path.with("what", |path| {
let value = raw_what
.ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})?;
let mut field_agent = Vec::new();
if let Some(raw) = raw_agent {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_agent.push(path.with_index("agent", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
role: field_role,
what: field_what,
agent: field_agent,
})
}
}
impl serde::Serialize for ProvenanceEntity {
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, "_role", &self.role)?;
if !self.agent.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "agent", &self.agent)?;
}
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,
)?;
}
super::super::codec::value_entry(&mut map, "role", &self.role)?;
serde::ser::SerializeMap::serialize_entry(&mut map, "what", &self.what)?;
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for ProvenanceEntity {
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("ProvenanceEntity");
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 ProvenanceOccurred {
Period(Box<super::period::Period>),
DateTime(super::primitives::DateTime),
}
impl ProvenanceOccurred {
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::Period(inner) => Ok((
"Period",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::DateTime(inner) => Ok((
"DateTime",
super::super::codec::Primitive::value_json(inner)?,
super::super::codec::Primitive::element_json(inner)?,
)),
}
}
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 {
"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)))
}
"DateTime" => Ok(Self::DateTime(
super::super::codec::Primitive::from_json_parts(value, element, path)?,
)),
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}