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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! What a run produced, and the declared set of losses it took.
//!
//! An element the program cannot map refuses the unit (no specification
//! governs this: our own design), so a warning here is never "something went
//! wrong and the run carried on". Each variant of [`Loss`] is a loss the
//! specification itself declares: a mapping the direction skips (a gate, a
//! `unidirectional` marker, a reference that resolves to nothing, or an
//! optional code its concept map does not translate), a
//! `TERM_MAPPING` whose `match` is not `=` and so has no FHIR form, a
//! `TERM_MAPPING` of a value written into an element with no coding, the
//! `defining_code` of a coded text written into a `string`, a
//! composition field the engine defaulted, the occurrences a `0..n` into a
//! `0..1` dropped, a one-way row a data-type cell took, and a reference whose
//! resolution belongs to the facade. Nothing else is a warning, and the
//! round-trip tests assert the set exactly. Every [`Warning`] pairs its loss
//! with the [`Location`] it happened at.

use core::fmt;

use fhir_types::codec::Value;
use openehr_rm::v1_2::data_types::text::code_phrase::CodePhrase;

use crate::engine::lens::Fallback;
use crate::engine::lens::term_mapping::Cause;
use crate::engine::lens::term_mapping::Withheld;
use crate::engine::subject::SubjectIdentifier;

/// A produced document with the losses the run declared.
///
/// A run out of openEHR may create resources beside the one it maps, through
/// a `reference` mapping or a `hierarchy.split`; they travel as
/// [`Outcome::created`], each with the id the identity sink assigned.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Outcome<T> {
    value: T,
    warnings: Vec<Warning>,
    created: Vec<Value>,
    consumed: Vec<String>,
    subject: Vec<SubjectIdentifier>,
    reads: Vec<Location>,
}

impl<T> Outcome<T> {
    /// Wraps a produced value that took no declared loss.
    #[must_use]
    pub const fn clean(value: T) -> Self {
        Self {
            value,
            warnings: Vec::new(),
            created: Vec::new(),
            consumed: Vec::new(),
            subject: Vec::new(),
            reads: Vec::new(),
        }
    }

    /// Wraps a produced value with the losses the run declared.
    #[must_use]
    pub const fn new(value: T, warnings: Vec<Warning>) -> Self {
        Self {
            value,
            warnings,
            created: Vec::new(),
            consumed: Vec::new(),
            subject: Vec::new(),
            reads: Vec::new(),
        }
    }

    /// Returns this outcome with the resources the run created beside its
    /// value.
    #[must_use]
    pub fn with_created(mut self, created: Vec<Value>) -> Self {
        self.created = created;
        self
    }

    /// Returns this outcome with the references a `slotContext` consumed.
    #[must_use]
    pub fn with_consumed(mut self, consumed: Vec<String>) -> Self {
        self.consumed = consumed;
        self
    }

    /// Returns this outcome with the identifiers a `mappingCode` function
    /// offered as the subject reference of the EHR.
    #[must_use]
    pub fn with_subject(mut self, subject: Vec<SubjectIdentifier>) -> Self {
        self.subject = subject;
        self
    }

    /// Returns this outcome with the input elements the run read.
    #[must_use]
    pub fn with_reads(mut self, reads: Vec<Location>) -> Self {
        self.reads = reads;
        self
    }

    /// Returns every FHIR element a mapping read going into openEHR, with the
    /// openEHR node it wrote, in the order the run read them.
    ///
    /// A data-type cell reads its element whole, so an element below one listed
    /// here travelled with it. An element a mapping does not name is never
    /// listed: FHIRconnect is silent on unnamed input, so this list is how a
    /// caller tells what the run took from what it left (no specification
    /// governs this: our own design). Empty going out of openEHR.
    #[must_use]
    pub fn reads(&self) -> &[Location] {
        &self.reads
    }

    /// Returns the identifiers the run offered as the subject reference of
    /// its EHR, in the order it read them ([`crate::engine::subject`]).
    #[must_use]
    pub fn subject(&self) -> &[SubjectIdentifier] {
        &self.subject
    }

