1use super::*;
2const fn r(key: u16, type_tag: u8, count: Option<u32>, required: bool) -> FieldRule {
3 FieldRule {
4 key,
5 type_tag,
6 count,
7 required,
8 }
9}
10const EXT: &[FieldRule] = &[
11 r(1, 1, Some(1), true),
12 r(2, 1, Some(1), true),
13 r(3, 8, Some(1), true),
14 r(4, 8, Some(1), false),
15 r(5, 8, Some(1), true),
16 r(6, 6, None, true),
17 r(7, 6, None, true),
18 r(8, 2, Some(3), true),
19 r(9, 1, Some(3), true),
20 r(10, 1, Some(1), true),
21 r(11, 3, Some(1), true),
22 r(12, 8, Some(1), false),
23 r(13, 8, Some(1), false),
24 r(14, 8, Some(1), false),
25 r(15, 8, Some(1), false),
26 r(16, 8, Some(1), false),
27 r(17, 8, Some(1), false),
28 r(18, 1, Some(1), false),
29 r(19, 1, Some(1), false),
30 r(20, 1, Some(3), false),
31 r(21, 6, None, false),
32 r(22, 8, Some(1), false),
33 r(23, 2, None, false),
34 r(24, 6, None, false),
35 r(25, 8, None, false),
36 r(26, 9, None, false),
37 r(27, 3, None, false),
38 r(28, 2, None, false),
39 r(29, 9, None, false),
40 r(30, 8, Some(1), false),
41];
42const POL: &[FieldRule] = &[
43 r(1, 6, None, true),
44 r(2, 6, None, true),
45 r(3, 6, None, true),
46 r(4, 6, None, true),
47];
48pub fn extension_rules() -> &'static [FieldRule] {
49 EXT
50}
51pub fn polarization_rules() -> &'static [FieldRule] {
52 POL
53}
54pub const fn canonical_emf_mode(mode: u16) -> Option<u16> {
56 match mode {
57 0 | 0x0100 => Some(0x0100),
58 1 | 0x0101 => Some(0x0101),
59 2 | 0x0102 => Some(0x0102),
60 3 | 0x0103 => Some(0x0103),
61 4 | 0x0104 => Some(0x0104),
62 5 | 0x0105 => Some(0x0105),
63 6 | 0x0106 => Some(0x0106),
64 7 | 0x0107 => Some(0x0107),
65 _ => None,
66 }
67}
68
69#[derive(Clone, Copy, Debug, Default)]
71pub struct ProfileRegistry;
72impl DomainRegistry for ProfileRegistry {
73 fn rules(&self, schema: u16, revision: u16) -> Option<&'static [FieldRule]> {
74 if revision != 1 {
75 return None;
76 }
77 match schema {
78 103 => Some(EXT),
79 105 => Some(schemas::material_binding_rules()),
80 106 => Some(schemas::coordinate_transform_rules()),
81 107 => Some(schemas::calibration_rules()),
82 132 => Some(spectrum::schema_rules()),
83 133 => Some(schemas::constitutive_model_rules()),
84 134 => Some(schemas::model_evidence_rules()),
85 135 => Some(POL),
86 136 => Some(schemas::acquisition_rules()),
87 137 => Some(schemas::bio_context_rules()),
88 _ => None,
89 }
90 }
91 fn descriptor_target_schema(&self, key: u16) -> Option<u16> {
92 match key {
93 4 => Some(105),
94 5 => Some(106),
95 6 => Some(107),
96 _ => None,
97 }
98 }
99 fn validate_block(
100 &self,
101 block: Block<'_>,
102 ctx: &Context<'_, '_>,
103 ) -> Result<(), semantics::Error> {
104 let result = (|| -> Result<(), Error> {
105 match block.schema_type() {
106 103 => extension(block, ctx),
107 105 => schemas::validate_material_binding(block, ctx),
108 106 => schemas::validate_coordinate_transform(block, ctx),
109 107 => schemas::validate_calibration(block, ctx),
110 133 => schemas::validate_constitutive_model(block, ctx),
111 134 => schemas::validate_model_evidence(block, ctx),
112 135 => polarization(block),
113 136 => schemas::validate_acquisition(block, ctx),
114 137 => schemas::validate_bio_context(block, ctx),
115 132 => {
116 let positions = field(block, 5).ok_or(Error::Descriptor)?.element_count();
117 let semantics = text(required(block, 2)?)?;
118 let samples = if semantics == "point_density" {
119 u64::from(positions)
120 } else {
121 u64::from(positions).checked_sub(1).ok_or(Error::Shape)?
122 };
123 let available = ctx.remaining_work();
124 let response = schemas::ProfileResponseValidator { ctx };
125 let axis = spectrum::admit_axis(
126 block,
127 samples,
128 spectrum::Limits {
129 samples: 4096,
130 work: available,
131 },
132 &response,
133 )
134 .map_err(Error::Spectrum)?;
135 ctx.charge_work(available - axis.remaining_work())
136 .map_err(Error::Shared)?;
137 match axis.sigma() {
138 SigmaInterpretation::LegacyDisplay400700nm
139 | SigmaInterpretation::LegacyEtalonDisplay380750nm => {
140 Err(Error::UnsupportedProfile)
141 }
142 _ => Ok(()),
143 }
144 }
145 _ => Err(Error::UnsupportedProfile),
146 }
147 })();
148 result.map_err(profile_error)
149 }
150 fn validate_grid_frame(
151 &self,
152 frame: Block<'_>,
153 ctx: &Context<'_, '_>,
154 ) -> Result<(), semantics::Error> {
155 (|| -> Result<(), Error> {
156 let mut found = false;
157 for block in ctx.blocks() {
158 let block = block.map_err(Error::Shared)?;
159 ctx.charge_work(block.fields().count() as u64)
160 .map_err(Error::Shared)?;
161 if block.schema_type() != 103 || block.revision() != 1 {
162 continue;
163 }
164 let Some(reference) = field(block, 4) else {
165 continue;
166 };
167 if reference.type_tag() != 8
168 || reference.element_count() != 1
169 || u32_at(reference.value(), 0) != frame.id()
170 {
171 continue;
172 }
173 semantics::validate_fields(block, extension_rules()).map_err(Error::Shared)?;
174 let mode_u32 = scalar32(block, 1)?;
175 let mode_u16 = u16::try_from(mode_u32).map_err(|_| Error::UnsupportedProfile)?;
176 let Some(cmode) = canonical_emf_mode(mode_u16) else {
177 return Err(Error::UnsupportedProfile);
178 };
179 let prof = text(required(block, 6)?)?;
180 if !matches!(cmode, 0x0100 | 0x0102)
181 || (prof != FIELD_PROFILE && prof != yee::YEE_PROFILE)
182 {
183 return Err(Error::UnsupportedProfile);
184 }
185 let layout = spatial::from_metadata(frame, block)?;
186 if !(matches!(layout.spec().indexing, GridIndexing::Hilbert { .. })
187 || (matches!(layout.spec().indexing, GridIndexing::Morton { .. })
188 && layout.spec().dimensions.iter().any(|&n| n > 1 << 21)))
189 {
190 return Err(Error::Frame);
191 }
192 found = true;
193 }
194 if !found {
195 return Err(Error::UnsupportedProfile);
196 }
197 Ok(())
198 })()
199 .map_err(profile_error)
200 }
201 fn validate_spatial_entry(
202 &self,
203 entry: Entry<'_>,
204 frame: Block<'_>,
205 ctx: &Context<'_, '_>,
206 ) -> Result<(), semantics::Error> {
207 (|| -> Result<(), Error> {
208 if !matches!(canonical_emf_mode(entry.mode()), Some(0x0100 | 0x0102)) {
209 return Err(Error::UnsupportedProfile);
210 }
211 let desc = ctx
212 .require_schema(entry.descriptor_id(), 3)
213 .map_err(Error::Shared)?;
214 let ext = bindings::extension_for(ctx, desc)?;
215 if scalar32(ext, 4)? != frame.id() || scalar32(ext, 1)? != u32::from(entry.mode()) {
216 return Err(Error::Frame);
217 }
218 let brick = spatial::from_metadata(frame, ext)?.brick(entry.spatial_key())?;
219 let times = if entry.flags() & 2 != 0 {
220 entry.time_count()
221 } else {
222 1
223 };
224 if brick
225 .cell_count()
226 .checked_mul(times)
227 .ok_or(Error::Overflow)?
228 != entry.record_count()
229 {
230 return Err(Error::Shape);
231 }
232 Ok(())
233 })()
234 .map_err(profile_error)
235 }
236 fn validate_units(&self, units: &str, _ctx: &Context<'_, '_>) -> Result<(), semantics::Error> {
237 if matches!(
238 units,
239 "W m^-2"
240 | "W sr^-1 m^-2"
241 | "W m^-2 Hz^-1"
242 | "W m^-2 m^-1"
243 | "W sr^-1 m^-2 Hz^-1"
244 | "W sr^-1 m^-2 m^-1"
245 | "V/m"
246 | "T"
247 | "A/m"
248 | "V"
249 | "Gy"
250 | "Sv"
251 | "photons m^-2"
252 | "J"
253 | "A m"
254 | "A*m"
255 | "sr^-1"
256 | "1"
257 | "counts"
258 | "mixed:mode-registry"
259 | "V/W"
260 | "A/W"
261 ) {
262 Ok(())
263 } else {
264 Err(semantics::Error::UnsupportedDomain)
265 }
266 }
267}
268fn req_critical_ref<'a>(
269 block: Block<'a>,
270 key: u16,
271 expected_schema: u16,
272 ctx: &Context<'_, 'a>,
273) -> Result<Block<'a>, Error> {
274 let f = required(block, key)?;
275 if f.flags() & 1 == 0 {
276 return Err(Error::Descriptor);
277 }
278 ctx.require_schema(u32_at(f.value(), 0), expected_schema)
279 .map_err(Error::Shared)
280}
281fn opt_ref<'a>(
282 block: Block<'a>,
283 key: u16,
284 expected_schema: u16,
285 ctx: &Context<'_, 'a>,
286) -> Result<Option<Block<'a>>, Error> {
287 if let Some(f) = field(block, key) {
288 let b = ctx
289 .require_schema(u32_at(f.value(), 0), expected_schema)
290 .map_err(Error::Shared)?;
291 Ok(Some(b))
292 } else {
293 Ok(None)
294 }
295}
296fn extension(block: Block<'_>, ctx: &Context<'_, '_>) -> Result<(), Error> {
297 let raw_mode = u16::try_from(scalar32(block, 1)?).map_err(|_| Error::UnsupportedProfile)?;
298 let profile = text(required(block, 6)?)?;
299 if scalar32(block, 2)? != 1 {
300 return Err(Error::UnsupportedProfile);
301 }
302 let clock = ctx
303 .require_schema(scalar32(block, 3)?, 1)
304 .map_err(Error::Shared)?;
305 if raw_mode == 0x0106 && profile == VALIDITY_PROFILE {
306 if field(block, 4).is_some() || scalar64(block, 11)? != 0 || scalar32(clock, 1)? != 0 {
307 return Err(Error::UnsupportedProfile);
308 }
309 ctx.require_schema(scalar32(block, 5)?, 4)
310 .map_err(Error::Shared)?;
311 for f in block.fields() {
312 if f.key() >= 12 {
313 return Err(Error::UnsupportedProfile);
314 }
315 }
316 return Ok(());
317 }
318 let canonical = canonical_emf_mode(raw_mode).ok_or(Error::UnsupportedProfile)?;
319 let expected = match canonical {
320 0x0100 => FIELD_PROFILE,
321 0x0101 => RAY_PROFILE,
322 0x0102 => yee::YEE_PROFILE,
323 0x0103 => SPECTRUM_PROFILE,
324 0x0104 => mueller::MUELLER_PROFILE,
325 0x0105 => iq::IQ_PROFILE,
326 0x0106 => dose_biofield::DOSE_PROFILE,
327 0x0107 => dose_biofield::BIOFIELD_PROFILE,
328 _ => return Err(Error::UnsupportedProfile),
329 };
330 if profile != expected {
331 return Err(Error::UnsupportedProfile);
332 }
333 let frame_f = required(block, 4)?;
334 if frame_f.flags() & 1 == 0 {
335 return Err(Error::Descriptor);
336 }
337 let frame = ctx
338 .require_schema(u32_at(frame_f.value(), 0), 2)
339 .map_err(Error::Shared)?;
340 ctx.require_schema(scalar32(block, 5)?, 4)
341 .map_err(Error::Shared)?;
342
343 if canonical == 0x0102 {
344 yee::validate_yee_extension(block, frame, ctx)?;
345 } else if canonical == 0x0100 && [5, 6, 7].iter().any(|&key| field(frame, key).is_some()) {
346 spatial::from_metadata(frame, block)?;
347 } else {
348 if text(required(block, 7)?)? != "linear" || scalar32(block, 10)? != 0 {
349 return Err(Error::UnsupportedProfile);
350 }
351 for v in required(block, 8)?.value().chunks_exact(8) {
352 if u64_at(v, 0) != 1 {
353 return Err(Error::UnsupportedProfile);
354 }
355 }
356 for v in required(block, 9)?.value().chunks_exact(4) {
357 if u32_at(v, 0) != 1 {
358 return Err(Error::UnsupportedProfile);
359 }
360 }
361 }
362 if (scalar64(block, 11)? as i64) < 0 {
363 return Err(Error::Time);
364 }
365 let _ = opt_ref(block, 13, 105, ctx)?;
367 let _ = opt_ref(block, 14, 133, ctx)?;
368 let _ = opt_ref(block, 15, 136, ctx)?;
369 let _ = opt_ref(block, 16, 135, ctx)?;
370 let _ = opt_ref(block, 17, 134, ctx)?;
371 let _ = opt_ref(block, 22, 137, ctx)?;
372 let _ = opt_ref(block, 30, 107, ctx)?;
373 for f in block.fields() {
374 let k = f.key();
375 if matches!(k, 20 | 21 | 23 | 24 | 25)
376 || (k == 18 && canonical != 0x0105)
377 || (matches!(k, 26..=29) && canonical != 0x0102)
378 || ((k == 12 || k == 19) && canonical != 0x0103)
379 || (k == 14 && canonical != 0x0102)
380 || (k == 15 && canonical != 0x0105)
381 || (k == 16 && !matches!(canonical, 0x0101 | 0x0104))
382 || (k == 22 && canonical != 0x0107)
383 {
384 return Err(Error::UnsupportedProfile);
385 }
386 }
387 match canonical {
388 0x0100 => {}
389 0x0101 => {
390 let _ = req_critical_ref(block, 16, 135, ctx)?;
391 }
392 0x0102 => {
393 let _ = req_critical_ref(block, 14, 133, ctx)?;
394 }
395 0x0103 => {
396 let _ = req_critical_ref(block, 12, 132, ctx)?;
397 let f19 = required(block, 19)?;
398 if f19.flags() & 1 == 0 {
399 return Err(Error::Descriptor);
400 }
401 if !(1..=4096).contains(&u32_at(f19.value(), 0)) {
402 return Err(Error::Shape);
403 }
404 }
405 0x0104 => {
406 let _ = req_critical_ref(block, 16, 135, ctx)?;
407 let _ = req_critical_ref(block, 17, 134, ctx)?;
408 }
409 0x0105 => {
410 let acq = req_critical_ref(block, 15, 136, ctx)?;
411 let cal = req_critical_ref(block, 30, 107, ctx)?;
412 if scalar32(acq, 6)? != cal.id() {
413 return Err(Error::Descriptor);
414 }
415 let n_f = required(block, 18)?;
416 if !(1..=iq::MAX_IQ_SAMPLES).contains(&u32_at(n_f.value(), 0)) {
417 return Err(Error::Shape);
418 }
419 }
420 0x0106 => {
421 let ev = req_critical_ref(block, 17, 134, ctx)?;
422 let _ = required(ev, 9)?;
423 let _ = required(ev, 10)?;
424 let _ = required(ev, 12)?;
425 let _ = req_critical_ref(block, 30, 107, ctx)?;
426 }
427 0x0107 => {
428 let ev = req_critical_ref(block, 17, 134, ctx)?;
429 let bio = req_critical_ref(block, 22, 137, ctx)?;
430 let cal = req_critical_ref(block, 30, 107, ctx)?;
431 if let Some(bc) = field(bio, 5) {
432 if u32_at(bc.value(), 0) != cal.id() {
433 return Err(Error::Descriptor);
434 }
435 }
436 if let Some(be) = field(bio, 6) {
437 if u32_at(be.value(), 0) != ev.id() {
438 return Err(Error::Descriptor);
439 }
440 }
441 }
442 _ => return Err(Error::UnsupportedProfile),
443 }
444 Ok(())
445}
446fn polarization(block: Block<'_>) -> Result<(), Error> {
447 if text(required(block, 1)?)? != "deterministic_transverse_v1"
448 || text(required(block, 2)?)? != "I_Q_U_V_v1"
449 || text(required(block, 4)?)? != "time_averaged"
450 {
451 return Err(Error::UnsupportedProfile);
452 }
453 if !["W m^-2", "W sr^-1 m^-2", "1", "sr^-1"].contains(&text(required(block, 3)?)?) {
454 return Err(Error::Descriptor);
455 }
456 Ok(())
457}
458
459fn profile_error(error: Error) -> semantics::Error {
460 match error {
461 Error::Shared(e) => e,
462 Error::UnsupportedProfile => semantics::Error::UnsupportedDomain,
463 Error::Frame => semantics::Error::Frame,
464 Error::Time => semantics::Error::Time,
465 Error::Overflow => semantics::Error::Overflow,
466 Error::Limit => semantics::Error::Limit,
467 _ => semantics::Error::Value,
468 }
469}