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

//! The keyword values of the grammar, each parsed against its documented set.

use core::fmt;
use core::str::FromStr;

use openehr_mapping_core::diagnostic::render_admitted;

/// Why a keyword value was refused.
///
/// The message lists every value the keyword admits, so a mapping author sees
/// the whole set rather than only that the value was wrong.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{value}` is not one of {}", render_admitted(admitted))]
pub struct KeywordError {
    /// The refused value, as the file writes it.
    pub value: String,
    /// Every value the keyword admits.
    pub admitted: &'static [&'static str],
}

/// The direction a `unidirectional` key pins a file or a mapping to.
///
/// The two spellings are fixed by the header page ("`unidirectional` can be
/// set in the `spec` with `\"openehr->fhir\"` or `\"fhir->openehr\"`",
/// `docs/specs/fhirconnect/modules/ROOT/pages/basics/main.adoc`, §Direction)
/// and repeated for a mapping method in
/// `docs/specs/fhirconnect/modules/ROOT/pages/basics/body.adoc`, §Undirectional.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Direction {
    /// The file or mapping runs openEHR to FHIR only.
    OpenehrToFhir,
    /// The file or mapping runs FHIR to openEHR only.
    FhirToOpenehr,
}

impl Direction {
    /// Every spelling the specification writes.
    pub const ADMITTED: &[&str] = &["openehr->fhir", "fhir->openehr"];

    /// Returns the spelling the specification writes.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::OpenehrToFhir => "openehr->fhir",
            Self::FhirToOpenehr => "fhir->openehr",
        }
    }
}

impl fmt::Display for Direction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for Direction {
    type Err = KeywordError;

    /// Parses the value case-insensitively inside the documented set.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a value outside the two spellings.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.eq_ignore_ascii_case(Self::OpenehrToFhir.as_str()) {
            Ok(Self::OpenehrToFhir)
        } else if value.eq_ignore_ascii_case(Self::FhirToOpenehr.as_str()) {
            Ok(Self::FhirToOpenehr)
        } else {
            Err(KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            })
        }
    }
}

/// How an extension mapping interacts with the model mapping it extends.
///
/// The three methods are fixed by
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/extension-methods.adoc`
/// and by the `properties.extension.enum` of the published model schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ExtensionMethod {
    /// Adds a mapping at the bottom of the model mapping.
    Add,
    /// Appends a `followedBy` to the mapping method `appendTo` names.
    Append,
    /// Replaces the mapping method of the same name.
    Overwrite,
}

impl ExtensionMethod {
    /// Every spelling the schema enum admits.
    pub const ADMITTED: &[&str] = &["add", "append", "overwrite"];

    /// Returns the spelling the schema enum fixes.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Add => "add",
            Self::Append => "append",
            Self::Overwrite => "overwrite",
        }
    }
}

impl fmt::Display for ExtensionMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for ExtensionMethod {
    type Err = KeywordError;

    /// Parses the value case-exactly.
    ///
    /// The published model schema fixes the value with a JSON Schema `enum`
    /// (`properties.extension.enum`), and a JSON Schema enum matches
    /// case-sensitively, so `Add` is not this value.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a value outside the three methods.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "add" => Ok(Self::Add),
            "append" => Ok(Self::Append),
            "overwrite" => Ok(Self::Overwrite),
            _ => Err(KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            }),
        }
    }
}

/// Whether a context is a root or is entered only through `slotContext`.
///
/// "A sub-context is an ordinary `type: context` file with `scope: sub` in
/// its `context` block. It is never a root ... `scope` defaults to `root`"
/// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/sub-contexts.adoc`,
/// §`scope`), and the repository-source context schema fixes the two values
/// with an `enum`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum ContextScope {
    /// The context splits a Bundle and runs on its own.
    #[default]
    Root,
    /// The context is entered through `slotContext` only.
    Sub,
}

impl ContextScope {
    /// Every spelling the schema enum admits.
    pub const ADMITTED: &[&str] = &["root", "sub"];

    /// Returns the spelling the schema enum fixes.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Root => "root",
            Self::Sub => "sub",
        }
    }
}

impl fmt::Display for ContextScope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for ContextScope {
    type Err = KeywordError;

    /// Parses the value case-exactly, as a JSON Schema `enum` matches.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a value outside `root` and `sub`.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "root" => Ok(Self::Root),
            "sub" => Ok(Self::Sub),
            _ => Err(KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            }),
        }
    }
}

/// The data type a `with` block pins the pair of paths to.
///
/// The set is the `enum` of the published model schema
/// (`#/$defs/mapping/properties/type/enum`); the deprecated static-typing
/// table in
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/data-type/data-mappings.adoc`
/// names the same identifiers, and `NONE` is the iteration marker described in
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/concept-mappings.adoc`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DataType {
    /// No transformation; the mapping iterates so its children can map.
    None,
    /// A FHIR Quantity.
    Quantity,
    /// A FHIR dateTime.
    DateTime,
    /// A FHIR `CodeableConcept`.
    CodeableConcept,
    /// A FHIR Coding.
    Coding,
    /// A FHIR string.
    String,
    /// A FHIR Dosage.
    Dosage,
    /// A FHIR id.
    Id,
    /// A FHIR Identifier.
    Identifier,
    /// A FHIR Ratio, against an openEHR `DV_PROPORTION`.
    Proportion,
}

impl DataType {
    /// Every spelling the schema enum admits.
    pub const ADMITTED: &[&str] = &[
        "NONE",
        "QUANTITY",
        "DATETIME",
        "CODEABLECONCEPT",
        "CODING",
        "STRING",
        "DOSAGE",
        "ID",
        "IDENTIFIER",
        "PROPORTION",
    ];

