fhirconnect 0.2.3

The FHIRconnect mapping language for Rust: the mapping file model and its validation, context resolution into an immutable program, and the bidirectional interpreter between openEHR compositions and FHIR resources
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! The typed request and response of each operation.
//!
//! The FHIR R4 operations framework carries an invocation in a `Parameters`
//! resource and answers with one too, except where a single `out` parameter
//! named `return` carries a resource, which is returned directly
//! (<https://hl7.org/fhir/R4/operations.html>). `ToFhir.fsh` declares exactly
//! that shape, so `$tofhir` answers a `Bundle`; `ToOpenEhr.fsh` declares two
//! `out` parameters, so `$toopenehr` answers a `Parameters`.
//!
//! Reading is strict in both directions: a parameter the operation does not
//! declare, one given twice, one carrying the wrong value type and a flat
//! composition with no `templateId` are each a typed refusal.

pub mod parameters;

use fhir_types::operation::ParametersError;
use fhir_types::r4::bundle::Bundle;
use fhir_types::r4::operation_outcome::OperationOutcome;
use fhir_types::r4::parameters::Parameters;
use fhir_types::r4::parameters::ParametersParameter;
use fhir_types::r4::parameters::ParametersParameterValue;
use fhir_types::r4::resource::Resource;

use crate::engine::context::CallContext;
use crate::operations::TO_FHIR;
use crate::operations::TO_OPENEHR;
use crate::operations::error::OperationError;
use crate::resolve::program::binding::TemplateId;

use crate::operations::contract::parameters::call_context;
use crate::operations::contract::parameters::code;
use crate::operations::contract::parameters::context_parameter;
use crate::operations::contract::parameters::named;
use crate::operations::contract::parameters::once;
use crate::operations::contract::parameters::string;
use crate::operations::contract::parameters::value_parameter;

/// The `composition` parameter name.
const COMPOSITION: &str = "composition";

/// The `templateId` parameter name.
const TEMPLATE_ID: &str = "templateId";

/// The `context` parameter name.
const CONTEXT: &str = "context";

/// The `bundle` parameter name.
const BUNDLE: &str = "bundle";

/// The `format` parameter name.
const FORMAT: &str = "format";

/// The `outcome` parameter name.
const OUTCOME: &str = "outcome";

/// The serialization a composition is carried in.
///
/// "openEHR Compositions are exchanged in two interchangeable JSON
/// serializations, and an engine MUST accept both wherever a Composition is
/// supplied" (`engine/rest-api.adoc` §Composition formats).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompositionPayload {
    /// The openEHR Reference Model structure, with `_type` on every node.
    Canonical(serde_json::Map<String, serde_json::Value>),
    /// The single-level simSDT object whose keys are template paths.
    Flat(serde_json::Map<String, serde_json::Value>),
}

impl CompositionPayload {
    /// Reads `text` as a composition in whichever serialization it carries.
    ///
    /// A canonical composition is the Reference Model structure and carries
    /// `_type`; a flat one is a single-level object of template paths and
    /// carries none. The chapter names no discriminator, so reading `_type` is
    /// FerroBRIDGE's own design (recorded on FerroBRIDGE issue #192).
    ///
    /// # Errors
    ///
    /// Returns [`OperationError::Payload`] when the text is not a JSON object.
    pub fn parse(text: &str) -> Result<Self, OperationError> {
        let value: serde_json::Value =
            serde_json::from_str(text).map_err(|source| OperationError::Payload { source })?;
        let serde_json::Value::Object(object) = value else {
            return Err(OperationError::NotAnObject {
                what: "composition",
            });
        };
        if object.contains_key("_type") {
            Ok(Self::Canonical(object))
        } else {
            Ok(Self::Flat(object))
        }
    }

    /// Returns the members the payload carries.
    #[must_use]
    pub const fn members(&self) -> &serde_json::Map<String, serde_json::Value> {
        match *self {
            Self::Canonical(ref object) | Self::Flat(ref object) => object,
        }
    }

    /// Returns the template the payload names inline, when it carries one.
    ///
    /// "A canonical Composition carries its template inline at
    /// `archetype_details.template_id`" and a flat one carries none
    /// (`engine/rest-api.adoc` §Composition formats).
    #[must_use]
    pub fn template_id(&self) -> Option<TemplateId> {
        let Self::Canonical(ref object) = *self else {
            return None;
        };
        object
            .get("archetype_details")?
            .get("template_id")?
            .get("value")?
            .as_str()
            .map(TemplateId::new)
    }

    /// Returns the payload as the JSON string a `valueString` carries.
    #[must_use]
    pub fn to_json_string(&self) -> String {
        serde_json::Value::Object(self.members().clone()).to_string()
    }
}

