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
//! Name of a human - parts and usage

/// Name of a human - parts and usage
///
/// A human's name with the ability to identify parts and usage.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct HumanName {
    /// 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>,
    /// usual | official | temp | nickname | anonymous | old | maiden
    ///
    /// Identifies the purpose for this name.
    pub r#use: Option<super::primitives::Code>,
    /// Text representation of the full name
    ///
    /// A full text representation of the name.
    pub text: Option<super::primitives::String>,
    /// Family name (often called 'Surname')
    ///
    /// The part of a name that links to the genealogy. In some cultures (e.g.
    /// Eritrea) the family name of a son is the first name of his father.
    pub family: Option<super::primitives::String>,
    /// Given names (not always 'first'). Includes middle names
    ///
    /// Given name.
    pub given: Vec<super::primitives::String>,
    /// Parts that come before the name
    ///
    /// Part of the name that is acquired as a title due to academic, legal,
    /// employment or nobility status, etc. and that appears at the start of the
    /// name.
    pub prefix: Vec<super::primitives::String>,
    /// Parts that come after the name
    ///
    /// Part of the name that is acquired as a title due to academic, legal,
    /// employment or nobility status, etc. and that appears at the end of the name.
    pub suffix: Vec<super::primitives::String>,
    /// Time period when name was/is in use
    ///
    /// Indicates the period of time when this name was valid for the named person.
    pub period: Option<super::period::Period>,
}

impl super::super::codec::Json for HumanName {
    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.r#use {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("use"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_use"), e);
            }
        }
        if let Some(item) = &self.text {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("text"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_text"), e);
            }
        }
        if let Some(item) = &self.family {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("family"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_family"), e);
            }
        }
        if !self.given.is_empty() {
            let (values, elements) = super::super::codec::primitive_arrays(&self.given)?;
            object.insert(std::string::String::from("given"), values);
            if let Some(elements) = elements {
                object.insert(std::string::String::from("_given"), elements);
            }
        }
        if !self.prefix.is_empty() {
            let (values, elements) = super::super::codec::primitive_arrays(&self.prefix)?;
            object.insert(std::string::String::from("prefix"), values);
            if let Some(elements) = elements {
                object.insert(std::string::String::from("_prefix"), elements);
            }
        }
        if !self.suffix.is_empty() {
            let (values, elements) = super::super::codec::primitive_arrays(&self.suffix)?;
            object.insert(std::string::String::from("suffix"), values);
            if let Some(elements) = elements {
                object.insert(std::string::String::from("_suffix"), elements);
            }
        }
        if let Some(item) = &self.period {
            object.insert(
                std::string::String::from("period"),
                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_use: Option<&super::super::codec::Value> = None;
        let mut raw_use_element: Option<&super::super::codec::Value> = None;
        let mut raw_text: Option<&super::super::codec::Value> = None;
        let mut raw_text_element: Option<&super::super::codec::Value> = None;
        let mut raw_family: Option<&super::super::codec::Value> = None;
        let mut raw_family_element: Option<&super::super::codec::Value> = None;
        let mut raw_given: Option<&super::super::codec::Value> = None;
        let mut raw_given_element: Option<&super::super::codec::Value> = None;
        let mut raw_prefix: Option<&super::super::codec::Value> = None;
        let mut raw_prefix_element: Option<&super::super::codec::Value> = None;
        let mut raw_suffix: Option<&super::super::codec::Value> = None;
        let mut raw_suffix_element: Option<&super::super::codec::Value> = None;
        let mut raw_period: 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);
                }
                "use" => {
                    raw_use = Some(value);
                }
                "_use" => {
                    raw_use_element = Some(value);
                }
                "text" => {
                    raw_text = Some(value);
                }
                "_text" => {
                    raw_text_element = Some(value);
                }
                "family" => {
                    raw_family = Some(value);
                }
                "_family" => {
                    raw_family_element = Some(value);
                }
                "given" => {
                    raw_given = Some(value);
                }
                "_given" => {
                    raw_given_element = Some(value);
                }
                "prefix" => {
                    raw_prefix = Some(value);
                }
                "_prefix" => {
                    raw_prefix_element = Some(value);
                }
                "suffix" => {
                    raw_suffix = Some(value);
                }
                "_suffix" => {
                    raw_suffix_element = Some(value);
                }
                "period" => {
                    raw_period = 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_use = match (raw_use, raw_use_element) {
            (None, None) => None,
            (value, element) => Some(path.with("use", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_text = match (raw_text, raw_text_element) {
            (None, None) => None,
            (value, element) => Some(path.with("text", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_family = match (raw_family, raw_family_element) {
            (None, None) => None,
            (value, element) => Some(path.with("family", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let mut field_given = Vec::new();
        for (index, (value, element)) in
            super::super::codec::pair_arrays(raw_given, raw_given_element, path)?
                .into_iter()
                .enumerate()
        {
            field_given.push(path.with_index("given", index, |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?);
        }
        let mut field_prefix = Vec::new();
        for (index, (value, element)) in
            super::super::codec::pair_arrays(raw_prefix, raw_prefix_element, path)?
                .into_iter()
                .enumerate()
        {
            field_prefix.push(path.with_index("prefix", index, |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?);
        }
        let mut field_suffix = Vec::new();
        for (index, (value, element)) in
            super::super::codec::pair_arrays(raw_suffix, raw_suffix_element, path)?
                .into_iter()
                .enumerate()
        {
            field_suffix.push(path.with_index("suffix", index, |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?);
        }
        let field_period = raw_period
            .map(|value| {
                path.with("period", |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#use: field_use,
            text: field_text,
            family: field_family,
            given: field_given,
            prefix: field_prefix,
            suffix: field_suffix,
            period: field_period,
        })
    }
}

impl serde::Serialize for HumanName {
    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.family {
            super::super::codec::element_entry(&mut map, "_family", item)?;
        }
        super::super::codec::element_list_entry(&mut map, "_given", &self.given)?;
        super::super::codec::element_list_entry(&mut map, "_prefix", &self.prefix)?;
        super::super::codec::element_list_entry(&mut map, "_suffix", &self.suffix)?;
        if let Some(item) = &self.text {
            super::super::codec::element_entry(&mut map, "_text", item)?;
        }
        if let Some(item) = &self.r#use {
            super::super::codec::element_entry(&mut map, "_use", item)?;
        }
        if !self.extension.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
        }
        if let Some(item) = &self.family {
            super::super::codec::value_entry(&mut map, "family", item)?;
        }
        super::super::codec::value_list_entry(&mut map, "given", &self.given)?;
        if let Some(v) = &self.id {
            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
        }
        if let Some(item) = &self.period {
            serde::ser::SerializeMap::serialize_entry(&mut map, "period", item)?;
        }
        super::super::codec::value_list_entry(&mut map, "prefix", &self.prefix)?;
        super::super::codec::value_list_entry(&mut map, "suffix", &self.suffix)?;
        if let Some(item) = &self.text {
            super::super::codec::value_entry(&mut map, "text", item)?;
        }
        if let Some(item) = &self.r#use {
            super::super::codec::value_entry(&mut map, "use", item)?;
        }
        serde::ser::SerializeMap::end(map)
    }
}

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