fhir-types 0.1.115

Generated per-version FHIR types and terminology operation contracts
Documentation
// @generated by fhir-codegen from hl7.fhir.r6.core 6.0.0-ballot5. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: Apache-2.0
//! Types and value of financial information that apply to line item(s)

/// Types and value of financial information that apply to line item(s)
///
/// Financial line items use this datatype to commonly categorize the value, and
/// other factors that may effect how the value should be interpreted.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MonetaryComponent {
    /// 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>,
    /// base | surcharge | discount | tax | informational
    ///
    /// The type of monetary component, what the value is to be used for and how
    /// that should be applied in its context. e.g. A surchange would increase the
    /// cost, a deduction would reduce the cost.
    pub r#type: super::primitives::Code,
    /// Codes may be used to differentiate between kinds of taxes, surcharges,
    /// discounts etc.
    ///
    /// A codable breakdown of the type of monetary component. e.g. State Tax,
    /// Federal Tax, VIP-Discount.
    pub code: Option<super::codeable_concept::CodeableConcept>,
    /// Factor used for calculating this component
    ///
    /// The factor that has been applied to the base price (in another monetary
    /// component value) when performing calculations.
    pub factor: Option<super::primitives::Decimal>,
    /// Explicit value amount to be used
    ///
    /// The explicit value amount of the component (based on type/code).
    pub amount: Option<super::money::Money>,
}

impl super::super::codec::Json for MonetaryComponent {
    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"),
                super::super::codec::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(super::super::codec::Value::Object(
                    super::super::codec::Json::to_json(item)?,
                ));
            }
            object.insert(
                std::string::String::from("extension"),
                super::super::codec::Value::Array(items),
            );
        }
        if let Some(v) = super::super::codec::Primitive::value_json(&self.r#type)? {
            object.insert(std::string::String::from("type"), v);
        }
        if let Some(e) = super::super::codec::Primitive::element_json(&self.r#type)? {
            object.insert(std::string::String::from("_type"), e);
        }
        if let Some(item) = &self.code {
            object.insert(
                std::string::String::from("code"),
                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        if let Some(item) = &self.factor {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("factor"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_factor"), e);
            }
        }
        if let Some(item) = &self.amount {
            object.insert(
                std::string::String::from("amount"),
                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
            );
        }
        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<&super::super::codec::Value> = None;
        let mut raw_extension: Option<&super::super::codec::Value> = None;
        let mut raw_type: Option<&super::super::codec::Value> = None;
        let mut raw_type_element: Option<&super::super::codec::Value> = None;
        let mut raw_code: Option<&super::super::codec::Value> = None;
        let mut raw_factor: Option<&super::super::codec::Value> = None;
        let mut raw_factor_element: Option<&super::super::codec::Value> = None;
        let mut raw_amount: Option<&super::super::codec::Value> = None;
        for (key, value) in object {
            match key.as_str() {
                "id" => {
                    raw_id = Some(value);
                }
                "extension" => {
                    raw_extension = Some(value);
                }
                "type" => {
                    raw_type = Some(value);
                }
                "_type" => {
                    raw_type_element = Some(value);
                }
                "code" => {
                    raw_code = Some(value);
                }
                "factor" => {
                    raw_factor = Some(value);
                }
                "_factor" => {
                    raw_factor_element = Some(value);
                }
                "amount" => {
                    raw_amount = 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::primitives::checked_string(
                        super::super::codec::expect_string(value, path)?,
                        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_type = path.with("type", |path| {
            super::super::codec::Primitive::from_json_parts(raw_type, raw_type_element, path)
        })?;
        let field_code = raw_code
            .map(|value| {
                path.with("code", |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(
                            super::super::codec::expect_single(value, path)?,
                            path,
                        )?,
                        path,
                    )
                })
            })
            .transpose()?;
        let field_factor = match (raw_factor, raw_factor_element) {
            (None, None) => None,
            (value, element) => Some(path.with("factor", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_amount = raw_amount
            .map(|value| {
                path.with("amount", |path| {
                    super::super::codec::Json::from_json(
                        super::super::codec::expect_object(
                            super::super::codec::expect_single(value, path)?,
                            path,
                        )?,
                        path,
                    )
                })
            })
            .transpose()?;
        Ok(Self {
            id: field_id,
            extension: field_extension,
            r#type: field_type,
            code: field_code,
            factor: field_factor,
            amount: field_amount,
        })
    }
}

impl serde::Serialize for MonetaryComponent {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serde::Serializer::serialize_map(serializer, None)?;
        if let Some(item) = &self.factor {
            super::super::codec::element_entry(&mut map, "_factor", item)?;
        }
        super::super::codec::element_entry(&mut map, "_type", &self.r#type)?;
        if let Some(item) = &self.amount {
            serde::ser::SerializeMap::serialize_entry(&mut map, "amount", item)?;
        }
        if let Some(item) = &self.code {
            serde::ser::SerializeMap::serialize_entry(&mut map, "code", item)?;
        }
        if !self.extension.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
        }
        if let Some(item) = &self.factor {
            super::super::codec::value_entry(&mut map, "factor", item)?;
        }
        if let Some(v) = &self.id {
            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
        }
        super::super::codec::value_entry(&mut map, "type", &self.r#type)?;
        serde::ser::SerializeMap::end(map)
    }
}

impl<'de> serde::Deserialize<'de> for MonetaryComponent {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
        let mut path = super::super::codec::Path::root("MonetaryComponent");
        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)
    }
}