fhir_types/r6/
codeable_concept.rs1#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct CodeableConcept {
13 pub id: Option<std::string::String>,
18 pub extension: Vec<super::extension::Extension>,
27 pub coding: Vec<super::coding::Coding>,
31 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}