1#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct TriggerDefinition {
13 pub id: Option<std::string::String>,
18 pub extension: Vec<super::extension::Extension>,
27 pub r#type: super::primitives::Code,
32 pub name: Option<super::primitives::String>,
38 pub code: Option<super::codeable_concept::CodeableConcept>,
42 pub subscription_topic: Option<super::primitives::Canonical>,
48 pub timing: Option<TriggerDefinitionTiming>,
52 pub data: Vec<super::data_requirement::DataRequirement>,
58 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 "timingDate" => {
197 raw_timing.value("Date", value, path)?;
198 }
199 "_timingDate" => {
200 raw_timing.element("Date", value, path)?;
201 }
202 "timingDateTime" => {
203 raw_timing.value("DateTime", value, path)?;
204 }
205 "_timingDateTime" => {
206 raw_timing.element("DateTime", value, path)?;
207 }
208 "data" => {
209 raw_data = Some(value);
210 }
211 "condition" => {
212 raw_condition = Some(value);
213 }
214 other => {
215 return path.with(other, |path| {
216 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
217 });
218 }
219 }
220 }
221 let field_id = raw_id
222 .map(|value| {
223 path.with("id", |path| {
224 let value = super::super::codec::expect_single(value, path)?;
225 super::primitives::checked_string(
226 super::super::codec::expect_string(value, path)?,
227 path,
228 )
229 })
230 })
231 .transpose()?;
232 let mut field_extension = Vec::new();
233 if let Some(raw) = raw_extension {
234 for (index, value) in path
235 .with("extension", |path| {
236 super::super::codec::expect_array(raw, path)
237 })?
238 .iter()
239 .enumerate()
240 {
241 field_extension.push(path.with_index("extension", index, |path| {
242 super::super::codec::Json::from_json(
243 super::super::codec::expect_object(value, path)?,
244 path,
245 )
246 })?);
247 }
248 }
249 let field_type = path.with("type", |path| {
250 super::super::codec::Primitive::from_json_parts(raw_type, raw_type_element, path)
251 })?;
252 let field_name = match (raw_name, raw_name_element) {
253 (None, None) => None,
254 (value, element) => Some(path.with("name", |path| {
255 super::super::codec::Primitive::from_json_parts(value, element, path)
256 })?),
257 };
258 let field_code = raw_code
259 .map(|value| {
260 path.with("code", |path| {
261 super::super::codec::Json::from_json(
262 super::super::codec::expect_object(
263 super::super::codec::expect_single(value, path)?,
264 path,
265 )?,
266 path,
267 )
268 })
269 })
270 .transpose()?;
271 let field_subscription_topic =
272 match (raw_subscription_topic, raw_subscription_topic_element) {
273 (None, None) => None,
274 (value, element) => Some(path.with("subscriptionTopic", |path| {
275 super::super::codec::Primitive::from_json_parts(value, element, path)
276 })?),
277 };
278 let field_timing = match raw_timing.suffix {
279 None => None,
280 Some(suffix) => Some(path.with("timing", |path| {
281 TriggerDefinitionTiming::from_json_parts(
282 suffix,
283 raw_timing.value,
284 raw_timing.element,
285 path,
286 )
287 })?),
288 };
289 let mut field_data = Vec::new();
290 if let Some(raw) = raw_data {
291 for (index, value) in path
292 .with("data", |path| super::super::codec::expect_array(raw, path))?
293 .iter()
294 .enumerate()
295 {
296 field_data.push(path.with_index("data", index, |path| {
297 super::super::codec::Json::from_json(
298 super::super::codec::expect_object(value, path)?,
299 path,
300 )
301 })?);
302 }
303 }
304 let field_condition = raw_condition
305 .map(|value| {
306 path.with("condition", |path| {
307 super::super::codec::Json::from_json(
308 super::super::codec::expect_object(
309 super::super::codec::expect_single(value, path)?,
310 path,
311 )?,
312 path,
313 )
314 })
315 })
316 .transpose()?;
317 Ok(Self {
318 id: field_id,
319 extension: field_extension,
320 r#type: field_type,
321 name: field_name,
322 code: field_code,
323 subscription_topic: field_subscription_topic,
324 timing: field_timing,
325 data: field_data,
326 condition: field_condition,
327 })
328 }
329}
330
331impl serde::Serialize for TriggerDefinition {
332 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
333 let mut map = serde::Serializer::serialize_map(serializer, None)?;
334 if let Some(item) = &self.name {
335 super::super::codec::element_entry(&mut map, "_name", item)?;
336 }
337 if let Some(item) = &self.subscription_topic {
338 super::super::codec::element_entry(&mut map, "_subscriptionTopic", item)?;
339 }
340 match &self.timing {
341 Some(TriggerDefinitionTiming::Date(inner)) => {
342 super::super::codec::element_entry(&mut map, "_timingDate", inner)?;
343 }
344 Some(TriggerDefinitionTiming::DateTime(inner)) => {
345 super::super::codec::element_entry(&mut map, "_timingDateTime", inner)?;
346 }
347 _ => {}
348 }
349 super::super::codec::element_entry(&mut map, "_type", &self.r#type)?;
350 if let Some(item) = &self.code {
351 serde::ser::SerializeMap::serialize_entry(&mut map, "code", item)?;
352 }
353 if let Some(item) = &self.condition {
354 serde::ser::SerializeMap::serialize_entry(&mut map, "condition", item)?;
355 }
356 if !self.data.is_empty() {
357 serde::ser::SerializeMap::serialize_entry(&mut map, "data", &self.data)?;
358 }
359 if !self.extension.is_empty() {
360 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
361 }
362 if let Some(v) = &self.id {
363 serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
364 }
365 if let Some(item) = &self.name {
366 super::super::codec::value_entry(&mut map, "name", item)?;
367 }
368 if let Some(item) = &self.subscription_topic {
369 super::super::codec::value_entry(&mut map, "subscriptionTopic", item)?;
370 }
371 match &self.timing {
372 Some(TriggerDefinitionTiming::Date(inner)) => {
373 super::super::codec::value_entry(&mut map, "timingDate", inner)?;
374 }
375 Some(TriggerDefinitionTiming::DateTime(inner)) => {
376 super::super::codec::value_entry(&mut map, "timingDateTime", inner)?;
377 }
378 Some(TriggerDefinitionTiming::Timing(inner)) => {
379 serde::ser::SerializeMap::serialize_entry(
380 &mut map,
381 "timingTiming",
382 inner.as_ref(),
383 )?;
384 }
385 None => {}
386 }
387 super::super::codec::value_entry(&mut map, "type", &self.r#type)?;
388 serde::ser::SerializeMap::end(map)
389 }
390}
391
392impl<'de> serde::Deserialize<'de> for TriggerDefinition {
393 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
394 let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
395 let mut path = super::super::codec::Path::root("TriggerDefinition");
396 let object =
397 super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
398 super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
399 }
400}
401
402#[derive(Debug, Clone, PartialEq, Eq)]
406pub enum TriggerDefinitionTiming {
407 Timing(Box<super::timing::Timing>),
409 Date(super::primitives::Date),
411 DateTime(super::primitives::DateTime),
413}
414
415impl TriggerDefinitionTiming {
416 pub fn to_json_parts(
422 &self,
423 ) -> Result<
424 (
425 &'static str,
426 Option<super::super::codec::Value>,
427 Option<super::super::codec::Value>,
428 ),
429 super::super::codec::EncodeError,
430 > {
431 match self {
432 Self::Timing(inner) => Ok((
433 "Timing",
434 Some(super::super::codec::Value::Object(
435 super::super::codec::Json::to_json(inner.as_ref())?,
436 )),
437 None,
438 )),
439 Self::Date(inner) => Ok((
440 "Date",
441 super::super::codec::Primitive::value_json(inner)?,
442 super::super::codec::Primitive::element_json(inner)?,
443 )),
444 Self::DateTime(inner) => Ok((
445 "DateTime",
446 super::super::codec::Primitive::value_json(inner)?,
447 super::super::codec::Primitive::element_json(inner)?,
448 )),
449 }
450 }
451
452 pub fn from_json_parts(
458 suffix: &str,
459 value: Option<&super::super::codec::Value>,
460 element: Option<&super::super::codec::Value>,
461 path: &mut super::super::codec::Path,
462 ) -> Result<Self, super::super::codec::DecodeError> {
463 match suffix {
464 "Timing" => {
465 if element.is_some() {
466 return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
467 expected: "no underscore form for a complex type",
468 }));
469 }
470 let value = value.ok_or_else(|| {
471 path.error(super::super::codec::DecodeErrorKind::MissingProperty)
472 })?;
473 let inner = super::super::codec::Json::from_json(
474 super::super::codec::expect_object(
475 super::super::codec::expect_single(value, path)?,
476 path,
477 )?,
478 path,
479 )?;
480 Ok(Self::Timing(Box::new(inner)))
481 }
482 "Date" => Ok(Self::Date(super::super::codec::Primitive::from_json_parts(
483 value, element, path,
484 )?)),
485 "DateTime" => Ok(Self::DateTime(
486 super::super::codec::Primitive::from_json_parts(value, element, path)?,
487 )),
488 _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
489 }
490 }
491}