Skip to main content

fhir_types/r6/
codeable_concept.rs

1// @generated by fhir-codegen from hl7.fhir.r6.core 6.0.0-ballot5. 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//! Concept - reference to a terminology or just text
6
7/// Concept - reference to a terminology or just text
8///
9/// A concept that may be defined by a formal reference to a terminology or
10/// ontology or may be provided by text.
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct CodeableConcept {
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    /// Code defined by a terminology system
28    ///
29    /// A reference to a code defined by a terminology system.
30    pub coding: Vec<super::coding::Coding>,
31    /// Plain text representation of the concept
32    ///
33    /// A human language representation of the concept as seen/selected/uttered by
34    /// the user who entered the data and/or which represents the intended meaning
35    /// of the user.
36    pub text: Option<super::primitives::String>,
37}
38
39impl super::super::codec::Json for CodeableConcept {
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 !self.coding.is_empty() {
61            let mut items = Vec::with_capacity(self.coding.len());
62            for item in &self.coding {
63                items.push(super::super::codec::Value::Object(
64                    super::super::codec::Json::to_json(item)?,
65                ));
66            }
67            object.insert(
68                std::string::String::from("coding"),
69                super::super::codec::Value::Array(items),
70            );
71        }
72        if let Some(item) = &self.text {
73            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
74                object.insert(std::string::String::from("text"), v);
75            }
76            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
77                object.insert(std::string::String::from("_text"), e);
78            }
79        }
80        Ok(object)
81    }
82
83    fn from_json(
84        object: &super::super::codec::Object,
85        path: &mut super::super::codec::Path,
86    ) -> Result<Self, super::super::codec::DecodeError> {
87        let mut raw_id: Option<&super::super::codec::Value> = None;
88        let mut raw_extension: Option<&super::super::codec::Value> = None;
89        let mut raw_coding: Option<&super::super::codec::Value> = None;
90        let mut raw_text: Option<&super::super::codec::Value> = None;
91        let mut raw_text_element: Option<&super::super::codec::Value> = None;
92        for (key, value) in object {
93            match key.as_str() {
94                "id" => {
95                    raw_id = Some(value);
96                }
97                "extension" => {
98                    raw_extension = Some(value);
99                }
100                "coding" => {
101                    raw_coding = Some(value);
102                }
103                "text" => {
104                    raw_text = Some(value);
105                }
106                "_text" => {
107                    raw_text_element = Some(value);
108                }
109                other => {
110                    return path.with(other, |path| {
111                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
112                    });
113                }
114            }
115        }
116        let field_id = raw_id
117            .map(|value| {
118                path.with("id", |path| {
119                    let value = super::super::codec::expect_single(value, path)?;
120                    super::primitives::checked_string(
121                        super::super::codec::expect_string(value, path)?,
122                        path,
123                    )
124                })
125            })
126            .transpose()?;
127        let mut field_extension = Vec::new();
128        if let Some(raw) = raw_extension {
129            for (index, value) in path
130                .with("extension", |path| {
131                    super::super::codec::expect_array(raw, path)
132                })?
133                .iter()
134                .enumerate()
135            {
136                field_extension.push(path.with_index("extension", index, |path| {
137                    super::super::codec::Json::from_json(
138                        super::super::codec::expect_object(value, path)?,
139                        path,
140                    )
141                })?);
142            }
143        }
144        let mut field_coding = Vec::new();
145        if let Some(raw) = raw_coding {
146            for (index, value) in path
147                .with("coding", |path| {
148                    super::super::codec::expect_array(raw, path)
149                })?
150                .iter()
151                .enumerate()
152            {
153                field_coding.push(path.with_index("coding", index, |path| {
154                    super::super::codec::Json::from_json(
155                        super::super::codec::expect_object(value, path)?,
156                        path,
157                    )
158                })?);
159            }
160        }
161        let field_text = match (raw_text, raw_text_element) {
162            (None, None) => None,
163            (value, element) => Some(path.with("text", |path| {
164                super::super::codec::Primitive::from_json_parts(value, element, path)
165            })?),
166        };
167        Ok(Self {
168            id: field_id,
169            extension: field_extension,
170            coding: field_coding,
171            text: field_text,
172        })
173    }
174}
175
176impl serde::Serialize for CodeableConcept {
177    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
178        let mut map = serde::Serializer::serialize_map(serializer, None)?;
179        if let Some(item) = &self.text {
180            super::super::codec::element_entry(&mut map, "_text", item)?;
181        }
182        if !self.coding.is_empty() {
183            serde::ser::SerializeMap::serialize_entry(&mut map, "coding", &self.coding)?;
184        }
185        if !self.extension.is_empty() {
186            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
187        }
188        if let Some(v) = &self.id {
189            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
190        }
191        if let Some(item) = &self.text {
192            super::super::codec::value_entry(&mut map, "text", item)?;
193        }
194        serde::ser::SerializeMap::end(map)
195    }
196}
197
198impl<'de> serde::Deserialize<'de> for CodeableConcept {
199    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
200        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
201        let mut path = super::super::codec::Path::root("CodeableConcept");
202        let object =
203            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
204        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
205    }
206}