    /// Returns the literal references whose resources a `slotContext` pulled
    /// into this run, in the order it entered them.
    ///
    /// "A resource consumed through `slotContext` is marked as resolved. The
    /// engine does not process it again as a root"
    /// (`docs/specs/fhirconnect/modules/ROOT/pages/types-of-mapping-files/sub-contexts.adoc`,
    /// §Rules), so a caller splitting a Bundle leaves these out.
    #[must_use]
    pub fn consumed(&self) -> &[String] {
        &self.consumed
    }

    /// Returns the resources the run created beside its value, in the order
    /// it created them.
    #[must_use]
    pub fn created(&self) -> &[Value] {
        &self.created
    }

    /// Returns the produced value and the created resources, consuming the
    /// outcome.
    #[must_use]
    pub fn into_parts(self) -> (T, Vec<Value>) {
        (self.value, self.created)
    }

    /// Returns the produced value.
    #[must_use]
    pub const fn value(&self) -> &T {
        &self.value
    }

    /// Returns the produced value, consuming the outcome.
    #[must_use]
    pub fn into_value(self) -> T {
        self.value
    }

    /// Returns the losses the run declared, in the order it took them.
    #[must_use]
    pub fn warnings(&self) -> &[Warning] {
        &self.warnings
    }

    /// Records one loss.
    pub fn warn(&mut self, warning: Warning) {
        self.warnings.push(warning);
    }

    /// Returns this outcome with `value` in place of the produced one.
    #[must_use]
    pub fn map<U>(self, transform: impl FnOnce(T) -> U) -> Outcome<U> {
        Outcome {
            value: transform(self.value),
            warnings: self.warnings,
            created: self.created,
            consumed: self.consumed,
            subject: self.subject,
            reads: self.reads,
        }
    }
}

/// Where a loss happened, in both trees, with no value in it.
///
/// [`Location::fhir`] is the FHIRPath of the FHIR element the loss concerns:
/// the input element going into openEHR, the output element coming out to
/// FHIR. It is written in the simple subset `OperationOutcome.issue.expression`
/// admits, "element names, repetition indicators and the default child
/// accessor"
/// (<https://hl7.org/fhir/R4/operationoutcome-definitions.html#OperationOutcome.issue.expression>),
/// rooted at the type of the resource the run reads or writes at that point
/// (a resource a `reference` mapping resolved or created included), with an
/// index on every repeating element whose instance the run knows. A choice
/// element carries no type suffix: "choice elements are labeled according to
/// the name without the '\[x\]' suffix" (<https://hl7.org/fhir/R4/fhirpath.html>,
/// §Polymorphism in FHIR). An
/// `extension(url)` step is the `extension` element at the index the url
/// matched in the document, for example `Condition.code.coding[1].code`,
/// `Observation.value` or `Patient.extension[2].value`.
///
/// [`Location::openehr`] is the archetype path of the RM node the loss
/// concerns: the `aqlPath` the Web Template gives the node, with the attribute
/// tail below it appended, for example
/// `/content[openEHR-EHR-OBSERVATION.blood_pressure.v2]/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude`.
/// It carries node ids and the template's own names, never an instance value.
///
/// [`Location::resource`] names the resource the FHIR element lies in as
/// `Type/id` (<https://hl7.org/fhir/R4/references.html>, a relative literal
/// reference), when that resource carries an id, so two resources of one type
/// a run reads are told apart.
///
/// Either side is `None` where the loss has no element there: a composition
/// field the engine defaulted has no FHIR element, and a mapping with no
/// `with.openehr` has no openEHR node. No specification governs the shape:
/// our own design.
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Location {
    fhir: Option<String>,
    openehr: Option<String>,
    resource: Option<String>,
}

impl Location {
    /// Returns this location with `path` as its FHIRPath.
    #[must_use]
    pub fn with_fhir(mut self, path: impl Into<String>) -> Self {
        self.fhir = Some(path.into());
        self
    }

    /// Returns this location with `path` as its openEHR path.
    #[must_use]
    pub fn with_openehr(mut self, path: impl Into<String>) -> Self {
        self.openehr = Some(path.into());
        self
    }

