fhir-types 0.1.19

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.
//! A reference from one resource to another

/// A reference from one resource to another.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Reference {
    /// 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
    /// manageable, 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>,
    /// Literal reference, Relative, internal or absolute URL
    ///
    /// A reference to a location at which the other resource is found. The
    /// reference may be a relative reference, in which case it is relative to the
    /// service base URL, or an absolute URL that resolves to the location where the
    /// resource is found. The reference may be version specific or not. If the
    /// reference is not to a FHIR RESTful server, then it should be assumed to be
    /// version specific. Internal fragment references (start with '\#') refer to
    /// contained resources.
    pub reference: Option<super::primitives::String>,
    /// Type the reference refers to (e.g. "Patient")
    ///
    /// The expected type of the target of the reference. If both Reference.type and
    /// Reference.reference are populated and Reference.reference is a FHIR URL,
    /// both SHALL be consistent. The type is the Canonical URL of Resource
    /// Definition that is the type this reference refers to. References are URLs
    /// that are relative to <http://hl7.org/fhir/StructureDefinition/> e.g.
    /// "Patient" is a reference to
    /// <http://hl7.org/fhir/StructureDefinition/Patient>. Absolute URLs are only
    /// allowed for logical models (and can only be used in references in logical
    /// models, not resources).
    pub r#type: Option<super::primitives::Uri>,
    /// Logical reference, when literal reference is not known
    ///
    /// An identifier for the target resource. This is used when there is no way to
    /// reference the other resource directly, either because the entity it
    /// represents is not available through a FHIR server, or because there is no
    /// way for the author of the resource to convert a known identifier to an
    /// actual location. There is no requirement that a Reference.identifier point
    /// to something that is actually exposed as a FHIR instance, but it SHALL point
    /// to a business concept that would be expected to be exposed as a FHIR
    /// instance, and that instance would need to be of a FHIR resource type allowed
    /// by the reference.
    pub identifier: Option<Box<super::identifier::Identifier>>,
    /// Text alternative for the resource
    ///
    /// Plain text narrative that identifies the resource in addition to the
    /// resource reference.
    pub display: Option<super::primitives::String>,
}

impl super::super::codec::Json for Reference {
    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.reference {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("reference"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_reference"), e);
            }
        }
        if let Some(item) = &self.r#type {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("type"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_type"), e);
            }
        }
        if let Some(item) = &self.identifier {
            object.insert(
                std::string::String::from("identifier"),
                serde_json::Value::Object(super::super::codec::Json::to_json(item.as_ref())?),
            );
        }
        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);
            }
        }
        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_reference: Option<&serde_json::Value> = None;
        let mut raw_reference_element: 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_identifier: Option<&serde_json::Value> = None;
        let mut raw_display: Option<&serde_json::Value> = None;
        let mut raw_display_element: Option<&serde_json::Value> = None;
        for (key, value) in object {
            match key.as_str() {
                "id" => raw_id = Some(value),
                "extension" => raw_extension = Some(value),
                "reference" => raw_reference = Some(value),
                "_reference" => raw_reference_element = Some(value),
                "type" => raw_type = Some(value),
                "_type" => raw_type_element = Some(value),
                "identifier" => raw_identifier = Some(value),
                "display" => raw_display = Some(value),
                "_display" => raw_display_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 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_reference = match (raw_reference, raw_reference_element) {
            (None, None) => None,
            (value, element) => Some(path.with("reference", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_type = match (raw_type, raw_type_element) {
            (None, None) => None,
            (value, element) => Some(path.with("type", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_identifier = raw_identifier
            .map(|value| {
                path.with("identifier", |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()?;
        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)
            })?),
        };
        Ok(Self {
            id: field_id,
            extension: field_extension,
            reference: field_reference,
            r#type: field_type,
            identifier: field_identifier,
            display: field_display,
        })
    }
}

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