Skip to main content

fhir_types/r4b/
timing.rs

1// @generated by fhir-codegen from hl7.fhir.r4b.core 4.3.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//! A timing schedule that specifies an event that may occur multiple times
6
7/// A timing schedule that specifies an event that may occur multiple times
8///
9/// Specifies an event that may occur multiple times. Timing schedules are used
10/// to record when things are planned, expected or requested to occur. The most
11/// common usage is in dosage instructions for medications. They are also used
12/// when planning care of various kinds, and may be used for reporting the
13/// schedule to which past regular activities were carried out.
14#[derive(Debug, Clone, Default, PartialEq, Eq)]
15pub struct Timing {
16    /// Unique id for inter-element referencing
17    ///
18    /// Unique id for the element within a resource (for internal references). This
19    /// may be any string value that does not contain spaces.
20    pub id: Option<std::string::String>,
21    /// Additional content defined by implementations
22    ///
23    /// May be used to represent additional information that is not part of the
24    /// basic definition of the element. To make the use of extensions safe and
25    /// manageable, there is a strict set of governance applied to the definition
26    /// and use of extensions. Though any implementer can define an extension, there
27    /// is a set of requirements that SHALL be met as part of the definition of the
28    /// extension.
29    pub extension: Vec<super::extension::Extension>,
30    /// Extensions that cannot be ignored even if unrecognized
31    ///
32    /// May be used to represent additional information that is not part of the
33    /// basic definition of the element and that modifies the understanding of the
34    /// element in which it is contained and/or the understanding of the containing
35    /// element's descendants. Usually modifier elements provide negation or
36    /// qualification. To make the use of extensions safe and manageable, there is a
37    /// strict set of governance applied to the definition and use of extensions.
38    /// Though any implementer can define an extension, there is a set of
39    /// requirements that SHALL be met as part of the definition of the extension.
40    /// Applications processing a resource are required to check for modifier
41    /// extensions. Modifier extensions SHALL NOT change the meaning of any elements
42    /// on Resource or DomainResource (including cannot change the meaning of
43    /// modifierExtension itself).
44    pub modifier_extension: Vec<super::extension::Extension>,
45    /// When the event occurs
46    ///
47    /// Identifies specific times when the event occurs.
48    pub event: Vec<super::primitives::DateTime>,
49    /// When the event is to occur
50    ///
51    /// A set of rules that describe when the event is scheduled.
52    pub repeat: Option<TimingRepeat>,
53    /// BID | TID | QID | AM | PM | QD | QOD | +
54    ///
55    /// A code for the timing schedule (or just text in code.text). Some codes such
56    /// as BID are ubiquitous, but many institutions define their own additional
57    /// codes. If a code is provided, the code is understood to be a complete
58    /// statement of whatever is specified in the structured timing data, and either
59    /// the code or the data may be used to interpret the Timing, with the exception
60    /// that .repeat.bounds still applies over the code (and is not contained in the
61    /// code).
62    pub code: Option<super::codeable_concept::CodeableConcept>,
63}
64
65impl super::super::codec::Json for Timing {
66    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
67        let mut object = super::super::codec::Object::new();
68        if let Some(v) = &self.id {
69            object.insert(
70                std::string::String::from("id"),
71                super::super::codec::Value::String(v.clone()),
72            );
73        }
74        if !self.extension.is_empty() {
75            let mut items = Vec::with_capacity(self.extension.len());
76            for item in &self.extension {
77                items.push(super::super::codec::Value::Object(
78                    super::super::codec::Json::to_json(item)?,
79                ));
80            }
81            object.insert(
82                std::string::String::from("extension"),
83                super::super::codec::Value::Array(items),
84            );
85        }
86        if !self.modifier_extension.is_empty() {
87            let mut items = Vec::with_capacity(self.modifier_extension.len());
88            for item in &self.modifier_extension {
89                items.push(super::super::codec::Value::Object(
90                    super::super::codec::Json::to_json(item)?,
91                ));
92            }
93            object.insert(
94                std::string::String::from("modifierExtension"),
95                super::super::codec::Value::Array(items),
96            );
97        }
98        if !self.event.is_empty() {
99            let (values, elements) = super::super::codec::primitive_arrays(&self.event)?;
100            if let Some(values) = values {
101                object.insert(std::string::String::from("event"), values);
102            }
103            if let Some(elements) = elements {
104                object.insert(std::string::String::from("_event"), elements);
105            }
106        }
107        if let Some(item) = &self.repeat {
108            object.insert(
109                std::string::String::from("repeat"),
110                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
111            );
112        }
113        if let Some(item) = &self.code {
114            object.insert(
115                std::string::String::from("code"),
116                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
117            );
118        }
119        Ok(object)
120    }
121
122    fn from_json(
123        object: &super::super::codec::Object,
124        path: &mut super::super::codec::Path,
125    ) -> Result<Self, super::super::codec::DecodeError> {
126        let mut raw_id: Option<&super::super::codec::Value> = None;
127        let mut raw_extension: Option<&super::super::codec::Value> = None;
128        let mut raw_modifier_extension: Option<&super::super::codec::Value> = None;
129        let mut raw_event: Option<&super::super::codec::Value> = None;
130        let mut raw_event_element: Option<&super::super::codec::Value> = None;
131        let mut raw_repeat: Option<&super::super::codec::Value> = None;
132        let mut raw_code: Option<&super::super::codec::Value> = None;
133        for (key, value) in object {
134            match key.as_str() {
135                "id" => {
136                    raw_id = Some(value);
137                }
138                "extension" => {
139                    raw_extension = Some(value);
140                }
141                "modifierExtension" => {
142                    raw_modifier_extension = Some(value);
143                }
144                "event" => {
145                    raw_event = Some(value);
146                }
147                "_event" => {
148                    raw_event_element = Some(value);
149                }
150                "repeat" => {
151                    raw_repeat = Some(value);
152                }
153                "code" => {
154                    raw_code = Some(value);
155                }
156                other => {
157                    return path.with(other, |path| {
158                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
159                    });
160                }
161            }
162        }
163        let field_id = raw_id
164            .map(|value| {
165                path.with("id", |path| {
166                    let value = super::super::codec::expect_single(value, path)?;
167                    super::primitives::checked_string(
168                        super::super::codec::expect_string(value, path)?,
169                        path,
170                    )
171                })
172            })
173            .transpose()?;
174        let mut field_extension = Vec::new();
175        if let Some(raw) = raw_extension {
176            for (index, value) in path
177                .with("extension", |path| {
178                    super::super::codec::expect_array(raw, path)
179                })?
180                .iter()
181                .enumerate()
182            {
183                field_extension.push(path.with_index("extension", index, |path| {
184                    super::super::codec::Json::from_json(
185                        super::super::codec::expect_object(value, path)?,
186                        path,
187                    )
188                })?);
189            }
190        }
191        let mut field_modifier_extension = Vec::new();
192        if let Some(raw) = raw_modifier_extension {
193            for (index, value) in path
194                .with("modifierExtension", |path| {
195                    super::super::codec::expect_array(raw, path)
196                })?
197                .iter()
198                .enumerate()
199            {
200                field_modifier_extension.push(path.with_index(
201                    "modifierExtension",
202                    index,
203                    |path| {
204                        super::super::codec::Json::from_json(
205                            super::super::codec::expect_object(value, path)?,
206                            path,
207                        )
208                    },
209                )?);
210            }
211        }
212        let mut field_event = Vec::new();
213        for (index, (value, element)) in path
214            .with("event", |path| {
215                super::super::codec::pair_arrays(raw_event, raw_event_element, path)
216            })?
217            .into_iter()
218            .enumerate()
219        {
220            field_event.push(path.with_index("event", index, |path| {
221                super::super::codec::Primitive::from_json_parts(value, element, path)
222            })?);
223        }
224        let field_repeat = raw_repeat
225            .map(|value| {
226                path.with("repeat", |path| {
227                    super::super::codec::Json::from_json(
228                        super::super::codec::expect_object(
229                            super::super::codec::expect_single(value, path)?,
230                            path,
231                        )?,
232                        path,
233                    )
234                })
235            })
236            .transpose()?;
237        let field_code = raw_code
238            .map(|value| {
239                path.with("code", |path| {
240                    super::super::codec::Json::from_json(
241                        super::super::codec::expect_object(
242                            super::super::codec::expect_single(value, path)?,
243                            path,
244                        )?,
245                        path,
246                    )
247                })
248            })
249            .transpose()?;
250        Ok(Self {
251            id: field_id,
252            extension: field_extension,
253            modifier_extension: field_modifier_extension,
254            event: field_event,
255            repeat: field_repeat,
256            code: field_code,
257        })
258    }
259}
260
261impl serde::Serialize for Timing {
262    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
263        let mut map = serde::Serializer::serialize_map(serializer, None)?;
264        super::super::codec::element_list_entry(&mut map, "_event", &self.event)?;
265        if let Some(item) = &self.code {
266            serde::ser::SerializeMap::serialize_entry(&mut map, "code", item)?;
267        }
268        super::super::codec::value_list_entry(&mut map, "event", &self.event)?;
269        if !self.extension.is_empty() {
270            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
271        }
272        if let Some(v) = &self.id {
273            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
274        }
275        if !self.modifier_extension.is_empty() {
276            serde::ser::SerializeMap::serialize_entry(
277                &mut map,
278                "modifierExtension",
279                &self.modifier_extension,
280            )?;
281        }
282        if let Some(item) = &self.repeat {
283            serde::ser::SerializeMap::serialize_entry(&mut map, "repeat", item)?;
284        }
285        serde::ser::SerializeMap::end(map)
286    }
287}
288
289impl<'de> serde::Deserialize<'de> for Timing {
290    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
291        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
292        let mut path = super::super::codec::Path::root("Timing");
293        let object =
294            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
295        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
296    }
297}
298
299/// When the event is to occur
300///
301/// A set of rules that describe when the event is scheduled.
302#[derive(Debug, Clone, Default, PartialEq, Eq)]
303pub struct TimingRepeat {
304    /// Unique id for inter-element referencing
305    ///
306    /// Unique id for the element within a resource (for internal references). This
307    /// may be any string value that does not contain spaces.
308    pub id: Option<std::string::String>,
309    /// Additional content defined by implementations
310    ///
311    /// May be used to represent additional information that is not part of the
312    /// basic definition of the element. To make the use of extensions safe and
313    /// manageable, there is a strict set of governance applied to the definition
314    /// and use of extensions. Though any implementer can define an extension, there
315    /// is a set of requirements that SHALL be met as part of the definition of the
316    /// extension.
317    pub extension: Vec<super::extension::Extension>,
318    /// Length/Range of lengths, or (Start and/or end) limits
319    ///
320    /// Either a duration for the length of the timing schedule, a range of possible
321    /// length, or outer bounds for start and/or end limits of the timing schedule.
322    pub bounds: Option<TimingRepeatBounds>,
323    /// Number of times to repeat
324    ///
325    /// A total count of the desired number of repetitions across the duration of
326    /// the entire timing specification. If countMax is present, this element
327    /// indicates the lower bound of the allowed range of count values.
328    pub count: Option<super::primitives::PositiveInt>,
329    /// Maximum number of times to repeat
330    ///
331    /// If present, indicates that the count is a range - so to perform the action
332    /// between \[count\] and \[countMax\] times.
333    pub count_max: Option<super::primitives::PositiveInt>,
334    /// How long when it happens
335    ///
336    /// How long this thing happens for when it happens. If durationMax is present,
337    /// this element indicates the lower bound of the allowed range of the duration.
338    pub duration: Option<super::primitives::Decimal>,
339    /// How long when it happens (Max)
340    ///
341    /// If present, indicates that the duration is a range - so to perform the
342    /// action between \[duration\] and \[durationMax\] time length.
343    pub duration_max: Option<super::primitives::Decimal>,
344    /// s | min | h | d | wk | mo | a - unit of time (UCUM)
345    ///
346    /// The units of time for the duration, in UCUM units.
347    pub duration_unit: Option<super::primitives::Code>,
348    /// Event occurs frequency times per period
349    ///
350    /// The number of times to repeat the action within the specified period. If
351    /// frequencyMax is present, this element indicates the lower bound of the
352    /// allowed range of the frequency.
353    pub frequency: Option<super::primitives::PositiveInt>,
354    /// Event occurs up to frequencyMax times per period
355    ///
356    /// If present, indicates that the frequency is a range - so to repeat between
357    /// \[frequency\] and \[frequencyMax\] times within the period or period range.
358    pub frequency_max: Option<super::primitives::PositiveInt>,
359    /// Event occurs frequency times per period
360    ///
361    /// Indicates the duration of time over which repetitions are to occur; e.g. to
362    /// express "3 times per day", 3 would be the frequency and "1 day" would be the
363    /// period. If periodMax is present, this element indicates the lower bound of
364    /// the allowed range of the period length.
365    pub period: Option<super::primitives::Decimal>,
366    /// Upper limit of period (3-4 hours)
367    ///
368    /// If present, indicates that the period is a range from \[period\] to
369    /// \[periodMax\], allowing expressing concepts such as "do this once every 3-5
370    /// days.
371    pub period_max: Option<super::primitives::Decimal>,
372    /// s | min | h | d | wk | mo | a - unit of time (UCUM)
373    ///
374    /// The units of time for the period in UCUM units.
375    pub period_unit: Option<super::primitives::Code>,
376    /// mon | tue | wed | thu | fri | sat | sun
377    ///
378    /// If one or more days of week is provided, then the action happens only on the
379    /// specified day(s).
380    pub day_of_week: Vec<super::primitives::Code>,
381    /// Time of day for action
382    ///
383    /// Specified time of day for action to take place.
384    pub time_of_day: Vec<super::primitives::Time>,
385    /// Code for time period of occurrence
386    ///
387    /// An approximate time period during the day, potentially linked to an event of
388    /// daily living that indicates when the action should occur.
389    pub when: Vec<super::primitives::Code>,
390    /// Minutes from event (before or after)
391    ///
392    /// The number of minutes from the event. If the event code does not indicate
393    /// whether the minutes is before or after the event, then the offset is assumed
394    /// to be after the event.
395    pub offset: Option<super::primitives::UnsignedInt>,
396}
397
398impl super::super::codec::Json for TimingRepeat {
399    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
400        let mut object = super::super::codec::Object::new();
401        if let Some(v) = &self.id {
402            object.insert(
403                std::string::String::from("id"),
404                super::super::codec::Value::String(v.clone()),
405            );
406        }
407        if !self.extension.is_empty() {
408            let mut items = Vec::with_capacity(self.extension.len());
409            for item in &self.extension {
410                items.push(super::super::codec::Value::Object(
411                    super::super::codec::Json::to_json(item)?,
412                ));
413            }
414            object.insert(
415                std::string::String::from("extension"),
416                super::super::codec::Value::Array(items),
417            );
418        }
419        if let Some(item) = &self.bounds {
420            let (suffix, value, element) = item.to_json_parts()?;
421            if let Some(value) = value {
422                object.insert(format!("bounds{suffix}"), value);
423            }
424            if let Some(element) = element {
425                object.insert(format!("_bounds{suffix}"), element);
426            }
427        }
428        if let Some(item) = &self.count {
429            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
430                object.insert(std::string::String::from("count"), v);
431            }
432            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
433                object.insert(std::string::String::from("_count"), e);
434            }
435        }
436        if let Some(item) = &self.count_max {
437            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
438                object.insert(std::string::String::from("countMax"), v);
439            }
440            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
441                object.insert(std::string::String::from("_countMax"), e);
442            }
443        }
444        if let Some(item) = &self.duration {
445            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
446                object.insert(std::string::String::from("duration"), v);
447            }
448            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
449                object.insert(std::string::String::from("_duration"), e);
450            }
451        }
452        if let Some(item) = &self.duration_max {
453            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
454                object.insert(std::string::String::from("durationMax"), v);
455            }
456            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
457                object.insert(std::string::String::from("_durationMax"), e);
458            }
459        }
460        if let Some(item) = &self.duration_unit {
461            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
462                object.insert(std::string::String::from("durationUnit"), v);
463            }
464            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
465                object.insert(std::string::String::from("_durationUnit"), e);
466            }
467        }
468        if let Some(item) = &self.frequency {
469            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
470                object.insert(std::string::String::from("frequency"), v);
471            }
472            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
473                object.insert(std::string::String::from("_frequency"), e);
474            }
475        }
476        if let Some(item) = &self.frequency_max {
477            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
478                object.insert(std::string::String::from("frequencyMax"), v);
479            }
480            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
481                object.insert(std::string::String::from("_frequencyMax"), e);
482            }
483        }
484        if let Some(item) = &self.period {
485            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
486                object.insert(std::string::String::from("period"), v);
487            }
488            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
489                object.insert(std::string::String::from("_period"), e);
490            }
491        }
492        if let Some(item) = &self.period_max {
493            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
494                object.insert(std::string::String::from("periodMax"), v);
495            }
496            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
497                object.insert(std::string::String::from("_periodMax"), e);
498            }
499        }
500        if let Some(item) = &self.period_unit {
501            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
502                object.insert(std::string::String::from("periodUnit"), v);
503            }
504            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
505                object.insert(std::string::String::from("_periodUnit"), e);
506            }
507        }
508        if !self.day_of_week.is_empty() {
509            let (values, elements) = super::super::codec::primitive_arrays(&self.day_of_week)?;
510            if let Some(values) = values {
511                object.insert(std::string::String::from("dayOfWeek"), values);
512            }
513            if let Some(elements) = elements {
514                object.insert(std::string::String::from("_dayOfWeek"), elements);
515            }
516        }
517        if !self.time_of_day.is_empty() {
518            let (values, elements) = super::super::codec::primitive_arrays(&self.time_of_day)?;
519            if let Some(values) = values {
520                object.insert(std::string::String::from("timeOfDay"), values);
521            }
522            if let Some(elements) = elements {
523                object.insert(std::string::String::from("_timeOfDay"), elements);
524            }
525        }
526        if !self.when.is_empty() {
527            let (values, elements) = super::super::codec::primitive_arrays(&self.when)?;
528            if let Some(values) = values {
529                object.insert(std::string::String::from("when"), values);
530            }
531            if let Some(elements) = elements {
532                object.insert(std::string::String::from("_when"), elements);
533            }
534        }
535        if let Some(item) = &self.offset {
536            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
537                object.insert(std::string::String::from("offset"), v);
538            }
539            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
540                object.insert(std::string::String::from("_offset"), e);
541            }
542        }
543        Ok(object)
544    }
545
546    fn from_json(
547        object: &super::super::codec::Object,
548        path: &mut super::super::codec::Path,
549    ) -> Result<Self, super::super::codec::DecodeError> {
550        let mut raw_id: Option<&super::super::codec::Value> = None;
551        let mut raw_extension: Option<&super::super::codec::Value> = None;
552        let mut raw_bounds = super::super::codec::ChoiceSlot::default();
553        let mut raw_count: Option<&super::super::codec::Value> = None;
554        let mut raw_count_element: Option<&super::super::codec::Value> = None;
555        let mut raw_count_max: Option<&super::super::codec::Value> = None;
556        let mut raw_count_max_element: Option<&super::super::codec::Value> = None;
557        let mut raw_duration: Option<&super::super::codec::Value> = None;
558        let mut raw_duration_element: Option<&super::super::codec::Value> = None;
559        let mut raw_duration_max: Option<&super::super::codec::Value> = None;
560        let mut raw_duration_max_element: Option<&super::super::codec::Value> = None;
561        let mut raw_duration_unit: Option<&super::super::codec::Value> = None;
562        let mut raw_duration_unit_element: Option<&super::super::codec::Value> = None;
563        let mut raw_frequency: Option<&super::super::codec::Value> = None;
564        let mut raw_frequency_element: Option<&super::super::codec::Value> = None;
565        let mut raw_frequency_max: Option<&super::super::codec::Value> = None;
566        let mut raw_frequency_max_element: Option<&super::super::codec::Value> = None;
567        let mut raw_period: Option<&super::super::codec::Value> = None;
568        let mut raw_period_element: Option<&super::super::codec::Value> = None;
569        let mut raw_period_max: Option<&super::super::codec::Value> = None;
570        let mut raw_period_max_element: Option<&super::super::codec::Value> = None;
571        let mut raw_period_unit: Option<&super::super::codec::Value> = None;
572        let mut raw_period_unit_element: Option<&super::super::codec::Value> = None;
573        let mut raw_day_of_week: Option<&super::super::codec::Value> = None;
574        let mut raw_day_of_week_element: Option<&super::super::codec::Value> = None;
575        let mut raw_time_of_day: Option<&super::super::codec::Value> = None;
576        let mut raw_time_of_day_element: Option<&super::super::codec::Value> = None;
577        let mut raw_when: Option<&super::super::codec::Value> = None;
578        let mut raw_when_element: Option<&super::super::codec::Value> = None;
579        let mut raw_offset: Option<&super::super::codec::Value> = None;
580        let mut raw_offset_element: Option<&super::super::codec::Value> = None;
581        for (key, value) in object {
582            match key.as_str() {
583                "id" => {
584                    raw_id = Some(value);
585                }
586                "extension" => {
587                    raw_extension = Some(value);
588                }
589                "boundsDuration" => {
590                    raw_bounds.value("Duration", value, path)?;
591                }
592                "_boundsDuration" => {
593                    raw_bounds.element("Duration", value, path)?;
594                }
595                "boundsRange" => {
596                    raw_bounds.value("Range", value, path)?;
597                }
598                "_boundsRange" => {
599                    raw_bounds.element("Range", value, path)?;
600                }
601                "boundsPeriod" => {
602                    raw_bounds.value("Period", value, path)?;
603                }
604                "_boundsPeriod" => {
605                    raw_bounds.element("Period", value, path)?;
606                }
607                "count" => {
608                    raw_count = Some(value);
609                }
610                "_count" => {
611                    raw_count_element = Some(value);
612                }
613                "countMax" => {
614                    raw_count_max = Some(value);
615                }
616                "_countMax" => {
617                    raw_count_max_element = Some(value);
618                }
619                "duration" => {
620                    raw_duration = Some(value);
621                }
622                "_duration" => {
623                    raw_duration_element = Some(value);
624                }
625                "durationMax" => {
626                    raw_duration_max = Some(value);
627                }
628                "_durationMax" => {
629                    raw_duration_max_element = Some(value);
630                }
631                "durationUnit" => {
632                    raw_duration_unit = Some(value);
633                }
634                "_durationUnit" => {
635                    raw_duration_unit_element = Some(value);
636                }
637                "frequency" => {
638                    raw_frequency = Some(value);
639                }
640                "_frequency" => {
641                    raw_frequency_element = Some(value);
642                }
643                "frequencyMax" => {
644                    raw_frequency_max = Some(value);
645                }
646                "_frequencyMax" => {
647                    raw_frequency_max_element = Some(value);
648                }
649                "period" => {
650                    raw_period = Some(value);
651                }
652                "_period" => {
653                    raw_period_element = Some(value);
654                }
655                "periodMax" => {
656                    raw_period_max = Some(value);
657                }
658                "_periodMax" => {
659                    raw_period_max_element = Some(value);
660                }
661                "periodUnit" => {
662                    raw_period_unit = Some(value);
663                }
664                "_periodUnit" => {
665                    raw_period_unit_element = Some(value);
666                }
667                "dayOfWeek" => {
668                    raw_day_of_week = Some(value);
669                }
670                "_dayOfWeek" => {
671                    raw_day_of_week_element = Some(value);
672                }
673                "timeOfDay" => {
674                    raw_time_of_day = Some(value);
675                }
676                "_timeOfDay" => {
677                    raw_time_of_day_element = Some(value);
678                }
679                "when" => {
680                    raw_when = Some(value);
681                }
682                "_when" => {
683                    raw_when_element = Some(value);
684                }
685                "offset" => {
686                    raw_offset = Some(value);
687                }
688                "_offset" => {
689                    raw_offset_element = Some(value);
690                }
691                other => {
692                    return path.with(other, |path| {
693                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
694                    });
695                }
696            }
697        }
698        let field_id = raw_id
699            .map(|value| {
700                path.with("id", |path| {
701                    let value = super::super::codec::expect_single(value, path)?;
702                    super::primitives::checked_string(
703                        super::super::codec::expect_string(value, path)?,
704                        path,
705                    )
706                })
707            })
708            .transpose()?;
709        let mut field_extension = Vec::new();
710        if let Some(raw) = raw_extension {
711            for (index, value) in path
712                .with("extension", |path| {
713                    super::super::codec::expect_array(raw, path)
714                })?
715                .iter()
716                .enumerate()
717            {
718                field_extension.push(path.with_index("extension", index, |path| {
719                    super::super::codec::Json::from_json(
720                        super::super::codec::expect_object(value, path)?,
721                        path,
722                    )
723                })?);
724            }
725        }
726        let field_bounds = match raw_bounds.suffix {
727            None => None,
728            Some(suffix) => Some(path.with("bounds", |path| {
729                TimingRepeatBounds::from_json_parts(
730                    suffix,
731                    raw_bounds.value,
732                    raw_bounds.element,
733                    path,
734                )
735            })?),
736        };
737        let field_count = match (raw_count, raw_count_element) {
738            (None, None) => None,
739            (value, element) => Some(path.with("count", |path| {
740                super::super::codec::Primitive::from_json_parts(value, element, path)
741            })?),
742        };
743        let field_count_max = match (raw_count_max, raw_count_max_element) {
744            (None, None) => None,
745            (value, element) => Some(path.with("countMax", |path| {
746                super::super::codec::Primitive::from_json_parts(value, element, path)
747            })?),
748        };
749        let field_duration = match (raw_duration, raw_duration_element) {
750            (None, None) => None,
751            (value, element) => Some(path.with("duration", |path| {
752                super::super::codec::Primitive::from_json_parts(value, element, path)
753            })?),
754        };
755        let field_duration_max = match (raw_duration_max, raw_duration_max_element) {
756            (None, None) => None,
757            (value, element) => Some(path.with("durationMax", |path| {
758                super::super::codec::Primitive::from_json_parts(value, element, path)
759            })?),
760        };
761        let field_duration_unit = match (raw_duration_unit, raw_duration_unit_element) {
762            (None, None) => None,
763            (value, element) => Some(path.with("durationUnit", |path| {
764                super::super::codec::Primitive::from_json_parts(value, element, path)
765            })?),
766        };
767        let field_frequency = match (raw_frequency, raw_frequency_element) {
768            (None, None) => None,
769            (value, element) => Some(path.with("frequency", |path| {
770                super::super::codec::Primitive::from_json_parts(value, element, path)
771            })?),
772        };
773        let field_frequency_max = match (raw_frequency_max, raw_frequency_max_element) {
774            (None, None) => None,
775            (value, element) => Some(path.with("frequencyMax", |path| {
776                super::super::codec::Primitive::from_json_parts(value, element, path)
777            })?),
778        };
779        let field_period = match (raw_period, raw_period_element) {
780            (None, None) => None,
781            (value, element) => Some(path.with("period", |path| {
782                super::super::codec::Primitive::from_json_parts(value, element, path)
783            })?),
784        };
785        let field_period_max = match (raw_period_max, raw_period_max_element) {
786            (None, None) => None,
787            (value, element) => Some(path.with("periodMax", |path| {
788                super::super::codec::Primitive::from_json_parts(value, element, path)
789            })?),
790        };
791        let field_period_unit = match (raw_period_unit, raw_period_unit_element) {
792            (None, None) => None,
793            (value, element) => Some(path.with("periodUnit", |path| {
794                super::super::codec::Primitive::from_json_parts(value, element, path)
795            })?),
796        };
797        let mut field_day_of_week = Vec::new();
798        for (index, (value, element)) in path
799            .with("dayOfWeek", |path| {
800                super::super::codec::pair_arrays(raw_day_of_week, raw_day_of_week_element, path)
801            })?
802            .into_iter()
803            .enumerate()
804        {
805            field_day_of_week.push(path.with_index("dayOfWeek", index, |path| {
806                super::super::codec::Primitive::from_json_parts(value, element, path)
807            })?);
808        }
809        let mut field_time_of_day = Vec::new();
810        for (index, (value, element)) in path
811            .with("timeOfDay", |path| {
812                super::super::codec::pair_arrays(raw_time_of_day, raw_time_of_day_element, path)
813            })?
814            .into_iter()
815            .enumerate()
816        {
817            field_time_of_day.push(path.with_index("timeOfDay", index, |path| {
818                super::super::codec::Primitive::from_json_parts(value, element, path)
819            })?);
820        }
821        let mut field_when = Vec::new();
822        for (index, (value, element)) in path
823            .with("when", |path| {
824                super::super::codec::pair_arrays(raw_when, raw_when_element, path)
825            })?
826            .into_iter()
827            .enumerate()
828        {
829            field_when.push(path.with_index("when", index, |path| {
830                super::super::codec::Primitive::from_json_parts(value, element, path)
831            })?);
832        }
833        let field_offset = match (raw_offset, raw_offset_element) {
834            (None, None) => None,
835            (value, element) => Some(path.with("offset", |path| {
836                super::super::codec::Primitive::from_json_parts(value, element, path)
837            })?),
838        };
839        Ok(Self {
840            id: field_id,
841            extension: field_extension,
842            bounds: field_bounds,
843            count: field_count,
844            count_max: field_count_max,
845            duration: field_duration,
846            duration_max: field_duration_max,
847            duration_unit: field_duration_unit,
848            frequency: field_frequency,
849            frequency_max: field_frequency_max,
850            period: field_period,
851            period_max: field_period_max,
852            period_unit: field_period_unit,
853            day_of_week: field_day_of_week,
854            time_of_day: field_time_of_day,
855            when: field_when,
856            offset: field_offset,
857        })
858    }
859}
860
861impl serde::Serialize for TimingRepeat {
862    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
863        let mut map = serde::Serializer::serialize_map(serializer, None)?;
864        if let Some(item) = &self.count {
865            super::super::codec::element_entry(&mut map, "_count", item)?;
866        }
867        if let Some(item) = &self.count_max {
868            super::super::codec::element_entry(&mut map, "_countMax", item)?;
869        }
870        super::super::codec::element_list_entry(&mut map, "_dayOfWeek", &self.day_of_week)?;
871        if let Some(item) = &self.duration {
872            super::super::codec::element_entry(&mut map, "_duration", item)?;
873        }
874        if let Some(item) = &self.duration_max {
875            super::super::codec::element_entry(&mut map, "_durationMax", item)?;
876        }
877        if let Some(item) = &self.duration_unit {
878            super::super::codec::element_entry(&mut map, "_durationUnit", item)?;
879        }
880        if let Some(item) = &self.frequency {
881            super::super::codec::element_entry(&mut map, "_frequency", item)?;
882        }
883        if let Some(item) = &self.frequency_max {
884            super::super::codec::element_entry(&mut map, "_frequencyMax", item)?;
885        }
886        if let Some(item) = &self.offset {
887            super::super::codec::element_entry(&mut map, "_offset", item)?;
888        }
889        if let Some(item) = &self.period {
890            super::super::codec::element_entry(&mut map, "_period", item)?;
891        }
892        if let Some(item) = &self.period_max {
893            super::super::codec::element_entry(&mut map, "_periodMax", item)?;
894        }
895        if let Some(item) = &self.period_unit {
896            super::super::codec::element_entry(&mut map, "_periodUnit", item)?;
897        }
898        super::super::codec::element_list_entry(&mut map, "_timeOfDay", &self.time_of_day)?;
899        super::super::codec::element_list_entry(&mut map, "_when", &self.when)?;
900        match &self.bounds {
901            Some(TimingRepeatBounds::Duration(inner)) => {
902                serde::ser::SerializeMap::serialize_entry(
903                    &mut map,
904                    "boundsDuration",
905                    inner.as_ref(),
906                )?;
907            }
908            Some(TimingRepeatBounds::Period(inner)) => {
909                serde::ser::SerializeMap::serialize_entry(
910                    &mut map,
911                    "boundsPeriod",
912                    inner.as_ref(),
913                )?;
914            }
915            Some(TimingRepeatBounds::Range(inner)) => {
916                serde::ser::SerializeMap::serialize_entry(&mut map, "boundsRange", inner.as_ref())?;
917            }
918            None => {}
919        }
920        if let Some(item) = &self.count {
921            super::super::codec::value_entry(&mut map, "count", item)?;
922        }
923        if let Some(item) = &self.count_max {
924            super::super::codec::value_entry(&mut map, "countMax", item)?;
925        }
926        super::super::codec::value_list_entry(&mut map, "dayOfWeek", &self.day_of_week)?;
927        if let Some(item) = &self.duration {
928            super::super::codec::value_entry(&mut map, "duration", item)?;
929        }
930        if let Some(item) = &self.duration_max {
931            super::super::codec::value_entry(&mut map, "durationMax", item)?;
932        }
933        if let Some(item) = &self.duration_unit {
934            super::super::codec::value_entry(&mut map, "durationUnit", item)?;
935        }
936        if !self.extension.is_empty() {
937            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
938        }
939        if let Some(item) = &self.frequency {
940            super::super::codec::value_entry(&mut map, "frequency", item)?;
941        }
942        if let Some(item) = &self.frequency_max {
943            super::super::codec::value_entry(&mut map, "frequencyMax", item)?;
944        }
945        if let Some(v) = &self.id {
946            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
947        }
948        if let Some(item) = &self.offset {
949            super::super::codec::value_entry(&mut map, "offset", item)?;
950        }
951        if let Some(item) = &self.period {
952            super::super::codec::value_entry(&mut map, "period", item)?;
953        }
954        if let Some(item) = &self.period_max {
955            super::super::codec::value_entry(&mut map, "periodMax", item)?;
956        }
957        if let Some(item) = &self.period_unit {
958            super::super::codec::value_entry(&mut map, "periodUnit", item)?;
959        }
960        super::super::codec::value_list_entry(&mut map, "timeOfDay", &self.time_of_day)?;
961        super::super::codec::value_list_entry(&mut map, "when", &self.when)?;
962        serde::ser::SerializeMap::end(map)
963    }
964}
965
966impl<'de> serde::Deserialize<'de> for TimingRepeat {
967    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
968        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
969        let mut path = super::super::codec::Path::root("TimingRepeat");
970        let object =
971            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
972        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
973    }
974}
975
976/// The \`bounds\[x\]\` choice of \`TimingRepeat\`.
977///
978/// Either a duration for the length of the timing schedule, a range of possible
979/// length, or outer bounds for start and/or end limits of the timing schedule.
980#[derive(Debug, Clone, PartialEq, Eq)]
981pub enum TimingRepeatBounds {
982    /// The `Duration` form.
983    Duration(Box<super::duration::Duration>),
984    /// The `Range` form.
985    Range(Box<super::range::Range>),
986    /// The `Period` form.
987    Period(Box<super::period::Period>),
988}
989
990impl TimingRepeatBounds {
991    /// The key suffix, the value part, and the `_name` part of this form.
992    ///
993    /// # Errors
994    ///
995    /// Returns [`super::super::codec::EncodeError`] when a held value has no JSON form.
996    pub fn to_json_parts(
997        &self,
998    ) -> Result<
999        (
1000            &'static str,
1001            Option<super::super::codec::Value>,
1002            Option<super::super::codec::Value>,
1003        ),
1004        super::super::codec::EncodeError,
1005    > {
1006        match self {
1007            Self::Duration(inner) => Ok((
1008                "Duration",
1009                Some(super::super::codec::Value::Object(
1010                    super::super::codec::Json::to_json(inner.as_ref())?,
1011                )),
1012                None,
1013            )),
1014            Self::Range(inner) => Ok((
1015                "Range",
1016                Some(super::super::codec::Value::Object(
1017                    super::super::codec::Json::to_json(inner.as_ref())?,
1018                )),
1019                None,
1020            )),
1021            Self::Period(inner) => Ok((
1022                "Period",
1023                Some(super::super::codec::Value::Object(
1024                    super::super::codec::Json::to_json(inner.as_ref())?,
1025                )),
1026                None,
1027            )),
1028        }
1029    }
1030
1031    /// Decodes the form named by `suffix` from its value and `_name` parts.
1032    ///
1033    /// # Errors
1034    ///
1035    /// Returns [`super::super::codec::DecodeError`] for an unknown suffix or a malformed part.
1036    pub fn from_json_parts(
1037        suffix: &str,
1038        value: Option<&super::super::codec::Value>,
1039        element: Option<&super::super::codec::Value>,
1040        path: &mut super::super::codec::Path,
1041    ) -> Result<Self, super::super::codec::DecodeError> {
1042        match suffix {
1043            "Duration" => {
1044                if element.is_some() {
1045                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1046                        expected: "no underscore form for a complex type",
1047                    }));
1048                }
1049                let value = value.ok_or_else(|| {
1050                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1051                })?;
1052                let inner = super::super::codec::Json::from_json(
1053                    super::super::codec::expect_object(
1054                        super::super::codec::expect_single(value, path)?,
1055                        path,
1056                    )?,
1057                    path,
1058                )?;
1059                Ok(Self::Duration(Box::new(inner)))
1060            }
1061            "Range" => {
1062                if element.is_some() {
1063                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1064                        expected: "no underscore form for a complex type",
1065                    }));
1066                }
1067                let value = value.ok_or_else(|| {
1068                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1069                })?;
1070                let inner = super::super::codec::Json::from_json(
1071                    super::super::codec::expect_object(
1072                        super::super::codec::expect_single(value, path)?,
1073                        path,
1074                    )?,
1075                    path,
1076                )?;
1077                Ok(Self::Range(Box::new(inner)))
1078            }
1079            "Period" => {
1080                if element.is_some() {
1081                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1082                        expected: "no underscore form for a complex type",
1083                    }));
1084                }
1085                let value = value.ok_or_else(|| {
1086                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1087                })?;
1088                let inner = super::super::codec::Json::from_json(
1089                    super::super::codec::expect_object(
1090                        super::super::codec::expect_single(value, path)?,
1091                        path,
1092                    )?,
1093                    path,
1094                )?;
1095                Ok(Self::Period(Box::new(inner)))
1096            }
1097            _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
1098        }
1099    }
1100}