    /// Returns this location with `reference` as the resource its FHIR
    /// element lies in.
    #[must_use]
    pub fn with_resource(mut self, reference: impl Into<String>) -> Self {
        self.resource = Some(reference.into());
        self
    }

    /// Returns the resource the FHIR element lies in, as `Type/id`, when the
    /// resource carries an id.
    #[must_use]
    pub fn resource(&self) -> Option<&str> {
        self.resource.as_deref()
    }

    /// Returns the FHIRPath of the FHIR element, when the loss has one.
    #[must_use]
    pub fn fhir(&self) -> Option<&str> {
        self.fhir.as_deref()
    }

    /// Returns the archetype path of the openEHR node, when the loss has one.
    #[must_use]
    pub fn openehr(&self) -> Option<&str> {
        self.openehr.as_deref()
    }
}

/// One loss a run declared, with where it happened.
///
/// Match on [`Warning::loss`] for what was lost and read
/// [`Warning::location`] for where. The location never carries a value, so a
/// consumer can log it without clinical content: its paths name elements and
/// nodes, and its resource the logical id the resource is addressed by. The
/// [`fmt::Display`] text is the loss's own.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Warning {
    loss: Loss,
    location: Location,
}

impl Warning {
    /// Pairs a loss with where it happened.
    #[must_use]
    pub const fn new(loss: Loss, location: Location) -> Self {
        Self { loss, location }
    }

    /// Records the one-way row a data-type cell took at `location`.
    #[must_use]
    pub fn fallback(taken: &Fallback, location: Location) -> Self {
        Self::new(
            Loss::OneWayFallback {
                cell: String::from(taken.cell()),
                field: String::from(taken.field()),
            },
            location,
        )
    }

    /// Records the term mapping a data-type cell of `mapping` left out of the
    /// FHIR element, at `location`.
    #[must_use]
    pub fn withheld(mapping: &str, taken: &Withheld, location: Location) -> Self {
        let terminology = String::from(taken.terminology());
        let code = String::from(taken.code());
        let reason = match taken.cause() {
            Cause::Match => SkipReason::TermMappingMatch {
                match_code: taken.match_code(),
                terminology,
                code,
            },
            Cause::Uncarried => SkipReason::TermMappingUncarried { terminology, code },
        };
        Self::new(
            Loss::Skipped {
                mapping: String::from(mapping),
                reason,
            },
            location,
        )
    }

    /// Records the `defining_code` a data-type cell of `mapping` left out of
    /// the FHIR `string` it wrote, at `location`.
    #[must_use]
    pub fn uncarried_code(mapping: &str, code: &CodePhrase, location: Location) -> Self {
        Self::new(
            Loss::Skipped {
                mapping: String::from(mapping),
                reason: SkipReason::DefiningCodeUncarried {
                    terminology: code.terminology_id.value.clone(),
                    code: code.code_string.clone(),
                },
            },
            location,
        )
    }

    /// Returns what was lost.
    #[must_use]
    pub const fn loss(&self) -> &Loss {
        &self.loss
    }

    /// Returns where the loss happened.
    #[must_use]
    pub const fn location(&self) -> &Location {
        &self.location
    }

    /// Returns the loss and its location, consuming the warning.
    #[must_use]
    pub fn into_parts(self) -> (Loss, Location) {
        (self.loss, self.location)
    }
}

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

