fhir_types/stu3/
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::super::codec::expect_string(value, path)
121 })
122 })
123 .transpose()?;
124 let mut field_extension = Vec::new();
125 if let Some(raw) = raw_extension {
126 for (index, value) in path
127 .with("extension", |path| {
128 super::super::codec::expect_array(raw, path)
129 })?
130 .iter()
131 .enumerate()
132 {
133 field_extension.push(path.with_index("extension", index, |path| {
134 super::super::codec::Json::from_json(
135 super::super::codec::expect_object(value, path)?,
136 path,
137 )
138 })?);
139 }
140 }
141 let mut field_coding = Vec::new();
142 if let Some(raw) = raw_coding {
143 for (index, value) in path
144 .with("coding", |path| {
145 super::super::codec::expect_array(raw, path)
146 })?
147 .iter()
148 .enumerate()
149 {
150 field_coding.push(path.with_index("coding", index, |path| {
151 super::super::codec::Json::from_json(
152 super::super::codec::expect_object(value, path)?,
153 path,
154 )
155 })?);
156 }
157 }
158 let field_text = match (raw_text, raw_text_element) {
159 (None, None) => None,
160 (value, element) => Some(path.with("text", |path| {
161 super::super::codec::Primitive::from_json_parts(value, element, path)
162 })?),
163 };
164 Ok(Self {
165 id: field_id,
166 extension: field_extension,
167 coding: field_coding,
168 text: field_text,
169 })
170 }
171}
172
173impl serde::Serialize for CodeableConcept {
174 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
175 let mut map = serde::Serializer::serialize_map(serializer, None)?;
176 if let Some(item) = &self.text {
177 super::super::codec::element_entry(&mut map, "_text", item)?;
178 }
179 if !self.coding.is_empty() {
180 serde::ser::SerializeMap::serialize_entry(&mut map, "coding", &self.coding)?;
181 }
182 if !self.extension.is_empty() {
183 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
184 }
185 if let Some(v) = &self.id {
186 serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
187 }
188 if let Some(item) = &self.text {
189 super::super::codec::value_entry(&mut map, "text", item)?;
190 }
191 serde::ser::SerializeMap::end(map)
192 }
193}
194
195impl<'de> serde::Deserialize<'de> for CodeableConcept {
196 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
197 let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
198 let mut path = super::super::codec::Path::root("CodeableConcept");
199 let object =
200 super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
201 super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
202 }
203}