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
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! The concept methods, the slots and the `manual` entries of a mapping.

use openehr_mapping_core::diagnostic::Diagnostic;
use openehr_mapping_core::diagnostic::ModelPath;
use openehr_mapping_core::header::metadata::MappingName;
use openehr_mapping_core::index::matching::node_id_matches;
use openehr_mapping_core::position::Located;

use core::str::FromStr;

use crate::model::ast::ContextMappingFile;
use crate::model::ast::ManualEntry;
use crate::model::ast::Mapping;
use crate::model::ast::ModelMappingFile;
use crate::model::ast::keyword::ContextScope;
use crate::model::ast::keyword::Direction;
use crate::model::ast::keyword::Variable;
use crate::resolve::error::ResolveCode;
use crate::resolve::extensions::Node;
use crate::resolve::extensions::diagnostic;
use crate::resolve::program::SLOT_DEPTH_LIMIT;
use crate::resolve::program::binding::ResourceType;
use crate::resolve::program::manual::Manual;
use crate::resolve::program::manual::ManualPath;
use crate::resolve::program::manual::ManualValue;
use crate::resolve::program::mapping::Method;
use crate::resolve::program::target::Target;
use crate::tree::path::FhirPath;

use crate::resolve::compile::Compiler;
use crate::resolve::compile::Enclosing;
use crate::resolve::compile::Scope;
use crate::resolve::compile::Site;
use crate::resolve::compile::fhir::root_expression;
use crate::resolve::compile::header::archetype_of;
use crate::resolve::compile::party;

