fhir-types 0.1.0

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.
//! A measured or measurable amount

/// A measured or measurable amount
///
/// A measured amount (or an amount that can potentially be measured). Note that
/// measured amounts include amounts that are not precisely quantified,
/// including amounts involving arbitrary units and floating currencies.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Count {
    /// 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>,
    /// Numerical value (with implicit precision)
    ///
    /// The value of the measured amount. The value includes an implicit precision
    /// in the presentation of the value.
    pub value: Option<super::primitives::Decimal>,
    /// \< | \<= | \>= | \> - how to understand the value
    ///
    /// How the value should be understood and represented - whether the actual
    /// value is greater or less than the stated value due to measurement issues;
    /// e.g. if the comparator is "\<" , then the real value is \< stated value.
    pub comparator: Option<super::primitives::Code>,
    /// Unit representation
    ///
    /// A human-readable form of the unit.
    pub unit: Option<super::primitives::String>,
    /// System that defines coded unit form
    ///
    /// The identification of the system that provides the coded form of the unit.
    pub system: Option<super::primitives::Uri>,
    /// Coded form of the unit
    ///
    /// A computer processable form of the unit in some unit representation system.
    pub code: Option<super::primitives::Code>,
}

impl super::super::codec::Json for Count {
    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.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.comparator {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("comparator"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_comparator"), e);
            }
        }
        if let Some(item) = &self.unit {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("unit"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_unit"), e);
            }
        }
        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.code {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("code"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_code"), 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_value: Option<&serde_json::Value> = None;
        let mut raw_value_element: Option<&serde_json::Value> = None;
        let mut raw_comparator: Option<&serde_json::Value> = None;
        let mut raw_comparator_element: Option<&serde_json::Value> = None;
        let mut raw_unit: Option<&serde_json::Value> = None;
        let mut raw_unit_element: 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_code: Option<&serde_json::Value> = None;
        let mut raw_code_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),
                "value" => raw_value = Some(value),
                "_value" => raw_value_element = Some(value),
                "comparator" => raw_comparator = Some(value),
                "_comparator" => raw_comparator_element = Some(value),
                "unit" => raw_unit = Some(value),
                "_unit" => raw_unit_element = Some(value),
                "system" => raw_system = Some(value),
                "_system" => raw_system_element = Some(value),
                "code" => raw_code = Some(value),
                "_code" => raw_code_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_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_comparator = match (raw_comparator, raw_comparator_element) {
            (None, None) => None,
            (value, element) => Some(path.with("comparator", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_unit = match (raw_unit, raw_unit_element) {
            (None, None) => None,
            (value, element) => Some(path.with("unit", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        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_code = match (raw_code, raw_code_element) {
            (None, None) => None,
            (value, element) => Some(path.with("code", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        Ok(Self {
            id: field_id,
            extension: field_extension,
            value: field_value,
            comparator: field_comparator,
            unit: field_unit,
            system: field_system,
            code: field_code,
        })
    }
}

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