eehrxf 0.4.2

The European interoperability software component of an EHR system under Regulation (EU) 2025/327: the format-neutral dataset model of the priority categories read from the Xt-EHR EHDS logical models, and the European electronic health record exchange format in FHIR R4, mapped from openEHR by FHIRconnect
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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! The FHIRconnect mappings the component ships (feature `openehr`).
//!
//! FerroBRIDGE authors one FHIRconnect 1.0.0 project per HL7 Europe profile
//! set it maps, and the crate ships each under `mappings/<project>/` in its
//! package, embedded here as text so a system that depends on the published
//! crate runs the same files the crate's tests run. A [`Project`] names its
//! files, in loading order, and the context mappings to compile from them;
//! [`crate::mapping::Mapping::compile_project`] compiles them from their
//! text with no file written, [`Project::with_context`] narrows a project to
//! one of its contexts, and [`Project::write_to`] writes them where a
//! system keeps its mappings for [`crate::mapping::Mapping::compile`]. No
//! specification governs where a mapping is shipped: our own design.

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

/// One shipped FHIRconnect project: its directory, its files, the context
/// mappings to compile from them and the received elements it accounts for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Project {
    directory: &'static str,
    contexts: &'static [&'static str],
    files: &'static [File],
    accounted: &'static [&'static str],
}

/// A context mapping a project does not compile, named to
/// [`Project::with_context`].
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("the {project} project compiles no context mapping named `{context}`")]
pub struct UnknownContext {
    /// The project's directory.
    pub project: &'static str,
    /// The context name asked for.
    pub context: String,
}

/// One shipped mapping file: its file name and its YAML text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct File {
    /// The file name, under the project's directory.
    pub name: &'static str,
    /// The file's YAML text, as the crate ships it.
    pub text: &'static str,
}

impl Project {
    /// Returns the name of the project's directory under `mappings/`.
    #[must_use]
    pub const fn directory(&self) -> &'static str {
        self.directory
    }

    /// Returns the `metadata.name` of each context mapping the project
    /// compiles.
    #[must_use]
    pub const fn contexts(&self) -> &'static [&'static str] {
        self.contexts
    }

    /// Returns this project narrowed to its one context mapping `name`, so
    /// [`crate::mapping::Mapping::compile_project`] compiles that context alone
    /// from the same files.
    ///
    /// A project whose contexts map one template, as `MPD`'s request and
    /// dispense contexts do, compiles one of them this way, so a run over that
    /// template has one program to select.
    ///
    /// # Errors
    ///
    /// Returns [`UnknownContext`] when the project compiles no context mapping
    /// named `name`.
    pub fn with_context(self, name: &str) -> Result<Self, UnknownContext> {
        let contexts = self.contexts;
        let chosen = contexts
            .iter()
            .find(|held| **held == name)
            .ok_or_else(|| UnknownContext {
                project: self.directory,
                context: name.to_owned(),
            })?;
        Ok(Self {
            contexts: std::slice::from_ref(chosen),
            ..self
        })
    }

    /// Returns the project's files, in loading order.
    #[must_use]
    pub const fn files(&self) -> &'static [File] {
        self.files
    }

    /// Returns the received elements the project accounts for, each a
    /// FHIRPath of element names rooted at a resource type: a received
    /// instance of one that the run carries into no structured element is
    /// declared in the run's `OperationOutcome`
    /// ([`crate::mapping::Mapping::accounting`]).
    #[must_use]
    pub const fn accounted(&self) -> &'static [&'static str] {
        self.accounted
    }

    /// Writes every file of the project into `directory`, which must exist,
    /// and answers the paths written, in loading order, the `files` a
    /// [`crate::mapping::Mapping`] compiles from.
    ///
    /// # Errors
    ///
    /// Returns the I/O error of the first file that cannot be written.
    pub fn write_to(&self, directory: &Path) -> io::Result<Vec<PathBuf>> {
        self.files
            .iter()
            .map(|file| {
                let path = directory.join(file.name);
                std::fs::write(&path, file.text).map(|()| path)
            })
            .collect()
    }
}