/// The serialization `$toopenehr` answers in.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Format {
    /// The canonical Reference Model structure, "the form openEHR CDRs expect
    /// when a Composition is committed" and the default.
    #[default]
    Canonical,
    /// The single-level simSDT object.
    Flat,
}

impl Format {
    /// Returns the code the `format` parameter carries.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Canonical => "canonical",
            Self::Flat => "flat",
        }
    }

    /// Returns the format `code` names.
    ///
    /// # Errors
    ///
    /// Returns [`OperationError::Parameters`] when `code` is neither of the
    /// two the chapter declares.
    pub fn parse(code: &str) -> Result<Self, OperationError> {
        match code {
            "canonical" => Ok(Self::Canonical),
            "flat" => Ok(Self::Flat),
            _ => Err(OperationError::Parameters {
                operation: TO_OPENEHR,
                source: ParametersError::WrongType {
                    operation: "$toopenehr",
                    name: FORMAT,
                    expected: "code of `canonical` or `flat`",
                },
            }),
        }
    }
}

/// The `in` parameters of `$tofhir`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToFhirRequest {
    composition: CompositionPayload,
    template_id: Option<TemplateId>,
    context: Option<CallContext>,
}

impl ToFhirRequest {
    /// Creates a request carrying `composition` alone.
    #[must_use]
    pub const fn new(composition: CompositionPayload) -> Self {
        Self {
            composition,
            template_id: None,
            context: None,
        }
    }

    /// Returns this request with `template` pinning the mapping.
    #[must_use]
    pub fn with_template_id(mut self, template: TemplateId) -> Self {
        self.template_id = Some(template);
        self
    }

    /// Returns this request with `context` as its call context.
    #[must_use]
    pub fn with_context(mut self, context: CallContext) -> Self {
        self.context = Some(context);
        self
    }

    /// Returns the composition the call carries.
    #[must_use]
    pub const fn composition(&self) -> &CompositionPayload {
        &self.composition
    }

    /// Returns the template the call pins, when it pins one.
    #[must_use]
    pub const fn template_id(&self) -> Option<&TemplateId> {
        self.template_id.as_ref()
    }

    /// Returns the call context, when the caller supplied one.
    #[must_use]
    pub const fn context(&self) -> Option<&CallContext> {
        self.context.as_ref()
    }

    /// Reads the request from a `Parameters` resource.
    ///
    /// # Errors
    ///
    /// Returns [`OperationError::Parameters`] for an undeclared, repeated,
    /// missing or wrongly typed parameter, [`OperationError::Payload`] when
    /// the composition string is not a JSON object, and
    /// [`OperationError::FlatTemplate`] when a flat composition arrives with
    /// no `templateId`.
    pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
        let mut composition: Option<CompositionPayload> = None;
        let mut template_id: Option<TemplateId> = None;
        let mut context: Option<CallContext> = None;
        for parameter in &parameters.parameter {
            match named(TO_FHIR, "$tofhir", parameter)? {
                COMPOSITION => {
                    once(TO_FHIR, "$tofhir", COMPOSITION, composition.is_some())?;
                    let text = string(TO_FHIR, "$tofhir", COMPOSITION, parameter)?;
                    composition = Some(CompositionPayload::parse(text)?);
                }
                TEMPLATE_ID => {
                    once(TO_FHIR, "$tofhir", TEMPLATE_ID, template_id.is_some())?;
                    template_id = Some(TemplateId::new(string(
                        TO_FHIR,
                        "$tofhir",
                        TEMPLATE_ID,
                        parameter,
                    )?));
                }
                CONTEXT => {
                    once(TO_FHIR, "$tofhir", CONTEXT, context.is_some())?;
                    context = Some(call_context(parameter)?);
                }
                other => {
                    return Err(OperationError::Parameters {
                        operation: TO_FHIR,
                        source: ParametersError::Undeclared {
                            operation: "$tofhir",
                            name: String::from(other),
                        },
                    });
                }
            }
        }
        let composition = composition.ok_or(OperationError::Parameters {
            operation: TO_FHIR,
            source: ParametersError::Missing {
                operation: "$tofhir",
                name: COMPOSITION,
            },
        })?;
        if matches!(composition, CompositionPayload::Flat(_)) && template_id.is_none() {
            return Err(OperationError::FlatTemplate);
        }
        Ok(Self {
            composition,
            template_id,
            context,
        })
    }

    /// Writes the request as a `Parameters` resource.
    ///
    /// The parameters come out in the order `ToFhir.fsh` declares them.
    #[must_use]
    pub fn to_parameters(&self) -> Parameters {
        let mut parameter = vec![value_parameter(
            COMPOSITION,
            ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
                self.composition.to_json_string(),
            )),
        )];
        if let Some(ref template) = self.template_id {
            parameter.push(value_parameter(
                TEMPLATE_ID,
                ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
                    template.as_str(),
                )),
            ));
        }
        if let Some(ref carried) = self.context {
            parameter.push(context_parameter(carried));
        }
        Parameters {
            parameter,
            ..Parameters::default()
        }
    }
}

