ferrobridge-fhirpath 0.1.127

A FHIRPath Normative Release 2.0.0 evaluator with the FHIR R4 additions, over the fhir-types element table
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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

//! Evaluating a parsed expression against a document.
//!
//! An [`Environment`] carries the type model, the caller's environment
//! variables and seams, and the clock; a [`Focus`] names the context node and
//! the resources `%resource` and `%rootResource` stand for (FHIR R4
//! §2.9.1.4). Evaluation returns a collection or an [`error::EvalError`],
//! two distinct outcomes: an empty collection is never an error and an error
//! is never an empty collection.

pub mod convert;
pub mod equality;
pub mod error;
mod functions;
mod navigate;
mod operators;
mod order;

use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;

use fhir_types::codec::Value;

use crate::ast::{Expr, Literal};
use crate::model::{Model, named_type, path_type};
use crate::quantity::Quantity;
use crate::seam::{Definitions, Profiles, Resolver, Terminology};
use crate::value::node::{Doc, Node};
use crate::value::{Collection, Item};
use convert::UCUM;
use error::EvalError;

/// The functions FHIRPath 3.0.0 adds as Standard for Trial Use that the HL7
/// FHIRPath test suite for FHIR R4 exercises
/// (<http://hl7.org/fhirpath/STU3/>, each a section marked STU).
///
/// FHIR R4 cites FHIRPath release 2, N1 2.0.0 (fhirpath.html §2.9), which
/// defines none of them, so an evaluation calls one only with
/// [`Environment::with_fhirpath_stu3_functions`]; without it the call is
/// [`EvalError::UnknownFunction`].
pub const STU3_FUNCTIONS: [&str; 13] = [
    "comparable",
    "decode",
    "encode",
    "escape",
    "highBoundary",
    "join",
    "lowBoundary",
    "matchesFull",
    "precision",
    "sort",
    "split",
    "trim",
    "unescape",
];

/// What an evaluation runs with: the type model, the environment variables
/// and the seams the caller supplies.
pub struct Environment<'a> {
    model: &'a dyn Model,
    variables: BTreeMap<String, Collection<'a>>,
    resolver: Option<&'a dyn Resolver>,
    terminology: Option<&'a dyn Terminology>,
    profiles: Option<&'a dyn Profiles>,
    definitions: Option<&'a dyn Definitions>,
    now: Option<jiff::Timestamp>,
    strict_ordering: bool,
    stu3_functions: bool,
}

impl fmt::Debug for Environment<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Environment")
            .field("model", &self.model)
            .field("variables", &self.variables.keys().collect::<Vec<_>>())
            .field("resolver", &self.resolver.is_some())
            .field("terminology", &self.terminology.is_some())
            .field("profiles", &self.profiles.is_some())
            .field("definitions", &self.definitions.is_some())
            .field("now", &self.now)
            .field("strict_ordering", &self.strict_ordering)
            .field("stu3_functions", &self.stu3_functions)
            .finish()
    }
}

impl<'a> Environment<'a> {
    /// An environment over `model` with no variables of its own and no seams.
    #[must_use]
    pub fn new(model: &'a dyn Model) -> Self {
        Self {
            model,
            variables: BTreeMap::new(),
            resolver: None,
            terminology: None,
            profiles: None,
            definitions: None,
            now: None,
            strict_ordering: false,
            stu3_functions: false,
        }
    }

    /// Defines the environment variable `%name` (N1 §9), which takes
    /// precedence over a variable of the same name N1 or FHIR R4 define.
    #[must_use]
    pub fn with_variable(mut self, name: impl Into<String>, value: Collection<'a>) -> Self {
        self.variables.insert(name.into(), value);
        self
    }

    /// Supplies the seam `resolve()` calls.
    #[must_use]
    pub fn with_resolver(mut self, resolver: &'a dyn Resolver) -> Self {
        self.resolver = Some(resolver);
        self
    }

