Skip to main content

structured_pades/
query.rs

1//! One live original PDF generation and its current LTV operation.
2
3use crate::{document::Document, range::RangeSource, timestamp::Node};
4use structured_asn1::memory::{Execution, Work};
5use structured_ltv::source::{Query as QueryContract, ScopeError, Snapshot};
6
7/// Metadata-only generation guard shared by material lookup and archive input
8/// coverage. It cannot replace the original document's source or execution.
9pub struct Query<'q, 's, S: RangeSource> {
10    document: &'q Document<'s, S>,
11    execution: Execution<'q>,
12}
13
14impl<S: RangeSource> Query<'_, '_, S> {
15    pub(crate) fn document(&self) -> &Document<'_, S> {
16        self.document
17    }
18    pub(crate) fn belongs(&self, session: &crate::Session) -> bool {
19        core::ptr::eq(self.document.session, session)
20    }
21}
22
23impl<'q, S: RangeSource> QueryContract for Query<'q, '_, S> {
24    type Id = Node;
25    type Document = S::Document;
26    type Generation = S::Generation;
27    type Work = Execution<'q>;
28    fn snapshot(&self) -> Snapshot<Self::Document, Self::Generation> {
29        self.document.stamp.snapshot
30    }
31    fn execution(&self) -> &Self::Work {
32        &self.execution
33    }
34    fn admit(&self, node: Node, units: u64) -> Result<(), ScopeError<Node>> {
35        if self.document.source.stamp() != self.document.stamp {
36            return Err(ScopeError::Changed { node });
37        }
38        self.execution
39            .charge_work(units)
40            .map_err(|cause| ScopeError::Resource { node, cause })?;
41        if self.document.source.stamp() != self.document.stamp {
42            return Err(ScopeError::Changed { node });
43        }
44        Ok(())
45    }
46}
47
48impl<'s, S: RangeSource> Document<'s, S> {
49    /// Borrows the actual original file and current operation for common LTV
50    /// queries. No parsing, source copy, allocation or account clone occurs.
51    pub fn query<'q, W: Work>(&'q self, operation: &'q W) -> Query<'q, 's, S> {
52        Query {
53            document: self,
54            execution: self.session.query(operation),
55        }
56    }
57}