Skip to main content

fhir_types/r5/
trigger_definition.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//! Defines an expected trigger for a module
6
7/// Defines an expected trigger for a module
8///
9/// A description of a triggering event. Triggering events can be named events,
10/// data events, or periodic, as determined by the type element.
11#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct TriggerDefinition {
13    /// Unique id for inter-element referencing
14    ///
15    /// Unique id for the element within a resource (for internal references). This
16    /// may be any string value that does not contain spaces.
17    pub id: Option<std::string::String>,
18    /// Additional content defined by implementations
19    ///
20    /// May be used to represent additional information that is not part of the
21    /// basic definition of the element. To make the use of extensions safe and
22    /// managable, there is a strict set of governance applied to the definition and
23    /// use of extensions. Though any implementer can define an extension, there is
24    /// a set of requirements that SHALL be met as part of the definition of the
25    /// extension.
26    pub extension: Vec<super::extension::Extension>,
27    /// named-event | periodic | data-changed | data-added | data-modified |
28    /// data-removed | data-accessed | data-access-ended
29    ///
30    /// The type of triggering event.
31    pub r#type: super::primitives::Code,
32    /// Name or URI that identifies the event
33    ///
34    /// A formal name for the event. This may be an absolute URI that identifies the
35    /// event formally (e.g. from a trigger registry), or a simple relative URI that
36    /// identifies the event in a local context.
37    pub name: Option<super::primitives::String>,
38    /// Coded definition of the event
39    ///
40    /// A code that identifies the event.
41    pub code: Option<super::codeable_concept::CodeableConcept>,
42    /// What event
43    ///
44    /// A reference to a SubscriptionTopic resource that defines the event. If this
45    /// element is provided, no other information about the trigger definition may
46    /// be supplied.
47    pub subscription_topic: Option<super::primitives::Canonical>,
48    /// Timing of the event
49    ///
50    /// The timing of the event (if this is a periodic trigger).
51    pub timing: Option<TriggerDefinitionTiming>,
52    /// Triggering data of the event (multiple = 'and')
53    ///
54    /// The triggering data of the event (if this is a data trigger). If more than
55    /// one data is requirement is specified, then all the data requirements must be
56    /// true.
57    pub data: Vec<super::data_requirement::DataRequirement>,
58    /// Whether the event triggers (boolean expression)
59    ///
60    /// A boolean-valued expression that is evaluated in the context of the
61    /// container of the trigger definition and returns whether or not the trigger
62    /// fires.
63    pub condition: Option<super::expression::Expression>,
64}
65
66impl super::super::codec::Json for TriggerDefinition {
67    fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
68        let mut object = super::super::codec::Object::new();
69        if let Some(v) = &self.id {
70            object.insert(
71                std::string::String::from("id"),
72                super::super::codec::Value::String(v.clone()),
73            );
74        }
75        if !self.extension.is_empty() {
76            let mut items = Vec::with_capacity(self.extension.len());
77            for item in &self.extension {
78                items.push(super::super::codec::Value::Object(
79                    super::super::codec::Json::to_json(item)?,
80                ));
81            }
82            object.insert(
83                std::string::String::from("extension"),
84                super::super::codec::Value::Array(items),
85            );
86        }
87        if let Some(v) = super::super::codec::Primitive::value_json(&self.r#type)? {
88            object.insert(std::string::String::from("type"), v);
89        }
90        if let Some(e) = super::super::codec::Primitive::element_json(&self.r#type)? {
91            object.insert(std::string::String::from("_type"), e);
92        }
93        if let Some(item) = &self.name {
94            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
95                object.insert(std::string::String::from("name"), v);
96            }
97            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
98                object.insert(std::string::String::from("_name"), e);
99            }
100        }
101        if let Some(item) = &self.code {
102            object.insert(
103                std::string::String::from("code"),
104                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
105            );
106        }
107        if let Some(item) = &self.subscription_topic {
108            if let Some(v) = super::super::codec::Primitive::value_json(item)? {
109                object.insert(std::string::String::from("subscriptionTopic"), v);
110            }
111            if let Some(e) = super::super::codec::Primitive::element_json(item)? {
112                object.insert(std::string::String::from("_subscriptionTopic"), e);
113            }
114        }
115        if let Some(item) = &self.timing {
116            let (suffix, value, element) = item.to_json_parts()?;
117            if let Some(value) = value {
118                object.insert(format!("timing{suffix}"), value);
119            }
120            if let Some(element) = element {
121                object.insert(format!("_timing{suffix}"), element);
122            }
123        }
124        if !self.data.is_empty() {
125            let mut items = Vec::with_capacity(self.data.len());
126            for item in &self.data {
127                items.push(super::super::codec::Value::Object(
128                    super::super::codec::Json::to_json(item)?,
129                ));
130            }
131            object.insert(
132                std::string::String::from("data"),
133                super::super::codec::Value::Array(items),
134            );
135        }
136        if let Some(item) = &self.condition {
137            object.insert(
138                std::string::String::from("condition"),
139                super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
140            );
141        }
142        Ok(object)
143    }
144
145    fn from_json(
146        object: &super::super::codec::Object,
147        path: &mut super::super::codec::Path,
148    ) -> Result<Self, super::super::codec::DecodeError> {
149        let mut raw_id: Option<&super::super::codec::Value> = None;
150        let mut raw_extension: Option<&super::super::codec::Value> = None;
151        let mut raw_type: Option<&super::super::codec::Value> = None;
152        let mut raw_type_element: Option<&super::super::codec::Value> = None;
153        let mut raw_name: Option<&super::super::codec::Value> = None;
154        let mut raw_name_element: Option<&super::super::codec::Value> = None;
155        let mut raw_code: Option<&super::super::codec::Value> = None;
156        let mut raw_subscription_topic: Option<&super::super::codec::Value> = None;
157        let mut raw_subscription_topic_element: Option<&super::super::codec::Value> = None;
158        let mut raw_timing = super::super::codec::ChoiceSlot::default();
159        let mut raw_data: Option<&super::super::codec::Value> = None;
160        let mut raw_condition: Option<&super::super::codec::Value> = None;
161        for (key, value) in object {
162            match key.as_str() {
163                "id" => {
164                    raw_id = Some(value);
165                }
166                "extension" => {
167                    raw_extension = Some(value);
168                }
169                "type" => {
170                    raw_type = Some(value);
171                }
172                "_type" => {
173                    raw_type_element = Some(value);
174                }
175                "name" => {
176                    raw_name = Some(value);
177                }
178                "_name" => {
179                    raw_name_element = Some(value);
180                }
181                "code" => {
182                    raw_code = Some(value);
183                }
184                "subscriptionTopic" => {
185                    raw_subscription_topic = Some(value);
186                }
187                "_subscriptionTopic" => {
188                    raw_subscription_topic_element = Some(value);
189                }
190                "timingTiming" => {
191                    raw_timing.value("Timing", value, path)?;
192                }
193                "_timingTiming" => {
194                    raw_timing.element("Timing", value, path)?;
195                }
196                "timingReference" => {
197                    raw_timing.value("Reference", value, path)?;
198                }
199                "_timingReference" => {
200                    raw_timing.element("Reference", value, path)?;
201                }
202                "timingDate" => {
203                    raw_timing.value("Date", value, path)?;
204                }
205                "_timingDate" => {
206                    raw_timing.element("Date", value, path)?;
207                }
208                "timingDateTime" => {
209                    raw_timing.value("DateTime", value, path)?;
210                }
211                "_timingDateTime" => {
212                    raw_timing.element("DateTime", value, path)?;
213                }
214                "data" => {
215                    raw_data = Some(value);
216                }
217                "condition" => {
218                    raw_condition = Some(value);
219                }
220                other => {
221                    return path.with(other, |path| {
222                        Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
223                    });
224                }
225            }
226        }
227        let field_id = raw_id
228            .map(|value| {
229                path.with("id", |path| {
230                    let value = super::super::codec::expect_single(value, path)?;
231                    super::primitives::checked_string(
232                        super::super::codec::expect_string(value, path)?,
233                        path,
234                    )
235                })
236            })
237            .transpose()?;
238        let mut field_extension = Vec::new();
239        if let Some(raw) = raw_extension {
240            for (index, value) in path
241                .with("extension", |path| {
242                    super::super::codec::expect_array(raw, path)
243                })?
244                .iter()
245                .enumerate()
246            {
247                field_extension.push(path.with_index("extension", index, |path| {
248                    super::super::codec::Json::from_json(
249                        super::super::codec::expect_object(value, path)?,
250                        path,
251                    )
252                })?);
253            }
254        }
255        let field_type = path.with("type", |path| {
256            super::super::codec::Primitive::from_json_parts(raw_type, raw_type_element, path)
257        })?;
258        let field_name = match (raw_name, raw_name_element) {
259            (None, None) => None,
260            (value, element) => Some(path.with("name", |path| {
261                super::super::codec::Primitive::from_json_parts(value, element, path)
262            })?),
263        };
264        let field_code = raw_code
265            .map(|value| {
266                path.with("code", |path| {
267                    super::super::codec::Json::from_json(
268                        super::super::codec::expect_object(
269                            super::super::codec::expect_single(value, path)?,
270                            path,
271                        )?,
272                        path,
273                    )
274                })
275            })
276            .transpose()?;
277        let field_subscription_topic =
278            match (raw_subscription_topic, raw_subscription_topic_element) {
279                (None, None) => None,
280                (value, element) => Some(path.with("subscriptionTopic", |path| {
281                    super::super::codec::Primitive::from_json_parts(value, element, path)
282                })?),
283            };
284        let field_timing = match raw_timing.suffix {
285            None => None,
286            Some(suffix) => Some(path.with("timing", |path| {
287                TriggerDefinitionTiming::from_json_parts(
288                    suffix,
289                    raw_timing.value,
290                    raw_timing.element,
291                    path,
292                )
293            })?),
294        };
295        let mut field_data = Vec::new();
296        if let Some(raw) = raw_data {
297            for (index, value) in path
298                .with("data", |path| super::super::codec::expect_array(raw, path))?
299                .iter()
300                .enumerate()
301            {
302                field_data.push(path.with_index("data", index, |path| {
303                    super::super::codec::Json::from_json(
304                        super::super::codec::expect_object(value, path)?,
305                        path,
306                    )
307                })?);
308            }
309        }
310        let field_condition = raw_condition
311            .map(|value| {
312                path.with("condition", |path| {
313                    super::super::codec::Json::from_json(
314                        super::super::codec::expect_object(
315                            super::super::codec::expect_single(value, path)?,
316                            path,
317                        )?,
318                        path,
319                    )
320                })
321            })
322            .transpose()?;
323        Ok(Self {
324            id: field_id,
325            extension: field_extension,
326            r#type: field_type,
327            name: field_name,
328            code: field_code,
329            subscription_topic: field_subscription_topic,
330            timing: field_timing,
331            data: field_data,
332            condition: field_condition,
333        })
334    }
335}
336
337impl serde::Serialize for TriggerDefinition {
338    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
339        let mut map = serde::Serializer::serialize_map(serializer, None)?;
340        if let Some(item) = &self.name {
341            super::super::codec::element_entry(&mut map, "_name", item)?;
342        }
343        if let Some(item) = &self.subscription_topic {
344            super::super::codec::element_entry(&mut map, "_subscriptionTopic", item)?;
345        }
346        match &self.timing {
347            Some(TriggerDefinitionTiming::Date(inner)) => {
348                super::super::codec::element_entry(&mut map, "_timingDate", inner)?;
349            }
350            Some(TriggerDefinitionTiming::DateTime(inner)) => {
351                super::super::codec::element_entry(&mut map, "_timingDateTime", inner)?;
352            }
353            _ => {}
354        }
355        super::super::codec::element_entry(&mut map, "_type", &self.r#type)?;
356        if let Some(item) = &self.code {
357            serde::ser::SerializeMap::serialize_entry(&mut map, "code", item)?;
358        }
359        if let Some(item) = &self.condition {
360            serde::ser::SerializeMap::serialize_entry(&mut map, "condition", item)?;
361        }
362        if !self.data.is_empty() {
363            serde::ser::SerializeMap::serialize_entry(&mut map, "data", &self.data)?;
364        }
365        if !self.extension.is_empty() {
366            serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
367        }
368        if let Some(v) = &self.id {
369            serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
370        }
371        if let Some(item) = &self.name {
372            super::super::codec::value_entry(&mut map, "name", item)?;
373        }
374        if let Some(item) = &self.subscription_topic {
375            super::super::codec::value_entry(&mut map, "subscriptionTopic", item)?;
376        }
377        match &self.timing {
378            Some(TriggerDefinitionTiming::Date(inner)) => {
379                super::super::codec::value_entry(&mut map, "timingDate", inner)?;
380            }
381            Some(TriggerDefinitionTiming::DateTime(inner)) => {
382                super::super::codec::value_entry(&mut map, "timingDateTime", inner)?;
383            }
384            Some(TriggerDefinitionTiming::Reference(inner)) => {
385                serde::ser::SerializeMap::serialize_entry(
386                    &mut map,
387                    "timingReference",
388                    inner.as_ref(),
389                )?;
390            }
391            Some(TriggerDefinitionTiming::Timing(inner)) => {
392                serde::ser::SerializeMap::serialize_entry(
393                    &mut map,
394                    "timingTiming",
395                    inner.as_ref(),
396                )?;
397            }
398            None => {}
399        }
400        super::super::codec::value_entry(&mut map, "type", &self.r#type)?;
401        serde::ser::SerializeMap::end(map)
402    }
403}
404
405impl<'de> serde::Deserialize<'de> for TriggerDefinition {
406    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
407        let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
408        let mut path = super::super::codec::Path::root("TriggerDefinition");
409        let object =
410            super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
411        super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
412    }
413}
414
415/// The \`timing\[x\]\` choice of \`TriggerDefinition\`.
416///
417/// The timing of the event (if this is a periodic trigger).
418#[derive(Debug, Clone, PartialEq, Eq)]
419pub enum TriggerDefinitionTiming {
420    /// The `Timing` form.
421    Timing(Box<super::timing::Timing>),
422    /// The `Reference` form.
423    Reference(Box<super::reference::Reference>),
424    /// The `date` form.
425    Date(super::primitives::Date),
426    /// The `dateTime` form.
427    DateTime(super::primitives::DateTime),
428}
429
430impl TriggerDefinitionTiming {
431    /// The key suffix, the value part, and the `_name` part of this form.
432    ///
433    /// # Errors
434    ///
435    /// Returns [`super::super::codec::EncodeError`] when a held value has no JSON form.
436    pub fn to_json_parts(
437        &self,
438    ) -> Result<
439        (
440            &'static str,
441            Option<super::super::codec::Value>,
442            Option<super::super::codec::Value>,
443        ),
444        super::super::codec::EncodeError,
445    > {
446        match self {
447            Self::Timing(inner) => Ok((
448                "Timing",
449                Some(super::super::codec::Value::Object(
450                    super::super::codec::Json::to_json(inner.as_ref())?,
451                )),
452                None,
453            )),
454            Self::Reference(inner) => Ok((
455                "Reference",
456                Some(super::super::codec::Value::Object(
457                    super::super::codec::Json::to_json(inner.as_ref())?,
458                )),
459                None,
460            )),
461            Self::Date(inner) => Ok((
462                "Date",
463                super::super::codec::Primitive::value_json(inner)?,
464                super::super::codec::Primitive::element_json(inner)?,
465            )),
466            Self::DateTime(inner) => Ok((
467                "DateTime",
468                super::super::codec::Primitive::value_json(inner)?,
469                super::super::codec::Primitive::element_json(inner)?,
470            )),
471        }
472    }
473
474    /// Decodes the form named by `suffix` from its value and `_name` parts.
475    ///
476    /// # Errors
477    ///
478    /// Returns [`super::super::codec::DecodeError`] for an unknown suffix or a malformed part.
479    pub fn from_json_parts(
480        suffix: &str,
481        value: Option<&super::super::codec::Value>,
482        element: Option<&super::super::codec::Value>,
483        path: &mut super::super::codec::Path,
484    ) -> Result<Self, super::super::codec::DecodeError> {
485        match suffix {
486            "Timing" => {
487                if element.is_some() {
488                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
489                        expected: "no underscore form for a complex type",
490                    }));
491                }
492                let value = value.ok_or_else(|| {
493                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
494                })?;
495                let inner = super::super::codec::Json::from_json(
496                    super::super::codec::expect_object(
497                        super::super::codec::expect_single(value, path)?,
498                        path,
499                    )?,
500                    path,
501                )?;
502                Ok(Self::Timing(Box::new(inner)))
503            }
504            "Reference" => {
505                if element.is_some() {
506                    return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
507                        expected: "no underscore form for a complex type",
508                    }));
509                }
510                let value = value.ok_or_else(|| {
511                    path.error(super::super::codec::DecodeErrorKind::MissingProperty)
512                })?;
513                let inner = super::super::codec::Json::from_json(
514                    super::super::codec::expect_object(
515                        super::super::codec::expect_single(value, path)?,
516                        path,
517                    )?,
518                    path,
519                )?;
520                Ok(Self::Reference(Box::new(inner)))
521            }
522            "Date" => Ok(Self::Date(super::super::codec::Primitive::from_json_parts(
523                value, element, path,
524            )?)),
525            "DateTime" => Ok(Self::DateTime(
526                super::super::codec::Primitive::from_json_parts(value, element, path)?,
527            )),
528            _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
529        }
530    }
531}