fml-engine 0.2.1

StructureMap engine for the FHIR Mapping Language: run maps over FHIR JSON
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
//! The values a map works on: source and target content as one tree of typed nodes.
//!
//! Variables point at nodes, so a target created by one rule and filled by the next is the same
//! node. Each node knows its FHIR type where the definitions tell it, which the engine needs for
//! type filters (`src.value : string`), choice elements (`value` holding a Reference is written as
//! `valueReference`), repeating elements, and default groups chosen by type.

use fml_check::{Lookup, Type, Types};
use serde_json::{Map, Value};

/// A node in the [`Tree`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeId(usize);

#[derive(Debug, Clone)]
struct Node {
    ty: Type,
    /// The FHIR type when no definition is known for it (a FHIRPath result, for example).
    type_name: Option<String>,
    /// A resource: written with `resourceType`.
    resource: bool,
    data: Data,
}

#[derive(Debug, Clone)]
enum Data {
    /// A primitive value (`Null` until it is assigned), and its element's `id` and `extension`,
    /// which FHIR JSON writes as the `_name` sibling.
    Primitive(Value, Vec<Property>),
    Complex(Vec<Property>),
}

#[derive(Debug, Clone)]
struct Property {
    /// The element name; for a choice, without the type suffix (`value`).
    name: String,
    choice: bool,
    many: bool,
    /// Position in the definition, for writing elements in order.
    index: Option<usize>,
    values: Vec<NodeId>,
}

/// An element of a node, as the definitions describe it.
struct Slot {
    name: String,
    choice: bool,
    many: bool,
    index: Option<usize>,
    /// The element's type when it has a single one.
    ty: Type,
}

#[derive(Debug, Default, Clone)]
pub struct Tree {
    nodes: Vec<Node>,
    /// Where each node of the input is in it, as a FHIRPath (`Patient.name[0].given[1]`); only
    /// when [`record_paths`](Self::record_paths) was called.
    paths: std::collections::HashMap<NodeId, String>,
}

impl Tree {
    /// Records the location of every node under `root` (the input), starting with `root_name`
    /// (`Patient`): repeating elements get an index, choices their base name (`value`).
    pub fn record_paths(&mut self, root: NodeId, root_name: &str) {
        let mut pending = vec![(root, root_name.to_owned())];
        while let Some((node, path)) = pending.pop() {
            for property in self.properties(node) {
                for (index, &child) in property.values.iter().enumerate() {
                    let child_path = if property.many {
                        format!("{path}.{}[{index}]", property.name)
                    } else {
                        format!("{path}.{}", property.name)
                    };
                    pending.push((child, child_path));
                }
            }
            self.paths.insert(node, path);
        }
    }

    /// The location of an input node, if [`record_paths`](Self::record_paths) saw it.
    pub fn path(&self, id: NodeId) -> Option<&str> {
        self.paths.get(&id).map(String::as_str)
    }