    /// Returns the spelling the schema enum fixes.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::None => "NONE",
            Self::Quantity => "QUANTITY",
            Self::DateTime => "DATETIME",
            Self::CodeableConcept => "CODEABLECONCEPT",
            Self::Coding => "CODING",
            Self::String => "STRING",
            Self::Dosage => "DOSAGE",
            Self::Id => "ID",
            Self::Identifier => "IDENTIFIER",
            Self::Proportion => "PROPORTION",
        }
    }
}

impl fmt::Display for DataType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for DataType {
    type Err = KeywordError;

    /// Parses the value case-exactly, as the schema `enum` fixes it.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a value outside the ten data types.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "NONE" => Ok(Self::None),
            "QUANTITY" => Ok(Self::Quantity),
            "DATETIME" => Ok(Self::DateTime),
            "CODEABLECONCEPT" => Ok(Self::CodeableConcept),
            "CODING" => Ok(Self::Coding),
            "STRING" => Ok(Self::String),
            "DOSAGE" => Ok(Self::Dosage),
            "ID" => Ok(Self::Id),
            "IDENTIFIER" => Ok(Self::Identifier),
            "PROPORTION" => Ok(Self::Proportion),
            _ => Err(KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            }),
        }
    }
}

/// The test a condition applies to the values under its `targetAttributes`.
///
/// The five operators and their combining logic come from the operator table
/// in `docs/specs/fhirconnect/modules/ROOT/pages/basics/Conditions.adoc`,
/// §operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ConditionOperator {
    /// The path holds one of the criteria; the criteria combine with OR.
    OneOf,
    /// The path holds none of the criteria; the criteria combine with AND.
    NotOf,
    /// The path is empty; it takes no criteria.
    Empty,
    /// The path is not empty; it takes no criteria.
    NotEmpty,
    /// The element at the path is of the type named in the criteria.
    Type,
}

impl ConditionOperator {
    /// Every spelling the operator table writes.
    pub const ADMITTED: &[&str] = &["one of", "not of", "empty", "not empty", "type"];

    /// Returns the spelling the operator table writes.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::OneOf => "one of",
            Self::NotOf => "not of",
            Self::Empty => "empty",
            Self::NotEmpty => "not empty",
            Self::Type => "type",
        }
    }

    /// Whether the operator takes a `criteria`.
    ///
    /// "`criteria` is not required for `empty` or `not empty`"
    /// (`docs/specs/fhirconnect/modules/ROOT/pages/basics/Conditions.adoc`,
    /// §criteria), and both operator rows say the operator "does not include a
    /// `criteria`".
    #[must_use]
    pub const fn takes_criteria(self) -> bool {
        match self {
            Self::OneOf | Self::NotOf | Self::Type => true,
            Self::Empty | Self::NotEmpty => false,
        }
    }
}

impl fmt::Display for ConditionOperator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for ConditionOperator {
    type Err = KeywordError;

    /// Parses the value case-exactly, as the operator table spells it.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a value outside the five operators.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "one of" => Ok(Self::OneOf),
            "not of" => Ok(Self::NotOf),
            "empty" => Ok(Self::Empty),
            "not empty" => Ok(Self::NotEmpty),
            "type" => Ok(Self::Type),
            _ => Err(KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            }),
        }
    }
}

/// A `$name` variable at the head of a FHIRconnect path.
///
/// The seven variables are the rows of the variable table in
/// `docs/specs/fhirconnect/modules/ROOT/pages/basics/Variables.adoc`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Variable {
    /// The root of the FHIR resource.
    Resource,
    /// The parent FHIR path of the enclosing mapping step.
    FhirRoot,
    /// The root of the openEHR archetype.
    Archetype,
    /// The parent openEHR path of the enclosing mapping step.
    OpenehrRoot,
    /// The root of the enclosing openEHR composition.
    Composition,
    /// The marker for a path skipped because a reference resolves it.
    Reference,
    /// The values passed in on the REST call, in operational mappings.
    Context,
}

impl Variable {
    /// Every variable name the table lists, without the leading `$`.
    pub const ADMITTED: &[&str] = &[
        "resource",
        "fhirRoot",
        "archetype",
        "openehrRoot",
        "composition",
        "reference",
        "context",
    ];

    /// Returns the name the table writes, without the leading `$`.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Resource => "resource",
            Self::FhirRoot => "fhirRoot",
            Self::Archetype => "archetype",
            Self::OpenehrRoot => "openehrRoot",
            Self::Composition => "composition",
            Self::Reference => "reference",
            Self::Context => "context",
        }
    }
}

impl fmt::Display for Variable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "${}", self.as_str())
    }
}

impl FromStr for Variable {
    type Err = KeywordError;

    /// Parses a variable name, with or without the leading `$`,
    /// case-insensitively inside the documented set.
    ///
    /// # Errors
    ///
    /// Returns [`KeywordError`] for a name outside the seven variables.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let name = value.strip_prefix('$').unwrap_or(value);
        let admitted = [
            Self::Resource,
            Self::FhirRoot,
            Self::Archetype,
            Self::OpenehrRoot,
            Self::Composition,
            Self::Reference,
            Self::Context,
        ];
        admitted
            .into_iter()
            .find(|variable| name.eq_ignore_ascii_case(variable.as_str()))
            .ok_or_else(|| KeywordError {
                value: value.to_owned(),
                admitted: Self::ADMITTED,
            })
    }
}