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

//! The FHIRconnect file model.
//!
//! One Rust type per construct the FHIRconnect v1.0.0 specification defines
//! for a model, extension or context mapping file. Every node keeps the
//! [`Position`] of the YAML it was read from, so a refusal raised anywhere
//! downstream can name the place in the file that caused it.
//!
//! The nodes are plain records: the parser in [`crate::model::parse`] builds
//! them and nothing else mutates them, so their fields are public and every
//! invariant the grammar fixes is carried by the field's own type.
//!
//! Keyword values compare case-insensitively inside the documented set, and a
//! value outside the set is refused. YAML keys stay exact, because the
//! published schemas fix them with a JSON Schema `enum`, which matches
//! case-sensitively.

pub mod keyword;

use std::path::Path;
use std::path::PathBuf;

use openehr_mapping_core::header::Header;
use openehr_mapping_core::header::metadata::MappingName;
use openehr_mapping_core::position::Located;
use openehr_mapping_core::position::Position;

use crate::model::ast::keyword::ConditionOperator;
use crate::model::ast::keyword::ContextScope;
use crate::model::ast::keyword::DataType;
use crate::model::ast::keyword::Direction;
use crate::model::ast::keyword::ExtensionMethod;

/// One condition on a mapping method or on the file preprocessor.
///
/// Both `fhirCondition` and `openehrCondition` carry this shape
/// (`docs/specs/fhirconnect/modules/ROOT/pages/basics/Conditions.adoc`). The
/// singular `targetAttribute` and `criteria` keys and the plural
/// `targetAttributes` and `criterias` keys both lower into the vectors below,
/// so a file's spelling does not reach the interpreter.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Condition {
    /// Where the condition sits in the file.
    pub position: Position,
    /// The element returned once the filter has run.
    pub target_root: Located<String>,
    /// The paths, relative to `target_root`, the filter is applied to.
    pub target_attributes: Vec<Located<String>>,
    /// The test applied to the values found at the attributes.
    pub operator: Located<ConditionOperator>,
    /// The values the operator tests against.
    pub criteria: Vec<Located<String>>,
    /// Whether the condition identifies the element, as the published schemas
    /// spell the key.
    pub identifying: Option<Located<bool>>,
}

/// The `with` block: the two paths a mapping method maps against each other.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct With {
    /// Where the block sits in the file.
    pub position: Position,
    /// The `FHIRPath` side.
    pub fhir: Option<Located<String>>,
    /// The openEHR path side.
    pub openehr: Option<Located<String>>,
    /// The data type the pair is pinned to.
    pub data_type: Option<Located<DataType>>,
    /// The literal value the published model schema admits beside the paths.
    pub value: Option<Located<String>>,
}

/// One `path` and `value` pair inside a manual mapping entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ManualPath {
    /// Where the pair sits in the file.
    pub position: Position,
    /// The path the value is written to.
    pub path: Located<String>,
    /// The value written at the path.
    pub value: Located<String>,
}

/// One entry of a `manual` mapping.
///
/// "Manual mappings are initialized using the `manual:` key which is followed
/// by a name … followed by a list of paths `- path:` and the value we map
/// `- value:`"
/// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/manual.adoc`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ManualEntry {
    /// Where the entry sits in the file.
    pub position: Position,
    /// The entry name.
    pub name: Located<String>,
    /// The FHIR-side paths and values.
    pub fhir: Vec<ManualPath>,
    /// The openEHR-side paths and values.
    pub openehr: Vec<ManualPath>,
    /// The FHIR-side condition selecting this entry.
    pub fhir_condition: Option<Condition>,
    /// The openEHR-side condition selecting this entry.
    pub openehr_condition: Option<Condition>,
    /// The literal value the published model schema admits on an entry.
    pub value: Option<Located<String>>,
    /// The direction the entry is restricted to.
    pub unidirectional: Option<Located<Direction>>,
}

/// A `link` mapping, which redirects part of a resource into another
/// composition and records an openEHR LINK to it.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/concept-mappings.adoc`,
/// §Linked mappings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkMapping {
    /// Where the block sits in the file.
    pub position: Position,
    /// The `LINK.meaning` of the link that is created.
    pub meaning: Option<Located<String>>,
    /// The `LINK.type` of the link that is created.
    pub link_type: Option<Located<String>>,
}

/// A `reference` mapping, which initializes a further FHIR resource.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/Reference.adoc`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReferenceMapping {
    /// Where the block sits in the file.
    pub position: Position,
    /// The FHIR resource type the reference initializes.
    pub resource_type: Located<String>,
    /// The mappings that run against the initialized resource.
    pub mappings: Vec<Mapping>,
}

/// A `followedBy` block: the mappings that run against the parent's path.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/basics/FollowedBy.adoc`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FollowedBy {
    /// Where the block sits in the file.
    pub position: Position,
    /// The child mappings, in document order.
    pub mappings: Vec<Mapping>,
}

