fhir-types 0.1.37

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.
//! An expression that can be used to generate a value

/// An expression that can be used to generate a value
///
/// A expression that is evaluated in a specified context and returns a value.
/// The context of use of the expression must specify the context in which the
/// expression is evaluated, and how the result of the expression is used.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Expression {
    /// 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>,
    /// Natural language description of the condition
    ///
    /// A brief, natural language description of the condition that effectively
    /// communicates the intended semantics.
    pub description: Option<super::primitives::String>,
    /// Short name assigned to expression for reuse
    ///
    /// A short name assigned to the expression to allow for multiple reuse of the
    /// expression in the context where it is defined.
    pub name: Option<super::primitives::Id>,
    /// text/cql | text/fhirpath | application/x-fhir-query | etc.
    ///
    /// The media type of the language for the expression.
    pub language: super::primitives::Code,
    /// Expression in specified language
    ///
    /// An expression in the specified language that returns a value.
    pub expression: Option<super::primitives::String>,
    /// Where the expression is found
    ///
    /// A URI that defines where the expression is found.
    pub reference: Option<super::primitives::Uri>,
}

impl super::super::codec::Json for Expression {
    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.description {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("description"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_description"), e);
            }
        }
        if let Some(item) = &self.name {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("name"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_name"), e);
            }
        }
        if let Some(v) = super::super::codec::Primitive::value_json(&self.language)? {
            object.insert(std::string::String::from("language"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.language)? {
            object.insert(std::string::String::from("_language"), e);
        }
        if let Some(item) = &self.expression {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("expression"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_expression"), e);
            }
        }
        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);
            }
        }
        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_description: Option<&serde_json::Value> = None;
        let mut raw_description_element: Option<&serde_json::Value> = None;
        let mut raw_name: Option<&serde_json::Value> = None;
        let mut raw_name_element: Option<&serde_json::Value> = None;
        let mut raw_language: Option<&serde_json::Value> = None;
        let mut raw_language_element: Option<&serde_json::Value> = None;
        let mut raw_expression: Option<&serde_json::Value> = None;
        let mut raw_expression_element: Option<&serde_json::Value> = None;
        let mut raw_reference: Option<&serde_json::Value> = None;
        let mut raw_reference_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),
                "description" => raw_description = Some(value),
                "_description" => raw_description_element = Some(value),
                "name" => raw_name = Some(value),
                "_name" => raw_name_element = Some(value),
                "language" => raw_language = Some(value),
                "_language" => raw_language_element = Some(value),
                "expression" => raw_expression = Some(value),
                "_expression" => raw_expression_element = Some(value),
                "reference" => raw_reference = Some(value),
                "_reference" => raw_reference_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_description = match (raw_description, raw_description_element) {
            (None, None) => None,
            (value, element) => Some(path.with("description", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_name = match (raw_name, raw_name_element) {
            (None, None) => None,
            (value, element) => Some(path.with("name", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_language = path.with("language", |path| {
            super::super::codec::Primitive::from_json_parts(
                raw_language,
                raw_language_element,
                path,
            )
        })?;
        let field_expression = match (raw_expression, raw_expression_element) {
            (None, None) => None,
            (value, element) => Some(path.with("expression", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, 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)
            })?),
        };
        Ok(Self {
            id: field_id,
            extension: field_extension,
            description: field_description,
            name: field_name,
            language: field_language,
            expression: field_expression,
            reference: field_reference,
        })
    }
}

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