Skip to main content

fhir_types/r5/
timing.rs

1// @generated by fhir-codegen from hl7.fhir.r5.core 5.0.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    /// managable, there is a strict set of governance applied to the definition and
26    /// use of extensions. Though any implementer can define an extension, there is
27    /// 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 managable, 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    /// C | 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    /// managable, there is a strict set of governance applied to the definition and
314    /// use of extensions. Though any implementer can define an extension, there is
315    /// 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 Normal practice is to use
347    /// the 'mo' code as a calendar month when calculating the next occurrence.
348    pub duration_unit: Option<super::primitives::Code>,
349    /// Indicates the number of repetitions that should occur within a period. I.e.
350    /// Event occurs frequency times per period
351    ///
352    /// The number of times to repeat the action within the specified period. If
353    /// frequencyMax is present, this element indicates the lower bound of the
354    /// allowed range of the frequency.
355    pub frequency: Option<super::primitives::PositiveInt>,
356    /// Event occurs up to frequencyMax times per period
357    ///
358    /// If present, indicates that the frequency is a range - so to repeat between
359    /// \[frequency\] and \[frequencyMax\] times within the period or period range.
360    pub frequency_max: Option<super::primitives::PositiveInt>,
361    /// The duration to which the frequency applies. I.e. Event occurs frequency
362    /// times per period
363    ///
364    /// Indicates the duration of time over which repetitions are to occur; e.g. to
365    /// express "3 times per day", 3 would be the frequency and "1 day" would be the
366    /// period. If periodMax is present, this element indicates the lower bound of
367    /// the allowed range of the period length.
368    pub period: Option<super::primitives::Decimal>,
369    /// Upper limit of period (3-4 hours)
370    ///
371    /// If present, indicates that the period is a range from \[period\] to
372    /// \[periodMax\], allowing expressing concepts such as "do this once every 3-5
373    /// days.
374    pub period_max: Option<super::primitives::Decimal>,
375    /// s | min | h | d | wk | mo | a - unit of time (UCUM)
376    ///
377    /// The units of time for the period in UCUM units Normal practice is to use the
378    /// 'mo' code as a calendar month when calculating the next occurrence.
379    pub period_unit: Option<super::primitives::Code>,
380    /// mon | tue | wed | thu | fri | sat | sun
381    ///
382    /// If one or more days of week is provided, then the action happens only on the
383    /// specified day(s).
384    pub day_of_week: Vec<super::primitives::Code>,
385    /// Time of day for action
386    ///
387    /// Specified time of day for action to take place.
388    pub time_of_day: Vec<super::primitives::Time>,
389    /// Code for time period of occurrence
390    ///
391    /// An approximate time period during the day, potentially linked to an event of
392    /// daily living that indicates when the action should occur.
393    pub when: Vec<super::primitives::Code>,
394    /// Minutes from event (before or after)
395    ///
396    /// The number of minutes from the event. If the event code does not indicate
397    /// whether the minutes is before or after the event, then the offset is assumed
398    /// to be after the event.
399    pub offset: Option<super::primitives::UnsignedInt>,
400}
401
402impl super::super::codec::Json for TimingRepeat {
403    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
404        let mut object = super::super::codec::Object::new();
405        if let Some(v) = &self.id {
406            object.insert(
407                std::string::String::from("id"),
408                super::super::codec::Value::String(v.clone()),
409            );
410        }
411        if !self.extension.is_empty() {
412            let mut items = Vec::with_capacity(self.extension.len());
413            for item in &self.extension {
414                items.push(super::super::codec::Value::Object(
415                    super::super::codec::Json::to_json(item)?,
416                ));
417            }
418            object.insert(
419                std::string::String::from("extension"),
420                super::super::codec::Value::Array(items),
421            );
422        }
423        if let Some(item) = &self.bounds {
424            let (suffix, value, element) = item.to_json_parts()?;
425            if let Some(value) = value {
426                object.insert(format!("bounds{suffix}"), value);
427            }
428            if let Some(element) = element {
429                object.insert(format!("_bounds{suffix}"), element);
430            }
431        }
432        if let Some(item) = &self.count {
433            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
434                object.insert(std::string::String::from("count"), v);
435            }
436            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
437                object.insert(std::string::String::from("_count"), e);
438            }
439        }
440        if let Some(item) = &self.count_max {
441            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
442                object.insert(std::string::String::from("countMax"), v);
443            }
444            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
445                object.insert(std::string::String::from("_countMax"), e);
446            }
447        }
448        if let Some(item) = &self.duration {
449            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
450                object.insert(std::string::String::from("duration"), v);
451            }
452            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
453                object.insert(std::string::String::from("_duration"), e);
454            }
455        }
456        if let Some(item) = &self.duration_max {
457            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
458                object.insert(std::string::String::from("durationMax"), v);
459            }
460            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
461                object.insert(std::string::String::from("_durationMax"), e);
462            }
463        }
464        if let Some(item) = &self.duration_unit {
465            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
466                object.insert(std::string::String::from("durationUnit"), v);
467            }
468            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
469                object.insert(std::string::String::from("_durationUnit"), e);
470            }
471        }
472        if let Some(item) = &self.frequency {
473            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
474                object.insert(std::string::String::from("frequency"), v);
475            }
476            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
477                object.insert(std::string::String::from("_frequency"), e);
478            }
479        }
480        if let Some(item) = &self.frequency_max {
481            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
482                object.insert(std::string::String::from("frequencyMax"), v);
483            }
484            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
485                object.insert(std::string::String::from("_frequencyMax"), e);
486            }
487        }
488        if let Some(item) = &self.period {
489            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
490                object.insert(std::string::String::from("period"), v);
491            }
492            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
493                object.insert(std::string::String::from("_period"), e);
494            }
495        }
496        if let Some(item) = &self.period_max {
497            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
498                object.insert(std::string::String::from("periodMax"), v);
499            }
500            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
501                object.insert(std::string::String::from("_periodMax"), e);
502            }
503        }
504        if let Some(item) = &self.period_unit {
505            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
506                object.insert(std::string::String::from("periodUnit"), v);
507            }
508            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
509                object.insert(std::string::String::from("_periodUnit"), e);
510            }
511        }
512        if !self.day_of_week.is_empty() {
513            let (values, elements) = super::super::codec::primitive_arrays(&self.day_of_week)?;
514            if let Some(values) = values {
515                object.insert(std::string::String::from("dayOfWeek"), values);
516            }
517            if let Some(elements) = elements {
518                object.insert(std::string::String::from("_dayOfWeek"), elements);
519            }
520        }
521        if !self.time_of_day.is_empty() {
522            let (values, elements) = super::super::codec::primitive_arrays(&self.time_of_day)?;
523            if let Some(values) = values {
524                object.insert(std::string::String::from("timeOfDay"), values);
525            }
526            if let Some(elements) = elements {
527                object.insert(std::string::String::from("_timeOfDay"), elements);
528            }
529        }
530        if !self.when.is_empty() {
531            let (values, elements) = super::super::codec::primitive_arrays(&self.when)?;
532            if let Some(values) = values {
533                object.insert(std::string::String::from("when"), values);
534            }
535            if let Some(elements) = elements {
536                object.insert(std::string::String::from("_when"), elements);
537            }
538        }
539        if let Some(item) = &self.offset {
540            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
541                object.insert(std::string::String::from("offset"), v);
542            }
543            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
544                object.insert(std::string::String::from("_offset"), e);
545            }
546        }
547        Ok(object)
548    }
549
550    fn from_json(
551        object: &super::super::codec::Object,
552        path: &mut super::super::codec::Path,
553    ) -> Result<Self, super::super::codec::DecodeError> {
554        let mut raw_id: Option<&super::super::codec::Value> = None;
555        let mut raw_extension: Option<&super::super::codec::Value> = None;
556        let mut raw_bounds = super::super::codec::ChoiceSlot::default();
557        let mut raw_count: Option<&super::super::codec::Value> = None;
558        let mut raw_count_element: Option<&super::super::codec::Value> = None;
559        let mut raw_count_max: Option<&super::super::codec::Value> = None;
560        let mut raw_count_max_element: Option<&super::super::codec::Value> = None;
561        let mut raw_duration: Option<&super::super::codec::Value> = None;
562        let mut raw_duration_element: Option<&super::super::codec::Value> = None;
563        let mut raw_duration_max: Option<&super::super::codec::Value> = None;
564        let mut raw_duration_max_element: Option<&super::super::codec::Value> = None;
565        let mut raw_duration_unit: Option<&super::super::codec::Value> = None;
566        let mut raw_duration_unit_element: Option<&super::super::codec::Value> = None;
567        let mut raw_frequency: Option<&super::super::codec::Value> = None;
568        let mut raw_frequency_element: Option<&super::super::codec::Value> = None;
569        let mut raw_frequency_max: Option<&super::super::codec::Value> = None;
570        let mut raw_frequency_max_element: Option<&super::super::codec::Value> = None;
571        let mut raw_period: Option<&super::super::codec::Value> = None;
572        let mut raw_period_element: Option<&super::super::codec::Value> = None;
573        let mut raw_period_max: Option<&super::super::codec::Value> = None;
574        let mut raw_period_max_element: Option<&super::super::codec::Value> = None;
575        let mut raw_period_unit: Option<&super::super::codec::Value> = None;
576        let mut raw_period_unit_element: Option<&super::super::codec::Value> = None;
577        let mut raw_day_of_week: Option<&super::super::codec::Value> = None;
578        let mut raw_day_of_week_element: Option<&super::super::codec::Value> = None;
579        let mut raw_time_of_day: Option<&super::super::codec::Value> = None;
580        let mut raw_time_of_day_element: Option<&super::super::codec::Value> = None;
581        let mut raw_when: Option<&super::super::codec::Value> = None;
582        let mut raw_when_element: Option<&super::super::codec::Value> = None;
583        let mut raw_offset: Option<&super::super::codec::Value> = None;
584        let mut raw_offset_element: Option<&super::super::codec::Value> = None;
585        for (key, value) in object {
586            match key.as_str() {
587                "id" => {
588                    raw_id = Some(value);
589                }
590                "extension" => {
591                    raw_extension = Some(value);
592                }
593                "boundsDuration" => {
594                    raw_bounds.value("Duration", value, path)?;
595                }
596                "_boundsDuration" => {
597                    raw_bounds.element("Duration", value, path)?;
598                }
599                "boundsRange" => {
600                    raw_bounds.value("Range", value, path)?;
601                }
602                "_boundsRange" => {
603                    raw_bounds.element("Range", value, path)?;
604                }
605                "boundsPeriod" => {
606                    raw_bounds.value("Period", value, path)?;
607                }
608                "_boundsPeriod" => {
609                    raw_bounds.element("Period", value, path)?;
610                }
611                "count" => {
612                    raw_count = Some(value);
613                }
614                "_count" => {
615                    raw_count_element = Some(value);
616                }
617                "countMax" => {
618                    raw_count_max = Some(value);
619                }
620                "_countMax" => {
621                    raw_count_max_element = Some(value);
622                }
623                "duration" => {
624                    raw_duration = Some(value);
625                }
626                "_duration" => {
627                    raw_duration_element = Some(value);
628                }
629                "durationMax" => {
630                    raw_duration_max = Some(value);
631                }
632                "_durationMax" => {
633                    raw_duration_max_element = Some(value);
634                }
635                "durationUnit" => {
636                    raw_duration_unit = Some(value);
637                }
638                "_durationUnit" => {
639                    raw_duration_unit_element = Some(value);
640                }
641                "frequency" => {
642                    raw_frequency = Some(value);
643                }
644                "_frequency" => {
645                    raw_frequency_element = Some(value);
646                }
647                "frequencyMax" => {
648                    raw_frequency_max = Some(value);
649                }
650                "_frequencyMax" => {
651                    raw_frequency_max_element = Some(value);
652                }
653                "period" => {
654                    raw_period = Some(value);
655                }
656                "_period" => {
657                    raw_period_element = Some(value);
658                }
659                "periodMax" => {
660                    raw_period_max = Some(value);
661                }
662                "_periodMax" => {
663                    raw_period_max_element = Some(value);
664                }
665                "periodUnit" => {
666                    raw_period_unit = Some(value);
667                }
668                "_periodUnit" => {
669                    raw_period_unit_element = Some(value);
670                }
671                "dayOfWeek" => {
672                    raw_day_of_week = Some(value);
673                }
674                "_dayOfWeek" => {
675                    raw_day_of_week_element = Some(value);
676                }
677                "timeOfDay" => {
678                    raw_time_of_day = Some(value);
679                }
680                "_timeOfDay" => {
681                    raw_time_of_day_element = Some(value);
682                }
683                "when" => {
684                    raw_when = Some(value);
685                }
686                "_when" => {
687                    raw_when_element = Some(value);
688                }
689                "offset" => {
690                    raw_offset = Some(value);
691                }
692                "_offset" => {
693                    raw_offset_element = Some(value);
694                }
695                other => {
696                    return path.with(other, |path| {
697                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
698                    });
699                }
700            }
701        }
702        let field_id = raw_id
703            .map(|value| {
704                path.with("id", |path| {
705                    let value = super::super::codec::expect_single(value, path)?;
706                    super::primitives::checked_string(
707                        super::super::codec::expect_string(value, path)?,
708                        path,
709                    )
710                })
711            })
712            .transpose()?;
713        let mut field_extension = Vec::new();
714        if let Some(raw) = raw_extension {
715            for (index, value) in path
716                .with("extension", |path| {
717                    super::super::codec::expect_array(raw, path)
718                })?
719                .iter()
720                .enumerate()
721            {
722                field_extension.push(path.with_index("extension", index, |path| {
723                    super::super::codec::Json::from_json(
724                        super::super::codec::expect_object(value, path)?,
725                        path,
726                    )
727                })?);
728            }
729        }
730        let field_bounds = match raw_bounds.suffix {
731            None => None,
732            Some(suffix) => Some(path.with("bounds", |path| {
733                TimingRepeatBounds::from_json_parts(
734                    suffix,
735                    raw_bounds.value,
736                    raw_bounds.element,
737                    path,
738                )
739            })?),
740        };
741        let field_count = match (raw_count, raw_count_element) {
742            (None, None) => None,
743            (value, element) => Some(path.with("count", |path| {
744                super::super::codec::Primitive::from_json_parts(value, element, path)
745            })?),
746        };
747        let field_count_max = match (raw_count_max, raw_count_max_element) {
748            (None, None) => None,
749            (value, element) => Some(path.with("countMax", |path| {
750                super::super::codec::Primitive::from_json_parts(value, element, path)
751            })?),
752        };
753        let field_duration = match (raw_duration, raw_duration_element) {
754            (None, None) => None,
755            (value, element) => Some(path.with("duration", |path| {
756                super::super::codec::Primitive::from_json_parts(value, element, path)
757            })?),
758        };
759        let field_duration_max = match (raw_duration_max, raw_duration_max_element) {
760            (None, None) => None,
761            (value, element) => Some(path.with("durationMax", |path| {
762                super::super::codec::Primitive::from_json_parts(value, element, path)
763            })?),
764        };
765        let field_duration_unit = match (raw_duration_unit, raw_duration_unit_element) {
766            (None, None) => None,
767            (value, element) => Some(path.with("durationUnit", |path| {
768                super::super::codec::Primitive::from_json_parts(value, element, path)
769            })?),
770        };
771        let field_frequency = match (raw_frequency, raw_frequency_element) {
772            (None, None) => None,
773            (value, element) => Some(path.with("frequency", |path| {
774                super::super::codec::Primitive::from_json_parts(value, element, path)
775            })?),
776        };
777        let field_frequency_max = match (raw_frequency_max, raw_frequency_max_element) {
778            (None, None) => None,
779            (value, element) => Some(path.with("frequencyMax", |path| {
780                super::super::codec::Primitive::from_json_parts(value, element, path)
781            })?),
782        };
783        let field_period = match (raw_period, raw_period_element) {
784            (None, None) => None,
785            (value, element) => Some(path.with("period", |path| {
786                super::super::codec::Primitive::from_json_parts(value, element, path)
787            })?),
788        };
789        let field_period_max = match (raw_period_max, raw_period_max_element) {
790            (None, None) => None,
791            (value, element) => Some(path.with("periodMax", |path| {
792                super::super::codec::Primitive::from_json_parts(value, element, path)
793            })?),
794        };
795        let field_period_unit = match (raw_period_unit, raw_period_unit_element) {
796            (None, None) => None,
797            (value, element) => Some(path.with("periodUnit", |path| {
798                super::super::codec::Primitive::from_json_parts(value, element, path)
799            })?),
800        };
801        let mut field_day_of_week = Vec::new();
802        for (index, (value, element)) in path
803            .with("dayOfWeek", |path| {
804                super::super::codec::pair_arrays(raw_day_of_week, raw_day_of_week_element, path)
805            })?
806            .into_iter()
807            .enumerate()
808        {
809            field_day_of_week.push(path.with_index("dayOfWeek", index, |path| {
810                super::super::codec::Primitive::from_json_parts(value, element, path)
811            })?);
812        }
813        let mut field_time_of_day = Vec::new();
814        for (index, (value, element)) in path
815            .with("timeOfDay", |path| {
816                super::super::codec::pair_arrays(raw_time_of_day, raw_time_of_day_element, path)
817            })?
818            .into_iter()
819            .enumerate()
820        {
821            field_time_of_day.push(path.with_index("timeOfDay", index, |path| {
822                super::super::codec::Primitive::from_json_parts(value, element, path)
823            })?);
824        }
825        let mut field_when = Vec::new();
826        for (index, (value, element)) in path
827            .with("when", |path| {
828                super::super::codec::pair_arrays(raw_when, raw_when_element, path)
829            })?
830            .into_iter()
831            .enumerate()
832        {
833            field_when.push(path.with_index("when", index, |path| {
834                super::super::codec::Primitive::from_json_parts(value, element, path)
835            })?);
836        }
837        let field_offset = match (raw_offset, raw_offset_element) {
838            (None, None) => None,
839            (value, element) => Some(path.with("offset", |path| {
840                super::super::codec::Primitive::from_json_parts(value, element, path)
841            })?),
842        };
843        Ok(Self {
844            id: field_id,
845            extension: field_extension,
846            bounds: field_bounds,
847            count: field_count,
848            count_max: field_count_max,
849            duration: field_duration,
850            duration_max: field_duration_max,
851            duration_unit: field_duration_unit,
852            frequency: field_frequency,
853            frequency_max: field_frequency_max,
854            period: field_period,
855            period_max: field_period_max,
856            period_unit: field_period_unit,
857            day_of_week: field_day_of_week,
858            time_of_day: field_time_of_day,
859            when: field_when,
860            offset: field_offset,
861        })
862    }
863}
864
865impl serde::Serialize for TimingRepeat {
866    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
867        let mut map = serde::Serializer::serialize_map(serializer, None)?;
868        if let Some(item) = &self.count {
869            super::super::codec::element_entry(&mut map, "_count", item)?;
870        }
871        if let Some(item) = &self.count_max {
872            super::super::codec::element_entry(&mut map, "_countMax", item)?;
873        }
874        super::super::codec::element_list_entry(&mut map, "_dayOfWeek", &self.day_of_week)?;
875        if let Some(item) = &self.duration {
876            super::super::codec::element_entry(&mut map, "_duration", item)?;
877        }
878        if let Some(item) = &self.duration_max {
879            super::super::codec::element_entry(&mut map, "_durationMax", item)?;
880        }
881        if let Some(item) = &self.duration_unit {
882            super::super::codec::element_entry(&mut map, "_durationUnit", item)?;
883        }
884        if let Some(item) = &self.frequency {
885            super::super::codec::element_entry(&mut map, "_frequency", item)?;
886        }
887        if let Some(item) = &self.frequency_max {
888            super::super::codec::element_entry(&mut map, "_frequencyMax", item)?;
889        }
890        if let Some(item) = &self.offset {
891            super::super::codec::element_entry(&mut map, "_offset", item)?;
892        }
893        if let Some(item) = &self.period {
894            super::super::codec::element_entry(&mut map, "_period", item)?;
895        }
896        if let Some(item) = &self.period_max {
897            super::super::codec::element_entry(&mut map, "_periodMax", item)?;
898        }
899        if let Some(item) = &self.period_unit {
900            super::super::codec::element_entry(&mut map, "_periodUnit", item)?;
901        }
902        super::super::codec::element_list_entry(&mut map, "_timeOfDay", &self.time_of_day)?;
903        super::super::codec::element_list_entry(&mut map, "_when", &self.when)?;
904        match &self.bounds {
905            Some(TimingRepeatBounds::Duration(inner)) => {
906                serde::ser::SerializeMap::serialize_entry(
907                    &mut map,
908                    "boundsDuration",
909                    inner.as_ref(),
910                )?;
911            }
912            Some(TimingRepeatBounds::Period(inner)) => {
913                serde::ser::SerializeMap::serialize_entry(
914                    &mut map,
915                    "boundsPeriod",
916                    inner.as_ref(),
917                )?;
918            }
919            Some(TimingRepeatBounds::Range(inner)) => {
920                serde::ser::SerializeMap::serialize_entry(&mut map, "boundsRange", inner.as_ref())?;
921            }
922            None => {}
923        }
924        if let Some(item) = &self.count {
925            super::super::codec::value_entry(&mut map, "count", item)?;
926        }
927        if let Some(item) = &self.count_max {
928            super::super::codec::value_entry(&mut map, "countMax", item)?;
929        }
930        super::super::codec::value_list_entry(&mut map, "dayOfWeek", &self.day_of_week)?;
931        if let Some(item) = &self.duration {
932            super::super::codec::value_entry(&mut map, "duration", item)?;
933        }
934        if let Some(item) = &self.duration_max {
935            super::super::codec::value_entry(&mut map, "durationMax", item)?;
936        }
937        if let Some(item) = &self.duration_unit {
938            super::super::codec::value_entry(&mut map, "durationUnit", item)?;
939        }
940        if !self.extension.is_empty() {
941            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
942        }
943        if let Some(item) = &self.frequency {
944            super::super::codec::value_entry(&mut map, "frequency", item)?;
945        }
946        if let Some(item) = &self.frequency_max {
947            super::super::codec::value_entry(&mut map, "frequencyMax", item)?;
948        }
949        if let Some(v) = &self.id {
950            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
951        }
952        if let Some(item) = &self.offset {
953            super::super::codec::value_entry(&mut map, "offset", item)?;
954        }
955        if let Some(item) = &self.period {
956            super::super::codec::value_entry(&mut map, "period", item)?;
957        }
958        if let Some(item) = &self.period_max {
959            super::super::codec::value_entry(&mut map, "periodMax", item)?;
960        }
961        if let Some(item) = &self.period_unit {
962            super::super::codec::value_entry(&mut map, "periodUnit", item)?;
963        }
964        super::super::codec::value_list_entry(&mut map, "timeOfDay", &self.time_of_day)?;
965        super::super::codec::value_list_entry(&mut map, "when", &self.when)?;
966        serde::ser::SerializeMap::end(map)
967    }
968}
969
970impl<'de> serde::Deserialize<'de> for TimingRepeat {
971    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
972        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
973        let mut path = super::super::codec::Path::root("TimingRepeat");
974        let object =
975            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
976        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
977    }
978}
979
980/// The \`bounds\[x\]\` choice of \`TimingRepeat\`.
981///
982/// Either a duration for the length of the timing schedule, a range of possible
983/// length, or outer bounds for start and/or end limits of the timing schedule.
984#[derive(Debug, Clone, PartialEq, Eq)]
985pub enum TimingRepeatBounds {
986    /// The `Duration` form.
987    Duration(Box<super::duration::Duration>),
988    /// The `Range` form.
989    Range(Box<super::range::Range>),
990    /// The `Period` form.
991    Period(Box<super::period::Period>),
992}
993
994impl TimingRepeatBounds {
995    /// The key suffix, the value part, and the `_name` part of this form.
996    ///
997    /// # Errors
998    ///
999    /// Returns [`super::super::codec::EncodeError`] when a held value has no JSON form.
1000    pub fn to_json_parts(
1001        &self,
1002    ) -> Result<
1003        (
1004            &'static str,
1005            Option<super::super::codec::Value>,
1006            Option<super::super::codec::Value>,
1007        ),
1008        super::super::codec::EncodeError,
1009    > {
1010        match self {
1011            Self::Duration(inner) => Ok((
1012                "Duration",
1013                Some(super::super::codec::Value::Object(
1014                    super::super::codec::Json::to_json(inner.as_ref())?,
1015                )),
1016                None,
1017            )),
1018            Self::Range(inner) => Ok((
1019                "Range",
1020                Some(super::super::codec::Value::Object(
1021                    super::super::codec::Json::to_json(inner.as_ref())?,
1022                )),
1023                None,
1024            )),
1025            Self::Period(inner) => Ok((
1026                "Period",
1027                Some(super::super::codec::Value::Object(
1028                    super::super::codec::Json::to_json(inner.as_ref())?,
1029                )),
1030                None,
1031            )),
1032        }
1033    }
1034
1035    /// Decodes the form named by `suffix` from its value and `_name` parts.
1036    ///
1037    /// # Errors
1038    ///
1039    /// Returns [`super::super::codec::DecodeError`] for an unknown suffix or a malformed part.
1040    pub fn from_json_parts(
1041        suffix: &str,
1042        value: Option<&super::super::codec::Value>,
1043        element: Option<&super::super::codec::Value>,
1044        path: &mut super::super::codec::Path,
1045    ) -> Result<Self, super::super::codec::DecodeError> {
1046        match suffix {
1047            "Duration" => {
1048                if element.is_some() {
1049                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1050                        expected: "no underscore form for a complex type",
1051                    }));
1052                }
1053                let value = value.ok_or_else(|| {
1054                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1055                })?;
1056                let inner = super::super::codec::Json::from_json(
1057                    super::super::codec::expect_object(
1058                        super::super::codec::expect_single(value, path)?,
1059                        path,
1060                    )?,
1061                    path,
1062                )?;
1063                Ok(Self::Duration(Box::new(inner)))
1064            }
1065            "Range" => {
1066                if element.is_some() {
1067                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1068                        expected: "no underscore form for a complex type",
1069                    }));
1070                }
1071                let value = value.ok_or_else(|| {
1072                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1073                })?;
1074                let inner = super::super::codec::Json::from_json(
1075                    super::super::codec::expect_object(
1076                        super::super::codec::expect_single(value, path)?,
1077                        path,
1078                    )?,
1079                    path,
1080                )?;
1081                Ok(Self::Range(Box::new(inner)))
1082            }
1083            "Period" => {
1084                if element.is_some() {
1085                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
1086                        expected: "no underscore form for a complex type",
1087                    }));
1088                }
1089                let value = value.ok_or_else(|| {
1090                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
1091                })?;
1092                let inner = super::super::codec::Json::from_json(
1093                    super::super::codec::expect_object(
1094                        super::super::codec::expect_single(value, path)?,
1095                        path,
1096                    )?,
1097                    path,
1098                )?;
1099                Ok(Self::Period(Box::new(inner)))
1100            }
1101            _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
1102        }
1103    }
1104}