use cadmpeg_ir::{report::decode::DecodeReport, CadIr, DecodeResult, SourceFidelity};
use cadmpeg_registry::Selection;
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct LoadedDocument {
pub(crate) ir: CadIr,
pub(crate) origin: LoadOrigin,
pub(crate) input_bytes: u64,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum LoadOrigin {
Neutral,
Restored {
report: DecodeReport,
fidelity: Box<SourceFidelity>,
},
Decoded {
selection: Selection,
report: DecodeReport,
fidelity: Box<SourceFidelity>,
},
}
impl LoadedDocument {
pub(crate) const fn neutral(ir: CadIr, input_bytes: u64) -> Self {
Self {
ir,
origin: LoadOrigin::Neutral,
input_bytes,
}
}
pub(crate) fn decoded(result: DecodeResult, selection: Selection, input_bytes: u64) -> Self {
let (ir, report, fidelity) = result.into_parts();
Self {
ir,
origin: LoadOrigin::Decoded {
report,
fidelity: Box::new(fidelity),
selection,
},
input_bytes,
}
}
pub(crate) fn restored(
ir: CadIr,
report: DecodeReport,
fidelity: SourceFidelity,
input_bytes: u64,
) -> Self {
Self {
ir,
origin: LoadOrigin::Restored {
report,
fidelity: Box::new(fidelity),
},
input_bytes,
}
}
pub(crate) const fn decode_report(&self) -> Option<&DecodeReport> {
match &self.origin {
LoadOrigin::Neutral => None,
LoadOrigin::Decoded { report, .. } | LoadOrigin::Restored { report, .. } => {
Some(report)
}
}
}
pub(crate) const fn fidelity(&self) -> Option<&SourceFidelity> {
match &self.origin {
LoadOrigin::Neutral => None,
LoadOrigin::Decoded { fidelity, .. } | LoadOrigin::Restored { fidelity, .. } => {
Some(fidelity)
}
}
}
}