1#[cfg(test)]
2use std::{collections::HashMap, sync::OnceLock};
3
4use serde::{Deserialize, Serialize};
5
6#[cfg(test)]
7static SCHEMA: OnceLock<SchemaTree> = OnceLock::new();
8
9#[cfg(test)]
10pub(crate) fn schema() -> &'static SchemaTree {
11 SCHEMA.get_or_init(|| {
12 let mut tree: SchemaTree =
13 serde_json::from_str(include_str!("schema.json")).expect("schema.json (tree)");
14 tree.build_index();
15 tree
16 })
17}
18
19#[repr(u8)]
20#[non_exhaustive]
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub enum TagId {
24 FileContent = 0,
25 SourceFile = 1,
26 Contact = 2,
27 ReferenceableParamGroup = 3,
28 Sample = 4,
29
30 #[serde(rename = "instrumentConfiguration")]
31 Instrument = 5,
32
33 #[serde(rename = "source")]
34 ComponentSource = 6,
35 #[serde(rename = "analyzer")]
36 ComponentAnalyzer = 7,
37 #[serde(rename = "detector")]
38 ComponentDetector = 8,
39
40 Software = 9,
41 ProcessingMethod = 10,
42 ScanSettings = 11,
43 Target = 12,
44 Run = 13,
45
46 Spectrum = 14,
47 SpectrumDescription = 15,
48 Scan = 16,
49 ScanWindow = 17,
50 Precursor = 18,
51 IsolationWindow = 19,
52 SelectedIon = 20,
53 Activation = 21,
54 Product = 22,
55 BinaryDataArray = 23,
56
57 Chromatogram = 24,
58
59 FileDescription = 25,
60
61 SourceFileList = 26,
62 SourceFileRef = 27,
63 SourceFileRefList = 28,
64
65 ReferenceableParamGroupList = 29,
66 ReferenceableParamGroupRef = 30,
67
68 SampleList = 31,
69
70 InstrumentConfigurationList = 32,
71 ComponentList = 33,
72
73 SoftwareList = 34,
74 SoftwareParam = 35,
75 SoftwareRef = 36,
76
77 DataProcessing = 37,
78 DataProcessingList = 38,
79
80 ScanSettingsList = 39,
81 AcquisitionSettings = 40,
82 AcquisitionSettingsList = 41,
83
84 TargetList = 42,
85
86 SpectrumList = 43,
87 ScanList = 44,
88 ScanWindowList = 45,
89
90 PrecursorList = 46,
91 SelectedIonList = 47,
92 ProductList = 48,
93
94 BinaryDataArrayList = 49,
95 Binary = 50,
96
97 ChromatogramList = 51,
98
99 CvParam = 52,
100 UserParam = 53,
101 CvList = 54,
102 Cv = 55,
103 MzML = 56,
104 IndexList = 57,
105 IndexListOffset = 58,
106 FileChecksum = 59,
107 IndexedmzML = 60,
108
109 Unknown = 255,
110}
111
112impl TagId {
113 #[inline]
114 pub(crate) fn from_xml_tag(s: &str) -> TagId {
115 match s {
116 "mzML" => TagId::MzML,
117 "fileContent" => TagId::FileContent,
118 "sourceFile" => TagId::SourceFile,
119 "contact" => TagId::Contact,
120 "referenceableParamGroup" => TagId::ReferenceableParamGroup,
121 "sample" => TagId::Sample,
122
123 "instrumentConfiguration" => TagId::Instrument,
124 "source" => TagId::ComponentSource,
125 "analyzer" => TagId::ComponentAnalyzer,
126 "detector" => TagId::ComponentDetector,
127
128 "software" => TagId::Software,
129 "processingMethod" => TagId::ProcessingMethod,
130 "scanSettings" => TagId::ScanSettings,
131 "target" => TagId::Target,
132 "run" => TagId::Run,
133
134 "spectrum" => TagId::Spectrum,
135 "spectrumDescription" => TagId::SpectrumDescription,
136 "scan" => TagId::Scan,
137 "scanWindow" => TagId::ScanWindow,
138 "precursor" => TagId::Precursor,
139 "isolationWindow" => TagId::IsolationWindow,
140 "selectedIon" => TagId::SelectedIon,
141 "activation" => TagId::Activation,
142 "product" => TagId::Product,
143 "binaryDataArray" => TagId::BinaryDataArray,
144
145 "chromatogram" => TagId::Chromatogram,
146 "fileDescription" => TagId::FileDescription,
147
148 "sourceFileList" => TagId::SourceFileList,
149 "sourceFileRef" => TagId::SourceFileRef,
150 "sourceFileRefList" => TagId::SourceFileRefList,
151
152 "referenceableParamGroupList" => TagId::ReferenceableParamGroupList,
153 "referenceableParamGroupRef" => TagId::ReferenceableParamGroupRef,
154
155 "sampleList" => TagId::SampleList,
156
157 "instrumentConfigurationList" => TagId::InstrumentConfigurationList,
158 "componentList" => TagId::ComponentList,
159
160 "softwareList" => TagId::SoftwareList,
161 "softwareParam" => TagId::SoftwareParam,
162 "softwareRef" => TagId::SoftwareRef,
163
164 "dataProcessing" => TagId::DataProcessing,
165 "dataProcessingList" => TagId::DataProcessingList,
166
167 "scanSettingsList" => TagId::ScanSettingsList,
168 "acquisitionSettings" => TagId::AcquisitionSettings,
169 "acquisitionSettingsList" => TagId::AcquisitionSettingsList,
170
171 "targetList" => TagId::TargetList,
172
173 "spectrumList" => TagId::SpectrumList,
174 "scanList" => TagId::ScanList,
175 "scanWindowList" => TagId::ScanWindowList,
176
177 "precursorList" => TagId::PrecursorList,
178 "selectedIonList" => TagId::SelectedIonList,
179 "productList" => TagId::ProductList,
180
181 "binaryDataArrayList" => TagId::BinaryDataArrayList,
182 "binary" => TagId::Binary,
183
184 "chromatogramList" => TagId::ChromatogramList,
185
186 "cvParam" => TagId::CvParam,
187 "userParam" => TagId::UserParam,
188
189 "cvList" => TagId::CvList,
190 "cv" => TagId::Cv,
191
192 _ => TagId::Unknown,
193 }
194 }
195
196 #[inline]
197 pub(crate) fn from_u8(b: u8) -> Option<TagId> {
198 const MAX_TAG: u8 = TagId::Cv as u8;
199 match b {
200 0..=MAX_TAG | 255 => Some(TagId::from(b)),
201 _ => None,
202 }
203 }
204
205 #[cfg(test)]
206 #[inline]
207 pub(crate) fn as_u8(self) -> u8 {
208 self as u8
209 }
210}
211
212impl From<u8> for TagId {
213 #[inline]
214 fn from(b: u8) -> Self {
215 match b {
216 0 => TagId::FileContent,
217 1 => TagId::SourceFile,
218 2 => TagId::Contact,
219 3 => TagId::ReferenceableParamGroup,
220 4 => TagId::Sample,
221 5 => TagId::Instrument,
222 6 => TagId::ComponentSource,
223 7 => TagId::ComponentAnalyzer,
224 8 => TagId::ComponentDetector,
225 9 => TagId::Software,
226 10 => TagId::ProcessingMethod,
227 11 => TagId::ScanSettings,
228 12 => TagId::Target,
229 13 => TagId::Run,
230 14 => TagId::Spectrum,
231 15 => TagId::SpectrumDescription,
232 16 => TagId::Scan,
233 17 => TagId::ScanWindow,
234 18 => TagId::Precursor,
235 19 => TagId::IsolationWindow,
236 20 => TagId::SelectedIon,
237 21 => TagId::Activation,
238 22 => TagId::Product,
239 23 => TagId::BinaryDataArray,
240 24 => TagId::Chromatogram,
241 25 => TagId::FileDescription,
242 26 => TagId::SourceFileList,
243 27 => TagId::SourceFileRef,
244 28 => TagId::SourceFileRefList,
245 29 => TagId::ReferenceableParamGroupList,
246 30 => TagId::ReferenceableParamGroupRef,
247 31 => TagId::SampleList,
248 32 => TagId::InstrumentConfigurationList,
249 33 => TagId::ComponentList,
250 34 => TagId::SoftwareList,
251 35 => TagId::SoftwareParam,
252 36 => TagId::SoftwareRef,
253 37 => TagId::DataProcessing,
254 38 => TagId::DataProcessingList,
255 39 => TagId::ScanSettingsList,
256 40 => TagId::AcquisitionSettings,
257 41 => TagId::AcquisitionSettingsList,
258 42 => TagId::TargetList,
259 43 => TagId::SpectrumList,
260 44 => TagId::ScanList,
261 45 => TagId::ScanWindowList,
262 46 => TagId::PrecursorList,
263 47 => TagId::SelectedIonList,
264 48 => TagId::ProductList,
265 49 => TagId::BinaryDataArrayList,
266 50 => TagId::Binary,
267 51 => TagId::ChromatogramList,
268 52 => TagId::CvParam,
269 53 => TagId::UserParam,
270 54 => TagId::CvList,
271 55 => TagId::Cv,
272 56 => TagId::MzML,
273 255 => TagId::Unknown,
274 _ => TagId::Unknown,
275 }
276 }
277}
278
279#[cfg(test)]
280#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
281#[serde(rename_all = "lowercase")]
282pub(crate) enum Use {
283 Required,
284 Optional,
285}
286
287#[cfg(test)]
288impl Default for Use {
289 #[inline]
290 fn default() -> Self {
291 Use::Optional
292 }
293}
294
295#[cfg(test)]
296fn default_child_key_by_tag() -> Vec<Option<String>> {
297 vec![None; 256]
298}
299
300#[cfg(test)]
301#[derive(Debug, Clone, Serialize, Deserialize, Default)]
302pub(crate) struct SchemaNode {
303 #[serde(rename = "self", default)]
304 pub(crate) self_tags: Vec<TagId>,
305
306 #[serde(default)]
307 pub(crate) attributes: HashMap<String, Vec<String>>,
308
309 #[serde(default)]
310 pub(crate) children: HashMap<String, SchemaNode>,
311
312 #[serde(rename = "use", default)]
313 pub(crate) use_: Use,
314
315 #[serde(default)]
316 pub(crate) accessions: Vec<String>,
317
318 #[serde(default)]
319 pub(crate) attributes_use: HashMap<String, Use>,
320
321 #[serde(skip, default = "default_child_key_by_tag")]
322 pub(crate) child_key_by_tag: Vec<Option<String>>,
323}
324
325#[cfg(test)]
326impl SchemaNode {
327 #[inline]
328 pub(crate) fn build_children_lookup(&mut self) {
329 if self.child_key_by_tag.len() != 256 {
330 self.child_key_by_tag = default_child_key_by_tag();
331 } else {
332 self.child_key_by_tag.fill(None);
333 }
334
335 for (child_key, child) in &mut self.children {
336 child.build_children_lookup();
337
338 for &tag in &child.self_tags {
339 let idx = tag as usize;
340 if idx < 256 {
341 self.child_key_by_tag[idx] = Some(child_key.clone());
342 }
343 }
344 }
345 }
346}
347
348#[cfg(test)]
349fn default_key_by_tag() -> Vec<Option<String>> {
350 vec![None; 256]
351}
352
353#[cfg(test)]
354#[derive(Debug, Clone, Serialize, Deserialize, Default)]
355pub(crate) struct SchemaTree {
356 #[serde(flatten)]
357 pub(crate) roots: HashMap<String, SchemaNode>,
358
359 #[serde(skip, default = "default_key_by_tag")]
360 root_key_by_tag: Vec<Option<String>>,
361}
362
363#[cfg(test)]
364impl SchemaTree {
365 pub(crate) fn build_index(&mut self) {
366 if self.root_key_by_tag.len() != 256 {
367 self.root_key_by_tag = default_key_by_tag();
368 } else {
369 self.root_key_by_tag.fill(None);
370 }
371
372 for (root_key, root_node) in self.roots.iter_mut() {
373 root_node.build_children_lookup();
374
375 for &tag in &root_node.self_tags {
376 let idx = tag.as_u8() as usize;
377 if idx < 256 {
378 self.root_key_by_tag[idx] = Some(root_key.clone());
379 }
380 }
381 }
382 }
383
384 #[inline]
385 pub(crate) fn root_key_for_tag(&self, tag: TagId) -> Option<&str> {
386 self.root_key_by_tag
387 .get(tag.as_u8() as usize)
388 .and_then(|x| x.as_deref())
389 }
390
391 #[inline]
392 pub(crate) fn root_by_tag(&self, tag: TagId) -> Option<&SchemaNode> {
393 self.root_key_for_tag(tag).and_then(|k| self.roots.get(k))
394 }
395}