fhir-types 0.1.92

Generated per-version FHIR types and terminology operation contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// @generated by fhir-codegen from hl7.fhir.r5.core 5.0.0. DO NOT EDIT.
// Change the emitter (tools/fhir-codegen) and regenerate.
//! An address expressed using postal conventions (as opposed to GPS or other location definition formats)

/// An address expressed using postal conventions (as opposed to GPS or other
/// location definition formats)
///
/// An address expressed using postal conventions (as opposed to GPS or other
/// location definition formats). This data type may be used to convey addresses
/// for use in delivering mail as well as for visiting locations which might not
/// be valid for mail delivery. There are a variety of postal address formats
/// defined around the world. The ISO21090-codedString may be used to provide a
/// coded representation of the contents of strings in an Address.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Address {
    /// 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>,
    /// home | work | temp | old | billing - purpose of this address
    ///
    /// The purpose of this address.
    pub r#use: Option<super::primitives::Code>,
    /// postal | physical | both
    ///
    /// Distinguishes between physical addresses (those you can visit) and mailing
    /// addresses (e.g. PO Boxes and care-of addresses). Most addresses are both.
    pub r#type: Option<super::primitives::Code>,
    /// Text representation of the address
    ///
    /// Specifies the entire address as it should be displayed e.g. on a postal
    /// label. This may be provided instead of or as well as the specific parts.
    pub text: Option<super::primitives::String>,
    /// Street name, number, direction & P.O. Box etc.
    ///
    /// This component contains the house number, apartment number, street name,
    /// street direction, P.O. Box number, delivery hints, and similar address
    /// information.
    pub line: Vec<super::primitives::String>,
    /// Name of city, town etc.
    ///
    /// The name of the city, town, suburb, village or other community or delivery
    /// center.
    pub city: Option<super::primitives::String>,
    /// District name (aka county)
    ///
    /// The name of the administrative area (county).
    pub district: Option<super::primitives::String>,
    /// Sub-unit of country (abbreviations ok)
    ///
    /// Sub-unit of a country with limited sovereignty in a federally organized
    /// country. A code may be used if codes are in common use (e.g. US 2 letter
    /// state codes).
    pub state: Option<super::primitives::String>,
    /// Postal code for area
    ///
    /// A postal code designating a region defined by the postal service.
    pub postal_code: Option<super::primitives::String>,
    /// Country (e.g. may be ISO 3166 2 or 3 letter code)
    ///
    /// Country - a nation as commonly understood or generally accepted.
    pub country: Option<super::primitives::String>,
    /// Time period when address was/is in use.
    pub period: Option<super::period::Period>,
}

