fhir-types 0.1.19

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r6.core 6.0.0-ballot5. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
//! Related artifacts for a knowledge resource

/// Related artifacts for a knowledge resource
///
/// Related artifacts such as dependencies, components, additional
/// documentation, justification, or bibliographic references.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RelatedArtifact {
    /// Unique id for inter-element referencing
    ///
    /// Unique id for the element within a resource (for internal references). This
    /// may be any string value that does not contain spaces.
    pub id: Option<std::string::String>,
    /// Additional content defined by implementations
    ///
    /// May be used to represent additional information that is not part of the
    /// basic definition of the element. To make the use of extensions safe and
    /// managable, there is a strict set of governance applied to the definition and
    /// use of extensions. Though any implementer can define an extension, there is
    /// a set of requirements that SHALL be met as part of the definition of the
    /// extension.
    pub extension: Vec<super::extension::Extension>,
    /// documentation | justification | citation | predecessor | successor |
    /// derived-from | depends-on | composed-of | part-of
    ///
    /// The type of relationship to the related artifact.
    pub r#type: super::primitives::Code,
    /// Short label
    ///
    /// A short label that can be used to reference the citation from elsewhere in
    /// the containing artifact, such as a footnote index.
    pub label: Option<super::primitives::String>,
    /// Brief description of the related artifact
    ///
    /// A brief description of the document or knowledge resource being referenced,
    /// suitable for display to a consumer.
    pub display: Option<super::primitives::String>,
    /// Bibliographic citation for the artifact
    ///
    /// A bibliographic citation for the related artifact. This text SHOULD be
    /// formatted according to an accepted citation format.
    pub citation: Option<super::primitives::Markdown>,
    /// A url for the artifact that can be followed to access the actual content.
    pub url: Option<super::primitives::Url>,
    /// What document is being referenced
    ///
    /// The document being referenced, represented as an attachment. This is
    /// exclusive with the resource element.
    pub document: Option<super::attachment::Attachment>,
    /// What artifact is being referenced
    ///
    /// The related artifact, such as a library, value set, profile, or other
    /// knowledge resource.
    pub resource: Option<super::primitives::Canonical>,
    /// What artifact, if not a conformance resource
    ///
    /// The related artifact, if the artifact is not a canonical resource, or a
    /// resource reference to a canonical resource.
    pub resource_reference: Option<super::reference::Reference>,
}

impl super::super::codec::Json for RelatedArtifact {
    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"),
                serde_json::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(serde_json::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("extension"),
                serde_json::Value::Array(items),
            );
        }
        if let Some(v) = super::super::codec::Primitive::value_json(&self.r#type)? {
            object.insert(std::string::String::from("type"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.r#type)? {
            object.insert(std::string::String::from("_type"), e);
        }
        if let Some(item) = &self.label {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("label"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_label"), e);
            }
        }
        if let Some(item) = &self.display {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("display"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_display"), e);
            }
        }
        if let Some(item) = &self.citation {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("citation"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_citation"), e);
            }
        }
        if let Some(item) = &self.url {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("url"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_url"), e);
            }
        }
        if let Some(item) = &self.document {
            object.insert(
                std::string::String::from("document"),
                serde_json::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        if let Some(item) = &self.resource {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("resource"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_resource"), e);
            }
        }
        if let Some(item) = &self.resource_reference {
            object.insert(
                std::string::String::from("resourceReference"),
                serde_json::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<&serde_json::Value> = None;
        let mut raw_extension: Option<&serde_json::Value> = None;
        let mut raw_type: Option<&serde_json::Value> = None;
        let mut raw_type_element: Option<&serde_json::Value> = None;
        let mut raw_label: Option<&serde_json::Value> = None;
        let mut raw_label_element: Option<&serde_json::Value> = None;
        let mut raw_display: Option<&serde_json::Value> = None;
        let mut raw_display_element: Option<&serde_json::Value> = None;
        let mut raw_citation: Option<&serde_json::Value> = None;
        let mut raw_citation_element: Option<&serde_json::Value> = None;
        let mut raw_url: Option<&serde_json::Value> = None;
        let mut raw_url_element: Option<&serde_json::Value> = None;
        let mut raw_document: Option<&serde_json::Value> = None;
        let mut raw_resource: Option<&serde_json::Value> = None;
        let mut raw_resource_element: Option<&serde_json::Value> = None;
        let mut raw_resource_reference: Option<&serde_json::Value> = None;
        for (key, value) in object {
            match key.as_str() {
                "id" => raw_id = Some(value),
                "extension" => raw_extension = Some(value),
                "type" => raw_type = Some(value),
                "_type" => raw_type_element = Some(value),
                "label" => raw_label = Some(value),
                "_label" => raw_label_element = Some(value),
                "display" => raw_display = Some(value),
                "_display" => raw_display_element = Some(value),
                "citation" => raw_citation = Some(value),
                "_citation" => raw_citation_element = Some(value),
                "url" => raw_url = Some(value),
                "_url" => raw_url_element = Some(value),
                "document" => raw_document = Some(value),
                "resource" => raw_resource = Some(value),
                "_resource" => raw_resource_element = Some(value),
                "resourceReference" => raw_resource_reference = 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::super::codec::expect_string(value, path)
                })
            })
            .transpose()?;
        let mut field_extension = Vec::new();
        if let Some(raw) = raw_extension {
            for (index, value) in super::super::codec::expect_array(raw, path)?
                .iter()
                .enumerate()
            {
                field_extension.push(path.with_index("extension", index, |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(value, path)?,
                        path,
                    )
                })?);
            }
        }
        let field_type = path.with("type", |path| {
            super::super::codec::Primitive::from_json_parts(raw_type, raw_type_element, path)
        })?;
        let field_label = match (raw_label, raw_label_element) {
            (None, None) => None,
            (value, element) => Some(path.with("label", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_display = match (raw_display, raw_display_element) {
            (None, None) => None,
            (value, element) => Some(path.with("display", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_citation = match (raw_citation, raw_citation_element) {
            (None, None) => None,
            (value, element) => Some(path.with("citation", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_url = match (raw_url, raw_url_element) {
            (None, None) => None,
            (value, element) => Some(path.with("url", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_document = raw_document
            .map(|value| {
                path.with("document", |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(
                            super::super::codec::expect_single(value, path)?,
                            path,
                        )?,
                        path,
                    )
                })
            })
            .transpose()?;
        let field_resource = match (raw_resource, raw_resource_element) {
            (None, None) => None,
            (value, element) => Some(path.with("resource", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_resource_reference = raw_resource_reference
            .map(|value| {
                path.with("resourceReference", |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,
            r#type: field_type,
            label: field_label,
            display: field_display,
            citation: field_citation,
            url: field_url,
            document: field_document,
            resource: field_resource,
            resource_reference: field_resource_reference,
        })
    }
}

impl serde::Serialize for RelatedArtifact {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        super::super::codec::Json::to_json(self)
            .map_err(serde::ser::Error::custom)?
            .serialize(serializer)
    }
}

impl<'de> serde::Deserialize<'de> for RelatedArtifact {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = serde_json::Value::deserialize(deserializer)?;
        let mut path = super::super::codec::Path::root("RelatedArtifact");
        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)
    }
}