openehr-lang 0.0.2

openEHR LANG 1.0.0: BMM/P_BMM object model (generated from BMM) plus a hand-written ODIN instance parser (the `odin` module — deliberately off the codegen path).
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
//! Hand-written spec functions of the BMM **v3** class-feature family
//! (`BMM_FORMAL_ELEMENT` / `BMM_FEATURE` / `BMM_ROUTINE` and their leaves) — and
//! the home of that family's invariant boundary.
//!
//! Spec: `LANG/docs/bmm3/master08-core-features.adoc` plus the class definitions
//! under `LANG/docs/UML/classes/`: `org.openehr.lang.bmm3.bmm_formal_element.adoc`
//! §Functions (`signature`, `is_boolean`), `…bmm3.bmm_routine.adoc` §Functions
//! (`arity`), `…bmm3.bmm_function.adoc`, `…bmm3.bmm_procedure.adoc`,
//! `…bmm3.bmm_property.adoc`, `…bmm3.bmm_constant.adoc`, all §Functions and
//! §Invariants.
//!
//! NOTE (adjudicated boundary — the declared feature INVARIANTS are not
//! enforced): `BMM_FUNCTION.Operator_validity` ("`operator_def /= Void implies
//! arity in |1..2|`"), `BMM_FUNCTION.Inv_signature_has_result`,
//! `BMM_FUNCTION.Inv_result_type` ("`type = Result.type`"),
//! `BMM_PROCEDURE.Inv_signature_no_result`, `BMM_PROPERTY.Inv_signature_no_args`
//! and `BMM_CONSTANT.Inv_not_nullable` are class-definition invariants of a
//! meta-model this workspace only ever CONSTRUCTS from a vendored schema, never
//! edits: the v3 generation has no materialisation source that could produce a
//! violating instance (P_BMM is the v2.x persistence form,
//! `LANG/docs/bmm/master06-persistence.adoc`, and its routine/constant
//! attributes are opaque strings), and openEHR publishes no validation-report
//! shape for a BMM model. Enforcement would therefore be a check with no input
//! and no consumer. The functions the invariants are written against
//! ([`crate::bmm3::core::feature::bmm_feature_impl`]'s `signature`, `arity`) ARE
//! implemented here, so an invariant pass becomes ordinary work the day a v3
//! model is built from an editable source.

use crate::bmm3::core::bmm_formal_element::BmmFormalElement;
use crate::bmm3::core::entity::bmm_container_type::BmmContainerType;
use crate::bmm3::core::entity::bmm_function_type::BmmFunctionType;
use crate::bmm3::core::entity::bmm_procedure_type::BmmProcedureType;
use crate::bmm3::core::entity::bmm_property_type::BmmPropertyType;
use crate::bmm3::core::entity::bmm_routine_type::BmmRoutineType;
use crate::bmm3::core::entity::bmm_signature::BmmSignature;
use crate::bmm3::core::entity::bmm_tuple_type::BmmTupleType;
use crate::bmm3::core::entity::bmm_type::BmmType;
use crate::bmm3::core::feature::bmm_constant::BmmConstant;
use crate::bmm3::core::feature::bmm_container_property::BmmContainerProperty;
use crate::bmm3::core::feature::bmm_feature::BmmFeature;
use crate::bmm3::core::feature::bmm_function::BmmFunction;
use crate::bmm3::core::feature::bmm_parameter::BmmParameter;
use crate::bmm3::core::feature::bmm_procedure::BmmProcedure;
use crate::bmm3::core::feature::bmm_property::BmmProperty;
use crate::bmm3::core::feature::bmm_routine::BmmRoutine;
use crate::bmm3::core::feature::bmm_singleton::BmmSingleton;
use crate::bmm3::core::feature::bmm_unitary_property::BmmUnitaryProperty;

/// The type name a notionally-Boolean element's type carries
/// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions `is_boolean`:
/// "a `BMM_SIMPLE_TYPE` with `_type_name()_` = `'Boolean'`").
pub const BOOLEAN_TYPE_NAME: &str = "Boolean";