impl<'a> Compiler<'a> {
    /// Decides what a mapping method does beyond mapping its two paths.
    pub(super) fn method(&mut self, node: &Node<'a>, scope: &Scope, path: &ModelPath) -> Method {
        let method = node.mapping;
        let file = node.origin;
        let owner = file.header().name().value();
        let written = declared_methods(method);
        if written.len() > 1 {
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::ConflictingMappingMethods,
                method.position,
                path,
                format!(
                    "`{}` writes {}, and a mapping method is one of them",
                    method.name.value(),
                    written.join(", ")
                ),
            ));
        }
        if let Some(ref reference) = method.reference {
            let resource = ResourceType::new(reference.resource_type.value().clone());
            if !self.table.is_resource(resource.as_str()) {
                self.diagnostics.push(diagnostic(
                    file.file(),
                    owner,
                    ResolveCode::UnknownResourceType,
                    reference.resource_type.position(),
                    &path.field("reference").field("resourceType"),
                    format!("`{resource}` names no resource type of this FHIR version"),
                ));
                return Method::Value;
            }
            // NOTE: `path_operators.adoc` never says what `^` does at a
            // `reference` boundary, and crossing it for no `^` is the reading
            // its worked example needs (reported on issue #183).
            let mut enclosing = vec![Enclosing {
                resource: scope.resource.clone(),
                fhir: scope.fhir.clone(),
            }];
            enclosing.extend(scope.enclosing.iter().cloned());
            let inner = Scope {
                resource: resource.clone(),
                fhir: root_expression(),
                enclosing,
                element_root: None,
                ..scope.clone()
            };
            return Method::Reference {
                resource,
                mappings: self.mappings(&node.reference, &inner),
            };
        }
        if let Some(ref slot) = method.slot_archetype
            && !(self.party.is_some() && slots_composition(method))
        {
            return self.slot(file, slot, scope, &path.field("slotArchetype"));
        }
        if let Some(ref entered) = method.slot_context {
            return self.slot_context(file, entered, scope, &path.field("slotContext"));
        }
        if let Some(ref link) = method.link {
            if self.party_refuses_link(file.file(), owner, method, path) {
                return Method::Value;
            }
            return Method::Link {
                meaning: link.meaning.as_ref().map(|value| value.value().clone()),
                link_type: link.link_type.as_ref().map(|value| value.value().clone()),
            };
        }
        if let Some(ref code) = method.mapping_code {
            if !self.codes.contains(code.value()) {
                self.diagnostics.push(diagnostic(
                    file.file(),
                    owner,
                    ResolveCode::UnknownContextReference,
                    code.position(),
                    &path.field("mappingCode"),
                    format!("`{}` names no function the engine registers", code.value()),
                ));
            }
            return Method::Programmed {
                code: code.value().clone(),
                ports: Vec::new(),
            };
        }
        if let Some(ref function) = method.participations_function {
            if self.party_refuses_participation(file.file(), owner, method, path) {
                return Method::Value;
            }
            return Method::Participation {
                function: function.value().clone(),
            };
        }
        Method::Value
    }

    /// Compiles the model mapping a `slotArchetype` hands over to.
    ///
    /// The slot names the openEHR path the slotted mapping is transformed at
    /// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mappings/concept-type/SlotArchetypes.adoc`),
    /// so `$archetype` inside it is that path. The slotted mapping keeps the
    /// caller's `$fhirRoot`, and `$resource` names the root of the model's
    /// own `fhirConfig` type (`basics/Variables.adoc`, "Path of the root
    /// resource"), as `$archetype` names the root of its own archetype: the
    /// element the slot hands over where that type is no resource
    /// ([`Self::element_root`]), the enclosing resource otherwise.
    fn slot(
        &mut self,
        file: &'a ModelMappingFile,
        slot: &Located<MappingName>,
        scope: &Scope,
        path: &ModelPath,
    ) -> Method {
        let owner = file.header().name().value();
        let at = path.clone();
        let Some(slotted) = self.set.model(slot.value()) else {
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::UnknownContextReference,
                slot.position(),
                &at,
                format!("`{}` is no model mapping of the loaded set", slot.value()),
            ));
            return Method::Value;
        };
        if let Some(message) = slot_depth(slot.value(), scope) {
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::SlotDepth,
                slot.position(),
                &at,
                format!("`{owner}` slots {message}"),
            ));
            return Method::Value;
        }
        let archetype = archetype_of(slotted).cloned();
        if let (Some(archetype), Ok(reached)) = (archetype, self.locate(&scope.openehr)) {
            let node = reached.node;
            match node.node_id() {
                Some(carried) if node_id_matches(archetype.as_str(), carried) => {
                    self.check_revision(slotted, Some(carried));
                }
                carried => self.diagnostics.push(diagnostic(
                    file.file(),
                    owner,
                    ResolveCode::ArchetypeMismatch,
                    slot.position(),
                    &at,
                    format!(
                        "`{}` declares the archetype `{archetype}` and the slot resolves to the \
                         node `{}`",
                        slot.value(),
                        carried.unwrap_or("with no archetype")
                    ),
                )),
            }
        }
        let mut chain = scope.chain.clone();
        chain.push(slot.value().clone());
        let mut entered_at = scope.entered_at.clone();
        entered_at.push(scope.openehr.clone());
        let inner = Scope {
            archetype: scope.openehr.clone(),
            direction: slotted
                .spec()
                .unidirectional
                .as_ref()
                .map_or(scope.direction, |located| Some(*located.value())),
            file: slotted.file().to_path_buf(),
            conceptmap: slotted
                .spec()
                .conceptmap
                .as_ref()
                .map(|url| url.value().clone()),
            prefix: String::new(),
            chain,
            entered_at,
            element_root: self.element_root(slotted, scope),
            ..scope.clone()
        };
        let merged = self.merge(slotted);
        Method::Slot {
            model: slot.value().clone(),
            preprocessors: self.preprocessors(slotted, &inner),
            mappings: self.mappings(&merged.mappings, &inner),
        }
    }

    /// Compiles the context a `slotContext` enters.
    ///
    /// "The engine enters the referenced context, maps from its `start` with
    /// its own `archetypes` and `extensions`, and returns", the sub-context
    /// maps "into the parent composition", and it "must be listed under
    /// `contexts`"
    /// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/sub-contexts.adoc`,
    /// §`slotContext` and §Rules). So the entered context's `start` is slotted
    /// as a `slotArchetype` would slot it, under the entered context's
    /// extensions, and the composition model a root context's `start` slots
    /// is skipped there.
    fn slot_context(
        &mut self,
        file: &'a ModelMappingFile,
        entered: &Located<MappingName>,
        scope: &Scope,
        path: &ModelPath,
    ) -> Method {
        let owner = file.header().name().value();
        let name = entered.value();
        let Some(sub) = self.set.context(name) else {
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::UnknownContextReference,
                entered.position(),
                path,
                format!("`{name}` is no context mapping of the loaded set"),
            ));
            return refused_entry(name);
        };
        if !self.may_enter(file, entered, sub, path) {
            return refused_entry(name);
        }
        let declaration = sub.context();
        let start = Located::new(
            declaration.start.position(),
            declaration.start.value().clone(),
        );
        self.check_embedded_template(file, entered, sub, scope, path);
        let extensions = self.extensions_of(sub);
        let saved = core::mem::replace(&mut self.extensions, extensions);
        self.entered.push(sub);
        if !self.subs.iter().any(|seen| core::ptr::eq(*seen, sub)) {
            self.subs.push(sub);
        }
        let inner = Scope {
            nested: true,
            ..scope.clone()
        };
        let slotted = self.slot(file, &start, &inner, path);
        self.entered.pop();
        self.extensions = saved;
        match slotted {
            Method::Slot {
                model,
                preprocessors,
                mappings,
            } => Method::SlotContext {
                context: name.clone(),
                model,
                preprocessors,
                mappings,
            },
            other => other,
        }
    }

    /// Checks that the `template.id` of `sub` is the template the parent's
    /// operational template embeds at the slot node (`sub-contexts.adoc`,
    /// §Rules and §Validation errors, "template mismatch").
    ///
    /// A slot node at which the operational template states no embedded
    /// template leaves the rule undecided, and that is a warning. No
    /// specification governs the undecided case: our own design.
    fn check_embedded_template(
        &mut self,
        file: &'a ModelMappingFile,
        entered: &Located<MappingName>,
        sub: &'a ContextMappingFile,
        scope: &Scope,
        path: &ModelPath,
    ) {
        let Some(declared) = sub.context().template.id.as_ref() else {
            return;
        };
        let Ok(reached) = self.locate(&scope.openehr) else {
            return;
        };
        let node = reached.node;
        if !reached.tail.segments.is_empty() {
            return;
        }
        let owner = file.header().name().value();
        let name = entered.value();
        match self.template.embedded_template(node) {
            Some(embedded) if embedded == declared.value() => {}
            Some(embedded) => self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::SubContextTemplateMismatch,
                entered.position(),
                path,
                format!(
                    "`{name}` declares the template `{}` and the operational template embeds \
                     `{embedded}` at `{}`",
                    declared.value(),
                    node.aql_path().as_str()
                ),
            )),
            None => self.diagnostics.push(
                Diagnostic::warning(
                    file.file().to_path_buf(),
                    ResolveCode::SubContextTemplateUndecided.into(),
                    format!(
                        "`{name}` declares the template `{}` and the operational template \
                         states no embedded template at `{}`, so the two cannot be compared",
                        declared.value(),
                        node.aql_path().as_str()
                    ),
                )
                .with_position(entered.position())
                .with_mapping_name(owner.clone())
                .with_model_path(path.clone()),
            ),
        }
    }

    /// Answers whether the walk may enter `sub` at a `slotContext` naming it:
    /// the entering context lists it under `contexts`, it is not already on
    /// the chain of entered contexts, and a `scope: sub` context's `start`
    /// slots no composition (`sub-contexts.adoc`, §Rules and §Validation
    /// errors). Each refusal is reported.
    fn may_enter(
        &mut self,
        file: &'a ModelMappingFile,
        entered: &Located<MappingName>,
        sub: &'a ContextMappingFile,
        path: &ModelPath,
    ) -> bool {
        let owner = file.header().name().value();
        let name = entered.value();
        if let Some(parent) = self.entered.last()
            && !parent
                .context()
                .contexts
                .iter()
                .any(|listed| listed.value() == name)
        {
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::UndeclaredSubContext,
                entered.position(),
                path,
                format!(
                    "`{name}` is entered here and `{}` does not list it under `contexts`",
                    parent.header().name().value()
                ),
            ));
            return false;
        }
        if self
            .entered
            .iter()
            .any(|context| context.header().name().value() == name)
        {
            let chain: Vec<&str> = self
                .entered
                .iter()
                .map(|context| context.header().name().value().as_str())
                .collect();
            self.diagnostics.push(diagnostic(
                file.file(),
                owner,
                ResolveCode::CyclicSubContext,
                entered.position(),
                path,
                format!(
                    "`{name}` is entered again on the chain {}",
                    chain.join(" -> ")
                ),
            ));
            return false;
        }
        let declaration = sub.context();
        let start = declaration.start.value();
        let site = party::Site {
            file: file.file(),
            owner,
            position: entered.position(),
            path,
        };
        let slots = self
            .set
            .model(start)
            .is_some_and(|model| model.mappings().iter().any(slots_composition));
        if self.party_refuses_entry(&site, name, start, slots) {
            return false;
        }
        if declaration.scope() == ContextScope::Sub
            && let Some(model) = self.set.model(start)
            && let Some(composition) = model
                .mappings()
                .iter()
                .find(|mapping| slots_composition(mapping))
        {
            self.diagnostics.push(diagnostic(
                model.file(),
                start,
                ResolveCode::SubContextComposition,
                composition.position,
                &ModelPath::root().field("mappings"),
                format!(
                    "`{start}` starts the `scope: sub` context `{name}` and slots a composition \
                     model, and a sub-context never creates a composition"
                ),
            ));
            return false;
        }
        true
    }

    /// Compiles one `manual` entry.
    pub(super) fn manual(
        &mut self,
        file: &'a ModelMappingFile,
        entry: &ManualEntry,
        scope: &Scope,
        path: &ModelPath,
    ) -> Manual {
        let owner = file.header().name().value();
        let pinned = entry
            .unidirectional
            .as_ref()
            .map_or(scope.direction, |located| Some(*located.value()));
        let fhir = entry
            .fhir
            .iter()
            .enumerate()
            .filter_map(|(index, manual)| {
                let at = path.field("fhir").index(index);
                let value = self.manual_value(file, &manual.value, &at)?;
                self.fhir_target(
                    file.file(),
                    owner,
                    &manual.path,
                    scope,
                    &at,
                    Site::Write(pinned),
                )
                .map(|target| ManualPath::new(Target::Fhir(Box::new(target)), value))
            })
            .collect();
        let openehr = entry
            .openehr
            .iter()
            .enumerate()
            .filter_map(|(index, manual)| {
                let at = path.field("openehr").index(index);
                let value = self.manual_value(file, &manual.value, &at)?;
                let target = self.openehr_target(file.file(), owner, &manual.path, scope, &at)?;
                self.carried_tail(
                    file,
                    &format!("{}.{}", scope.prefix, entry.name.value()),
                    &target,
                    true,
                    manual.path.position(),
                    &at,
                );
                Some(ManualPath::new(Target::Openehr(Box::new(target)), value))
            })
            .collect();
        let openehr = self.free_text(file, entry, path, pinned, openehr);
        Manual::new(
            entry.name.value().clone(),
            fhir,
            openehr,
            entry.fhir_condition.as_ref().and_then(|condition| {
                self.condition(
                    file,
                    condition,
                    scope,
                    Direction::FhirToOpenehr,
                    &path.field("fhirCondition"),
                    scope.fhir.as_str(),
                )
            }),
            entry.openehr_condition.as_ref().and_then(|condition| {
                self.condition(
                    file,
                    condition,
                    scope,
                    Direction::OpenehrToFhir,
                    &path.field("openehrCondition"),
                    &scope.openehr.to_string(),
                )
            }),
            entry.value.as_ref().map(|value| value.value().clone()),
            entry.unidirectional.as_ref().map(|value| *value.value()),
        )
    }

    /// Reads what one `manual` path writes: a literal or a `$context` member.
    ///
    /// "`$context` holds values passed in on the REST call ... a context value
    /// is referenced from a `manual` `value`"
    /// (`docs/specs/fhirconnect/modules/ROOT/pages/basics/Variables.adoc`,
    /// §`$context`), and every example names a member of it, so a bare
    /// `$context` names no value and is refused.
    fn manual_value(
        &mut self,
        file: &'a ModelMappingFile,
        written: &Located<String>,
        path: &ModelPath,
    ) -> Option<ManualValue> {
        let text = written.value();
        let Some(rest) = text.strip_prefix("$context") else {
            return Some(ManualValue::Literal(text.clone()));
        };
        let Some(member) = rest.strip_prefix('.').filter(|name| !name.is_empty()) else {
            self.diagnostics.push(diagnostic(
                file.file(),
                file.header().name().value(),
                ResolveCode::MalformedContextValue,
                written.position(),
                &path.field("value"),
                format!("`{text}` names no `$context` member, so it carries no value"),
            ));
            return None;
        };
        Some(ManualValue::Context(member.to_owned()))
    }
}