/// The `out` parameter of `$tofhir`.
///
/// "The response MUST be a FHIR `Bundle` containing the mapped resources"
/// (`engine/rest-api.adoc` §$tofhir Output), and `ToFhir.fsh` names it
/// `return`, so the framework returns the Bundle itself.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToFhirResponse {
    bundle: Bundle,
}

impl ToFhirResponse {
    /// Wraps the Bundle a run produced.
    #[must_use]
    pub const fn new(bundle: Bundle) -> Self {
        Self { bundle }
    }

    /// Returns the Bundle.
    #[must_use]
    pub const fn bundle(&self) -> &Bundle {
        &self.bundle
    }

    /// Returns the Bundle, consuming the response.
    #[must_use]
    pub fn into_bundle(self) -> Bundle {
        self.bundle
    }
}

/// The `in` parameters of `$toopenehr`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToOpenehrRequest {
    bundle: Bundle,
    template_id: Option<TemplateId>,
    format: Format,
}

impl ToOpenehrRequest {
    /// Creates a request carrying `bundle` alone, answered in `canonical`.
    #[must_use]
    pub fn new(bundle: Bundle) -> Self {
        Self {
            bundle,
            template_id: None,
            format: Format::default(),
        }
    }

    /// Returns this request with `template` pinning the mapping.
    #[must_use]
    pub fn with_template_id(mut self, template: TemplateId) -> Self {
        self.template_id = Some(template);
        self
    }

    /// Returns this request answered in `format`.
    #[must_use]
    pub const fn with_format(mut self, format: Format) -> Self {
        self.format = format;
        self
    }

    /// Returns the Bundle to convert.
    #[must_use]
    pub const fn bundle(&self) -> &Bundle {
        &self.bundle
    }

    /// Returns the template the call pins, when it pins one.
    #[must_use]
    pub const fn template_id(&self) -> Option<&TemplateId> {
        self.template_id.as_ref()
    }

    /// Returns the serialization the call asked for.
    #[must_use]
    pub const fn format(&self) -> Format {
        self.format
    }

    /// Reads the request from a `Parameters` resource.
    ///
    /// `ToOpenEhr.fsh` declares `bundle`, `templateId` and `format` as `in`
    /// parameters, which is the enveloped form of the same call the chapter's
    /// prose describes as a bare `Bundle` body; the two do not agree, and both
    /// are read (recorded on FerroBRIDGE issue #192).
    ///
    /// # Errors
    ///
    /// Returns [`OperationError::Parameters`] for an undeclared, repeated,
    /// missing or wrongly typed parameter.
    pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
        let mut bundle: Option<Bundle> = None;
        let mut template_id: Option<TemplateId> = None;
        let mut format: Option<Format> = None;
        for parameter in &parameters.parameter {
            match named(TO_OPENEHR, "$toopenehr", parameter)? {
                BUNDLE => {
                    once(TO_OPENEHR, "$toopenehr", BUNDLE, bundle.is_some())?;
                    let Some(Resource::Bundle(ref carried)) = parameter.resource else {
                        return Err(OperationError::Parameters {
                            operation: TO_OPENEHR,
                            source: ParametersError::WrongType {
                                operation: "$toopenehr",
                                name: BUNDLE,
                                expected: "Bundle",
                            },
                        });
                    };
                    bundle = Some(carried.as_ref().clone());
                }
                TEMPLATE_ID => {
                    once(TO_OPENEHR, "$toopenehr", TEMPLATE_ID, template_id.is_some())?;
                    template_id = Some(TemplateId::new(string(
                        TO_OPENEHR,
                        "$toopenehr",
                        TEMPLATE_ID,
                        parameter,
                    )?));
                }
                FORMAT => {
                    once(TO_OPENEHR, "$toopenehr", FORMAT, format.is_some())?;
                    format = Some(Format::parse(code(
                        TO_OPENEHR,
                        "$toopenehr",
                        FORMAT,
                        parameter,
                    )?)?);
                }
                other => {
                    return Err(OperationError::Parameters {
                        operation: TO_OPENEHR,
                        source: ParametersError::Undeclared {
                            operation: "$toopenehr",
                            name: String::from(other),
                        },
                    });
                }
            }
        }
        let bundle = bundle.ok_or(OperationError::Parameters {
            operation: TO_OPENEHR,
            source: ParametersError::Missing {
                operation: "$toopenehr",
                name: BUNDLE,
            },
        })?;
        Ok(Self {
            bundle,
            template_id,
            format: format.unwrap_or_default(),
        })
    }

    /// Writes the request as a `Parameters` resource.
    #[must_use]
    pub fn to_parameters(&self) -> Parameters {
        let mut parameter = vec![ParametersParameter {
            name: fhir_types::r4::primitives::String::from(BUNDLE),
            resource: Some(Resource::Bundle(Box::new(self.bundle.clone()))),
            ..ParametersParameter::default()
        }];
        if let Some(ref template) = self.template_id {
            parameter.push(value_parameter(
                TEMPLATE_ID,
                ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
                    template.as_str(),
                )),
            ));
        }
        parameter.push(value_parameter(
            FORMAT,
            ParametersParameterValue::Code(fhir_types::r4::primitives::Code::from(
                self.format.as_str(),
            )),
        ));
        Parameters {
            parameter,
            ..Parameters::default()
        }
    }
}