/// What one declared loss was.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Loss {
    /// A mapping the direction does not run.
    Skipped {
        /// The dotted name of the mapping inside its model mapping.
        mapping: String,
        /// Why the mapping did not run.
        reason: SkipReason,
    },
    /// A composition field the engine filled because no mapping did.
    ///
    /// The defaults are the ones `engine/defaults-for-fields.adoc` names, and
    /// they apply going into openEHR only. The `INSTRUCTION.narrative` the
    /// composition builder fills when no mapping writes it is declared the
    /// same way, once per instruction (no specification governs this: our own
    /// design).
    Defaulted {
        /// The openEHR path of the field.
        field: String,
    },
    /// The occurrences a `0..n` input lost writing into a `0..1` output.
    ///
    /// "If we transform from a `0..n` to a `0..1` field, only the last entry
    /// of `0..n` is mapped" (`recurrence/TransformingList.adoc`).
    LastOfMany {
        /// The output path that took the last occurrence.
        path: String,
        /// How many occurrences the write dropped.
        dropped: usize,
    },
    /// A data-type row the specification writes for one direction only.
    OneWayFallback {
        /// The cell that took the row.
        cell: String,
        /// The attribute the row filled.
        field: String,
    },
    /// The further occurrences of the start archetype a run over its first
    /// occurrence left unmapped.
    ///
    /// No specification governs a context's start archetype held more than
    /// once by one composition: our own design maps each occurrence on its own
    /// through [`crate::engine::traverse::to_fhir_each`], and a run that maps
    /// one names how many it left.
    UnmappedOccurrences {
        /// The archetype the context starts at.
        archetype: String,
        /// How many occurrences after the first the run did not map.
        count: usize,
    },
    /// The occurrences of the start archetype at which the context's
    /// `openehrCondition` does not hold, so a run over every occurrence
    /// mapped no resource for them.
    ///
    /// No specification governs a start archetype held more than once: our
    /// own design, after [`crate::engine::traverse::to_fhir_each`].
    InapplicableOccurrences {
        /// The archetype the context starts at.
        archetype: String,
        /// How many occurrences the condition did not admit.
        count: usize,
    },
    /// An element a mapping wrote out of openEHR with only the FHIR R4
    /// data-absent-reason extension, because the content holds no value for
    /// it ([`crate::engine::absent`]).
    Absent {
        /// The extension's `valueCode`.
        reason: String,
    },
    /// An element of the composition, at the occurrence the run mapped, that
    /// no mapping of the program read going out to FHIR.
    ///
    /// A composition may hold elements a context's mappings do not name, and
    /// the resource the run answers then carries nothing of them; the run
    /// names each so a reader of the resource is told the source holds more
    /// (no specification governs the declaration: our own design).
    Unread {
        /// The node identifier of the element in its archetype (`at0009`).
        node: String,
    },
}

impl fmt::Display for Loss {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Skipped {
                ref mapping,
                ref reason,
            } => write!(f, "the mapping {mapping} did not run: {reason}"),
            Self::Defaulted { ref field } => write!(f, "the engine filled {field}"),
            Self::LastOfMany { ref path, dropped } => write!(
                f,
                "{path} holds one value, so the write dropped {dropped} earlier occurrences"
            ),
            Self::OneWayFallback {
                ref cell,
                ref field,
            } => write!(f, "{cell} filled {field} by a rule that runs one way"),
            Self::UnmappedOccurrences {
                ref archetype,
                count,
            } => write!(
                f,
                "the composition holds {archetype} {count} more times, which this run over its first occurrence did not map"
            ),
            Self::InapplicableOccurrences {
                ref archetype,
                count,
            } => write!(
                f,
                "the context's condition does not hold at {count} occurrences of {archetype}, so no resource maps them"
            ),
            Self::Absent { ref reason } => write!(
                f,
                "the element is written absent, data-absent-reason `{reason}`"
            ),
            Self::Unread { ref node } => write!(
                f,
                "the composition holds the element {node}, which no mapping carries into the resource"
            ),
        }
    }
}

