fhirconnect 0.2.4

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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! The data-type pair a mapping with no `type` key converts through.
//!
//! The `type` key is deprecated "since the information is derivable from the
//! instances of FHIR and openEHR"
//! (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/data-type/data-mappings.adoc`,
//! §Deprecated), so the compiler derives the pair once, from the two sides it
//! already resolved: the FHIR type code the element table gives the element
//! and the reference-model class the Web Template gives the node. The pairs
//! themselves are the rows of the data-type chapter
//! (`types-of-mappings/data-type/*.adoc`) that the engine's cells carry.
//!
//! The chapter fixes which pairs convert and no order among them, so which
//! pair wins when a side admits several is our own design: the order a class's
//! chapter page lists its pairs in.

use fhir_types::schema::Kind;
use fhir_types::schema::ValueKind;
use openehr_rm::v1_2::model;
use openehr_rm::v1_2::typing::RmType;
use openehr_rm::v1_2::typing::attribute_type;
use openehr_rm::v1_2::typing::base_name;

use crate::model::ast::keyword::DataType;
use crate::model::ast::keyword::Direction;

/// The FHIR type codes each openEHR class converts with, in the order the
/// class's page of the data-type chapter lists them.
///
/// A row holds only the pairs a data-type cell of the engine carries both
/// ways, so a derived pair is always one the engine runs. A `DV_INTERVAL` row
/// is keyed by its parameter, because the bound class decides the cell:
/// `DV_INTERVAL<DV_DATE_TIME>` pairs with `Period` and
/// `DV_INTERVAL<DV_QUANTITY>` with `Range` (`DV_INTERVAL.adoc` §Range,
/// `data-mappings.adoc`), and the bare `DV_INTERVAL` row is the fallback for a
/// class name that carries no parameter.
/// `DV_BOOLEAN` against `boolean` is a primitive pair the chapter leaves out
/// as "straight forward" (`data-mappings.adoc`).
/// `DV_ORDINAL` against `CodeableConcept` carries the symbol, the value coming
/// from the options the template lists (`DV_ORDINAL.adoc` §`CodeableConcept`).
/// `PARTY_REF` against `Reference` is no row of the chapter: it carries
/// `ROLE.performer` in a party context, and no specification governs it, so
/// it is our own design.
pub const PAIRS: &[(&str, &[&str])] = &[
    ("DV_TEXT", &["string", "Coding", "CodeableConcept"]),
    ("DV_CODED_TEXT", &["CodeableConcept", "Coding"]),
    ("CODE_PHRASE", &["Coding"]),
    ("DV_DATE", &["date"]),
    // NOTE: no specification governs this: our own design, DV_DATE_TIME.adoc lists no instant,
    // and the R4 lexical form of instant is a strict subset of dateTime's (datatypes.html#primitive).
    ("DV_DATE_TIME", &["dateTime", "Period", "instant"]),
    ("DV_INTERVAL<DV_DATE_TIME>", &["Period"]),
    ("DV_INTERVAL<DV_QUANTITY>", &["Range"]),
    ("DV_INTERVAL", &["Period", "Range"]),
    ("PARTY_IDENTIFIED", &["Reference"]),
    ("PARTY_PROXY", &["Reference"]),
    ("DV_IDENTIFIER", &["Identifier"]),
    ("DV_PROPORTION", &["Quantity"]),
    ("DV_QUANTITY", &["Quantity", "decimal", "integer"]),
    // NOTE: R4 datatypes.html#unsignedInt and #positiveInt are integers from 0 and from 1, and
    // DV_COUNT.adoc names only integer; no specification governs the pairing: our own design.
    (
        "DV_COUNT",
        &["integer", "unsignedInt", "positiveInt", "Quantity", "Count"],
    ),
    ("DV_ORDINAL", &["CodeableConcept"]),
    ("DV_TIME", &["time"]),
    ("DV_DURATION", &["Duration"]),
    ("DV_URI", &["url"]),
    ("DV_MULTIMEDIA", &["Attachment"]),
    ("DV_PARSABLE", &["Attachment"]),
    ("DV_BOOLEAN", &["boolean"]),
    ("PARTY_REF", &["Reference"]),
];

/// The FHIR type code every primitive that travels as a JSON string falls
/// back to.
pub const TEXT: &str = "string";

/// The reference-model class every node that holds other nodes descends from.
///
/// `PATHABLE` is the root of the classes a path can step through
/// (<https://specifications.openehr.org/releases/RM/Release-1.1.0/common.html#_pathable_class>),
/// and no data value is one.
pub const STRUCTURAL: &str = "PATHABLE";