    /// Supplies the seam `memberOf()`, `subsumes()` and `subsumedBy()` call.
    #[must_use]
    pub fn with_terminology(mut self, terminology: &'a dyn Terminology) -> Self {
        self.terminology = Some(terminology);
        self
    }

    /// Supplies the seam `conformsTo()` calls.
    #[must_use]
    pub fn with_profiles(mut self, profiles: &'a dyn Profiles) -> Self {
        self.profiles = Some(profiles);
        self
    }

    /// Supplies the seam `elementDefinition()` and `slice()` call.
    #[must_use]
    pub fn with_definitions(mut self, definitions: &'a dyn Definitions) -> Self {
        self.definitions = Some(definitions);
        self
    }

    /// Fixes the instant `now()`, `today()` and `timeOfDay()` answer; without
    /// it they read the clock once per evaluation.
    #[must_use]
    pub const fn with_now(mut self, now: jiff::Timestamp) -> Self {
        self.now = Some(now);
        self
    }

    /// Refuses an order-dependent operation on a collection whose order is
    /// undefined.
    ///
    /// N1 §3.1 leaves the order of `children()` and `descendants()`, and of
    /// every collection derived from them, undefined, and lets an
    /// implementation return an error when `first()`, `last()`, `tail()`,
    /// `skip()`, `take()` or the indexer runs over such a collection. With
    /// this mode on, that is [`EvalError::Unordered`]; without it, those
    /// operations answer in the order the document holds.
    #[must_use]
    pub const fn with_strict_ordering(mut self) -> Self {
        self.strict_ordering = true;
        self
    }

    /// Admits the functions of [`STU3_FUNCTIONS`], which FHIRPath 3.0.0 adds
    /// as Standard for Trial Use.
    ///
    /// Without it the evaluator is FHIRPath N1 2.0.0, the release FHIR R4
    /// cites (fhirpath.html §2.9), and a call to one of them is
    /// [`EvalError::UnknownFunction`]. Those this crate does not implement yet
    /// stay that error with the option on.
    #[must_use]
    pub const fn with_fhirpath_stu3_functions(mut self) -> Self {
        self.stu3_functions = true;
        self
    }

    /// The type model.
    #[must_use]
    pub fn model(&self) -> &'a dyn Model {
        self.model
    }
}

/// The context an expression is evaluated on.
#[derive(Clone)]
enum Start<'a> {
    /// A resource.
    Resource(&'a Value),
    /// An element of the named type, with a primitive's `_name` object and
    /// the element path of its definition.
    Element {
        value: Option<&'a Value>,
        element: Option<&'a Value>,
        type_name: String,
        definition: Option<String>,
    },
    /// Items from an earlier evaluation.
    Items(Collection<'a>),
}

/// The context node and the resources `%resource` and `%rootResource` name.
///
/// Its `Debug` form names what kind of context it is and leaves the document
/// out.
#[derive(Clone)]
pub struct Focus<'a> {
    start: Start<'a>,
    resource: Option<&'a Value>,
    root: Option<&'a Value>,
}

impl fmt::Debug for Focus<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let start = match &self.start {
            Start::Resource(_) => String::from("resource"),
            Start::Element { type_name, .. } => format!("element of {type_name}"),
            Start::Items(items) => format!("{} items", items.len()),
        };
        f.debug_struct("Focus")
            .field("start", &start)
            .field("resource", &self.resource.is_some())
            .field("root", &self.root.is_some())
            .finish()
    }
}

impl<'a> Focus<'a> {
    /// A resource as the context, `%resource` and `%rootResource`.
    #[must_use]
    pub const fn resource(resource: &'a Value) -> Self {
        Self {
            start: Start::Resource(resource),
            resource: Some(resource),
            root: Some(resource),
        }
    }

