pub struct DocumentView<'a> { /* private fields */ }Expand description
A document to READ: its object graph, the byte source its stream spans resolve against, and the version it declares.
Built by Document::view (the file
as loaded) or EditSession::view (the
file as the operator currently has it). Every read path in pdfcer that
can meaningfully run against either — the rasterizer, the vector object
model, pageops’ cross-document copier — takes one of these rather than
a &Document, which is what makes “does this show unsaved edits?” a
property of the caller’s choice of view instead of a property
scattered across fifty call sites.
§impl ObjectGraph is the whole trick
DocumentView implements ObjectGraph by delegating value and
trailer_entry to the graph it wraps. Because resolve, resolved,
catalog_dict and catalog_id are provided trait methods with
signatures identical to Document’s
inherent ones, changing a function’s parameter from &Document to
&DocumentView<'_> leaves its body untouched. Decision 018 measured
this: 45 of pdfcer-render’s 50 Document call sites compiled
unchanged. The only bodies that needed editing were the five
span.slice(doc.bytes()) sites, which became
DocumentView::slice.
§⚠️ Read-only. Never hand this to the writer.
docs/decisions/018 §10 hazard 1, restated where it cannot be missed:
the writer’s source of truth is &Document +
DirtySet::combined_source.
A DocumentView over an
EditSession carries a
StreamSource::Split; code that assumed it held base bytes would
splice staged payloads at base offsets and emit a corrupt file with no
error — a silent breach of the ARCHITECTURE.md §5 round-trip
invariant, which is the worst failure mode this project has. Nothing
the writer needs is implemented here, and nothing should be added.
§Examples
use pdfcer_model::document::Document;
use pdfcer_model::graph::ObjectGraph;
let doc = Document::from_bytes(
include_bytes!("../../../fixtures/synthetic/hello.pdf").to_vec(),
)?;
let view = doc.view();
// The view answers graph questions exactly as the document does.
assert_eq!(view.catalog_id(), doc.catalog_id());Implementations§
Source§impl<'a> DocumentView<'a>
impl<'a> DocumentView<'a>
Sourcepub const fn new(
graph: &'a dyn ObjectGraph,
bytes: &'a [u8],
version: PdfVersion,
) -> Self
pub const fn new( graph: &'a dyn ObjectGraph, bytes: &'a [u8], version: PdfVersion, ) -> Self
Build a view whose stream spans index one contiguous bytes
buffer.
This is the constructor every pre-decision-018 caller already uses
(DocumentView::new(&doc, doc.bytes(), doc.version())), kept with
its original signature so pageops, pdfcer and the page-op
tests are untouched by the promotion out of pageops::assemble.
For an editing session use
EditSession::view, which builds
the StreamSource::Split form — passing
session.document().bytes() here would resolve authored appearance
spans off the end of the base buffer (the X5 failure the original
DocumentView doc comment was written to catch).
Sourcepub const fn with_source(
graph: &'a dyn ObjectGraph,
source: StreamSource<'a>,
version: PdfVersion,
) -> Self
pub const fn with_source( graph: &'a dyn ObjectGraph, source: StreamSource<'a>, version: PdfVersion, ) -> Self
Build a view over an explicit StreamSource.
The general constructor; DocumentView::new is the contiguous
special case. Used by
EditSession::view and by
DocumentView::clone_view to carry a source
through unchanged.
Sourcepub const fn graph(&self) -> &'a dyn ObjectGraph
pub const fn graph(&self) -> &'a dyn ObjectGraph
The object graph this view reads.
Callers that already hold a &DocumentView should prefer the
ObjectGraph methods directly on the view (view.resolve(…));
this accessor exists for the places that must pass a
&dyn ObjectGraph onward, such as pageops’ copier.
Sourcepub const fn source(&self) -> StreamSource<'a>
pub const fn source(&self) -> StreamSource<'a>
The byte source this view’s stream spans resolve against.
Sourcepub fn slice(&self, span: ByteSpan) -> Option<&'a [u8]>
pub fn slice(&self, span: ByteSpan) -> Option<&'a [u8]>
The bytes span covers in this view’s source, or None.
The replacement for the span.slice(doc.bytes()) idiom. See
StreamSource::slice for what None means and why it is not a
panic.
Sourcepub const fn bytes(&self) -> Option<&'a [u8]>
pub const fn bytes(&self) -> Option<&'a [u8]>
The single contiguous buffer this view’s spans index, or None
when the view is over an editing session and therefore has two.
Kept (per decision 018 §8) for callers that genuinely need a whole
buffer rather than one span’s worth of it, but returning Option
rather than the pre-018 &[u8]: for a
StreamSource::Split view there is no such buffer, and any
answer other than “there isn’t one” would be the X5 mis-slice
hazard wearing a plausible face. Prefer DocumentView::slice,
which works for both shapes.
Sourcepub const fn version(&self) -> PdfVersion
pub const fn version(&self) -> PdfVersion
The version this view’s document declares (§7.5.2 / the catalog’s
/Version override).
Source§impl DocumentView<'_>
impl DocumentView<'_>
Sourcepub const fn clone_view(&self) -> DocumentView<'_>
pub const fn clone_view(&self) -> DocumentView<'_>
A second handle on the same borrowed document.
DocumentView is a pair of shared borrows and is therefore
trivially copyable in principle; it is not Clone-derived because
&dyn ObjectGraph blocks the derive’s bounds. This is the manual
equivalent, and exists so pageops::insert can put the same view into a
slice twice-shaped API without the caller pre-building one.
Carries the crate::view::StreamSource through as-is rather than
going via bytes(), so a view over an editing session copies as a
session view (decision 018 §4); reconstructing it from a single
buffer would be the X5 mis-slice.
Trait Implementations§
Source§impl Debug for DocumentView<'_>
impl Debug for DocumentView<'_>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Hand-written because &dyn ObjectGraph is not Debug and adding
that bound would infect every implementor for the sake of one
derive. Prints the facts a debugging session actually wants —
including which shape of source this is, since “why is my edit
invisible?” is answered by exactly that.
Source§impl ObjectGraph for DocumentView<'_>
Delegating impl — the mechanism that let decision 018 change 27
function signatures without touching 45 call-site bodies (type docs).
impl ObjectGraph for DocumentView<'_>
Delegating impl — the mechanism that let decision 018 change 27 function signatures without touching 45 call-site bodies (type docs).
Source§fn resolve<'a>(&'a self, obj: &'a Object) -> &'a Object
fn resolve<'a>(&'a self, obj: &'a Object) -> &'a Object
null rather
than an error, and the chain is depth-guarded
(MAX_RESOLVE_DEPTH) so 5 0 obj 5 0 R endobj — legal syntax
— cannot loop.