fhir-types 0.1.102

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r4b.core 4.3.0. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
// SPDX-FileCopyrightText: Ruben Talstra
// SPDX-License-Identifier: Apache-2.0
//! Pure binary content defined by a format other than FHIR

/// Pure binary content defined by a format other than FHIR
///
/// A resource that represents the data of a single raw artifact as digital
/// content accessible in its native format. A Binary resource can contain any
/// content, whether text, image, pdf, zip archive, etc.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Binary {
    /// Logical id of this artifact
    ///
    /// The logical id of the resource, as used in the URL for the resource. Once
    /// assigned, this value never changes.
    pub id: Option<std::string::String>,
    /// Metadata about the resource
    ///
    /// The metadata about the resource. This is content that is maintained by the
    /// infrastructure. Changes to the content might not always be associated with
    /// version changes to the resource.
    pub meta: Option<super::meta::Meta>,
    /// A set of rules under which this content was created
    ///
    /// A reference to a set of rules that were followed when the resource was
    /// constructed, and which must be understood when processing the content.
    /// Often, this is a reference to an implementation guide that defines the
    /// special rules along with other profiles etc.
    pub implicit_rules: Option<super::primitives::Uri>,
    /// Language of the resource content
    ///
    /// The base language in which the resource is written.
    pub language: Option<super::primitives::Code>,
    /// MimeType of the binary content
    ///
    /// MimeType of the binary content represented as a standard MimeType (BCP 13).
    pub content_type: super::primitives::Code,
    /// Identifies another resource to use as proxy when enforcing access control
    ///
    /// This element identifies another resource that can be used as a proxy of the
    /// security sensitivity to use when deciding and enforcing access control rules
    /// for the Binary resource. Given that the Binary resource contains very few
    /// elements that can be used to determine the sensitivity of the data and
    /// relationships to individuals, the referenced resource stands in as a proxy
    /// equivalent for this purpose. This referenced resource may be related to the
    /// Binary (e.g. Media, DocumentReference), or may be some non-related Resource
    /// purely as a security proxy. E.g. to identify that the binary resource
    /// relates to a patient, and access should only be granted to applications that
    /// have access to the patient.
    pub security_context: Option<super::reference::Reference>,
    /// The actual content
    ///
    /// The actual content, base64 encoded.
    pub data: Option<super::primitives::Base64Binary>,
}

impl super::super::codec::Json for Binary {
    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("Binary")),
        );
        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(v) = super::super::codec::Primitive::value_json(&self.content_type)? {
            object.insert(std::string::String::from("contentType"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.content_type)? {
            object.insert(std::string::String::from("_contentType"), e);
        }
        if let Some(item) = &self.security_context {
            object.insert(
                std::string::String::from("securityContext"),
                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        if let Some(item) = &self.data {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("data"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_data"), e);
            }
        }
        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_content_type: Option<&super::super::codec::Value> = None;
        let mut raw_content_type_element: Option<&super::super::codec::Value> = None;
        let mut raw_security_context: Option<&super::super::codec::Value> = None;
        let mut raw_data: Option<&super::super::codec::Value> = None;
        let mut raw_data_element: Option<&super::super::codec::Value> = None;
        for (key, value) in object {
            match key.as_str() {
                "resourceType" => {
                    if value.as_str() != Some("Binary") {
                        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);
                }
                "contentType" => {
                    raw_content_type = Some(value);
                }
                "_contentType" => {
                    raw_content_type_element = Some(value);
                }
                "securityContext" => {
                    raw_security_context = Some(value);
                }
                "data" => {
                    raw_data = Some(value);
                }
                "_data" => {
                    raw_data_element = 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 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_content_type = path.with("contentType", |path| {
            super::super::codec::Primitive::from_json_parts(
                raw_content_type,
                raw_content_type_element,
                path,
            )
        })?;
        let field_security_context = raw_security_context
            .map(|value| {
                path.with("securityContext", |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(
                            super::super::codec::expect_single(value, path)?,
                            path,
                        )?,
                        path,
                    )
                })
            })
            .transpose()?;
        let field_data = match (raw_data, raw_data_element) {
            (None, None) => None,
            (value, element) => Some(path.with("data", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        Ok(Self {
            id: field_id,
            meta: field_meta,
            implicit_rules: field_implicit_rules,
            language: field_language,
            content_type: field_content_type,
            security_context: field_security_context,
            data: field_data,
        })
    }
}

impl serde::Serialize for Binary {
    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, "_contentType", &self.content_type)?;
        if let Some(item) = &self.data {
            super::super::codec::element_entry(&mut map, "_data", item)?;
        }
        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)?;
        }
        super::super::codec::value_entry(&mut map, "contentType", &self.content_type)?;
        if let Some(item) = &self.data {
            super::super::codec::value_entry(&mut map, "data", item)?;
        }
        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.meta {
            serde::ser::SerializeMap::serialize_entry(&mut map, "meta", item)?;
        }
        serde::ser::SerializeMap::serialize_entry(&mut map, "resourceType", "Binary")?;
        if let Some(item) = &self.security_context {
            serde::ser::SerializeMap::serialize_entry(&mut map, "securityContext", item)?;
        }
        serde::ser::SerializeMap::end(map)
    }
}

impl<'de> serde::Deserialize<'de> for Binary {
    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("Binary");
        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)
    }
}