fhir-types 0.1.109

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r3.core 3.0.2. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: Apache-2.0
//! Metadata about a resource

/// Metadata about a resource
///
/// The metadata about a resource. This is content in the resource that is
/// maintained by the infrastructure. Changes to the content may not always be
/// associated with version changes to the resource.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Meta {
    /// xml:id (or equivalent in JSON)
    ///
    /// 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. In order to make the use of extensions safe
    /// and manageable, there is a strict set of governance applied to the
    /// definition and use of extensions. Though any implementer is allowed to
    /// 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>,
    /// Version specific identifier
    ///
    /// The version specific identifier, as it appears in the version portion of the
    /// URL. This values changes when the resource is created, updated, or deleted.
    pub version_id: Option<super::primitives::Id>,
    /// When the resource version last changed
    ///
    /// When the resource last changed - e.g. when the version changed.
    pub last_updated: Option<super::primitives::Instant>,
    /// Profiles this resource claims to conform to
    ///
    /// A list of profiles (references to
    /// \[StructureDefinition\](structuredefinition.html\#) resources) that this
    /// resource claims to conform to. The URL is a reference to
    /// \[StructureDefinition.url\]().
    pub profile: Vec<super::primitives::Uri>,
    /// Security Labels applied to this resource
    ///
    /// Security labels applied to this resource. These tags connect specific
    /// resources to the overall security policy and infrastructure.
    pub security: Vec<super::coding::Coding>,
    /// Tags applied to this resource
    ///
    /// Tags applied to this resource. Tags are intended to be used to identify and
    /// relate resources to process and workflow, and applications are not required
    /// to consider the tags when interpreting the meaning of a resource.
    pub tag: Vec<super::coding::Coding>,
}

impl super::super::codec::Json for Meta {
    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
        let mut object = super::super::codec::Object::new();
        if let Some(v) = &self.id {
            object.insert(
                std::string::String::from("id"),
                super::super::codec::Value::String(v.clone()),
            );
        }
        if !self.extension.is_empty() {
            let mut items = Vec::with_capacity(self.extension.len());
            for item in &self.extension {
                items.push(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("extension"),
                super::super::codec::Value::Array(items),
            );
        }
        if let Some(item) = &self.version_id {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("versionId"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_versionId"), e);
            }
        }
        if let Some(item) = &self.last_updated {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("lastUpdated"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_lastUpdated"), e);
            }
        }
        if !self.profile.is_empty() {
            let (values, elements) = super::super::codec::primitive_arrays(&self.profile)?;
            object.insert(std::string::String::from("profile"), values);
            if let Some(elements) = elements {
                object.insert(std::string::String::from("_profile"), elements);
            }
        }
        if !self.security.is_empty() {
            let mut items = Vec::with_capacity(self.security.len());
            for item in &self.security {
                items.push(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("security"),
                super::super::codec::Value::Array(items),
            );
        }
        if !self.tag.is_empty() {
            let mut items = Vec::with_capacity(self.tag.len());
            for item in &self.tag {
                items.push(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("tag"),
                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_version_id: Option<&super::super::codec::Value> = None;
        let mut raw_version_id_element: Option<&super::super::codec::Value> = None;
        let mut raw_last_updated: Option<&super::super::codec::Value> = None;
        let mut raw_last_updated_element: Option<&super::super::codec::Value> = None;
        let mut raw_profile: Option<&super::super::codec::Value> = None;
        let mut raw_profile_element: Option<&super::super::codec::Value> = None;
        let mut raw_security: Option<&super::super::codec::Value> = None;
        let mut raw_tag: 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);
                }
                "versionId" => {
                    raw_version_id = Some(value);
                }
                "_versionId" => {
                    raw_version_id_element = Some(value);
                }
                "lastUpdated" => {
                    raw_last_updated = Some(value);
                }
                "_lastUpdated" => {
                    raw_last_updated_element = Some(value);
                }
                "profile" => {
                    raw_profile = Some(value);
                }
                "_profile" => {
                    raw_profile_element = Some(value);
                }
                "security" => {
                    raw_security = Some(value);
                }
                "tag" => {
                    raw_tag = 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_version_id = match (raw_version_id, raw_version_id_element) {
            (None, None) => None,
            (value, element) => Some(path.with("versionId", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_last_updated = match (raw_last_updated, raw_last_updated_element) {
            (None, None) => None,
            (value, element) => Some(path.with("lastUpdated", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let mut field_profile = Vec::new();
        for (index, (value, element)) in
            super::super::codec::pair_arrays(raw_profile, raw_profile_element, path)?
                .into_iter()
                .enumerate()
        {
            field_profile.push(path.with_index("profile", index, |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?);
        }
        let mut field_security = Vec::new();
        if let Some(raw) = raw_security {
            for (index, value) in super::super::codec::expect_array(raw, path)?
                .iter()
                .enumerate()
            {
                field_security.push(path.with_index("security", index, |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(value, path)?,
                        path,
                    )
                })?);
            }
        }
        let mut field_tag = Vec::new();
        if let Some(raw) = raw_tag {
            for (index, value) in super::super::codec::expect_array(raw, path)?
                .iter()
                .enumerate()
            {
                field_tag.push(path.with_index("tag", index, |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(value, path)?,
                        path,
                    )
                })?);
            }
        }
        Ok(Self {
            id: field_id,
            extension: field_extension,
            version_id: field_version_id,
            last_updated: field_last_updated,
            profile: field_profile,
            security: field_security,
            tag: field_tag,
        })
    }
}

impl serde::Serialize for Meta {
    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.last_updated {
            super::super::codec::element_entry(&mut map, "_lastUpdated", item)?;
        }
        super::super::codec::element_list_entry(&mut map, "_profile", &self.profile)?;
        if let Some(item) = &self.version_id {
            super::super::codec::element_entry(&mut map, "_versionId", 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 let Some(item) = &self.last_updated {
            super::super::codec::value_entry(&mut map, "lastUpdated", item)?;
        }
        super::super::codec::value_list_entry(&mut map, "profile", &self.profile)?;
        if !self.security.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "security", &self.security)?;
        }
        if !self.tag.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "tag", &self.tag)?;
        }
        if let Some(item) = &self.version_id {
            super::super::codec::value_entry(&mut map, "versionId", item)?;
        }
        serde::ser::SerializeMap::end(map)
    }
}

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