/// The method of a `slotContext` the compiler refused: the entry into the
/// named context with nothing compiled under it, so the refusal is not
/// followed by one about the mapping as a plain value.
fn refused_entry(context: &MappingName) -> Method {
    Method::SlotContext {
        context: context.clone(),
        model: context.clone(),
        preprocessors: Vec::new(),
        mappings: Vec::new(),
    }
}

/// Returns why a slot of `model` under `scope` is refused as too deep, `None`
/// when the recursion may go on.
///
/// A re-entry at no place below the one the model was entered at can never
/// end, and a chain may enter one model at most [`SLOT_DEPTH_LIMIT`] times.
/// No specification governs a recursive slot: our own design.
fn slot_depth(model: &MappingName, scope: &Scope) -> Option<String> {
    let depth = scope
        .chain
        .iter()
        .filter(|entered| *entered == model)
        .count();
    let previous = scope
        .chain
        .iter()
        .zip(&scope.entered_at)
        .rev()
        .find(|(entered, _)| *entered == model)
        .map(|(_, at)| at);
    let descends = previous.is_none_or(|at| {
        scope.openehr.segments.len() > at.segments.len()
            && scope.openehr.segments.starts_with(&at.segments)
    });
    let chain: Vec<&str> = scope.chain.iter().map(MappingName::as_str).collect();
    let chain = chain.join(" -> ");
    if !descends {
        return Some(format!(
            "`{model}` again on the chain {chain} at no place below the one it was entered at, \
             so the recursion never ends"
        ));
    }
    (depth >= SLOT_DEPTH_LIMIT).then(|| {
        format!(
            "`{model}` a {}th time on the chain {chain}, past the limit of {SLOT_DEPTH_LIMIT}",
            depth.saturating_add(1)
        )
    })
}