    /// A contained resource as the context and `%resource`, its container as
    /// `%rootResource` (FHIR R4 §2.9.1.4).
    #[must_use]
    pub const fn contained(resource: &'a Value, container: &'a Value) -> Self {
        Self {
            start: Start::Resource(resource),
            resource: Some(resource),
            root: Some(container),
        }
    }

    /// An element as the context, typed by `type_name` (a type name or an
    /// element path such as `Patient.contact`), inside `resource`.
    ///
    /// A primitive element is its JSON value; [`Focus::primitive`] adds the
    /// `_name` object.
    #[must_use]
    pub fn element(value: &'a Value, type_name: impl Into<String>, resource: &'a Value) -> Self {
        Self {
            start: Start::Element {
                value: Some(value),
                element: None,
                type_name: type_name.into(),
                definition: None,
            },
            resource: Some(resource),
            root: Some(resource),
        }
    }

    /// A primitive element as the context: its JSON value, its `_name`
    /// object, or both, inside `resource`.
    #[must_use]
    pub fn primitive(
        value: Option<&'a Value>,
        element: Option<&'a Value>,
        type_name: impl Into<String>,
        resource: &'a Value,
    ) -> Self {
        Self {
            start: Start::Element {
                value,
                element,
                type_name: type_name.into(),
                definition: None,
            },
            resource: Some(resource),
            root: Some(resource),
        }
    }

    /// Names the element definition an element context is an occurrence
    /// of, by its element path (`RiskAssessment.prediction.probability[x]`);
    /// any other context is left as it was.
    ///
    /// On an occurrence of a choice element, a member another type of the
    /// choice defines navigates to an empty collection where the
    /// occurrence's own type lacks it (N1 §3.2, FHIR R4 §2.9.1.1).
    #[must_use]
    pub fn defined_by(mut self, path: impl Into<String>) -> Self {
        if let Start::Element { definition, .. } = &mut self.start {
            *definition = Some(path.into());
        }
        self
    }

    /// Items from an earlier evaluation as the context.
    #[must_use]
    pub const fn items(items: Collection<'a>, resource: Option<&'a Value>) -> Self {
        Self {
            start: Start::Items(items),
            resource,
            root: resource,
        }
    }

    /// Names the resources `%resource` and `%rootResource` stand for,
    /// replacing what the constructor set (FHIR R4 §2.9.1.4): an element of
    /// a contained resource has that resource as `%resource` and its
    /// container as `%rootResource`, and `None` leaves a variable empty.
    #[must_use]
    pub fn within(mut self, resource: Option<&'a Value>, root: Option<&'a Value>) -> Self {
        self.resource = resource;
        self.root = root;
        self
    }

    /// No context at all: every path is empty.
    #[must_use]
    pub const fn none() -> Self {
        Self {
            start: Start::Items(Vec::new()),
            resource: None,
            root: None,
        }
    }
}

/// The state of one evaluation.
pub(crate) struct Engine<'e, 'a> {
    env: &'e Environment<'a>,
    context: Collection<'a>,
    resource: Collection<'a>,
    root: Collection<'a>,
    now: jiff::Timestamp,
}

/// The variables of the expression being evaluated: the focus (`$this`), the
/// iteration index and the aggregate total.
#[derive(Clone)]
pub(crate) struct Frame<'a> {
    focus: Collection<'a>,
    index: Option<usize>,
    total: Option<Collection<'a>>,
}

impl<'a> Frame<'a> {
    /// The frame of a top-level evaluation over `focus`.
    pub(crate) const fn top(focus: Collection<'a>) -> Self {
        Self {
            focus,
            index: None,
            total: None,
        }
    }
}