/// The `_argument_types_` tuple of a routine signature: each parameter's type,
/// keyed by the parameter name — "represented as a type-tuple (list of arbitrary
/// types)" whose items are "keyed by purpose in the tuple"
/// (`org.openehr.lang.bmm3.bmm_routine_type.adoc` §Attributes,
/// `…bmm3.bmm_tuple_type.adoc` §Attributes). `None` for a routine with no
/// parameters, since `_argument_types_` is `0..1` ("Type of arguments in the
/// signature, **if any**").
fn argument_types(parameters: &[BmmParameter]) -> Option<BmmTupleType> {
    if parameters.is_empty() {
        return None;
    }
    Some(BmmTupleType {
        item_types: parameters
            .iter()
            .map(|p| (p.name.clone(), p.r#type.clone()))
            .collect(),
    })
}

/// Is `t` the notionally-Boolean type — "a `BMM_SIMPLE_TYPE` with
/// `_type_name()_` = `'Boolean'`" (`…bmm3.bmm_formal_element.adoc` §Functions)?
///
/// The name comparison is case-insensitive per the BMM naming convention
/// ("it is assumed that case-insensitive matching is used",
/// `LANG/docs/bmm3/master05-core-model.adoc` §Naming Convention).
fn type_is_boolean(t: &BmmType) -> bool {
    matches!(t, BmmType::BmmSimpleType(simple)
        if simple.type_name().eq_ignore_ascii_case(BOOLEAN_TYPE_NAME))
}

impl BmmContainerProperty {
    /// `BMM_CONTAINER_PROPERTY.type` (redefined): the container type of this
    /// property (`org.openehr.lang.bmm3.bmm_container_property.adoc` §Attributes),
    /// for either container-property form.
    #[must_use]
    pub fn r#type(&self) -> BmmContainerType {
        match self {
            Self::BmmIndexedContainerProperty(indexed) => {
                BmmContainerType::BmmIndexedContainerType(Box::new(indexed.r#type.clone()))
            }
            Self::BmmContainerProperty(data) => data.r#type.clone(),
        }
    }

    /// `BMM_MODEL_ELEMENT.name`: "Name of this model element"
    /// (`org.openehr.lang.bmm3.bmm_model_element.adoc` §Attributes).
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::BmmIndexedContainerProperty(indexed) => indexed.name.as_str(),
            Self::BmmContainerProperty(data) => data.name.as_str(),
        }
    }
}

impl BmmProperty {
    /// `BMM_MODEL_ELEMENT.name`: "Name of this model element"
    /// (`org.openehr.lang.bmm3.bmm_model_element.adoc` §Attributes).
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::BmmContainerProperty(property) => property.name(),
            Self::BmmUnitaryProperty(property) => property.name.as_str(),
        }
    }
}

impl BmmFeature {
    /// `BMM_MODEL_ELEMENT.name`: "Name of this model element"
    /// (`org.openehr.lang.bmm3.bmm_model_element.adoc` §Attributes) — the key
    /// every feature map of `BMM_CLASS` is keyed by (`…bmm3.bmm_class.adoc`
    /// §Attributes).
    #[must_use]
    pub fn name(&self) -> &str {
        match self {
            Self::BmmConstant(constant) => constant.name.as_str(),
            Self::BmmContainerProperty(property) => property.name(),
            Self::BmmFunction(function) => function.name.as_str(),
            Self::BmmProcedure(procedure) => procedure.name.as_str(),
            Self::BmmSingleton(singleton) => singleton.name.as_str(),
            Self::BmmUnitaryProperty(property) => property.name.as_str(),
        }
    }
}

impl BmmFunction {
    /// `BMM_ROUTINE.arity`: "Return number of arguments of this routine"
    /// (`org.openehr.lang.bmm3.bmm_routine.adoc` §Functions).
    #[must_use]
    pub fn arity(&self) -> i32 {
        i32::try_from(self.parameters.as_ref().map_or(0, Vec::len)).unwrap_or(i32::MAX)
    }

