qualia_core_db/q42/emf_profiles/
chunk.rs1use super::*;
2#[derive(Clone, Copy, Debug)]
5pub struct ProfileChunk<'a> {
6 bytes: &'a [u8],
7 count: u64,
8 stride: usize,
9 mode: u16,
10 canonical_mode: u16,
11 yee_policy: yee::YeeParsePolicy,
12 mask: Option<&'a [u8]>,
13 axis: Option<AxisView<'a>>,
14 bins: u32,
15 entry_index: u32,
16 descriptor_id: u32,
17 frame_id: u32,
18 clock_id: u32,
19 profile: &'a str,
20 quantity: &'a str,
21 units: &'a str,
22 model_uri: &'a str,
23 start_tick: Option<i64>,
24 temporal_step_ticks: Option<i64>,
25 brick: Option<BrickLayout>,
26 spatial_key: Option<u64>,
27}
28impl<'a> ProfileChunk<'a> {
29 pub fn entry_index(self) -> u32 {
31 self.entry_index
32 }
33 pub fn descriptor_id(self) -> u32 {
34 self.descriptor_id
35 }
36 pub fn frame_id(self) -> u32 {
37 self.frame_id
38 }
39 pub fn clock_id(self) -> u32 {
41 self.clock_id
42 }
43 pub fn profile(self) -> &'a str {
44 self.profile
45 }
46 pub fn quantity(self) -> &'a str {
47 self.quantity
48 }
49 pub fn units(self) -> &'a str {
50 self.units
51 }
52 pub fn model_uri(self) -> &'a str {
53 self.model_uri
54 }
55 pub fn start_tick(self) -> Option<i64> {
56 self.start_tick
57 }
58 pub fn temporal_step_ticks(self) -> Option<i64> {
59 self.temporal_step_ticks
60 }
61 pub fn verified_stored_bytes(self) -> &'a [u8] {
63 self.bytes
64 }
65 pub fn brick_layout(self) -> Option<BrickLayout> {
66 self.brick
67 }
68 pub fn spatial_key(self) -> Option<u64> {
69 self.spatial_key
70 }
71 pub fn mode(self) -> u16 {
72 self.mode
73 }
74 pub fn canonical_mode(self) -> u16 {
75 self.canonical_mode
76 }
77 pub fn record_count(self) -> u64 {
78 self.count
79 }
80 pub fn spectral_axis(self) -> Option<AxisView<'a>> {
81 self.axis
82 }
83 fn record(self, index: u64) -> Result<Option<&'a [u8]>, Error> {
84 if index >= self.count {
85 return Err(Error::Index);
86 }
87 if self.mask.is_some_and(|m| m[index as usize] == 0) {
88 return Ok(None);
89 }
90 let start = index as usize * self.stride;
91 Ok(Some(&self.bytes[start..start + self.stride]))
92 }
93 pub fn field(self, index: u64) -> Result<Option<FieldRecord<'a>>, Error> {
94 if self.canonical_mode != 0x0100 {
95 return Err(Error::WrongMode);
96 }
97 Ok(self.record(index)?.map(|bytes| FieldRecord { bytes }))
98 }
99 pub fn ray(self, index: u64) -> Result<Option<RayRecord<'a>>, Error> {
100 if self.canonical_mode != 0x0101 {
101 return Err(Error::WrongMode);
102 }
103 Ok(self.record(index)?.map(|bytes| RayRecord { bytes }))
104 }
105 pub fn yee(self, index: u64) -> Result<Option<yee::YeeRecord>, Error> {
106 if self.canonical_mode != 0x0102 {
107 return Err(Error::WrongMode);
108 }
109 match self.record(index)? {
110 None => Ok(None),
111 Some(bytes) => yee::YeeRecord::parse(bytes, true, self.yee_policy),
112 }
113 }
114 pub fn spectrum(self, index: u64) -> Result<Option<SpectrumRecord<'a>>, Error> {
115 if self.canonical_mode != 0x0103 {
116 return Err(Error::WrongMode);
117 }
118 Ok(self.record(index)?.map(|bytes| SpectrumRecord {
119 bytes,
120 bins: self.bins,
121 }))
122 }
123 pub fn mueller(self, index: u64) -> Result<Option<mueller::MuellerRecord>, Error> {
124 if self.canonical_mode != 0x0104 {
125 return Err(Error::WrongMode);
126 }
127 match self.record(index)? {
128 None => Ok(None),
129 Some(bytes) => mueller::MuellerRecord::parse(bytes, true, true),
130 }
131 }
132 pub fn iq(self, index: u64) -> Result<Option<iq::IqPacketView<'a>>, Error> {
133 if self.canonical_mode != 0x0105 {
134 return Err(Error::WrongMode);
135 }
136 match self.record(index)? {
137 None => Ok(None),
138 Some(bytes) => iq::IqPacketView::parse(bytes, true),
139 }
140 }
141 pub fn dose(self, index: u64) -> Result<Option<dose_biofield::DoseTransportRecord>, Error> {
142 if self.canonical_mode != 0x0106 {
143 return Err(Error::WrongMode);
144 }
145 match self.record(index)? {
146 None => Ok(None),
147 Some(bytes) => dose_biofield::DoseTransportRecord::parse(bytes, true),
148 }
149 }
150 pub fn biofield(self, index: u64) -> Result<Option<dose_biofield::BiofieldRecord>, Error> {
151 if self.canonical_mode != 0x0107 {
152 return Err(Error::WrongMode);
153 }
154 match self.record(index)? {
155 None => Ok(None),
156 Some(bytes) => dose_biofield::BiofieldRecord::parse(bytes, true),
157 }
158 }
159}
160pub fn admit_chunk<'a>(
161 view: &StructuralView<'a>,
162 entry_index: u32,
163 scratch: &mut Scratch<'_>,
164 limits: Limits,
165) -> Result<ProfileChunk<'a>, Error> {
166 if view.family() != super::super::field_envelope::Family::EmfV4Candidate {
167 return Err(Error::UnsupportedProfile);
168 }
169 let entry = view.entry(entry_index).map_err(Error::Storage)?;
170 if entry.record_count() > limits.records {
171 return Err(Error::Limit);
172 }
173 let admitted = semantics::admit_shared(view, &ProfileRegistry, scratch, limits.shared)
174 .map_err(shared_error)?;
175 admit_with_context(view, &admitted.context, entry_index, limits)
176}
177pub(super) fn admit_with_context<'a>(
178 view: &StructuralView<'a>,
179 context: &Context<'_, 'a>,
180 entry_index: u32,
181 limits: Limits,
182) -> Result<ProfileChunk<'a>, Error> {
183 if view.family() != super::super::field_envelope::Family::EmfV4Candidate {
184 return Err(Error::UnsupportedProfile);
185 }
186 let entry = view.entry(entry_index).map_err(Error::Storage)?;
187 if entry.record_count() > limits.records {
188 return Err(Error::Limit);
189 }
190 let binding = bindings::bind(context, entry_index)?;
191 let ext_block = context.require_schema(binding.ext_id, 103).map_err(Error::Shared)?;
192 let yee_policy = yee::YeeParsePolicy {
193 require_no_phi_placeholder: true,
194 require_charge_free: false,
195 expected_constitutive_id: binding.constitutive_id,
196 has_auxiliary_state_streams: field(ext_block, 26).is_some(),
197 };
198 let mut work = limits.payload_work;
199 charge(
200 &mut work,
201 entry.stored_bytes().checked_mul(2).ok_or(Error::Overflow)?,
202 )?;
203 let bytes = view.verify_chunk(entry_index).map_err(Error::Storage)?;
204 let mask = if let Some(index) = binding.mask {
205 let e = view.entry(index).map_err(Error::Storage)?;
206 charge(&mut work, e.stored_bytes())?;
207 Some(view.verify_chunk(index).map_err(Error::Storage)?)
208 } else {
209 None
210 };
211 if mask.is_some_and(|m| (m.len() as u64) < entry.record_count()) {
212 return Err(Error::Shape);
213 }
214 let stride = entry.stride() as usize;
215 for (index, record) in bytes.chunks_exact(stride).enumerate() {
216 let valid = mask.is_none_or(|mask| mask[index] != 0);
217 records::validate(
218 record,
219 context,
220 &binding,
221 ext_block,
222 yee_policy,
223 valid,
224 binding.tick(index as u64)?,
225 )?;
226 if valid && binding.canonical_mode == 0x0100 {
227 if let Some(brick) = binding.brick {
228 let expected = brick.local_position_m(index as u64 % brick.cell_count())?;
229 if triple(record, 48) != expected {
230 return Err(Error::Frame);
231 }
232 }
233 }
234 }
235 Ok(ProfileChunk {
236 bytes,
237 count: entry.record_count(),
238 stride,
239 mode: entry.mode(),
240 canonical_mode: binding.canonical_mode,
241 yee_policy,
242 mask,
243 axis: binding.axis,
244 bins: binding.bins,
245 brick: binding.brick,
246 spatial_key: if entry.flags() & 4 != 0 {
247 Some(entry.spatial_key())
248 } else {
249 None
250 },
251 entry_index,
252 descriptor_id: entry.descriptor_id(),
253 frame_id: entry.frame_id(),
254 clock_id: binding.clock_id,
255 profile: binding.profile,
256 quantity: binding.quantity,
257 units: binding.units,
258 model_uri: binding.model_uri,
259 start_tick: if entry.flags() & 2 != 0 {
260 Some(entry.start_tick())
261 } else {
262 None
263 },
264 temporal_step_ticks: if entry.flags() & 2 != 0 {
265 Some(binding.step)
266 } else {
267 None
268 },
269 })
270}
271fn charge(work: &mut u64, amount: u64) -> Result<(), Error> {
272 *work = work.checked_sub(amount).ok_or(Error::Limit)?;
273 Ok(())
274}