fhir-types 0.1.19

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r5.core 5.0.0. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
//! An identifier intended for computation

/// An identifier intended for computation
///
/// An identifier - identifies some entity uniquely and unambiguously. Typically
/// this is used for business identifiers.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Identifier {
    /// 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>,
    /// usual | official | temp | secondary | old (If known)
    ///
    /// The purpose of this identifier.
    pub r#use: Option<super::primitives::Code>,
    /// Description of identifier
    ///
    /// A coded type for the identifier that can be used to determine which
    /// identifier to use for a specific purpose.
    pub r#type: Option<super::codeable_concept::CodeableConcept>,
    /// The namespace for the identifier value
    ///
    /// Establishes the namespace for the value - that is, an absolute URL that
    /// describes a set values that are unique.
    pub system: Option<super::primitives::Uri>,
    /// The value that is unique
    ///
    /// The portion of the identifier typically relevant to the user and which is
    /// unique within the context of the system.
    pub value: Option<super::primitives::String>,
    /// Time period when id is/was valid for use
    ///
    /// Time period during which identifier is/was valid for use.
    pub period: Option<super::period::Period>,
    /// Organization that issued id (may be just text)
    ///
    /// Organization that issued/manages the identifier.
    pub assigner: Option<Box<super::reference::Reference>>,
}

impl super::super::codec::Json for Identifier {
    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(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.r#type {
            object.insert(
                std::string::String::from("type"),
                serde_json::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        if let Some(item) = &self.system {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("system"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_system"), e);
            }
        }
        if let Some(item) = &self.value {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("value"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_value"), e);
            }
        }
        if let Some(item) = &self.period {
            object.insert(
                std::string::String::from("period"),
                serde_json::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        if let Some(item) = &self.assigner {
            object.insert(
                std::string::String::from("assigner"),
                serde_json::Value::Object(super::super::codec::Json::to_json(item.as_ref())?),
            );
        }
        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_use: Option<&serde_json::Value> = None;
        let mut raw_use_element: Option<&serde_json::Value> = None;
        let mut raw_type: Option<&serde_json::Value> = None;
        let mut raw_system: Option<&serde_json::Value> = None;
        let mut raw_system_element: Option<&serde_json::Value> = None;
        let mut raw_value: Option<&serde_json::Value> = None;
        let mut raw_value_element: Option<&serde_json::Value> = None;
        let mut raw_period: Option<&serde_json::Value> = None;
        let mut raw_assigner: Option<&serde_json::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),
                "type" => raw_type = Some(value),
                "system" => raw_system = Some(value),
                "_system" => raw_system_element = Some(value),
                "value" => raw_value = Some(value),
                "_value" => raw_value_element = Some(value),
                "period" => raw_period = Some(value),
                "assigner" => raw_assigner = 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_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 field_system = match (raw_system, raw_system_element) {
            (None, None) => None,
            (value, element) => Some(path.with("system", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_value = match (raw_value, raw_value_element) {
            (None, None) => None,
            (value, element) => Some(path.with("value", |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()?;
        let field_assigner = raw_assigner
            .map(|value| {
                path.with("assigner", |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(
                            super::super::codec::expect_single(value, path)?,
                            path,
                        )?,
                        path,
                    )
                    .map(Box::new)
                })
            })
            .transpose()?;
        Ok(Self {
            id: field_id,
            extension: field_extension,
            r#use: field_use,
            r#type: field_type,
            system: field_system,
            value: field_value,
            period: field_period,
            assigner: field_assigner,
        })
    }
}

impl serde::Serialize for Identifier {
    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 Identifier {
    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("Identifier");
        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)
    }
}