Skip to main content

fhir_types/r5/
codeable_reference.rs

1// @generated by fhir-codegen from hl7.fhir.r5.core 5.0.0. DO NOT EDIT.
2// Change the emitter (tools/fhir-codegen) and regenerate.
3// SPDX-FileCopyrightText: Cadasto B.V.
4// SPDX-License-Identifier: Apache-2.0
5//! Reference to a resource or a concept
6
7/// Reference to a resource or a concept
8///
9/// A reference to a resource (by instance), or instead, a reference to a
10/// concept defined in a terminology or ontology (by class).
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct CodeableReference {
13    /// Unique id for inter-element referencing
14    ///
15    /// Unique id for the element within a resource (for internal references). This
16    /// may be any string value that does not contain spaces.
17    pub id: Option<std::string::String>,
18    /// Additional content defined by implementations
19    ///
20    /// May be used to represent additional information that is not part of the
21    /// basic definition of the element. To make the use of extensions safe and
22    /// managable, there is a strict set of governance applied to the definition and
23    /// use of extensions. Though any implementer can define an extension, there is
24    /// a set of requirements that SHALL be met as part of the definition of the
25    /// extension.
26    pub extension: Vec<super::extension::Extension>,
27    /// Reference to a concept (by class)
28    ///
29    /// A reference to a concept - e.g. the information is identified by its general
30    /// class to the degree of precision found in the terminology.
31    pub concept: Option<super::codeable_concept::CodeableConcept>,
32    /// Reference to a resource (by instance)
33    ///
34    /// A reference to a resource the provides exact details about the information
35    /// being referenced.
36    pub reference: Option<super::reference::Reference>,
37}
38
39impl super::super::codec::Json for CodeableReference {
40    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
41        let mut object = super::super::codec::Object::new();
42        if let Some(v) = &self.id {
43            object.insert(
44                std::string::String::from("id"),
45                super::super::codec::Value::String(v.clone()),
46            );
47        }
48        if !self.extension.is_empty() {
49            let mut items = Vec::with_capacity(self.extension.len());
50            for item in &self.extension {
51                items.push(super::super::codec::Value::Object(
52                    super::super::codec::Json::to_json(item)?,
53                ));
54            }
55            object.insert(
56                std::string::String::from("extension"),
57                super::super::codec::Value::Array(items),
58            );
59        }
60        if let Some(item) = &self.concept {
61            object.insert(
62                std::string::String::from("concept"),
63                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
64            );
65        }
66        if let Some(item) = &self.reference {
67            object.insert(
68                std::string::String::from("reference"),
69                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
70            );
71        }
72        Ok(object)
73    }
74
75    fn from_json(
76        object: &super::super::codec::Object,
77        path: &mut super::super::codec::Path,
78    ) -> Result<Self, super::super::codec::DecodeError> {
79        let mut raw_id: Option<&super::super::codec::Value> = None;
80        let mut raw_extension: Option<&super::super::codec::Value> = None;
81        let mut raw_concept: Option<&super::super::codec::Value> = None;
82        let mut raw_reference: Option<&super::super::codec::Value> = None;
83        for (key, value) in object {
84            match key.as_str() {
85                "id" => {
86                    raw_id = Some(value);
87                }
88                "extension" => {
89                    raw_extension = Some(value);
90                }
91                "concept" => {
92                    raw_concept = Some(value);
93                }
94                "reference" => {
95                    raw_reference = Some(value);
96                }
97                other => {
98                    return path.with(other, |path| {
99                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
100                    });
101                }
102            }
103        }
104        let field_id = raw_id
105            .map(|value| {
106                path.with("id", |path| {
107                    let value = super::super::codec::expect_single(value, path)?;
108                    super::primitives::checked_string(
109                        super::super::codec::expect_string(value, path)?,
110                        path,
111                    )
112                })
113            })
114            .transpose()?;
115        let mut field_extension = Vec::new();
116        if let Some(raw) = raw_extension {
117            for (index, value) in path
118                .with("extension", |path| {
119                    super::super::codec::expect_array(raw, path)
120                })?
121                .iter()
122                .enumerate()
123            {
124                field_extension.push(path.with_index("extension", index, |path| {
125                    super::super::codec::Json::from_json(
126                        super::super::codec::expect_object(value, path)?,
127                        path,
128                    )
129                })?);
130            }
131        }
132        let field_concept = raw_concept
133            .map(|value| {
134                path.with("concept", |path| {
135                    super::super::codec::Json::from_json(
136                        super::super::codec::expect_object(
137                            super::super::codec::expect_single(value, path)?,
138                            path,
139                        )?,
140                        path,
141                    )
142                })
143            })
144            .transpose()?;
145        let field_reference = raw_reference
146            .map(|value| {
147                path.with("reference", |path| {
148                    super::super::codec::Json::from_json(
149                        super::super::codec::expect_object(
150                            super::super::codec::expect_single(value, path)?,
151                            path,
152                        )?,
153                        path,
154                    )
155                })
156            })
157            .transpose()?;
158        Ok(Self {
159            id: field_id,
160            extension: field_extension,
161            concept: field_concept,
162            reference: field_reference,
163        })
164    }
165}
166
167impl serde::Serialize for CodeableReference {
168    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
169        let mut map = serde::Serializer::serialize_map(serializer, None)?;
170        if let Some(item) = &self.concept {
171            serde::ser::SerializeMap::serialize_entry(&mut map, "concept", item)?;
172        }
173        if !self.extension.is_empty() {
174            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
175        }
176        if let Some(v) = &self.id {
177            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
178        }
179        if let Some(item) = &self.reference {
180            serde::ser::SerializeMap::serialize_entry(&mut map, "reference", item)?;
181        }
182        serde::ser::SerializeMap::end(map)
183    }
184}
185
186impl<'de> serde::Deserialize<'de> for CodeableReference {
187    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
188        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
189        let mut path = super::super::codec::Path::root("CodeableReference");
190        let object =
191            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
192        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
193    }
194}