Skip to main content

qualia_core_db/medical/dicom_source/
acquisition.rs

1//! Retain once, then inspect the same immutable bytes with explicit admission.
2use super::storage::{preserve, read_source};
3use super::{DicomSourceArtifact, DicomSourceBudget, DicomSourceError};
4use crate::medical::{
5    dicom_dataset::{DatasetError, ParseLimits, Part10Dataset},
6    dicom_frames::{FrameError, FrameLimits, FrameSet},
7};
8use std::path::Path;
9
10/// Cold-path acquisition owner. The source and byte identity cannot be replaced
11/// through this API after publication. Unsupported interpretation does not discard
12/// the original. This is not an IOD-conformance or clinical-validity assertion.
13pub struct RetainedDicom {
14    source: DicomSourceArtifact,
15    bytes: Vec<u8>,
16}
17impl RetainedDicom {
18    /// Read one bounded snapshot, publish it without replacement and keep that
19    /// exact snapshot for analysis. No second source-path read can change identity.
20    pub fn retain(
21        storage_root: &Path,
22        source_path: &Path,
23        budget: DicomSourceBudget,
24    ) -> Result<Self, DicomSourceError> {
25        let bytes = read_source(source_path, budget)?;
26        if !crate::dicom::is_dicom_part10(&bytes) {
27            return Err(DicomSourceError::NotPart10);
28        }
29        let source = preserve(storage_root, &bytes, budget)?;
30        Ok(Self { source, bytes })
31    }
32    pub fn source(&self) -> &DicomSourceArtifact {
33        &self.source
34    }
35    pub fn original_bytes(&self) -> &[u8] {
36        &self.bytes
37    }
38    /// Interpret only under the supplied parser/frame budgets. Parse failure
39    /// leaves the retained object usable for archival or another qualified adapter.
40    /// Frame extraction is a separate result: geometry/mapping failure must not
41    /// pretend the dataset or its original source disappeared.
42    pub fn inspect(
43        &self,
44        parse_limits: ParseLimits,
45        frame_limits: FrameLimits,
46    ) -> Result<AcquisitionView<'_>, DatasetError> {
47        let dataset = Part10Dataset::parse(&self.bytes, parse_limits)?;
48        let frames = FrameSet::extract(&dataset, frame_limits);
49        Ok(AcquisitionView { dataset, frames })
50    }
51}
52/// Both views borrow the retained source snapshot, not a reconstructed pixel file.
53pub struct AcquisitionView<'a> {
54    dataset: Part10Dataset<'a>,
55    frames: Result<FrameSet<'a>, FrameError>,
56}
57impl<'a> AcquisitionView<'a> {
58    pub fn dataset(&self) -> &Part10Dataset<'a> {
59        &self.dataset
60    }
61    pub fn frames(&self) -> Result<&FrameSet<'a>, &FrameError> {
62        self.frames.as_ref()
63    }
64}