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
//! Registry model (SPEC Chapter 22).
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
/// A contract-level reference to an external registry catalog.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RegistryRef {
/// Registry identifier.
pub id: String,
/// Registry URI or location.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub uri: Option<String>,
}
/// Publication status for a registry document (Ch 22 §4).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RegistryPublicationStatus {
/// Work in progress.
Draft,
/// Published for experimental use.
Experimental,
/// Normative standard publication.
Standard,
/// Still valid but discouraged.
Deprecated,
/// No longer supported.
Obsolete,
}
impl RegistryPublicationStatus {
/// Returns the serialized status name.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Draft => "draft",
Self::Experimental => "experimental",
Self::Standard => "standard",
Self::Deprecated => "deprecated",
Self::Obsolete => "obsolete",
}
}
}
/// Status of an individual registry entry (Ch 22 §10).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RegistryEntryStatus {
/// Work in progress.
Draft,
/// Published for experimental use.
Experimental,
/// Normative standard entry.
Standard,
/// Still valid but discouraged.
Deprecated,
/// No longer supported; identifier remains reserved.
Obsolete,
}
impl RegistryEntryStatus {
/// Returns the serialized status name.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Draft => "draft",
Self::Experimental => "experimental",
Self::Standard => "standard",
Self::Deprecated => "deprecated",
Self::Obsolete => "obsolete",
}
}
}
/// Category of a registry entry (Ch 22 §3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum RegistryCategory {
/// Semantic action identifier.
SemanticAction,
/// Function identifier.
Function,
/// Rule identifier.
Rule,
/// Logical type identifier.
LogicalType,
/// Diagnostic code.
Diagnostic,
/// Extension namespace.
ExtensionNamespace,
/// Conformance profile.
Profile,
/// Engine capability.
Capability,
}
impl RegistryCategory {
/// Returns the serialized category name.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::SemanticAction => "semanticAction",
Self::Function => "function",
Self::Rule => "rule",
Self::LogicalType => "logicalType",
Self::Diagnostic => "diagnostic",
Self::ExtensionNamespace => "extensionNamespace",
Self::Profile => "profile",
Self::Capability => "capability",
}
}
}
/// Compatibility requirement for an extension entry (Ch 21 §7).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ExtensionCompatibility {
/// Unsupported mandatory extensions prevent successful processing.
Mandatory,
/// Optional extensions may be preserved without interpretation.
Optional,
}
impl ExtensionCompatibility {
/// Returns the serialized compatibility name.
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Mandatory => "mandatory",
Self::Optional => "optional",
}
}
}
/// A single registry entry (Ch 22 §5).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegistryEntry {
/// Stable identifier (for example `dtcs:lowercase`).
///
/// When omitted in a map-keyed registry document, the map key is used.
#[serde(default)]
pub id: String,
/// Human-readable name.
pub name: String,
/// Entry category.
pub category: RegistryCategory,
/// Entry version.
pub version: String,
/// Publication status.
pub status: RegistryEntryStatus,
/// Compatibility requirement (primarily for extension namespaces).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub compatibility: Option<ExtensionCompatibility>,
/// Semantic definition summary.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub definition: Option<String>,
/// Normative references.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub references: Vec<String>,
/// Whether this implementation supports the entry.
#[serde(default = "default_supported")]
pub supported: bool,
}
fn default_supported() -> bool {
true
}
/// An authoritative registry catalog document (Ch 22 §4).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RegistryDocument {
/// Registry identifier.
pub id: String,
/// Registry version.
pub version: String,
/// Governing specification version.
pub governing_specification: String,
/// Publication status of the registry document.
pub publication_status: RegistryPublicationStatus,
/// Catalog entries keyed by stable identifier.
#[serde(default)]
pub entries: IndexMap<String, RegistryEntry>,
}
impl RegistryDocument {
/// Returns the entry for `id`, if present.
#[must_use]
pub fn get(&self, id: &str) -> Option<&RegistryEntry> {
self.entries.get(id)
}
/// Inserts or replaces an entry, keyed by `entry.id`.
pub fn insert(&mut self, entry: RegistryEntry) {
self.entries.insert(entry.id.clone(), entry);
}
/// Merges `other` into this document.
///
/// Non-`dtcs:` entries from `other` override existing entries. `dtcs:` entries
/// already present in this document are preserved (builtin authority). Novel
/// `dtcs:` keys from `other` are rejected.
pub fn merge(
&mut self,
other: &RegistryDocument,
) -> Result<(), crate::diagnostics::DiagnosticReport> {
for (id, entry) in &other.entries {
if id.starts_with("dtcs:") {
if self.entries.contains_key(id) {
continue;
}
let mut report = crate::diagnostics::DiagnosticReport::new();
report.push(
crate::diagnostics::Diagnostic::new(
crate::diagnostics::codes::INVALID_REGISTRY,
crate::diagnostics::Severity::Error,
crate::diagnostics::DiagnosticStage::Validation,
crate::diagnostics::DiagnosticCategory::Structure,
format!(
"registry merge rejected novel standard entry '{id}'; vendor catalogs cannot extend the dtcs: namespace"
),
)
.with_object_ref(format!("entries.{id}"))
.with_remediation(
"Use vendor namespaces for custom identifiers; only builtin dtcs: entries are authoritative",
),
);
return Err(report);
}
self.entries.insert(id.clone(), entry.clone());
}
Ok(())
}
/// Merges `other` into this document without rejecting novel `dtcs:` keys.
///
/// Reserved for trusted builtin catalog assembly.
pub(crate) fn merge_trusted(&mut self, other: &RegistryDocument) {
for (id, entry) in &other.entries {
if id.starts_with("dtcs:") && self.entries.contains_key(id) {
continue;
}
self.entries.insert(id.clone(), entry.clone());
}
}
/// Returns all entries in insertion order.
#[must_use]
pub fn list(&self) -> Vec<&RegistryEntry> {
self.entries.values().collect()
}
}