/// Embeds the file `name` of the shipped project `directory`.
#[cfg(any(
    feature = "prescription",
    feature = "dispensation",
    feature = "imaging",
    feature = "laboratory",
    feature = "patient-summary",
    feature = "discharge"
))]
macro_rules! shipped {
    ($directory:literal, $name:literal) => {
        File {
            name: $name,
            text: include_str!(concat!(
                env!("CARGO_MANIFEST_DIR"),
                "/mappings/",
                $directory,
                "/",
                $name
            )),
        }
    };
}

/// The HL7 Europe Medication Prescription and Dispense 1.0.0 project.
///
/// It maps over FerroBRIDGE's medication template: `MedicationRequest-eu-mpd`
/// from the medication order and `MedicationDispense-eu-mpd` from the
/// medication action at its dispensed careflow step, each with its
/// `Medication-eu-mpd`.
#[cfg(any(feature = "prescription", feature = "dispensation"))]
pub const MPD: Project = Project {
    directory: "mpd",
    contexts: &[
        "ferrobridge_eu_mpd_request.context",
        "ferrobridge_eu_mpd_dispense.context",
    ],
    files: &[
        shipped!("mpd", "ferrobridge_eu_mpd_request.context.yml"),
        shipped!("mpd", "ferrobridge_medication_order.v3.yml"),
        shipped!("mpd", "ferrobridge_medication.v2.yml"),
        shipped!("mpd", "ferrobridge_dosage.v2.yml"),
        shipped!("mpd", "ferrobridge_eu_mpd_request.yml"),
        shipped!("mpd", "ferrobridge_eu_mpd_medication.yml"),
        shipped!("mpd", "ferrobridge_eu_mpd_dispense.context.yml"),
        shipped!("mpd", "ferrobridge_medication.v1.yml"),
        shipped!("mpd", "ferrobridge_eu_mpd_dispense.yml"),
    ],
    accounted: &[],
};

/// The HL7 Europe Imaging Report 1.0.0-ballot project.
///
/// It maps over FerroBRIDGE's imaging report template: the imaging
/// examination result to the `DiagnosticReportEuImaging` the document holds
/// beside its composition, and the study it reports to the
/// `ImagingStudyEuImaging` the report references. A received report
/// declares each section none of whose entries it carries, and each element
/// of the report and of its studies it carries into no structured element,
/// such as the performing organization ([`Project::accounted`]).
#[cfg(feature = "imaging")]
pub const IMAGING: Project = Project {
    directory: "imaging",
    contexts: &["ferrobridge_eu_imaging.context"],
    files: &[
        shipped!("imaging", "ferrobridge_eu_imaging.context.yml"),
        shipped!("imaging", "ferrobridge_imaging_exam_result.v1.yml"),
        shipped!("imaging", "ferrobridge_eu_imaging_report.yml"),
    ],
    // NOTE: no specification governs this: our own design; every section and every element of the report and
    // its study but identity, subject, choice elements and `imagingStudy` (whose studies declare their own) is accounted.
    accounted: &[
        "Composition.section",
        "DiagnosticReport.extension",
        "DiagnosticReport.modifierExtension",
        "DiagnosticReport.identifier",
        "DiagnosticReport.basedOn",
        "DiagnosticReport.status",
        "DiagnosticReport.category",
        "DiagnosticReport.code",
        "DiagnosticReport.encounter",
        "DiagnosticReport.issued",
        "DiagnosticReport.performer",
        "DiagnosticReport.resultsInterpreter",
        "DiagnosticReport.specimen",
        "DiagnosticReport.result",
        "DiagnosticReport.media",
        "DiagnosticReport.conclusion",
        "DiagnosticReport.conclusionCode",
        "DiagnosticReport.presentedForm",
        "ImagingStudy.extension",
        "ImagingStudy.modifierExtension",
        "ImagingStudy.identifier",
        "ImagingStudy.status",
        "ImagingStudy.modality",
        "ImagingStudy.encounter",
        "ImagingStudy.started",
        "ImagingStudy.basedOn",
        "ImagingStudy.referrer",
        "ImagingStudy.interpreter",
        "ImagingStudy.endpoint",
        "ImagingStudy.numberOfSeries",
        "ImagingStudy.numberOfInstances",
        "ImagingStudy.procedureReference",
        "ImagingStudy.procedureCode",
        "ImagingStudy.location",
        "ImagingStudy.reasonCode",
        "ImagingStudy.reasonReference",
        "ImagingStudy.note",
        "ImagingStudy.description",
        "ImagingStudy.series",
    ],
};