    /// `BMM_FORMAL_ELEMENT.signature` as effected for a function: the
    /// `BMM_FUNCTION_TYPE` of its parameters and result
    /// (`org.openehr.lang.bmm3.bmm_function_type.adoc` §Description "Meta-type
    /// for function object signatures"; `…bmm3.bmm_formal_element.adoc`
    /// §Functions "Formal signature of this element, in the form: `name
    /// [arg1_name: T_arg1, ...][:T_value]`").
    ///
    /// The result type is the function's own `_type_`, which
    /// `BMM_FUNCTION.Inv_result_type` states equals `Result.type`
    /// (`…bmm3.bmm_function.adoc` §Invariants).
    #[must_use]
    pub fn signature(&self) -> BmmFunctionType {
        BmmFunctionType {
            result_type: self.r#type.clone(),
            argument_types: argument_types(self.parameters.as_deref().unwrap_or_default()),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`: "True if `_type_` is notionally Boolean"
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        type_is_boolean(&self.r#type)
    }
}

impl BmmProcedure {
    /// `BMM_ROUTINE.arity`: "Return number of arguments of this routine"
    /// (`org.openehr.lang.bmm3.bmm_routine.adoc` §Functions).
    #[must_use]
    pub fn arity(&self) -> i32 {
        i32::try_from(self.parameters.as_ref().map_or(0, Vec::len)).unwrap_or(i32::MAX)
    }

    /// `BMM_PROCEDURE.signature` (effected): the `BMM_PROCEDURE_TYPE` whose
    /// `_result_type_` is the built-in Status type
    /// (`org.openehr.lang.bmm3.bmm_procedure.adoc` §Functions + §Attributes —
    /// `type` is redefined to `BMM_STATUS_TYPE`;
    /// `…bmm3.bmm_procedure_type.adoc` §Description "with `_result_type_` being
    /// the special Status meta-type").
    #[must_use]
    pub fn signature(&self) -> BmmProcedureType {
        BmmProcedureType {
            result_type: Some(self.r#type.clone()),
            argument_types: argument_types(self.parameters.as_deref().unwrap_or_default()),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean` — always false for a procedure, whose
    /// `_type_` is the Status meta-type, not a `BMM_SIMPLE_TYPE`
    /// (`org.openehr.lang.bmm3.bmm_procedure.adoc` §Attributes).
    #[expect(
        clippy::unused_self,
        reason = "the class definitions declare is_boolean() on BMM_FORMAL_ELEMENT; for a procedure the redefined `type` makes the answer constant"
    )]
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        false
    }
}

impl BmmRoutine {
    /// `BMM_ROUTINE.arity`: "Return number of arguments of this routine"
    /// (`org.openehr.lang.bmm3.bmm_routine.adoc` §Functions).
    #[must_use]
    pub fn arity(&self) -> i32 {
        match self {
            Self::BmmFunction(function) => function.arity(),
            Self::BmmProcedure(procedure) => procedure.arity(),
        }
    }