/// Why a mapping did not run.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SkipReason {
    /// The mapping's `unidirectional` names the other direction.
    Unidirectional,
    /// A preprocessor gate of the slotted file did not admit the input.
    PreprocessorGate {
        /// The `metadata.name` of the slotted file whose gate closed.
        model: String,
    },
    /// The resource a reference points at could not be resolved.
    ///
    /// "If not possible, the engine proceeds with the mapping"
    /// (`engine/references.adoc`), so the mappings of the referenced
    /// resource do not run for it.
    UnresolvedReference {
        /// The reference as the document wrote it.
        reference: String,
    },
    /// The `Reference` carries no literal reference, so it names no resource
    /// to resolve or link.
    ///
    /// A `display` is "a very short description of the target resource" and
    /// an `identifier` a logical reference (R4 references.html §2.3.0.3 and
    /// §2.3.0.4), so neither alone names a resource the mapping can fetch,
    /// and a `Reference` holding only those is skipped the same way.
    EmptyReference,
    /// A `link` names a resource that no context mapping maps, so no linked
    /// composition is committed and no `LINK` is written.
    ///
    /// "If a context mapping for this `meta.url` is found, it is executed and
    /// saved as a separate composition"
    /// (`types-of-mappings/concept-type/concept-mappings.adoc`, §Linked
    /// mappings), and a resource whose `meta.profile` no context names has
    /// none.
    UnmappedLink {
        /// The reference as the document wrote it.
        reference: String,
    },
    /// The resource a `reference` mapping would create going out of openEHR
    /// stands in the linked composition at `target`, so the reference names
    /// that composition and the run creates no copy of the resource.
    ///
    /// A `link` whose FHIR side is the resource it sits in names the
    /// composition the resource was "saved as"
    /// (`types-of-mappings/concept-type/concept-mappings.adoc`, §Linked
    /// mappings), and a reference holds one target.
    LinkedResource {
        /// The `ehr:` URI of the linked composition.
        target: String,
    },
    /// The concept map the mapping names translates the input code to no
    /// equivalent concept, so the optional output element is not written.
    Untranslated {
        /// The `ConceptMap.url` the mapping names.
        conceptmap: String,
    },
    /// A `TERM_MAPPING` of the value has a `match` FHIR cannot carry.
    ///
    /// `match` is "hard coded to `=`, other operators not mappable to FHIR"
    /// (`types-of-mappings/data-type/TERM_MAPPING.adoc`), so the mapping
    /// wrote no `Coding` for this term; the rest of the value converted.
    TermMappingMatch {
        /// The `match` operator of the mapping: `>`, `<` or `?`.
        match_code: char,
        /// The `terminology_id` value of the mapping's target.
        terminology: String,
        /// The `code_string` of the mapping's target.
        code: String,
    },
    /// A `TERM_MAPPING` of the value has no place in the FHIR element.
    ///
    /// The `DV_TEXT` against `string` and the `DV_CODED_TEXT` against
    /// `Coding` tables give `mappings` no FHIR row
    /// (`types-of-mappings/data-type/DV_TEXT.adoc` §String and
    /// `DV_CODED_TEXT.adoc` §Coding), so the mapping wrote no `Coding` for
    /// this term; the rest of the value converted.
    TermMappingUncarried {
        /// The `terminology_id` value of the mapping's target.
        terminology: String,
        /// The `code_string` of the mapping's target.
        code: String,
    },
    /// The `defining_code` of a `DV_CODED_TEXT` has no place in the FHIR
    /// `string` the mapping wrote.
    ///
    /// The chapter writes no `string` row for `DV_CODED_TEXT`
    /// (`types-of-mappings/data-type/DV_CODED_TEXT.adoc` §STRING calls the
    /// pairing not possible), so the value converts as its supertype
    /// `DV_TEXT`, whose table has no `defining_code` row: the rubric was
    /// written and the code was not.
    DefiningCodeUncarried {
        /// The `terminology_id` value of the defining code.
        terminology: String,
        /// The `code_string` of the defining code.
        code: String,
    },
    /// The mapping writes the subject reference of the EHR, which the
    /// `EHR_STATUS` holds and a run over a composition never reads (openEHR
    /// RM EHR IM Release 1.1.0, the `EHR_STATUS` class).
    EhrStatusSubject,
    /// The `math_function` of an `INTERVAL_EVENT` has no place in the FHIR
    /// `Period` the mapping wrote.
    ///
    /// A `Period` carries the interval alone, and an interval event whose
    /// function is not `640|actual|` records a function of its data over the
    /// interval (openEHR RM data structures, `INTERVAL_EVENT.math_function`),
    /// so the period was written and the function was not.
    EventFunctionUncarried {
        /// The openEHR `event math function` code the event holds.
        code: String,
    },
    /// The `null_reason` of an `ELEMENT` names no `data-absent-reason` code.
    ///
    /// `types-of-mappings/data-type/NULL_FLAVOUR.adoc` reads `null_reason`
    /// back only "if exists and matches fhir code", and the extension carries
    /// a `code` alone, so the reason's text was not written; the code was.
    NullReasonUncarried,
}

