fhir-types 0.1.19

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r4.core 4.0.1. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
//! Human-readable summary of the resource (essential clinical and business information)

/// Human-readable summary of the resource (essential clinical and business
/// information)
///
/// A human-readable summary of the resource conveying the essential clinical
/// and business information for the resource.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Narrative {
    /// 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>,
    /// generated | extensions | additional | empty
    ///
    /// The status of the narrative - whether it's entirely generated (from just the
    /// defined data or the extensions too), or whether a human authored it and it
    /// may contain additional data.
    pub status: super::primitives::Code,
    /// Limited xhtml content
    ///
    /// The actual narrative content, a stripped down version of XHTML.
    pub div: super::primitives::Xhtml,
}

impl super::super::codec::Json for Narrative {
    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(v) = super::super::codec::Primitive::value_json(&self.status)? {
            object.insert(std::string::String::from("status"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.status)? {
            object.insert(std::string::String::from("_status"), e);
        }
        if let Some(v) = super::super::codec::Primitive::value_json(&self.div)? {
            object.insert(std::string::String::from("div"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.div)? {
            object.insert(std::string::String::from("_div"), 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_status: Option<&serde_json::Value> = None;
        let mut raw_status_element: Option<&serde_json::Value> = None;
        let mut raw_div: Option<&serde_json::Value> = None;
        let mut raw_div_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),
                "status" => raw_status = Some(value),
                "_status" => raw_status_element = Some(value),
                "div" => raw_div = Some(value),
                "_div" => raw_div_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_status = path.with("status", |path| {
            super::super::codec::Primitive::from_json_parts(raw_status, raw_status_element, path)
        })?;
        let field_div = path.with("div", |path| {
            super::super::codec::Primitive::from_json_parts(raw_div, raw_div_element, path)
        })?;
        Ok(Self {
            id: field_id,
            extension: field_extension,
            status: field_status,
            div: field_div,
        })
    }
}

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