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
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-FileCopyrightText: openEHR Foundation
// SPDX-License-Identifier: Apache-2.0
//! The **wire-boundary RM class-invariant dispatcher** — the thin entry points
//! that compose the validation tiers at the codec boundary.
//!
//! The tiers themselves are RM model semantics and live upstream in
//! [`openehr_rm::v1_2::validate`]: the allocation-free fast path and the
//! authoritative typed dispatch. Three things stay here because they need this
//! crate:
//!
//! - the generated five-crate structural fallthrough ([`structural_check`]),
//! which spans every spec crate at once and so can only be emitted
//! downstream of all of them;
//! - the undeclared-key refusal, which reads [`declared_fields`] and renders
//! the reader's own [`crate::json::JsonParseError`];
//! - the entry points, which fix the ORDER the tiers and the orthogonal layers
//! run in.
//!
//! A node that does not deserialize into its declared concrete RM type surfaces
//! `does not conform to RM type …`.
use Value;
use InvariantViolation;
use record_type_mismatch;
use crateJsonParseError;
use crate;
/// Run the **core** (non-terminology) RM class invariants for a single
/// canonical-JSON node, dispatching on its `_type`.
///
/// A node with no (or an unrecognised) `_type` runs no invariants.
///
/// Two tiers, because the pass visits every `_type` node of a commit (~1.5k for
/// a populated composition), so per-node cost is load-bearing:
///
/// 1. [`openehr_rm::v1_2::validate::try_fast_validate`] checks conformance
/// directly against the live JSON node through the same invariant cores the
/// typed impls call — no deserialization, byte-identical output;
/// 2. anything it cannot vouch for falls back to [`validate_rm_value_typed`].
///
/// The equivalence battery pins this tier: the fast path may vouch only when it
/// is byte-identical to the typed oracle. The terminology-backed invariants are
/// an orthogonal layer [`validate_rm_value`] adds.
/// [`validate_rm_invariants`] with the node's RM type supplied by the caller.
///
/// This is the entry a tree walker uses for a node whose wire `_type` is
/// legitimately absent (canonical JSON requires `_type` only where the declared
/// attribute type is abstract; see
/// [`openehr_rm::v1_2::model::declared_concrete_type`]). The `_type`-reading
/// wrapper stays for callers with no declaration context.
/// [`validate_rm_invariants_as`] under the `553|incomplete|` **presence
/// relaxation** — the entry the relaxed commit path uses per node.
///
/// RM common `master06-change_control_package.adoc` §Incomplete Content
/// (NOTE): "In the `incomplete` state, a limited form of invalidity is
/// allowed: mandatory attributes may be absent … All other validity
/// requirements must be satisfied. In other words, in an `incomplete` commit,
/// data may be missing, but it may not be wrong."
///
/// The three branches realize that sentence:
///
/// 1. an undeclared member is wrong, never missing — refused first;
/// 2. a node with nothing missing runs the strict tiers unchanged;
/// 3. a node that IS missing mandatory data skips typed construction, because
/// that tier's refusal is the presence rule the state lifts. Wrongness is
/// still judged: a positive type contradiction goes through the typed tier,
/// and the class invariants run wherever the fast path can still vouch for
/// the node as it stands.
///
/// Only the mandatory-presence and cardinality-lower-bound layers relax. The
/// orthogonal layers the caller runs beside this one — terminology,
/// `LOCATABLE.Archetyped_valid`, the data-structure shape duties and the
/// archetype-conformance pass — do not.
/// The violation for the first undeclared member of `value` under class `ty`,
/// or `None` when every member is declared (or `ty` names no emitted struct).
///
/// Byte-identical to what the typed tier reports for the same node: it builds
/// the reader's own error and runs it through [`record_type_mismatch`].
/// Runs **all** RM class invariants for a single canonical-JSON node.
///
/// The set is the core
/// (fast/typed) invariants ([`validate_rm_invariants`]) plus the
/// terminology-backed invariants
/// ([`openehr_rm::v1_2::validate::terminology::validate_rm_terminology`], the
/// openEHR-terminology group and code-set membership checks the generated
/// invariant cores cannot express mechanically). This is the unified dispatcher
/// every consumer calls; the composition validator invokes it per node and
/// prefixes the absolute RM path onto each [`InvariantViolation`].
/// The typed dispatch tier of [`validate_rm_value`]: deserialize the node
/// into its concrete RM type and run that type's `Validate` impl.
///
/// Authoritative for every node — the fast path may only skip it when the
/// result is provably identical — and the oracle its equivalence tests compare
/// against. Composed from three parts, in this order:
///
/// 1. the undeclared-key refusal, ahead of any decode;
/// 2. the typed table
/// ([`openehr_rm::v1_2::validate::typed_dispatch::dispatch_typed`]), for the
/// concrete types carrying a non-terminology class invariant;
/// 3. for every class the table declines, the generated [`structural_check`],
/// which decodes the node and discards it: the codec is the
/// structural-conformance authority for the whole emitted model, so a class
/// with no invariant is still refused when it is structurally defective.
///
/// Afterwards the inherited `LOCATABLE.Archetype_node_id_valid`
/// (`docs/specs/openehr/RM/docs/UML/classes/org.openehr.rm.common.locatable.adoc`
/// §Invariants) is closed out for every concrete LOCATABLE descendant via
/// [`openehr_rm::v1_2::validate::locatable_node_id_violation`], whose applicable
/// set is the generated model's descendant closure rather than a hand-kept
/// list. It is appended only when this node's own pass did not already report
/// it, so the violation appears exactly once. Closing it out in this tier — not
/// above both — keeps the fast-vs-typed equivalence exact.
/// The generated-dispatch fallthrough of [`validate_rm_value_typed`]: decode the
/// node as the class its `_type` names and record a structural violation when it
/// does not conform.
///
/// The classes the typed table
/// ([`openehr_rm::v1_2::validate::typed_dispatch::dispatch_typed`]) handles already
/// decode there — its arms report the same `does not conform to RM type …` on
/// failure — and the table returns `true` for them, so they never reach here: no
/// node is decoded twice. A `_type` naming no emitted class
/// ([`structural_check`] returns `None`) runs no check, unchanged: an
/// unrecognised type is not a structural claim this layer can adjudicate.