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
//! Navigating types: the type of a variable, its elements, and the types of those.
//!
//! Shared by the checker and the engine, so both see the same elements: inherited ones from base
//! structures, backbone elements declared inline, content references (`Questionnaire.item.item`),
//! a primitive's `value`, and the type a choice element takes.
use std::sync::Arc;
use crate::definitions::{Definitions, Element, Kind, Structure, same_primitive, type_url};
/// The type of a value: a structure, or a backbone element inside one.
#[derive(Debug, Clone)]
pub enum Type {
/// Not known: no definition, a choice without a chosen type, an expression result.
Unknown,
Node {
structure: Arc<Structure>,
/// The element path to a backbone element; empty for the structure itself.
path: Vec<String>,
},
}
impl Type {
/// A key that identifies the type, for caching and comparisons.
pub fn key(&self) -> String {
match self {
Type::Unknown => "?".to_owned(),
Type::Node { structure, path } => {
let mut key = structure.url.clone();
for segment in path {
key.push('.');
key.push_str(segment);
}
key
}
}
}
/// `Patient` or `Patient.contact`, for messages.
pub fn display(&self) -> String {
match self {
Type::Unknown => "?".to_owned(),
Type::Node { structure, path } => {
let mut name = structure.type_name.clone();
for segment in path {
name.push('.');
name.push_str(segment);
}
name
}
}
}
/// The FHIR type name, as `fhirType()` gives it: `Patient`, `string`, or `BackboneElement` for
/// an element declared inline.
pub fn fhir_type(&self) -> Option<String> {
match self {
Type::Unknown => None,
Type::Node { structure, path } if path.is_empty() => Some(structure.type_name.clone()),
Type::Node { .. } => Some("BackboneElement".to_owned()),
}
}
pub fn structure(&self) -> Option<&Arc<Structure>> {
match self {
Type::Unknown => None,
Type::Node { structure, .. } => Some(structure),
}
}
/// Whether this is a resource (a resource structure itself, not an element of one).
pub fn is_resource(&self) -> bool {
matches!(self, Type::Node { structure, path } if path.is_empty() && structure.kind == Kind::Resource)
}
pub fn is_primitive(&self) -> bool {
matches!(self, Type::Node { structure, path } if path.is_empty() && structure.kind == Kind::PrimitiveType)
}
}
/// An element found on a type.
#[derive(Debug, Clone)]
pub struct ElementRef {
/// The element's name, as looked up.
pub name: String,
pub element: Element,
/// The structure that defines the element.
pub owner: Arc<Structure>,
/// The path to the element inside `owner`.
pub path: Vec<String>,
}
/// The result of looking up an element.
#[derive(Debug, Clone)]
pub enum Lookup {
Found(Box<ElementRef>),
/// The type is known and has no such element.
Missing,
/// The type, or part of the chain needed to answer, is not known.
Unknown,
}
/// Navigates types through a set of definitions.
#[derive(Clone, Copy)]
pub struct Types<'a> {
pub definitions: &'a dyn Definitions,
}
impl<'a> Types<'a> {
pub fn new(definitions: &'a dyn Definitions) -> Self {
Self { definitions }
}
/// The type a canonical URL names.
pub fn structure(&self, url: &str) -> Type {
match self.definitions.structure(url) {
Some(structure) => Type::Node {
structure,
path: Vec::new(),
},
None => Type::Unknown,
}
}
/// The element `name` of `ty`, declared or inherited.
pub fn lookup(&self, ty: &Type, name: &str) -> Lookup {
let Type::Node { structure, path } = ty else {
return Lookup::Unknown;
};
if path.is_empty() {
return self.lookup_in_structure(structure.clone(), name, 0);
}
let Some(node) = self.element_at(structure, path) else {
return Lookup::Unknown;
};
if let Some(element) = node.children.get(name) {
let mut element_path = path.clone();
element_path.push(name.to_owned());
return Lookup::Found(Box::new(ElementRef {
name: name.to_owned(),
element: element.clone(),
owner: structure.clone(),
path: element_path,
}));
}
// Not declared inline: inherited from the backbone's own type (BackboneElement, Element).
match node.types.first() {
Some(code) => match self.definitions.structure(&type_url(code, &structure.url)) {
Some(base) => self.lookup_in_structure(base, name, 0),
None => Lookup::Unknown,
},
None => Lookup::Unknown,
}
}
fn lookup_in_structure(&self, structure: Arc<Structure>, name: &str, depth: usize) -> Lookup {
// A primitive's value: `string.value` in the StructureDefinition, not a separate element
// in schemas that store primitives as plain JSON values.
if structure.kind == Kind::PrimitiveType && name == "value" {
return Lookup::Found(Box::new(ElementRef {
name: name.to_owned(),
element: Element::default(),
owner: structure.clone(),
path: vec![name.to_owned()],
}));
}
if let Some(element) = structure.elements.get(name) {
return Lookup::Found(Box::new(ElementRef {
name: name.to_owned(),
element: element.clone(),
owner: structure.clone(),
path: vec![name.to_owned()],
}));
}
let Some(base) = &structure.base else {
return Lookup::Missing;
};
if depth > 16 {
return Lookup::Unknown;
}
match self.definitions.structure(base) {
Some(base) => self.lookup_in_structure(base, name, depth + 1),
None => Lookup::Unknown,
}
}
/// The element at `path` inside `structure`, following content references.
fn element_at(&self, structure: &Structure, path: &[String]) -> Option<Element> {
let (first, rest) = path.split_first()?;
let mut current = self.resolve_reference(structure.elements.get(first)?)?;
for segment in rest {
current = self.resolve_reference(current.children.get(segment)?)?;
}
Some(current)
}
/// The element itself, or the element its content reference points to.
fn resolve_reference(&self, element: &Element) -> Option<Element> {
match &element.content_reference {
Some((url, target)) => {
let referenced = self.definitions.structure(url)?;
self.element_at(&referenced, target)
}
None => Some(element.clone()),
}
}
/// The type of an element: its backbone, the content it references, or its type. `chosen`
/// picks the type of a choice element.
pub fn element_type(&self, element: &ElementRef, chosen: Option<&str>) -> Type {
if let Some((url, target)) = &element.element.content_reference {
return match self.definitions.structure(url) {
Some(structure) => Type::Node {
structure,
path: target.clone(),
},
None => Type::Unknown,
};
}
if !element.element.children.is_empty() {
// Children a snapshot lists for an element of a structure type (`typeId.root` under
// an `II`) are that type's own: the element has the type. Children the type does not
// have make it a backbone element.
if let [code] = element.element.types.as_slice() {
let typed = self.structure(&type_url(code, &element.owner.url));
if matches!(&typed, Type::Node { structure, .. } if !structure.is_abstract)
&& element
.element
.children
.keys()
.all(|name| matches!(self.lookup(&typed, name), Lookup::Found(_)))
{
return typed;
}
}
return Type::Node {
structure: element.owner.clone(),
path: element.path.clone(),
};
}
let code = match (chosen, element.element.types.as_slice()) {
(Some(chosen), _) => chosen.to_owned(),
(None, [single]) => single.clone(),
_ => return Type::Unknown,
};
self.structure(&type_url(&code, &element.owner.url))
}
/// The type `name` names for a value of `element`: one of the element's types, by structure
/// name (`IVL_TS`), type name or URL, or a type next to them that derives from one (a `CE` in
/// a `CD` element). This is how a logical model's value says its type (`xsi:type`).
pub fn named_type(&self, element: &ElementRef, name: &str) -> Type {
let candidates: Vec<String> = element
.element
.types
.iter()
.map(|code| type_url(code, &element.owner.url))
.collect();
for url in &candidates {
if let Some(structure) = self.definitions.structure(url)
&& (structure.name == name
|| structure.type_name == name
|| url == name
|| url.ends_with(&format!("/{name}")))
{
return Type::Node {
structure,
path: Vec::new(),
};
}
}
if name.contains("://") {
return self.structure(name);
}
// Model URLs name types by id, where `IVL_TS` is `IVL-TS`.
for url in &candidates {
let Some((prefix, _)) = url.rsplit_once('/') else {
continue;
};
for id in [name.to_owned(), name.replace('_', "-")] {
if let Some(structure) = self.definitions.structure(&format!("{prefix}/{id}"))
&& (structure.name == name || structure.type_name.ends_with(name))
{
return Type::Node {
structure,
path: Vec::new(),
};
}
}
}
Type::Unknown
}
/// Whether `chosen` is one of `types`, or derives from one of them (`OperationOutcome` for an
/// element of type `Resource`).
pub fn is_one_of(&self, chosen: &str, types: &[String], context: &str) -> bool {
let matches = |name: &str| {
types
.iter()
.any(|t| same_primitive(t, name) || t == name || t.ends_with(&format!("/{name}")))
};
if matches(chosen) {
return true;
}
let mut url = type_url(chosen, context);
for _ in 0..16 {
let Some(structure) = self.definitions.structure(&url) else {
// Not a known structure: a type of another model, nothing to compare with.
return !self.definitions.covers(&url);
};
if matches(&structure.type_name) {
return true;
}
match &structure.base {
Some(base) => url = base.clone(),
None => return false,
}
}
true
}
}