/// Evaluates `expr` in `env` on `focus`.
///
/// # Errors
///
/// Returns what the evaluation signals, as [`EvalError`].
pub(crate) fn evaluate<'a>(
    expr: &Expr,
    env: &Environment<'a>,
    focus: Focus<'a>,
) -> Result<Collection<'a>, EvalError> {
    let model = env.model;
    let as_resource = |value: Option<&'a Value>| -> Collection<'a> {
        value
            .map(|value| Item::Node(Node::resource(model, Doc::Borrowed(value))))
            .into_iter()
            .collect()
    };
    let context = match focus.start {
        Start::Resource(value) => as_resource(Some(value)),
        Start::Element {
            value,
            element,
            type_name,
            definition,
        } => {
            let ty = path_type(model, &type_name)
                .or_else(|| named_type(model, &type_name))
                .ok_or(EvalError::UnknownType { name: type_name })?;
            let definition = match definition {
                Some(path) => match model.schemas().element(&path) {
                    Some((_, field)) => Some(field.path),
                    None => return Err(EvalError::UnknownType { name: path }),
                },
                None => None,
            };
            let node = match (value, element) {
                (Some(value), element) => Some(Node::typed(
                    ty,
                    Doc::Borrowed(value),
                    element.map(Doc::Borrowed),
                )),
                (None, Some(element)) => Some(Node::typed(
                    ty,
                    Doc::Owned(Arc::new(Value::Null)),
                    Some(Doc::Borrowed(element)),
                )),
                (None, None) => None,
            };
            node.map(|node| match definition {
                Some(path) => node.with_definition(path),
                None => node,
            })
            .map(Item::Node)
            .into_iter()
            .collect()
        }
        Start::Items(items) => items,
    };
    let engine = Engine {
        env,
        resource: as_resource(focus.resource),
        root: as_resource(focus.root),
        now: env.now.unwrap_or_else(jiff::Timestamp::now),
        context: context.clone(),
    };
    engine.eval(expr, &Frame::top(context))
}