impl super::super::codec::Json for Address {
    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(item) = &self.r#use {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("use"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_use"), e);
            }
        }
        if let Some(item) = &self.r#type {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("type"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_type"), e);
            }
        }
        if let Some(item) = &self.text {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("text"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_text"), e);
            }
        }
        if !self.line.is_empty() {
            let (values, elements) = super::super::codec::primitive_arrays(&self.line)?;
            object.insert(std::string::String::from("line"), values);
            if let Some(elements) = elements {
                object.insert(std::string::String::from("_line"), elements);
            }
        }
        if let Some(item) = &self.city {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("city"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_city"), e);
            }
        }
        if let Some(item) = &self.district {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("district"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_district"), e);
            }
        }
        if let Some(item) = &self.state {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("state"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_state"), e);
            }
        }
        if let Some(item) = &self.postal_code {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("postalCode"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_postalCode"), e);
            }
        }
        if let Some(item) = &self.country {
            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
                object.insert(std::string::String::from("country"), v);
            }
            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
                object.insert(std::string::String::from("_country"), e);
            }
        }
        if let Some(item) = &self.period {
            object.insert(
                std::string::String::from("period"),
                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_use: Option<&super::super::codec::Value> = None;
        let mut raw_use_element: 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_text: Option<&super::super::codec::Value> = None;
        let mut raw_text_element: Option<&super::super::codec::Value> = None;
        let mut raw_line: Option<&super::super::codec::Value> = None;
        let mut raw_line_element: Option<&super::super::codec::Value> = None;
        let mut raw_city: Option<&super::super::codec::Value> = None;
        let mut raw_city_element: Option<&super::super::codec::Value> = None;
        let mut raw_district: Option<&super::super::codec::Value> = None;
        let mut raw_district_element: Option<&super::super::codec::Value> = None;
        let mut raw_state: Option<&super::super::codec::Value> = None;
        let mut raw_state_element: Option<&super::super::codec::Value> = None;
        let mut raw_postal_code: Option<&super::super::codec::Value> = None;
        let mut raw_postal_code_element: Option<&super::super::codec::Value> = None;
        let mut raw_country: Option<&super::super::codec::Value> = None;
        let mut raw_country_element: Option<&super::super::codec::Value> = None;
        let mut raw_period: 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),
                "use" => raw_use = Some(value),
                "_use" => raw_use_element = Some(value),
                "type" => raw_type = Some(value),
                "_type" => raw_type_element = Some(value),
                "text" => raw_text = Some(value),
                "_text" => raw_text_element = Some(value),
                "line" => raw_line = Some(value),
                "_line" => raw_line_element = Some(value),
                "city" => raw_city = Some(value),
                "_city" => raw_city_element = Some(value),
                "district" => raw_district = Some(value),
                "_district" => raw_district_element = Some(value),
                "state" => raw_state = Some(value),
                "_state" => raw_state_element = Some(value),
                "postalCode" => raw_postal_code = Some(value),
                "_postalCode" => raw_postal_code_element = Some(value),
                "country" => raw_country = Some(value),
                "_country" => raw_country_element = Some(value),
                "period" => raw_period = 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::super::codec::expect_string(value, 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_use = match (raw_use, raw_use_element) {
            (None, None) => None,
            (value, element) => Some(path.with("use", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_type = match (raw_type, raw_type_element) {
            (None, None) => None,
            (value, element) => Some(path.with("type", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_text = match (raw_text, raw_text_element) {
            (None, None) => None,
            (value, element) => Some(path.with("text", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let mut field_line = Vec::new();
        for (index, (value, element)) in
            super::super::codec::pair_arrays(raw_line, raw_line_element, path)?
                .into_iter()
                .enumerate()
        {
            field_line.push(path.with_index("line", index, |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?);
        }
        let field_city = match (raw_city, raw_city_element) {
            (None, None) => None,
            (value, element) => Some(path.with("city", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_district = match (raw_district, raw_district_element) {
            (None, None) => None,
            (value, element) => Some(path.with("district", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_state = match (raw_state, raw_state_element) {
            (None, None) => None,
            (value, element) => Some(path.with("state", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_postal_code = match (raw_postal_code, raw_postal_code_element) {
            (None, None) => None,
            (value, element) => Some(path.with("postalCode", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_country = match (raw_country, raw_country_element) {
            (None, None) => None,
            (value, element) => Some(path.with("country", |path| {
                super::super::codec::Primitive::from_json_parts(value, element, path)
            })?),
        };
        let field_period = raw_period
            .map(|value| {
                path.with("period", |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#use: field_use,
            r#type: field_type,
            text: field_text,
            line: field_line,
            city: field_city,
            district: field_district,
            state: field_state,
            postal_code: field_postal_code,
            country: field_country,
            period: field_period,
        })
    }
}

impl serde::Serialize for Address {
    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.city {
            super::super::codec::element_entry(&mut map, "_city", item)?;
        }
        if let Some(item) = &self.country {
            super::super::codec::element_entry(&mut map, "_country", item)?;
        }
        if let Some(item) = &self.district {
            super::super::codec::element_entry(&mut map, "_district", item)?;
        }
        super::super::codec::element_list_entry(&mut map, "_line", &self.line)?;
        if let Some(item) = &self.postal_code {
            super::super::codec::element_entry(&mut map, "_postalCode", item)?;
        }
        if let Some(item) = &self.state {
            super::super::codec::element_entry(&mut map, "_state", item)?;
        }
        if let Some(item) = &self.text {
            super::super::codec::element_entry(&mut map, "_text", item)?;
        }
        if let Some(item) = &self.r#type {
            super::super::codec::element_entry(&mut map, "_type", item)?;
        }
        if let Some(item) = &self.r#use {
            super::super::codec::element_entry(&mut map, "_use", item)?;
        }
        if let Some(item) = &self.city {
            super::super::codec::value_entry(&mut map, "city", item)?;
        }
        if let Some(item) = &self.country {
            super::super::codec::value_entry(&mut map, "country", item)?;
        }
        if let Some(item) = &self.district {
            super::super::codec::value_entry(&mut map, "district", item)?;
        }
        if !self.extension.is_empty() {
            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
        }
        if let Some(v) = &self.id {
            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
        }
        super::super::codec::value_list_entry(&mut map, "line", &self.line)?;
        if let Some(item) = &self.period {
            serde::ser::SerializeMap::serialize_entry(&mut map, "period", item)?;
        }
        if let Some(item) = &self.postal_code {
            super::super::codec::value_entry(&mut map, "postalCode", item)?;
        }
        if let Some(item) = &self.state {
            super::super::codec::value_entry(&mut map, "state", item)?;
        }
        if let Some(item) = &self.text {
            super::super::codec::value_entry(&mut map, "text", item)?;
        }
        if let Some(item) = &self.r#type {
            super::super::codec::value_entry(&mut map, "type", item)?;
        }
        if let Some(item) = &self.r#use {
            super::super::codec::value_entry(&mut map, "use", item)?;
        }
        serde::ser::SerializeMap::end(map)
    }
}

impl<'de> serde::Deserialize<'de> for Address {
    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("Address");
        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)
    }
}