/// The HL7 Europe Laboratory Report 2.0.0 project.
///
/// It maps over the published `KDS_Laborbericht` template. Its two contexts
/// map that one template, so a run compiles one of them with
/// [`Project::with_context`]: `ferrobridge_eu_laboratory.context` maps the
/// laboratory test result to the `DiagnosticReport-eu-lab` the document holds
/// beside its composition, each analyte to an `Observation-resultslab-eu-lab`
/// and each specimen to a `Specimen-eu-lab`, and
/// `ferrobridge_eu_lab_composition.context` maps the report composition to a
/// `Composition-eu-lab`.
#[cfg(feature = "laboratory")]
pub const LABORATORY: Project = Project {
    directory: "laboratory",
    contexts: &[
        "ferrobridge_eu_laboratory.context",
        "ferrobridge_eu_lab_composition.context",
    ],
    files: &[
        shipped!("laboratory", "ferrobridge_eu_laboratory.context.yml"),
        shipped!("laboratory", "ferrobridge_laboratory_test_result.v1.yml"),
        shipped!("laboratory", "ferrobridge_laboratory_test_analyte.v1.yml"),
        shipped!("laboratory", "ferrobridge_specimen.v1.yml"),
        shipped!("laboratory", "ferrobridge_eu_laboratory_result.yml"),
        shipped!("laboratory", "ferrobridge_eu_laboratory_analyte.yml"),
        shipped!("laboratory", "ferrobridge_eu_laboratory_specimen.yml"),
        shipped!("laboratory", "ferrobridge_eu_lab_composition.context.yml"),
        shipped!("laboratory", "ferrobridge_report_result.v1.Composition.yml"),
        shipped!("laboratory", "ferrobridge_eu_lab_composition.yml"),
        shipped!("laboratory", "ferrobridge_eu_lab_composition_report.yml"),
    ],
    // NOTE: no specification governs this: our own design; a result read without its
    // reference range or the order it answers is misread, so each loss of one is declared.
    accounted: &[
        "DiagnosticReport.basedOn.reference",
        "DiagnosticReport.basedOn.identifier",
        "DiagnosticReport.basedOn.display",
        "DiagnosticReport.basedOn.extension",
        "Observation.basedOn",
        "Observation.referenceRange",
    ],
};

/// The HL7 Europe Patient Summary 1.0.0-ballot project.
///
/// It maps over FerroBRIDGE's patient summary template, which places the
/// medication as the openEHR International Patient Summary template does. Its
/// one root context, `ferrobridge_eu_eps.context`, maps the composition to a
/// `composition-eu-eps` and enters each section of the template through the
/// sub-context its section path names (FHIRconnect 1.0.0,
/// `types-of-mapping-files/sub-contexts.adoc`):
/// `ferrobridge_eu_eps_medication.context` maps each medication order and
/// each medication action of the medication summary section to a
/// `MedicationStatement-eu-eps`, and
/// `ferrobridge_eu_eps_immunization.context` maps each medication action of
/// the immunisations section to an `immunization-eu-core`. The section path
/// is the placement criterion, so one archetype held in two sections is
/// placed by the section that holds it. A document takes the run with
/// [`crate::document::Document::with_mapped_composition`].
#[cfg(feature = "patient-summary")]
pub const EPS: Project = Project {
    directory: "eps",
    contexts: &["ferrobridge_eu_eps.context"],
    files: &[
        shipped!("eps", "ferrobridge_eu_eps.context.yml"),
        shipped!("eps", "ferrobridge_health_summary.v1.Composition.yml"),
        shipped!("eps", "ferrobridge_eu_eps_medication.context.yml"),
        shipped!("eps", "ferrobridge_section_adhoc.v1.yml"),
        shipped!("eps", "ferrobridge_eu_eps_medication_section.yml"),
        shipped!(
            "eps",
            "ferrobridge_medication_order.v3.MedicationStatement.yml"
        ),
        shipped!("eps", "ferrobridge_medication.v1.MedicationStatement.yml"),
        shipped!("eps", "ferrobridge_eu_eps_immunization.context.yml"),
        shipped!("eps", "ferrobridge_section_immunisation_list.v0.yml"),
        shipped!("eps", "ferrobridge_eu_eps_immunization_section.yml"),
        shipped!("eps", "ferrobridge_medication.v1.Immunization.yml"),
    ],
    accounted: &[],
};