impl fmt::Display for SkipReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Unidirectional => {
                f.write_str("its unidirectional marker names the other direction")
            }
            Self::PreprocessorGate { ref model } => {
                write!(
                    f,
                    "the preprocessor gate of {model} did not admit the input"
                )
            }
            Self::UnresolvedReference { ref reference } => {
                write!(f, "the reference {reference} resolves to no resource")
            }
            Self::EmptyReference => f.write_str("the Reference names no resource"),
            Self::UnmappedLink { ref reference } => {
                write!(f, "no context mapping maps the resource {reference} links")
            }
            Self::LinkedResource { ref target } => write!(
                f,
                "the resource stands in the linked composition {target}, so no copy is created"
            ),
            Self::Untranslated { ref conceptmap } => {
                write!(
                    f,
                    "{conceptmap} translates its code to no equivalent concept"
                )
            }
            Self::TermMappingMatch {
                match_code,
                ref terminology,
                ref code,
            } => write!(
                f,
                "its TERM_MAPPING `{match_code}` to {code} of {terminology} is no equivalence, \
                 and only `=` maps to a Coding"
            ),
            Self::TermMappingUncarried {
                ref terminology,
                ref code,
            } => write!(
                f,
                "its TERM_MAPPING to {code} of {terminology} has no place in an element \
                 that carries no coding"
            ),
            Self::DefiningCodeUncarried {
                ref terminology,
                ref code,
            } => write!(
                f,
                "its defining_code {code} of {terminology} has no place in a string, \
                 which carries the rubric only"
            ),
            Self::EhrStatusSubject => f.write_str(
                "it writes the subject of the EHR, which the EHR_STATUS holds and no composition does",
            ),
            Self::EventFunctionUncarried { ref code } => write!(
                f,
                "the math_function {code} of its interval event has no place in a Period"
            ),
            Self::NullReasonUncarried => f.write_str(
                "its null_reason names no data-absent-reason code, and the extension has no \
                 place for text",
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Location;
    use super::Loss;
    use super::Outcome;
    use super::SkipReason;
    use super::Warning;
    use crate::engine::lens::Fallback;

    #[test]
    fn a_clean_outcome_declares_no_loss() {
        let outcome = Outcome::clean(7);
        assert_eq!(*outcome.value(), 7);
        assert!(
            outcome.warnings().is_empty(),
            "a clean run declares no loss"
        );
    }

    #[test]
    fn the_losses_keep_the_order_the_run_took_them() {
        let mut outcome = Outcome::clean(String::from("document"));
        outcome.warn(Warning::new(
            Loss::Skipped {
                mapping: String::from("bodySite"),
                reason: SkipReason::Unidirectional,
            },
            Location::default(),
        ));
        outcome.warn(Warning::fallback(
            &Fallback::new("DV_CODED_TEXT against CodeableConcept", "DV_TEXT.value"),
            Location::default(),
        ));
        assert_eq!(outcome.warnings().len(), 2);
        assert_eq!(
            outcome.warnings().first().map(ToString::to_string),
            Some(String::from(
                "the mapping bodySite did not run: its unidirectional marker names the other direction"
            ))
        );
    }

    #[test]
    fn a_dropped_occurrence_names_its_path_and_its_count() {
        let warning = Warning::new(
            Loss::LastOfMany {
                path: String::from("$archetype/data[at0001]/items[at0002]"),
                dropped: 2,
            },
            Location::default(),
        );
        assert!(
            warning
                .to_string()
                .contains("dropped 2 earlier occurrences"),
            "the warning states how much was lost: {warning}"
        );
    }

    #[test]
    fn a_location_carries_each_side_it_was_given() {
        let location = Location::default()
            .with_fhir("Condition.code.coding[0]")
            .with_openehr("/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]/data[at0001]");
        assert_eq!(location.fhir(), Some("Condition.code.coding[0]"));
        assert_eq!(
            location.openehr(),
            Some("/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]/data[at0001]")
        );
        assert_eq!(Location::default().fhir(), None);
        assert_eq!(Location::default().openehr(), None);
    }
}