/// Returns the concept-method keys a mapping writes, in schema order.
pub(super) fn declared_methods(method: &Mapping) -> Vec<&'static str> {
    [
        method.reference.as_ref().map(|_| "reference"),
        method.slot_archetype.as_ref().map(|_| "slotArchetype"),
        method.slot_context.as_ref().map(|_| "slotContext"),
        method.link.as_ref().map(|_| "link"),
        method.mapping_code.as_ref().map(|_| "mappingCode"),
        method
            .participations_function
            .as_ref()
            .map(|_| "participationsFunction"),
    ]
    .into_iter()
    .flatten()
    .collect()
}

/// Whether `mapping` slots a composition model: a `slotArchetype` at
/// `openehr: $composition`, the composition slot a root context's `start`
/// carries (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/composition-element.adoc`).
pub(super) fn slots_composition(mapping: &Mapping) -> bool {
    mapping.slot_archetype.is_some()
        && mapping
            .with
            .as_ref()
            .and_then(|with| with.openehr.as_ref())
            .is_some_and(|openehr| {
                Variable::from_str(openehr.value())
                    .is_ok_and(|variable| variable == Variable::Composition)
            })
}

impl Compiler<'_> {
    /// Returns the element `$resource` names inside `slotted`, entered at
    /// `scope`: the slot's FHIR element when the model's
    /// `fhirConfig.structureDefinition` names no resource (a `BackboneElement`
    /// or a data type), `None` when it names one or none.
    ///
    /// `basics/Variables.adoc` defines `$resource` as the "Path of the root
    /// resource" of the model, so a model of an element is rooted at that
    /// element, as `$archetype` is rooted at the slotted archetype.
    fn element_root(&self, slotted: &ModelMappingFile, scope: &Scope) -> Option<FhirPath> {
        let definition = slotted.spec().structure_definition.as_ref()?;
        let text = definition.value();
        let named = text.rsplit('/').next().unwrap_or(text.as_str());
        (!self.table.is_resource(named)).then(|| scope.fhir.clone())
    }
}