impl<'e, 'a> Engine<'e, 'a> {
    /// The type model.
    pub(crate) fn model(&self) -> &'a dyn Model {
        self.env.model
    }

    /// Evaluates one node of the syntax tree.
    pub(crate) fn eval(&self, expr: &Expr, frame: &Frame<'a>) -> Result<Collection<'a>, EvalError> {
        match expr {
            Expr::Literal(literal) => Ok(vec![literal_item(literal)]),
            Expr::Empty => Ok(Vec::new()),
            Expr::External(name) => self.external(name),
            Expr::This => Ok(frame.focus.clone()),
            Expr::Index => Ok(frame
                .index
                .and_then(|index| i64::try_from(index).ok())
                .map(Item::Integer)
                .into_iter()
                .collect()),
            Expr::Total => Ok(frame.total.clone().unwrap_or_default()),
            Expr::Member { input, name } => {
                let items = self.input(input.as_deref(), frame)?;
                self.navigate(&items, name, input.is_none())
            }
            Expr::Call { input, name, args } => {
                if order::ORDERED_FUNCTIONS.contains(&name.as_str()) {
                    self.require_order(name, input.as_deref())?;
                }
                let items = self.input(input.as_deref(), frame)?;
                self.call(name, items, args, frame)
            }
            Expr::Invocation { input, variable } => {
                let items = self.eval(input, frame)?;
                self.eval(variable, &Frame::top(items))
            }
            Expr::Indexer { input, index } => {
                self.require_order("[]", Some(input))?;
                let items = self.eval(input, frame)?;
                let index =
                    convert::integer(self.model(), &self.eval(index, frame)?, "the indexer")?;
                Ok(index
                    .and_then(|index| usize::try_from(index).ok())
                    .and_then(|index| items.into_iter().nth(index))
                    .into_iter()
                    .collect())
            }
            Expr::Negate(operand) => operators::negate(self.model(), &self.eval(operand, frame)?),
            Expr::Plus(operand) => operators::plus(self.model(), &self.eval(operand, frame)?),
            Expr::Binary { op, left, right } => self.binary(*op, left, right, frame),
            Expr::Is {
                operand,
                type_specifier,
            } => {
                let items = self.eval(operand, frame)?;
                let ty = self.resolve_type(type_specifier)?;
                match convert::single(&items, "is")? {
                    Some(item) => Ok(vec![Item::Boolean(self.is_instance(item, &ty))]),
                    None => Ok(Vec::new()),
                }
            }
            Expr::As {
                operand,
                type_specifier,
            } => {
                let items = self.eval(operand, frame)?;
                let ty = self.resolve_type(type_specifier)?;
                match convert::single(&items, "as")? {
                    Some(item) if self.is_type(item, &ty) => Ok(vec![item.clone()]),
                    _ => Ok(Vec::new()),
                }
            }
            // NOTE: the FHIRPath 3.0.0 grammar admits a direction only on a sort() key, and
            // sort() reads it there; evaluated anywhere else the key is its selector.
            Expr::SortKey { key, .. } => self.eval(key, frame),
        }
    }

    /// Refuses the order-dependent `operation` on `input` in the strict
    /// ordering mode when N1 §3.1 leaves the input's order undefined.
    fn require_order(&self, operation: &str, input: Option<&Expr>) -> Result<(), EvalError> {
        if self.env.strict_ordering && input.is_some_and(order::unordered) {
            return Err(EvalError::Unordered {
                operation: String::from(operation),
            });
        }
        Ok(())
    }

    /// The input of a member or a call: the expression before the `.`, or
    /// the focus at the start of a path.
    fn input(&self, input: Option<&Expr>, frame: &Frame<'a>) -> Result<Collection<'a>, EvalError> {
        match input {
            Some(input) => self.eval(input, frame),
            None => Ok(frame.focus.clone()),
        }
    }

    /// An environment variable (N1 §9, FHIR R4 §2.9.1.4 and §2.9.1.7).
    fn external(&self, name: &str) -> Result<Collection<'a>, EvalError> {
        if let Some(value) = self.env.variables.get(name) {
            return Ok(value.clone());
        }
        let text = |value: &str| Ok(vec![Item::String(String::from(value))]);
        match name {
            "ucum" => text(UCUM),
            "sct" => text("http://snomed.info/sct"),
            "loinc" => text("http://loinc.org"),
            "context" => Ok(self.context.clone()),
            "resource" => Ok(self.resource.clone()),
            "rootResource" => Ok(self.root.clone()),
            _ => {
                if let Some(id) = name.strip_prefix("vs-") {
                    text(&format!("http://hl7.org/fhir/ValueSet/{id}"))
                } else if let Some(id) = name.strip_prefix("ext-") {
                    text(&format!("http://hl7.org/fhir/StructureDefinition/{id}"))
                } else {
                    Err(EvalError::UnknownVariable {
                        name: String::from(name),
                    })
                }
            }
        }
    }

    /// The `%resource` collection.
    pub(crate) fn resource(&self) -> &Collection<'a> {
        &self.resource
    }

    /// The `%rootResource` collection.
    pub(crate) fn root(&self) -> &Collection<'a> {
        &self.root
    }

    /// The instant of this evaluation.
    pub(crate) const fn now(&self) -> jiff::Timestamp {
        self.now
    }

    /// The environment.
    pub(crate) const fn env(&self) -> &'e Environment<'a> {
        self.env
    }
}

/// The item a literal stands for.
fn literal_item<'a>(literal: &Literal) -> Item<'a> {
    match literal {
        Literal::Boolean(value) => Item::Boolean(*value),
        Literal::String(value) => Item::String(value.clone()),
        Literal::Integer(value) => Item::Integer(*value),
        Literal::Decimal(value) => Item::Decimal(*value),
        Literal::Date(value) => Item::Date(*value),
        Literal::DateTime(value) => Item::DateTime(*value),
        Literal::Time(value) => Item::Time(*value),
        Literal::Quantity(value, unit) => Item::Quantity(Quantity::new(*value, unit.clone())),
    }
}