/// The `out` parameters of `$toopenehr`.
///
/// "The response MUST be a FHIR `Parameters` resource. On full success it
/// contains only the `composition` parameter" (`engine/rest-api.adoc`
/// §$toopenehr Output).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToOpenehrResponse {
    composition: String,
    outcome: Option<OperationOutcome>,
}

impl ToOpenehrResponse {
    /// Wraps the composition a run produced.
    #[must_use]
    pub const fn new(composition: String) -> Self {
        Self {
            composition,
            outcome: None,
        }
    }

    /// Returns this response with `outcome` beside the composition.
    #[must_use]
    pub fn with_outcome(mut self, outcome: OperationOutcome) -> Self {
        self.outcome = Some(outcome);
        self
    }

    /// Returns the composition, serialized as the request asked for.
    #[must_use]
    pub fn composition(&self) -> &str {
        &self.composition
    }

    /// Returns the outcome, when the run declared a loss.
    #[must_use]
    pub const fn outcome(&self) -> Option<&OperationOutcome> {
        self.outcome.as_ref()
    }

    /// Reads the response from a `Parameters` resource.
    ///
    /// # Errors
    ///
    /// Returns [`OperationError::Parameters`] for an undeclared, repeated,
    /// missing or wrongly typed parameter.
    pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
        let mut composition: Option<String> = None;
        let mut outcome: Option<OperationOutcome> = None;
        for parameter in &parameters.parameter {
            match named(TO_OPENEHR, "$toopenehr", parameter)? {
                COMPOSITION => {
                    once(TO_OPENEHR, "$toopenehr", COMPOSITION, composition.is_some())?;
                    composition = Some(String::from(string(
                        TO_OPENEHR,
                        "$toopenehr",
                        COMPOSITION,
                        parameter,
                    )?));
                }
                OUTCOME => {
                    once(TO_OPENEHR, "$toopenehr", OUTCOME, outcome.is_some())?;
                    let Some(Resource::OperationOutcome(ref carried)) = parameter.resource else {
                        return Err(OperationError::Parameters {
                            operation: TO_OPENEHR,
                            source: ParametersError::WrongType {
                                operation: "$toopenehr",
                                name: OUTCOME,
                                expected: "OperationOutcome",
                            },
                        });
                    };
                    outcome = Some(carried.as_ref().clone());
                }
                other => {
                    return Err(OperationError::Parameters {
                        operation: TO_OPENEHR,
                        source: ParametersError::Undeclared {
                            operation: "$toopenehr",
                            name: String::from(other),
                        },
                    });
                }
            }
        }
        let composition = composition.ok_or(OperationError::Parameters {
            operation: TO_OPENEHR,
            source: ParametersError::Missing {
                operation: "$toopenehr",
                name: COMPOSITION,
            },
        })?;
        Ok(Self {
            composition,
            outcome,
        })
    }

    /// Writes the response as a `Parameters` resource.
    #[must_use]
    pub fn to_parameters(&self) -> Parameters {
        let mut parameter = vec![value_parameter(
            COMPOSITION,
            ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
                self.composition.as_str(),
            )),
        )];
        if let Some(ref outcome) = self.outcome {
            parameter.push(ParametersParameter {
                name: fhir_types::r4::primitives::String::from(OUTCOME),
                resource: Some(Resource::OperationOutcome(Box::new(outcome.clone()))),
                ..ParametersParameter::default()
            });
        }
        Parameters {
            parameter,
            ..Parameters::default()
        }
    }
}