Skip to main content

DocumentView

Struct DocumentView 

Source
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>

Source

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).

Source

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.

Source

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.

Source

pub const fn source(&self) -> StreamSource<'a>

The byte source this view’s stream spans resolve against.

Source

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.

Source

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.

Source

pub const fn version(&self) -> PdfVersion

The version this view’s document declares (§7.5.2 / the catalog’s /Version override).

Source§

impl DocumentView<'_>

Source

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<'_>

Source§

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).

Source§

fn value(&self, id: ObjId) -> Option<&Object>

The current value of id, or None when this view has no such object. Read more
Source§

fn trailer_entry(&self, key: &[u8]) -> Option<&Object>

The current value of the trailer entry key, or None. Read more
Source§

fn resolve<'a>(&'a self, obj: &'a Object) -> &'a Object

Follow a reference chain to a non-reference value, applying §7.3.10’s rules: an unresolvable reference yields 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.
Source§

fn resolved(&self, id: ObjId) -> &Object

Resolve id directly to a non-reference value, or null. Read more
Source§

fn catalog_dict(&self) -> Option<&Dict>

The document catalog (§7.7.2) — the dictionary the trailer’s /Root points at — or None if the file has no usable one. Read more
Source§

fn catalog_id(&self) -> Option<ObjId>

The object id the trailer’s /Root names, if it is a reference. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for DocumentView<'a>

§

impl<'a> !UnwindSafe for DocumentView<'a>

§

impl<'a> Forget for DocumentView<'a>

§

impl<'a> Freeze for DocumentView<'a>

§

impl<'a> Move for DocumentView<'a>

§

impl<'a> Send for DocumentView<'a>

§

impl<'a> Sync for DocumentView<'a>

§

impl<'a> Unpin for DocumentView<'a>

§

impl<'a> UnsafeUnpin for DocumentView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.