    /// `BMM_FORMAL_ELEMENT.signature` for either routine kind, as the
    /// `BMM_ROUTINE_TYPE` slot both effected forms belong to
    /// (`org.openehr.lang.bmm3.bmm_routine_type.adoc`).
    #[must_use]
    pub fn signature(&self) -> BmmRoutineType {
        match self {
            Self::BmmFunction(function) => BmmRoutineType::BmmFunctionType(function.signature()),
            Self::BmmProcedure(procedure) => {
                BmmRoutineType::BmmProcedureType(procedure.signature())
            }
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        match self {
            Self::BmmFunction(function) => function.is_boolean(),
            Self::BmmProcedure(procedure) => procedure.is_boolean(),
        }
    }
}

impl BmmUnitaryProperty {
    /// `BMM_FORMAL_ELEMENT.signature` for a property: a `BMM_PROPERTY_TYPE`
    /// carrying only the result type — "Meta-type for property and variable
    /// signatures" (`org.openehr.lang.bmm3.bmm_property_type.adoc`
    /// §Description), which is the degree-zero signature
    /// `BMM_PROPERTY.Inv_signature_no_args` states
    /// (`…bmm3.bmm_property.adoc` §Invariants).
    #[must_use]
    pub fn signature(&self) -> BmmPropertyType {
        BmmPropertyType {
            result_type: BmmType::from(self.r#type.clone()),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        type_is_boolean(&BmmType::from(self.r#type.clone()))
    }
}

impl BmmContainerProperty {
    /// `BMM_FORMAL_ELEMENT.signature` for a container property: the
    /// `BMM_PROPERTY_TYPE` whose result type is the container type itself
    /// (`org.openehr.lang.bmm3.bmm_container_property.adoc` §Attributes redefines
    /// `type` to `BMM_CONTAINER_TYPE`; `…bmm3.bmm_property_type.adoc`
    /// §Description).
    #[must_use]
    pub fn signature(&self) -> BmmPropertyType {
        BmmPropertyType {
            result_type: BmmType::BmmContainerType(Box::new(self.r#type())),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean` — always false for a container property:
    /// its `_type_` is a `BMM_CONTAINER_TYPE`, never a `BMM_SIMPLE_TYPE`
    /// (`org.openehr.lang.bmm3.bmm_container_property.adoc` §Attributes).
    #[expect(
        clippy::unused_self,
        reason = "the class definitions declare is_boolean() on BMM_FORMAL_ELEMENT; for a container property the redefined `type` makes the answer constant"
    )]
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        false
    }
}

impl BmmProperty {
    /// `BMM_FORMAL_ELEMENT.signature` for either property kind
    /// (`org.openehr.lang.bmm3.bmm_property_type.adoc`).
    #[must_use]
    pub fn signature(&self) -> BmmPropertyType {
        match self {
            Self::BmmContainerProperty(property) => property.signature(),
            Self::BmmUnitaryProperty(property) => property.signature(),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        match self {
            Self::BmmContainerProperty(property) => property.is_boolean(),
            Self::BmmUnitaryProperty(property) => property.is_boolean(),
        }
    }
}

impl BmmConstant {
    /// `BMM_FORMAL_ELEMENT.signature` for a constant — the argument-less
    /// `BMM_PROPERTY_TYPE` of its type
    /// (`org.openehr.lang.bmm3.bmm_property_type.adoc` §Description "Meta-type
    /// for property and variable signatures").
    #[must_use]
    pub fn signature(&self) -> BmmPropertyType {
        BmmPropertyType {
            result_type: self.r#type.clone(),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        type_is_boolean(&self.r#type)
    }
}

impl BmmSingleton {
    /// `BMM_FORMAL_ELEMENT.signature` for a singleton — the argument-less
    /// `BMM_PROPERTY_TYPE` of its type
    /// (`org.openehr.lang.bmm3.bmm_property_type.adoc` §Description).
    #[must_use]
    pub fn signature(&self) -> BmmPropertyType {
        BmmPropertyType {
            result_type: self.r#type.clone(),
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        type_is_boolean(&self.r#type)
    }
}

impl BmmFormalElement {
    /// `BMM_FORMAL_ELEMENT.signature` (abstract, "Specific implementations in
    /// descendants" — `org.openehr.lang.bmm3.bmm_formal_element.adoc`
    /// §Functions), dispatched to the effected form of each leaf.
    #[must_use]
    pub fn signature(&self) -> BmmSignature {
        match self {
            Self::BmmConstant(constant) => {
                BmmSignature::BmmPropertyType(Box::new(constant.signature()))
            }
            Self::BmmContainerProperty(property) => {
                BmmSignature::BmmPropertyType(Box::new(property.signature()))
            }
            Self::BmmFunction(function) => BmmSignature::BmmRoutineType(Box::new(
                BmmRoutineType::BmmFunctionType(function.signature()),
            )),
            Self::BmmProcedure(procedure) => BmmSignature::BmmRoutineType(Box::new(
                BmmRoutineType::BmmProcedureType(procedure.signature()),
            )),
            Self::BmmSingleton(singleton) => {
                BmmSignature::BmmPropertyType(Box::new(singleton.signature()))
            }
            Self::BmmUnitaryProperty(property) => {
                BmmSignature::BmmPropertyType(Box::new(property.signature()))
            }
            // The four variable leaves (`BMM_LOCAL`, `BMM_PARAMETER`,
            // `BMM_RESULT`, `BMM_SELF`) are `BMM_VARIABLE`s, i.e. formal
            // elements with no arguments, so their signature is the same
            // argument-less `BMM_PROPERTY_TYPE` — "Meta-type for property and
            // variable signatures" (`…bmm3.bmm_property_type.adoc`
            // §Description).
            Self::BmmLocal(local) => BmmSignature::BmmPropertyType(Box::new(BmmPropertyType {
                result_type: local.r#type.clone(),
            })),
            Self::BmmParameter(parameter) => {
                BmmSignature::BmmPropertyType(Box::new(BmmPropertyType {
                    result_type: parameter.r#type.clone(),
                }))
            }
            Self::BmmResult(result) => BmmSignature::BmmPropertyType(Box::new(BmmPropertyType {
                result_type: result.r#type.clone(),
            })),
            Self::BmmSelf(self_variable) => {
                BmmSignature::BmmPropertyType(Box::new(BmmPropertyType {
                    result_type: self_variable.r#type.clone(),
                }))
            }
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`: "True if `_type_` is notionally Boolean
    /// (i.e. a `BMM_SIMPLE_TYPE` with `_type_name()_` = `'Boolean'`)"
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        match self {
            Self::BmmConstant(constant) => constant.is_boolean(),
            Self::BmmContainerProperty(property) => property.is_boolean(),
            Self::BmmFunction(function) => function.is_boolean(),
            Self::BmmProcedure(procedure) => procedure.is_boolean(),
            Self::BmmSingleton(singleton) => singleton.is_boolean(),
            Self::BmmUnitaryProperty(property) => property.is_boolean(),
            Self::BmmLocal(local) => type_is_boolean(&local.r#type),
            Self::BmmParameter(parameter) => type_is_boolean(&parameter.r#type),
            Self::BmmResult(result) => type_is_boolean(&result.r#type),
            Self::BmmSelf(self_variable) => type_is_boolean(&self_variable.r#type),
        }
    }
}

impl BmmFeature {
    /// `BMM_FORMAL_ELEMENT.signature` for any feature of a class
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn signature(&self) -> BmmSignature {
        match self {
            Self::BmmConstant(constant) => {
                BmmSignature::BmmPropertyType(Box::new(constant.signature()))
            }
            Self::BmmContainerProperty(property) => {
                BmmSignature::BmmPropertyType(Box::new(property.signature()))
            }
            Self::BmmFunction(function) => BmmSignature::BmmRoutineType(Box::new(
                BmmRoutineType::BmmFunctionType(function.signature()),
            )),
            Self::BmmProcedure(procedure) => BmmSignature::BmmRoutineType(Box::new(
                BmmRoutineType::BmmProcedureType(procedure.signature()),
            )),
            Self::BmmSingleton(singleton) => {
                BmmSignature::BmmPropertyType(Box::new(singleton.signature()))
            }
            Self::BmmUnitaryProperty(property) => {
                BmmSignature::BmmPropertyType(Box::new(property.signature()))
            }
        }
    }

    /// `BMM_FORMAL_ELEMENT.is_boolean`
    /// (`org.openehr.lang.bmm3.bmm_formal_element.adoc` §Functions).
    #[must_use]
    pub fn is_boolean(&self) -> bool {
        match self {
            Self::BmmConstant(constant) => constant.is_boolean(),
            Self::BmmContainerProperty(property) => property.is_boolean(),
            Self::BmmFunction(function) => function.is_boolean(),
            Self::BmmProcedure(procedure) => procedure.is_boolean(),
            Self::BmmSingleton(singleton) => singleton.is_boolean(),
            Self::BmmUnitaryProperty(property) => property.is_boolean(),
        }
    }
}