pub struct Dict(/* private fields */);Expand description
A PDF dictionary: key-value pairs in document order.
§Streams as values
ISO 32000-1 §7.3.8.1 forbids a file from writing a stream as a direct
dictionary value, and the reader drops one found inline while parsing.
That is a file-format constraint, not an in-memory invariant, and
this type does not police it:
Object::clone_direct flattens
references, so a /Resources whose /XObject entries are indirect
streams clones into a dictionary holding those streams directly, and
Dict::stream reads such a value back. Enforcing §7.3.8.1 is the
writer’s job: pdfrum-edit hoists a direct stream to an indirect
object at serialization time.
use pdfrum_object::{Dict, NoResolve, Object, names};
let dict = Dict::from_pairs([
(names::TYPE.clone(), Object::Name(names::PAGE.clone())),
(names::COUNT.clone(), Object::Int(3)),
]);
assert_eq!(dict.name(names::TYPE), Some(names::PAGE));
assert_eq!(dict.int(names::COUNT, &NoResolve), Some(3));
assert_eq!(dict.len(), 2);Implementations§
Source§impl Dict
impl Dict
Sourcepub fn from_pairs(pairs: impl IntoIterator<Item = (Name, Object)>) -> Self
pub fn from_pairs(pairs: impl IntoIterator<Item = (Name, Object)>) -> Self
A dictionary from key-value pairs, keeping their order.
Sourcepub fn push(&mut self, key: Name, value: Object)
pub fn push(&mut self, key: Name, value: Object)
Append a pair, keeping any earlier entry with the same key.
The later entry wins on lookup, so appending is how a reader records a duplicate key without losing what the file actually said.
Any object, a stream included — see the type-level note on §7.3.8.1.
Sourcepub fn insert(&mut self, key: Name, value: Object)
pub fn insert(&mut self, key: Name, value: Object)
Sets key to value: replaces the existing entry in place, keeping
its position, or appends.
Sourcepub fn remove(&mut self, key: &Name) -> Option<Object>
pub fn remove(&mut self, key: &Name) -> Option<Object>
Removes key, returning its value; None when absent.
Sourcepub fn keys(&self) -> impl Iterator<Item = &Name>
pub fn keys(&self) -> impl Iterator<Item = &Name>
The keys, in document order, duplicates included.
Sourcepub fn contains_key(&self, key: &Name) -> bool
pub fn contains_key(&self, key: &Name) -> bool
Whether any entry carries this key.
Sourcepub fn raw(&self, key: &Name) -> Option<&Object>
pub fn raw(&self, key: &Name) -> Option<&Object>
The stored value, whatever its type, without resolving references.
The last entry with this key wins.
Sourcepub fn direct_int(&self, key: &Name) -> Option<i64>
pub fn direct_int(&self, key: &Name) -> Option<i64>
The value of a Number-typed entry in the C-integer view, without
resolving.
This is how the cross-reference reader reads /Size, /Prev and
/XRefStm: an indirect value there is ignored rather than chased,
which is deliberate recovery behavior in files whose trailer points at
objects the table cannot yet describe.
Sourcepub fn name(&self, key: &Name) -> Option<&Name>
pub fn name(&self, key: &Name) -> Option<&Name>
The name a Name-typed entry holds, without resolving.
A reference here reads as absent: the type check happens before any
resolution, so /Type 5 0 R never names a type.
Sourcepub fn bool(&self, key: &Name) -> Option<bool>
pub fn bool(&self, key: &Name) -> Option<bool>
The value of a Boolean-typed entry, without resolving.
An Int(1) is not a boolean and reads as absent.
Sourcepub fn number_obj(&self, key: &Name) -> Option<&Object>
pub fn number_obj(&self, key: &Name) -> Option<&Object>
A Number-typed entry as an object, without resolving. Used where the
distinction between “not a number” and “zero” matters, such as
validating a cross-reference stream’s /Index.
Sourcepub fn string(&self, key: &Name) -> Option<&PdfString>
pub fn string(&self, key: &Name) -> Option<&PdfString>
The string a String-typed entry holds, without resolving.
Sourcepub fn get<'a>(&'a self, key: &Name, r: &impl Resolve) -> Option<Resolved<'a>>
pub fn get<'a>(&'a self, key: &Name, r: &impl Resolve) -> Option<Resolved<'a>>
The value, following one level of indirection.
Returns None for a missing key and for a reference the store
cannot produce — both are absence — and the store records the
underlying failure in its diagnostics.
Sourcepub fn int(&self, key: &Name, r: &impl Resolve) -> Option<i64>
pub fn int(&self, key: &Name, r: &impl Resolve) -> Option<i64>
The integer value of an entry of any type, in the C-integer view.
Coerces: booleans read as 0 and 1, reals truncate. A reference is followed one level, and a reference to a reference reads as absent.
Sourcepub fn number(&self, key: &Name, r: &impl Resolve) -> Option<f32>
pub fn number(&self, key: &Name, r: &impl Resolve) -> Option<f32>
The numeric value of an entry, coercing integers to f32.
Sourcepub fn byte_string(&self, key: &Name, r: &impl Resolve) -> Option<Vec<u8>>
pub fn byte_string(&self, key: &Name, r: &impl Resolve) -> Option<Vec<u8>>
The byte-string spelling of an entry of any type — see
Object::to_byte_string.
Sourcepub fn text(&self, key: &Name, r: &impl Resolve) -> Option<String>
pub fn text(&self, key: &Name, r: &impl Resolve) -> Option<String>
An entry read as text — see Object::to_text.
Sourcepub fn dict(&self, key: &Name, r: &impl Resolve) -> Option<Dict>
pub fn dict(&self, key: &Name, r: &impl Resolve) -> Option<Dict>
The dictionary an entry holds, following one level of indirection.
A stream answers with its own dictionary, so /Pages pointing at
either a dictionary or a stream reads the same way.
Returns an owned clone because the dictionary may live inside an
Arc the store owns; dictionaries are small and this keeps the
borrow story simple for callers.
Sourcepub fn array(&self, key: &Name, r: &impl Resolve) -> Option<Array>
pub fn array(&self, key: &Name, r: &impl Resolve) -> Option<Array>
The array an entry holds, following one level of indirection.
Sourcepub fn stream(&self, key: &Name, r: &impl Resolve) -> Option<Stream>
pub fn stream(&self, key: &Name, r: &impl Resolve) -> Option<Stream>
The stream an entry holds, following one level of indirection.
Sourcepub fn reference(&self, key: &Name) -> Option<ObjRef>
pub fn reference(&self, key: &Name) -> Option<ObjRef>
The reference an entry holds, without resolving it.