/// One mapping method.
///
/// A mapping method opens with `- name:` and carries whichever of the concept
/// keys applies. The presence of a key is what selects the concept type: a
/// `mappingCode` marks a PROGRAMMED mapping, a `link` a LINKED one, a
/// `participationsFunction` a PARTICIPATION one, and `with.type: NONE` the
/// iteration marker
/// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/concept-mappings.adoc`).
#[derive(Debug, Clone, PartialEq, Eq)]
#[expect(
    clippy::struct_field_names,
    reason = "mappingCode is the key the FHIRconnect grammar spells"
)]
pub struct Mapping {
    /// Where the mapping sits in the file.
    pub position: Position,
    /// The mapping-method name, which extensions reference.
    pub name: Located<String>,
    /// The extension method, in an extension mapping file.
    pub extension: Option<Located<ExtensionMethod>>,
    /// The mapping method an `append` extension appends to, as a dotted path
    /// of mapping-method names.
    pub append_to: Option<Located<String>>,
    /// The pair of paths this method maps.
    pub with: Option<With>,
    /// The direction this method and its children are restricted to.
    pub unidirectional: Option<Located<Direction>>,
    /// The manual entries of a MANUAL mapping.
    pub manual: Vec<ManualEntry>,
    /// The FHIR-side condition guarding this method.
    pub fhir_condition: Option<Condition>,
    /// The openEHR-side condition guarding this method.
    pub openehr_condition: Option<Condition>,
    /// The child mappings that run after this one.
    pub followed_by: Option<FollowedBy>,
    /// The resource this method initializes, in a REFERENCE mapping.
    pub reference: Option<ReferenceMapping>,
    /// The `metadata.name` of the model mapping a SLOT mapping redirects to.
    pub slot_archetype: Option<Located<MappingName>>,
    /// The `metadata.name` of the context a SLOT mapping enters
    /// (`types-of-mapping-files/sub-contexts.adoc`, §`slotContext`).
    pub slot_context: Option<Located<MappingName>>,
    /// The external function a PROGRAMMED mapping runs.
    pub mapping_code: Option<Located<String>>,
    /// The openEHR LINK a LINKED mapping creates.
    pub link: Option<LinkMapping>,
    /// The participation function a PARTICIPATION mapping carries.
    pub participations_function: Option<Located<String>>,
    /// The `ConceptMap.url` the codes of this method are translated through.
    pub conceptmap: Option<Located<String>>,
}

/// One side of a `hierarchy.split`.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/HierarchyMappings.adoc`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SplitTarget {
    /// Where the block sits in the file.
    pub position: Position,
    /// The kind of element to create for each occurrence.
    pub create: Option<Located<String>>,
    /// Where the element is created; absent when it is the archetype or the
    /// resource itself.
    pub path: Option<Located<String>>,
    /// The paths whose distinct values trigger a new element.
    pub unique: Vec<Located<String>>,
}

/// The `split` of a hierarchy mapping.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HierarchySplit {
    /// Where the block sits in the file.
    pub position: Position,
    /// What to create on the FHIR side.
    pub fhir: Option<SplitTarget>,
    /// What to create on the openEHR side.
    pub openehr: Option<SplitTarget>,
}

/// The one hierarchy mapping a file may carry in its preprocessor.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hierarchy {
    /// Where the block sits in the file.
    pub position: Position,
    /// The pair of paths the split iterates.
    pub with: Option<With>,
    /// What each occurrence creates.
    pub split: Option<HierarchySplit>,
}

/// The preprocessor block of a model or extension mapping file.
///
/// A preprocessor condition decides whether the file runs at all
/// (`docs/specs/fhirconnect/modules/ROOT/pages/basics/Conditions.adoc`,
/// §Conditions in the preprocessor), and the hierarchy mapping realigns the
/// two models before any mapping runs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Preprocessor {
    /// Where the block sits in the file.
    pub position: Position,
    /// The FHIR-side condition gating the file.
    pub fhir_condition: Option<Condition>,
    /// The openEHR-side condition gating the file.
    pub openehr_condition: Option<Condition>,
    /// The hierarchy realignment.
    pub hierarchy: Option<Hierarchy>,
}

/// The `spec` block of a model or extension mapping file.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/basics/main.adoc`, §Spec. The
/// archetype and the revision are read by the shared header model, so they
/// are reached through [`ModelMappingFile::header`] rather than repeated here.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelSpec {
    /// Where the block sits in the file.
    pub position: Position,
    /// The standard this file maps, which the published schema pins to `FHIR`.
    pub system: Located<String>,
    /// The release of that standard, which the published schema pins to `R4`.
    pub version: Located<String>,
    /// The `metadata.name` of the model mapping an extension file extends.
    pub extends: Option<Located<MappingName>>,
    /// The `StructureDefinition` the file records as its FHIR target.
    pub structure_definition: Option<Located<String>>,
    /// The `ConceptMap.url` every code in the file is translated through.
    pub conceptmap: Option<Located<String>>,
    /// The direction the whole file is restricted to.
    pub unidirectional: Option<Located<Direction>>,
}

