qualia_core_db/audio/qac/
storage.rs1use crate::q42::field_envelope::{self, Family, Limits, StructuralView};
3#[derive(Debug, Clone, Copy, Default)]
4pub struct QacStorageOptions {
5 pub envelope_limits: Limits,
6}
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum QacStorageError {
9 Envelope(field_envelope::Error),
10}
11#[derive(Debug)]
12pub struct QacStorageView<'a>(StructuralView<'a>);
13impl<'a> QacStorageView<'a> {
14 pub fn open(bytes: &'a [u8], options: QacStorageOptions) -> Result<Self, QacStorageError> {
15 StructuralView::open(bytes, Family::QacV1Candidate, options.envelope_limits)
16 .map(Self)
17 .map_err(QacStorageError::Envelope)
18 }
19 pub fn structure(&self) -> &StructuralView<'a> {
20 &self.0
21 }
22}
23#[cfg(test)]
24mod tests {
25 use super::*;
26 const CURRENT: &[u8] =
27 include_bytes!("../../q42/field_envelope/fixtures/candidate/qac_candidate-opaque.qac");
28 #[test]
29 fn current_format_is_default_and_development_bytes_are_rejected() {
30 let view = QacStorageView::open(CURRENT, QacStorageOptions::default()).unwrap();
31 assert_eq!(view.structure().family(), Family::QacV1Candidate);
32 view.structure().verify_chunk(1).unwrap();
33 assert!(QacStorageView::open(
34 include_bytes!("fixtures/q4au-v2-contract.qac"),
35 QacStorageOptions::default()
36 )
37 .is_err());
38 }
39 #[test]
40 fn invalid_current_version_is_not_reinterpreted() {
41 let mut changed = CURRENT.to_vec();
42 changed[8] = 99;
43 assert_eq!(
44 QacStorageView::open(&changed, QacStorageOptions::default()).unwrap_err(),
45 QacStorageError::Envelope(field_envelope::Error::Version)
46 );
47 }
48}