/// Returns the generic parameter a Web Template type name carries
/// (`DV_QUANTITY` of `DV_INTERVAL<DV_QUANTITY>`), `None` for a plain class
/// and for a name `openehr_rm::v1_2::typing::RmType` does not read.
#[must_use]
pub fn type_argument(rm_type: &str) -> Option<RmType> {
    rm_type
        .parse::<RmType>()
        .ok()
        .and_then(|parsed| parsed.params().first().cloned())
}

/// Returns the class an attribute of a node of `rm_type` holds: the node's
/// generic parameter where the attribute is typed by the class's formal
/// parameter, the attribute's declared type otherwise.
///
/// The attribute model resolves a formal parameter to its bound
/// (`DV_INTERVAL.lower` is `DV_ORDERED`), while `DV_INTERVAL<T>` types both
/// bounds `T` (<https://specifications.openehr.org/releases/RM/Release-1.1.0/data_types.html#_dv_interval_class>),
/// so a bound below a `DV_INTERVAL<DV_QUANTITY>` node is a `DV_QUANTITY`. An
/// attribute declared as the parameter's bound with no type arguments of its
/// own is read as typed by the parameter, and the argument replaces it only
/// when it conforms to that bound.
///
/// The substitution is `openehr_rm::v1_2::typing::attribute_type`; an
/// attribute the node's class does not declare itself (one of a descendant),
/// and an argument the class does not take, keep the declared type.
#[must_use]
pub fn attribute_class(rm_type: &str, attribute: &model::RmAttribute) -> &'static str {
    let declared = attribute.declared_type;
    let Ok(instantiated) = rm_type.parse::<RmType>() else {
        return declared;
    };
    attribute_type(&instantiated, attribute.name)
        .ok()
        .and_then(|typed| model::class(typed.item.name()))
        .map_or(declared, |class| class.name)
}

/// Answers whether `class` pairs with the FHIR type `code` in every
/// direction a mapping of `direction` runs.
///
/// A pair of the class's own row runs both ways. A mapping that only runs
/// out of openEHR may also read a class through a row of a class it
/// inherits from, since a `DV_CODED_TEXT` "inherits from `DV_TEXT`"
/// (`types-of-mappings/data-type/DV_CODED_TEXT.adoc`) and reads as its text;
/// going in, the inherited row cannot fill what the class adds.
#[must_use]
pub fn pairs_in(class: &str, code: &str, direction: Option<Direction>) -> bool {
    if pairs(class).contains(&code) {
        return true;
    }
    direction == Some(Direction::OpenehrToFhir)
        && PAIRS
            .iter()
            .any(|&(row, codes)| row != class && model::is_a(class, row) && codes.contains(&code))
}

/// Returns the FHIR type codes `class` converts with, empty for a class no
/// cell carries.
///
/// A generic class name is looked up whole, so an interval whose parameter
/// no row names pairs with nothing; a bare class name falls back to its row.
#[must_use]
pub fn pairs(class: &str) -> &'static [&'static str] {
    let row = |wanted: &str| {
        PAIRS
            .iter()
            .find(|&&(name, _)| name == wanted)
            .map(|&(_, codes)| codes)
    };
    row(class)
        .or_else(|| {
            type_argument(class)
                .is_none()
                .then(|| row(base_name(class)))
                .flatten()
        })
        .unwrap_or(&[])
}

/// Returns the FHIR type code an element of type `code` converts through
/// against a node of `class`.
///
/// The element's own type wins when the class pairs with it. A primitive that
/// travels as text and is no pair of the class converts as a `string`, the
/// FHIR type every such primitive specializes in its value space
/// (<https://hl7.org/fhir/R4/datatypes.html#primitive>), which is what the
/// engine did before the pair was derived.
#[must_use]
pub fn element(class: &str, code: &'static str, text: bool) -> &'static str {
    if text && !pairs(class).contains(&code) {
        return TEXT;
    }
    code
}

/// Returns the FHIR type code of the alternative a document carries for a
/// choice element, against a node of `class`.
///
/// A complex alternative names its type. A primitive's suffix is its type
/// code with the first letter raised (<https://hl7.org/fhir/R4/formats.html#choice>),
/// so it is matched against the class's pairs without regard to case, and an
/// alternative that travels as text and is no pair converts as a `string`.
/// A primitive that travels as a JSON boolean or a JSON number converts only
/// through a pair of the class, so it is `None` when no cell carries it.
#[must_use]
pub fn carried(class: &str, suffix: &str, kind: Kind) -> Option<&'static str> {
    match kind {
        Kind::Complex(name) => Some(name),
        Kind::Primitive(ValueKind::Text) | Kind::Attribute | Kind::Xhtml => Some(
            pairs(class)
                .iter()
                .find(|code| code.eq_ignore_ascii_case(suffix))
                .copied()
                .unwrap_or(TEXT),
        ),
        Kind::Primitive(ValueKind::Boolean | ValueKind::Integer | ValueKind::Decimal) => {
            pairs(class)
                .iter()
                .find(|code| code.eq_ignore_ascii_case(suffix))
                .copied()
        }
        Kind::Resource | Kind::Choice(_) => None,
    }
}