    /// The number of nodes created.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    fn push(&mut self, node: Node) -> NodeId {
        self.nodes.push(node);
        NodeId(self.nodes.len() - 1)
    }

    /// An empty node of a type: complex, or primitive when the type is a primitive.
    pub fn create(&mut self, ty: Type, type_name: Option<String>) -> NodeId {
        let data = if ty.is_primitive() {
            Data::Primitive(Value::Null, Vec::new())
        } else {
            Data::Complex(Vec::new())
        };
        self.push(Node {
            resource: ty.is_resource(),
            ty,
            type_name,
            data,
        })
    }

    /// A primitive node holding `value`.
    pub fn primitive(&mut self, value: Value, ty: Type, type_name: Option<String>) -> NodeId {
        self.push(Node {
            ty,
            type_name,
            resource: false,
            data: Data::Primitive(value, Vec::new()),
        })
    }

    pub fn fhir_type(&self, id: NodeId) -> Option<String> {
        let node = &self.nodes[id.0];
        node.ty.fhir_type().or_else(|| node.type_name.clone())
    }

    pub fn ty(&self, id: NodeId) -> &Type {
        &self.nodes[id.0].ty
    }

    pub fn is_primitive(&self, id: NodeId) -> bool {
        matches!(self.nodes[id.0].data, Data::Primitive(..))
    }

    /// The primitive value of a node, `None` for complex nodes.
    pub fn value(&self, id: NodeId) -> Option<&Value> {
        match &self.nodes[id.0].data {
            Data::Primitive(value, _) => Some(value),
            Data::Complex(_) => None,
        }
    }

    /// The primitive value as text, as FHIRPath's string conversion gives it.
    pub fn text(&self, id: NodeId) -> Option<String> {
        match self.value(id)? {
            Value::String(s) => Some(s.clone()),
            Value::Null => None,
            other => Some(other.to_string()),
        }
    }

    // ---- Loading ----------------------------------------------------------------------------

    /// Loads JSON as a node of type `ty`; nested elements get the types the definitions give.
    pub fn load(&mut self, json: &Value, ty: Type, types: &Types<'_>) -> NodeId {
        // `resourceType` marks a resource, unless the type has an element of that name
        // (ExampleScenario.instance.resourceType names the type of a resource it talks about).
        let marks_resource = json.get("resourceType").is_some()
            && !matches!(types.lookup(&ty, "resourceType"), Lookup::Found(_));
        if !marks_resource && json.is_object() {
            return self.load_object(json, ty, None, types);
        }
        let ty = match (&ty, json.get("resourceType").and_then(Value::as_str)) {
            // A resource in a `Resource`-typed element, or a top-level input: its own type.
            (_, Some(resource_type)) if ty.fhir_type().as_deref() != Some(resource_type) => {
                let context = ty
                    .structure()
                    .map(|s| s.url.clone())
                    .unwrap_or_else(|| "http://hl7.org/fhir/StructureDefinition/".to_owned());
                match types.structure(&fml_check::definitions::type_url(resource_type, &context)) {
                    Type::Unknown => ty,
                    known => known,
                }
            }
            _ => ty,
        };
        let type_name = json
            .get("resourceType")
            .and_then(Value::as_str)
            .map(str::to_owned);
        if !json.is_object() {
            return self.primitive(json.clone(), ty, type_name);
        }
        self.load_object(json, ty, type_name, types)
    }

    /// Loads a JSON object; `resource_type` is set when it is a resource.
    fn load_object(
        &mut self,
        json: &Value,
        ty: Type,
        resource_type: Option<String>,
        types: &Types<'_>,
    ) -> NodeId {
        let Value::Object(object) = json else {
            return self.primitive(json.clone(), ty, None);
        };
        let is_resource = resource_type.is_some();
        let id = self.push(Node {
            resource: is_resource || ty.is_resource(),
            ty: ty.clone(),
            type_name: resource_type,
            data: Data::Complex(Vec::new()),
        });
        for (key, value) in object {
            if (is_resource && key == "resourceType")
                || (key.starts_with('_') && object.contains_key(&key[1..]))
            {
                continue;
            }
            // `_name` without `name`: a primitive that has only an id or extensions.
            if let Some(base) = key.strip_prefix('_') {
                let (slot, child_ty) = self.slot_for_key(&ty, base, types);
                let elements: Vec<&Value> = match value {
                    Value::Array(items) => items.iter().collect(),
                    single => vec![single],
                };
                let children: Vec<NodeId> = elements
                    .into_iter()
                    .map(|element| {
                        let child = self.primitive(Value::Null, child_ty.clone(), None);
                        self.load_primitive_element(child, element, types);
                        child
                    })
                    .collect();
                let many = slot.many || value.is_array();
                self.properties_mut(id).push(Property {
                    name: slot.name,
                    choice: slot.choice,
                    many,
                    index: slot.index,
                    values: children,
                });
                continue;
            }
            let (slot, child_ty) = self.slot_for_key(&ty, key, types);
            let values: Vec<&Value> = match value {
                Value::Array(items) => items.iter().collect(),
                single => vec![single],
            };
            let mut children = Vec::with_capacity(values.len());
            for item in values {
                children.push(self.load(item, child_ty.clone(), types));
            }
            // The primitives' `id` and `extension`, from the `_name` sibling.
            if let Some(elements) = object.get(&format!("_{key}")) {
                let elements: Vec<&Value> = match elements {
                    Value::Array(items) => items.iter().collect(),
                    single => vec![single],
                };
                for (&child, element) in children.iter().zip(elements) {
                    self.load_primitive_element(child, element, types);
                }
            }
            let many = slot.many || value.is_array();
            self.properties_mut(id).push(Property {
                name: slot.name,
                choice: slot.choice,
                many,
                index: slot.index,
                values: children,
            });
        }
        id
    }

    /// Loads a primitive's `_name` object (`id`, `extension`) into the primitive node.
    fn load_primitive_element(&mut self, primitive: NodeId, element: &Value, types: &Types<'_>) {
        if !element.is_object() || !self.is_primitive(primitive) {
            return;
        }
        let holder = self.load(element, self.ty(primitive).clone(), types);
        let properties = std::mem::take(self.properties_mut(holder));
        *self.properties_mut(primitive) = properties;
    }

    /// The element a JSON key holds: `valueString` is the choice `value` with type `string`.
    fn slot_for_key(&self, ty: &Type, key: &str, types: &Types<'_>) -> (Slot, Type) {
        if let Lookup::Found(found) = types.lookup(ty, key) {
            if let Some(base) = &found.element.choice_of {
                let base_slot = match types.lookup(ty, base) {
                    Lookup::Found(base_found) => base_found.element.index,
                    _ => found.element.index,
                };
                let child = types.element_type(&found, None);
                return (
                    Slot {
                        name: base.clone(),
                        choice: true,
                        many: false,
                        index: base_slot,
                        ty: child.clone(),
                    },
                    child,
                );
            }
            let child = types.element_type(&found, None);
            return (
                Slot {
                    name: key.to_owned(),
                    choice: found.element.is_choice(),
                    many: found.element.array,
                    index: found.element.index,
                    ty: child.clone(),
                },
                child,
            );
        }
        (
            Slot {
                name: key.to_owned(),
                choice: false,
                many: false,
                index: None,
                ty: Type::Unknown,
            },
            Type::Unknown,
        )
    }

    // ---- Navigation -------------------------------------------------------------------------

    /// The elements of a node; for a primitive, those of its element (`id`, `extension`).
    fn properties(&self, id: NodeId) -> &[Property] {
        match &self.nodes[id.0].data {
            Data::Complex(properties) | Data::Primitive(_, properties) => properties,
        }
    }

    fn properties_mut(&mut self, id: NodeId) -> &mut Vec<Property> {
        match &mut self.nodes[id.0].data {
            Data::Complex(properties) | Data::Primitive(_, properties) => properties,
        }
    }

    /// The children named `name`. A choice is found by its name (`value`) or by the name with
    /// the type (`valueString`). A primitive's `value` is the primitive itself.
    pub fn children(&self, id: NodeId, name: &str) -> Vec<NodeId> {
        if name == "value" && self.is_primitive(id) {
            return vec![id];
        }
        let mut out = Vec::new();
        for property in self.properties(id) {
            if property.name == name {
                out.extend(&property.values);
            } else if property.choice && name.starts_with(&property.name) {
                for &value in &property.values {
                    let suffix = &name[property.name.len()..];
                    if self
                        .fhir_type(value)
                        .is_some_and(|t| capitalize(&t) == suffix)
                    {
                        out.push(value);
                    }
                }
            }
        }
        out
    }

    // ---- Writing ----------------------------------------------------------------------------

    /// The element `name` of `id`, as the definitions describe it.
    fn slot(&self, id: NodeId, name: &str, types: &Types<'_>) -> Result<Slot, String> {
        let ty = self.ty(id).clone();
        match types.lookup(&ty, name) {
            Lookup::Found(found) => Ok(Slot {
                name: name.to_owned(),
                choice: found.element.is_choice(),
                many: found.element.array,
                index: found.element.index,
                ty: types.element_type(&found, None),
            }),
            Lookup::Missing => Err(format!("`{}` has no element `{name}`", ty.display())),
            Lookup::Unknown => {
                // No definition: keep what is there. An element already holding several values
                // repeats.
                let existing = self.properties(id).iter().find(|p| p.name == name);
                Ok(Slot {
                    name: name.to_owned(),
                    choice: false,
                    many: existing.is_some_and(|p| p.many),
                    index: None,
                    ty: Type::Unknown,
                })
            }
        }
    }

    /// The target element `name` of `id` to write into: a new value for a repeating element, the
    /// existing (or a new) value for a single one.
    pub fn make_property(
        &mut self,
        id: NodeId,
        name: &str,
        types: &Types<'_>,
    ) -> Result<NodeId, String> {
        if self.is_primitive(id) && name == "value" {
            return Ok(id);
        }
        let slot = self.slot(id, name, types)?;
        if slot.choice {
            return Err(format!(
                "`{}.{name}` is a choice; give the type with a transform such as create('Type')",
                self.ty(id).display()
            ));
        }
        if !slot.many
            && let Some(existing) = self
                .properties(id)
                .iter()
                .find(|p| p.name == name)
                .and_then(|p| p.values.first().copied())
        {
            return Ok(existing);
        }
        let child = self.create(slot.ty.clone(), None);
        self.attach(id, slot, child);
        Ok(child)
    }

    /// Sets the element `name` of `id` to `value`: appended to a repeating element, replacing a
    /// single one. A primitive value takes the element's type.
    pub fn set_property(
        &mut self,
        id: NodeId,
        name: &str,
        value: NodeId,
        types: &Types<'_>,
    ) -> Result<(), String> {
        // A primitive's `value` is the primitive itself.
        if self.is_primitive(id) && name == "value" {
            let assigned = self.value(value).cloned().unwrap_or(Value::Null);
            if let Data::Primitive(current, _) = &mut self.nodes[id.0].data {
                *current = assigned;
            }
            return Ok(());
        }
        let slot = self.slot(id, name, types)?;
        if slot.choice && self.fhir_type(value).is_none() {
            return Err(format!(
                "cannot tell which type of `{}.{name}` the value has",
                self.ty(id).display()
            ));
        }
        if !slot.choice && self.is_primitive(value) && !matches!(slot.ty, Type::Unknown) {
            self.nodes[value.0].ty = slot.ty.clone();
        }
        if !slot.many {
            self.properties_mut(id).retain(|p| p.name != name);
        }
        self.attach(id, slot, value);
        Ok(())
    }

    fn attach(&mut self, id: NodeId, slot: Slot, child: NodeId) {
        let properties = self.properties_mut(id);
        match properties.iter_mut().find(|p| p.name == slot.name) {
            Some(property) => property.values.push(child),
            None => properties.push(Property {
                name: slot.name,
                choice: slot.choice,
                many: slot.many,
                index: slot.index,
                values: vec![child],
            }),
        }
    }

    /// Moves `value` within the repeating element `name` of `id`: to `index`, or to the end.
    pub fn move_value(&mut self, id: NodeId, name: &str, value: NodeId, index: Option<usize>) {
        let Some(property) = self
            .properties_mut(id)
            .iter_mut()
            .find(|p| p.name == name && p.many)
        else {
            return;
        };
        let Some(at) = property.values.iter().position(|&v| v == value) else {
            return;
        };
        property.values.remove(at);
        let index = index.map_or(property.values.len(), |i| i.min(property.values.len()));
        property.values.insert(index, value);
    }

    /// Copies the content of `from` into `to`, as when a value is assigned to an existing target.
    pub fn assign(&mut self, to: NodeId, from: NodeId) {
        let copy = self.deep_copy(from);
        let node = self.nodes[copy.0].clone();
        let target = &mut self.nodes[to.0];
        target.data = node.data;
        if matches!(target.ty, Type::Unknown) {
            target.ty = node.ty;
            target.type_name = node.type_name;
        }
    }

    /// A copy of the subtree at `id`.
    pub fn deep_copy(&mut self, id: NodeId) -> NodeId {
        let node = self.nodes[id.0].clone();
        let copy_properties = |properties: Vec<Property>, tree: &mut Self| -> Vec<Property> {
            properties
                .into_iter()
                .map(|property| Property {
                    values: property
                        .values
                        .iter()
                        .map(|&value| tree.deep_copy(value))
                        .collect(),
                    ..property
                })
                .collect()
        };
        let data = match node.data {
            Data::Primitive(value, properties) => {
                Data::Primitive(value, copy_properties(properties, self))
            }
            Data::Complex(properties) => Data::Complex(copy_properties(properties, self)),
        };
        self.push(Node { data, ..node })
    }

    /// Sets a primitive child, such as a resource's `id`.
    pub fn set_text(
        &mut self,
        id: NodeId,
        name: &str,
        text: &str,
        types: &Types<'_>,
    ) -> Result<(), String> {
        let value = self.primitive(
            Value::String(text.to_owned()),
            Type::Unknown,
            Some("string".to_owned()),
        );
        self.set_property(id, name, value, types)
    }

    /// The nodes reachable from `root`, `root` included.
    pub fn reachable(&self, root: NodeId) -> std::collections::HashSet<NodeId> {
        let mut seen = std::collections::HashSet::new();
        let mut stack = vec![root];
        while let Some(node) = stack.pop() {
            if seen.insert(node) {
                for property in self.properties(node) {
                    stack.extend(&property.values);
                }
            }
        }
        seen
    }

    // ---- Output -----------------------------------------------------------------------------

    /// The node as FHIR JSON, elements in definition order.
    pub fn to_json(&self, id: NodeId) -> Value {
        let node = &self.nodes[id.0];
        match &node.data {
            Data::Primitive(value, _) => value.clone(),
            Data::Complex(properties) => {
                let mut object = Map::new();
                if (node.resource || node.ty.is_resource())
                    && let Some(name) = self.fhir_type(id)
                {
                    object.insert("resourceType".to_owned(), Value::String(name));
                }
                self.write_properties(properties, &mut object);
                Value::Object(object)
            }
        }
    }

    /// A primitive's `id` and `extension` as the `_name` object, if it has any.
    fn primitive_element(&self, id: NodeId) -> Option<Value> {
        match &self.nodes[id.0].data {
            Data::Primitive(_, properties) if !properties.is_empty() => {
                let mut object = Map::new();
                self.write_properties(properties, &mut object);
                (!object.is_empty()).then_some(Value::Object(object))
            }
            _ => None,
        }
    }

    fn write_properties(&self, properties: &[Property], object: &mut Map<String, Value>) {
        let mut ordered: Vec<&Property> = properties.iter().collect();
        ordered.sort_by_key(|p| p.index.unwrap_or(usize::MAX));
        for property in ordered {
            let values: Vec<(NodeId, Value, Option<Value>)> = property
                .values
                .iter()
                .map(|&value| (value, self.to_json(value), self.primitive_element(value)))
                .filter(|(_, json, element)| !is_empty(json) || element.is_some())
                .collect();
            if values.is_empty() {
                continue;
            }
            let mut put = |key: String, json: Value, element: Option<Value>| {
                if !json.is_null() {
                    object.insert(key.clone(), json);
                }
                if let Some(element) = element {
                    object.insert(format!("_{key}"), element);
                }
            };
            if property.choice {
                for (value, json, element) in values {
                    let suffix = self
                        .fhir_type(value)
                        .map(|t| capitalize(&t))
                        .unwrap_or_default();
                    put(format!("{}{suffix}", property.name), json, element);
                }
            } else if property.many {
                let any_element = values.iter().any(|(_, _, e)| e.is_some());
                let any_value = values.iter().any(|(_, json, _)| !json.is_null());
                let (jsons, elements): (Vec<Value>, Vec<Value>) = values
                    .into_iter()
                    .map(|(_, json, element)| (json, element.unwrap_or(Value::Null)))
                    .unzip();
                if any_value {
                    object.insert(property.name.clone(), Value::Array(jsons));
                }
                if any_element {
                    object.insert(format!("_{}", property.name), Value::Array(elements));
                }
            } else {
                let (_, json, element) = values.into_iter().last().expect("not empty");
                put(property.name.clone(), json, element);
            }
        }
    }
}

/// Values left out of the output: unassigned primitives and elements with no content.
fn is_empty(json: &Value) -> bool {
    match json {
        Value::Null => true,
        Value::Object(map) => map.is_empty(),
        Value::Array(items) => items.is_empty(),
        _ => false,
    }
}

pub(crate) fn capitalize(text: &str) -> String {
    let mut chars = text.chars();
    match chars.next() {
        Some(first) => first.to_uppercase().chain(chars).collect(),
        None => String::new(),
    }
}