pub struct Dict(pub Vec<(Name, Object)>);Expand description
A dictionary (§7.3.7): name-keyed, unordered per spec — but pdfcer preserves the parsed entry order so that re-serializing a modified dictionary perturbs sibling entries as little as possible (smaller diffs; the spec says written order “shall be ignored”, so preserving it is always safe).
Backed by an ordered Vec with linear-scan lookup: PDF dictionaries
are small (a handful to a few dozen entries), where a Vec beats a
hash map on both speed and memory, and it keeps deterministic order
for free.
Tuple Fields§
§0: Vec<(Name, Object)>Implementations§
Source§impl Dict
impl Dict
Sourcepub fn get(&self, key: &[u8]) -> Option<&Object>
pub fn get(&self, key: &[u8]) -> Option<&Object>
Look up key, applying the §7.3.7/§7.3.9 rule: an entry whose
value is null is treated as if absent — this returns None
for it. Implemented here, once, so no call site ever needs a
second “is it null?” check (see the spec RAG’s API note).
Sourcepub fn contains_key(&self, key: &[u8]) -> bool
pub fn contains_key(&self, key: &[u8]) -> bool
Whether key is present with a non-null value (same collapse
rule as Dict::get).
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of stored entries (including any explicit-null entries —
this is the physical count, used by serialization; semantic
presence goes through Dict::get).
Sourcepub fn iter(&self) -> impl Iterator<Item = (&Name, &Object)>
pub fn iter(&self) -> impl Iterator<Item = (&Name, &Object)>
Iterate entries in preserved (parsed/inserted) order.
Sourcepub fn insert(&mut self, key: Name, value: Object)
pub fn insert(&mut self, key: Name, value: Object)
Insert or replace key → value, preserving position on
replace (append on fresh insert). The write-side primitive the
editing Passes build on.
Sourcepub fn remove(&mut self, key: &[u8]) -> Option<Object>
pub fn remove(&mut self, key: &[u8]) -> Option<Object>
Remove key entirely, returning its stored value if there was
one.
§Why removal, and not “set it to null”
§7.3.7 makes a null-valued entry semantically identical to an
absent one, so insert(key, Object::Null) would be correct.
It would also leave a physical entry behind, which means a
re-serialized dictionary carries a visible /Title null the
operator never asked for, and a later reader cannot tell “pdfcer
cleared this” from “the producer wrote an explicit null”. Under
the minimal-diff discipline the honest form of clear this field
is for the field not to be there.
Removes all entries with this key, not just the first.
Duplicate keys are malformed per §7.3.7 (“shall not”) and the
parser rejects them, so a parsed dictionary never has any — but a
Dict built programmatically could, and leaving a shadowed
duplicate behind would make removal look like it did nothing.