qualia_core_db/render/
portal_load_emf.rs1use crate::q42::{
5 emf_profiles as profiles,
6 field_envelope::{self as env, Family, StructuralView},
7 field_semantics as sem,
8};
9#[path = "portal_load_emf/admission.rs"]
10mod admission;
11#[path = "portal_load_emf/project.rs"]
12mod project;
13use crate::render::portal_emf_state::MAX_DISPLAY_CELLS;
14pub use crate::render::portal_emf_state::{DisplayEmfCell, DisplaySelection, LoadedEmf};
15#[derive(Clone, Copy, Debug)]
16pub struct PortalEmfLimits {
17 pub storage: env::Limits,
18 pub profiles: profiles::Limits,
19 pub total_payload_work: u64,
20 pub projection_work: u64,
21 pub source_bytes: usize,
22 pub peak_owned_bytes: usize,
23 pub display_cells: usize,
24 pub time_slices: usize,
25}
26impl Default for PortalEmfLimits {
27 fn default() -> Self {
28 Self {
29 storage: env::Limits::default(),
30 profiles: profiles::Limits::default(),
31 total_payload_work: 512 << 20,
32 projection_work: 64 << 20,
33 source_bytes: 128 << 20,
34 peak_owned_bytes: 256 << 20,
35 display_cells: MAX_DISPLAY_CELLS,
36 time_slices: 4096,
37 }
38 }
39}
40#[derive(Clone, Debug, PartialEq, Eq)]
41pub enum Error {
42 Storage(env::Error),
43 Profile(profiles::Error),
44 Limit,
45 Allocation,
46 Overflow,
47 UnsupportedProjection(&'static str),
48 AmbiguousSelection,
49 SelectionMissing,
50 Overlap,
51 OrphanValidityMask,
52}
53impl From<env::Error> for Error {
54 fn from(e: env::Error) -> Self {
55 Self::Storage(e)
56 }
57}
58impl From<profiles::Error> for Error {
59 fn from(e: profiles::Error) -> Self {
60 Self::Profile(e)
61 }
62}
63impl std::fmt::Display for Error {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(f, "emf: ")?;
66 match self {
67 Self::UnsupportedProjection(why) => f.write_str(why),
68 _ => write!(f, "{self:?}"),
69 }
70 }
71}
72impl std::error::Error for Error {}
73pub(super) fn charge(work: &mut u64, n: u64) -> Result<(), Error> {
74 *work = work.checked_sub(n).ok_or(Error::Limit)?;
75 Ok(())
76}
77pub(super) fn vector<T>(len: usize, budget: usize) -> Result<Vec<T>, Error> {
78 let bytes = len
79 .checked_mul(std::mem::size_of::<T>())
80 .ok_or(Error::Overflow)?;
81 if bytes > budget {
82 return Err(Error::Limit);
83 }
84 let mut v = Vec::new();
85 v.try_reserve_exact(len).map_err(|_| Error::Allocation)?;
86 Ok(v)
87}
88pub fn decode_emf_scientific(bytes: &[u8]) -> Result<LoadedEmf, String> {
91 decode_emf_with_selection(bytes, None, PortalEmfLimits::default()).map_err(|e| e.to_string())
92}
93pub fn decode_emf_with_selection(
94 bytes: &[u8],
95 selection: Option<DisplaySelection>,
96 limits: PortalEmfLimits,
97) -> Result<LoadedEmf, Error> {
98 if bytes.len() > limits.source_bytes {
99 return Err(Error::Limit);
100 }
101 let view = StructuralView::open(bytes, Family::EmfV4Candidate, limits.storage)?;
102 let mut work = limits.projection_work;
103 let chunks = admission::admit_all(&view, limits, &mut work)?;
104 project::project(bytes, &view, &chunks, selection, limits, &mut work)
105}
106
107#[cfg(test)]
108#[path = "portal_load_emf/tests.rs"]
109mod tests;