/// Returns the FHIR type code the `type` key names, `None` for `NONE`.
///
/// The key's identifiers are the Type IDs of the deprecated table in
/// `types-of-mappings/data-type/data-mappings.adoc`; `PROPORTION` travels as
/// the `Quantity` the engine's `DV_PROPORTION` cell carries.
#[must_use]
pub const fn declared(data_type: DataType) -> Option<&'static str> {
    match data_type {
        DataType::Quantity | DataType::Proportion => Some("Quantity"),
        DataType::DateTime => Some("dateTime"),
        DataType::CodeableConcept => Some("CodeableConcept"),
        DataType::Coding => Some("Coding"),
        DataType::String => Some("string"),
        DataType::Dosage => Some("Dosage"),
        DataType::Id => Some("id"),
        DataType::Identifier => Some("Identifier"),
        DataType::None => None,
    }
}

/// Returns the suffix of the alternative of a choice element that has the
/// type `code`, `None` when the choice admits no such alternative.
#[must_use]
pub fn suffix_of(code: &str, variants: &[(&'static str, Kind)]) -> Option<&'static str> {
    variants
        .iter()
        .find(|&&(suffix, _)| suffix.eq_ignore_ascii_case(code))
        .map(|&(suffix, _)| suffix)
}

/// Returns the suffix of the alternative a choice element is written as,
/// against a node of `class`.
///
/// The first pair of the class the choice admits is the one written; `None`
/// when the choice admits none of them.
#[must_use]
pub fn alternative(class: &str, variants: &[(&'static str, Kind)]) -> Option<&'static str> {
    pairs(class)
        .iter()
        .find_map(|code| suffix_of(code, variants))
}

#[cfg(test)]
mod tests {
    use fhir_types::schema::Kind;
    use fhir_types::schema::ValueKind;

    use super::STRUCTURAL;
    use super::alternative;
    use super::attribute_class;
    use super::carried;
    use super::element;
    use super::type_argument;
    use crate::model::ast::keyword::Direction;
    use openehr_rm::v1_2::model;
    use openehr_rm::v1_2::typing::RmType;

    #[test]
    fn a_date_time_element_against_a_date_time_node_converts_as_a_date_time() {
        assert_eq!(element("DV_DATE_TIME", "dateTime", true), "dateTime");
    }

    #[test]
    fn a_date_time_element_against_a_text_node_converts_as_text() {
        assert_eq!(element("DV_TEXT", "dateTime", true), "string");
    }

    #[test]
    fn a_complex_element_keeps_its_type_whatever_the_class() {
        assert_eq!(element("DV_TEXT", "Annotation", false), "Annotation");
    }

    #[test]
    fn the_structural_classes_are_the_pathable_ones() {
        for class in [
            "EVALUATION",
            "CLUSTER",
            "COMPOSITION",
            "SECTION",
            "EVENT_CONTEXT",
        ] {
            assert!(model::is_a(class, STRUCTURAL), "{class} holds other nodes");
        }
        for class in [
            "DV_CODED_TEXT",
            "CODE_PHRASE",
            "PARTY_PROXY",
            "DV_DATE_TIME",
        ] {
            assert!(!model::is_a(class, STRUCTURAL), "{class} is a value");
        }
    }

    /// The alternatives `Extension.value[x]` admits that the pairs name.
    const VALUES: &[(&str, Kind)] = &[
        ("CodeableConcept", Kind::Complex("CodeableConcept")),
        ("Coding", Kind::Complex("Coding")),
        ("DateTime", Kind::Primitive(ValueKind::Text)),
        ("Period", Kind::Complex("Period")),
        ("String", Kind::Primitive(ValueKind::Text)),
    ];

    #[test]
    fn a_choice_is_written_as_the_first_pair_it_admits() {
        assert_eq!(
            alternative("DV_CODED_TEXT", VALUES),
            Some("CodeableConcept")
        );
        assert_eq!(alternative("CODE_PHRASE", VALUES), Some("Coding"));
        assert_eq!(alternative("DV_TEXT", VALUES), Some("String"));
        assert_eq!(alternative("DV_DATE_TIME", VALUES), Some("DateTime"));
    }

    #[test]
    fn a_choice_admitting_no_pair_of_the_class_has_no_alternative() {
        assert_eq!(alternative("DV_IDENTIFIER", VALUES), None);
        assert_eq!(alternative("DV_BOOLEAN", VALUES), None);
    }

    #[test]
    fn the_alternative_a_document_carries_names_its_type_code() {
        assert_eq!(
            carried("DV_DATE_TIME", "DateTime", Kind::Primitive(ValueKind::Text)),
            Some("dateTime")
        );
        assert_eq!(
            carried("DV_CODED_TEXT", "Coding", Kind::Complex("Coding")),
            Some("Coding")
        );
        assert_eq!(
            carried("DV_CODED_TEXT", "Code", Kind::Primitive(ValueKind::Text)),
            Some("string")
        );
        assert_eq!(
            carried("DV_TEXT", "Boolean", Kind::Primitive(ValueKind::Boolean)),
            None
        );
    }

    #[test]
    fn a_number_alternative_converts_only_through_a_pair_of_the_class() {
        // DV_QUANTITY.adoc §Primitives and DV_COUNT.adoc §Primitive.
        assert_eq!(
            carried("DV_COUNT", "Integer", Kind::Primitive(ValueKind::Integer)),
            Some("integer")
        );
        assert_eq!(
            carried(
                "DV_QUANTITY",
                "Decimal",
                Kind::Primitive(ValueKind::Decimal)
            ),
            Some("decimal")
        );
        assert_eq!(
            carried("DV_TEXT", "Integer", Kind::Primitive(ValueKind::Integer)),
            None
        );
    }

    #[test]
    fn a_quantity_choice_is_written_in_the_order_of_its_page() {
        let values: &[(&str, Kind)] = &[
            ("Quantity", Kind::Complex("Quantity")),
            ("Integer", Kind::Primitive(ValueKind::Integer)),
        ];
        assert_eq!(alternative("DV_QUANTITY", values), Some("Quantity"));
        assert_eq!(alternative("DV_COUNT", values), Some("Integer"));
    }

    #[test]
    fn a_generic_node_names_its_parameter() {
        assert_eq!(
            type_argument("DV_INTERVAL<DV_QUANTITY>")
                .as_ref()
                .map(RmType::name),
            Some("DV_QUANTITY")
        );
        assert_eq!(type_argument("DV_QUANTITY"), None);
    }

    #[test]
    fn a_bound_of_a_generic_interval_is_its_parameter() {
        let lower = model::attribute("DV_INTERVAL", "lower").expect("the model has the bound");
        assert_eq!(
            attribute_class("DV_INTERVAL<DV_QUANTITY>", lower),
            "DV_QUANTITY"
        );
        assert_eq!(attribute_class("DV_INTERVAL", lower), "DV_ORDERED");
        let flag =
            model::attribute("DV_INTERVAL", "lower_included").expect("the model has the flag");
        assert_eq!(attribute_class("DV_INTERVAL<DV_QUANTITY>", flag), "Boolean");
    }

    #[test]
    fn an_argument_outside_the_bound_keeps_the_declared_type() {
        let lower = model::attribute("DV_INTERVAL", "lower").expect("the model has the bound");
        assert_eq!(attribute_class("DV_INTERVAL<DV_TEXT>", lower), "DV_ORDERED");
    }

    #[test]
    fn an_interval_choice_is_picked_by_the_parameter() {
        // DV_INTERVAL.adoc §Range pairs a quantity interval with Range;
        // data-mappings.adoc pairs a Period with a date-time interval.
        let onset: &[(&str, Kind)] = &[
            ("Period", Kind::Complex("Period")),
            ("Range", Kind::Complex("Range")),
        ];
        assert_eq!(
            alternative("DV_INTERVAL<DV_QUANTITY>", onset),
            Some("Range")
        );
        assert_eq!(
            alternative("DV_INTERVAL<DV_DATE_TIME>", onset),
            Some("Period")
        );
        assert_eq!(alternative("DV_INTERVAL", onset), Some("Period"));
    }

    #[test]
    fn an_interval_of_a_class_no_cell_carries_has_no_pair() {
        assert!(
            super::pairs("DV_INTERVAL<DV_COUNT>").is_empty(),
            "no cell carries a count interval"
        );
        assert_eq!(super::pairs("DV_INTERVAL<DV_QUANTITY>"), &["Range"]);
    }

    #[test]
    fn a_coded_text_reads_through_the_text_row_only_going_out() {
        // DV_CODED_TEXT.adoc: the class "inherits from `DV_TEXT`", whose row
        // pairs `string`; going in, that row cannot fill the defining code.
        assert!(super::pairs_in(
            "DV_CODED_TEXT",
            "string",
            Some(Direction::OpenehrToFhir)
        ));
        assert!(!super::pairs_in(
            "DV_CODED_TEXT",
            "string",
            Some(Direction::FhirToOpenehr)
        ));
        assert!(!super::pairs_in("DV_CODED_TEXT", "string", None));
        assert!(
            !super::pairs_in("DV_CODED_TEXT", "Period", Some(Direction::OpenehrToFhir)),
            "no inherited row pairs a Period"
        );
    }
}