/// One loaded model or extension mapping file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelMappingFile {
    file: PathBuf,
    header: Header,
    spec: ModelSpec,
    preprocessor: Option<Preprocessor>,
    mappings: Vec<Mapping>,
}

impl ModelMappingFile {
    /// Builds a model mapping file from its parts.
    #[must_use]
    pub fn new(
        file: impl Into<PathBuf>,
        header: Header,
        spec: ModelSpec,
        preprocessor: Option<Preprocessor>,
        mappings: Vec<Mapping>,
    ) -> Self {
        Self {
            file: file.into(),
            header,
            spec,
            preprocessor,
            mappings,
        }
    }

    /// Returns the file the mapping was loaded from.
    #[must_use]
    pub fn file(&self) -> &Path {
        &self.file
    }

    /// Returns the shared header.
    #[must_use]
    pub const fn header(&self) -> &Header {
        &self.header
    }

    /// Returns the FHIRconnect half of the `spec` block.
    #[must_use]
    pub const fn spec(&self) -> &ModelSpec {
        &self.spec
    }

    /// Returns the preprocessor, when the file carries one.
    #[must_use]
    pub const fn preprocessor(&self) -> Option<&Preprocessor> {
        self.preprocessor.as_ref()
    }

    /// Returns the top-level mappings, in document order.
    #[must_use]
    pub fn mappings(&self) -> &[Mapping] {
        &self.mappings
    }
}

/// The profile a context mapping targets.
///
/// `docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/context-mappings.adoc`,
/// §profileUrl & templateId.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProfileRef {
    /// Where the block sits in the file.
    pub position: Position,
    /// The canonical URL of the profile.
    pub url: Option<Located<String>>,
    /// The version of the profile.
    pub version: Option<Located<String>>,
}

/// The operational template a context mapping targets.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateRef {
    /// Where the block sits in the file.
    pub position: Position,
    /// The exact template id.
    pub id: Option<Located<String>>,
    /// The version of the template.
    pub sem_ver: Option<Located<String>>,
}

/// The `context` block of a context mapping file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MappingContext {
    /// Where the block sits in the file.
    pub position: Position,
    /// The profile being mapped.
    pub profile: ProfileRef,
    /// The template being mapped.
    pub template: TemplateRef,
    /// The model mappings this context loads, by `metadata.name`.
    pub archetypes: Vec<Located<MappingName>>,
    /// The extension mappings this context loads, by `metadata.name`.
    pub extensions: Vec<Located<MappingName>>,
    /// The operational models this context triggers, by `metadata.name`.
    pub operational: Vec<Located<MappingName>>,
    /// Whether the context is a root or a sub-context, `None` when the file
    /// writes no `scope`, which reads as `root`.
    pub scope: Option<Located<ContextScope>>,
    /// The sub-contexts this context enters through `slotContext`, by
    /// `metadata.name`.
    pub contexts: Vec<Located<MappingName>>,
    /// The mapping the engine starts at, by `metadata.name`.
    pub start: Located<MappingName>,
}

impl MappingContext {
    /// Returns the scope the context declares, `root` when it writes none.
    #[must_use]
    pub fn scope(&self) -> ContextScope {
        self.scope
            .as_ref()
            .map_or(ContextScope::Root, |scope| *scope.value())
    }
}

/// One loaded context mapping file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContextMappingFile {
    file: PathBuf,
    header: Header,
    context: MappingContext,
}

impl ContextMappingFile {
    /// Builds a context mapping file from its parts.
    #[must_use]
    pub fn new(file: impl Into<PathBuf>, header: Header, context: MappingContext) -> Self {
        Self {
            file: file.into(),
            header,
            context,
        }
    }

    /// Returns the file the mapping was loaded from.
    #[must_use]
    pub fn file(&self) -> &Path {
        &self.file
    }

    /// Returns the shared header.
    #[must_use]
    pub const fn header(&self) -> &Header {
        &self.header
    }

    /// Returns the `context` block.
    #[must_use]
    pub const fn context(&self) -> &MappingContext {
        &self.context
    }
}

/// Walks a mapping and every mapping nested under it, parents first.
///
/// The nesting a mapping can carry is `followedBy.mappings` and
/// `reference.mappings`, so both are walked in document order.
#[must_use]
pub fn walk(mappings: &[Mapping]) -> Vec<&Mapping> {
    let mut found = Vec::new();
    let mut stack: Vec<&Mapping> = mappings.iter().rev().collect();
    while let Some(mapping) = stack.pop() {
        found.push(mapping);
        let children = mapping
            .reference
            .as_ref()
            .map(|reference| reference.mappings.as_slice())
            .into_iter()
            .chain(
                mapping
                    .followed_by
                    .as_ref()
                    .map(|followed| followed.mappings.as_slice()),
            );
        for child in children.flatten().rev() {
            stack.push(child);
        }
    }
    found
}