/// The HL7 Europe Hospital Discharge Report 1.0.0 project.
///
/// It maps over FerroBRIDGE's discharge report template, a transfer of care
/// summary over published CKM archetypes. Its one context,
/// `ferrobridge_eu_hdr.context`, maps the composition to a
/// `composition-eu-hdr` naming an `encounter-eu-hdr` built from the
/// composition's context, the hospital stay, each problem or diagnosis of
/// the template's diagnostic summary section to a `condition-eu-core`, each
/// procedure of its significant procedures section to a `procedure-eu-core`
/// and each medication order of its discharge medications section to a
/// `medicationRequest-eu-core`, each an entry of the section of that name.
/// A received report declares each section none of whose entries it
/// carries, the hospital course among them, and each element of an entry it
/// carries into no structured element ([`Project::accounted`]).
#[cfg(feature = "discharge")]
pub const HDR: Project = Project {
    directory: "hdr",
    contexts: &["ferrobridge_eu_hdr.context"],
    files: &[
        shipped!("hdr", "ferrobridge_eu_hdr.context.yml"),
        shipped!("hdr", "ferrobridge_transfer_summary.v1.Composition.yml"),
        shipped!("hdr", "ferrobridge_problem_diagnosis.v1.yml"),
        shipped!("hdr", "ferrobridge_procedure.v1.yml"),
        shipped!("hdr", "ferrobridge_medication_order.v3.yml"),
    ],
    // NOTE: no specification governs this: our own design; every section and every element of an entry
    // but its identity, subject, encounter (the stay the context carries) and choice elements is accounted.
    accounted: &[
        "Composition.section",
        "Condition.extension",
        "Condition.modifierExtension",
        "Condition.identifier",
        "Condition.clinicalStatus",
        "Condition.verificationStatus",
        "Condition.category",
        "Condition.severity",
        "Condition.code",
        "Condition.bodySite",
        "Condition.recordedDate",
        "Condition.recorder",
        "Condition.asserter",
        "Condition.stage",
        "Condition.evidence",
        "Condition.note",
        "Procedure.extension",
        "Procedure.modifierExtension",
        "Procedure.identifier",
        "Procedure.instantiatesCanonical",
        "Procedure.instantiatesUri",
        "Procedure.basedOn",
        "Procedure.partOf",
        "Procedure.status",
        "Procedure.statusReason",
        "Procedure.category",
        "Procedure.code",
        "Procedure.recorder",
        "Procedure.asserter",
        "Procedure.performer",
        "Procedure.location",
        "Procedure.reasonCode",
        "Procedure.reasonReference",
        "Procedure.bodySite",
        "Procedure.outcome",
        "Procedure.report",
        "Procedure.complication",
        "Procedure.complicationDetail",
        "Procedure.followUp",
        "Procedure.note",
        "Procedure.focalDevice",
        "Procedure.usedReference",
        "Procedure.usedCode",
        "MedicationRequest.extension",
        "MedicationRequest.modifierExtension",
        "MedicationRequest.identifier",
        "MedicationRequest.status",
        "MedicationRequest.statusReason",
        "MedicationRequest.intent",
        "MedicationRequest.category",
        "MedicationRequest.priority",
        "MedicationRequest.doNotPerform",
        "MedicationRequest.supportingInformation",
        "MedicationRequest.authoredOn",
        "MedicationRequest.requester",
        "MedicationRequest.performer",
        "MedicationRequest.performerType",
        "MedicationRequest.recorder",
        "MedicationRequest.reasonCode",
        "MedicationRequest.reasonReference",
        "MedicationRequest.instantiatesCanonical",
        "MedicationRequest.instantiatesUri",
        "MedicationRequest.basedOn",
        "MedicationRequest.groupIdentifier",
        "MedicationRequest.courseOfTherapyType",
        "MedicationRequest.insurance",
        "MedicationRequest.note",
        "MedicationRequest.dosageInstruction",
        "MedicationRequest.dispenseRequest",
        "MedicationRequest.substitution",
        "MedicationRequest.priorPrescription",
        "MedicationRequest.detectedIssue",
        "MedicationRequest.eventHistory",
    ],
};