Skip to main content

Dict

Struct Dict 

Source
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

Source

pub fn new() -> Self

An empty dictionary.

Source

pub fn from_pairs(pairs: impl IntoIterator<Item = (Name, Object)>) -> Self

A dictionary from key-value pairs, keeping their order.

Source

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.

Source

pub fn insert(&mut self, key: Name, value: Object)

Sets key to value: replaces the existing entry in place, keeping its position, or appends.

Source

pub fn remove(&mut self, key: &Name) -> Option<Object>

Removes key, returning its value; None when absent.

Source

pub fn len(&self) -> usize

Number of stored pairs, duplicates included.

Source

pub fn is_empty(&self) -> bool

Whether the dictionary has no pairs.

Source

pub fn iter(&self) -> impl Iterator<Item = &(Name, Object)>

The pairs, in document order.

Source

pub fn keys(&self) -> impl Iterator<Item = &Name>

The keys, in document order, duplicates included.

Source

pub fn contains_key(&self, key: &Name) -> bool

Whether any entry carries this key.

Source

pub fn raw(&self, key: &Name) -> Option<&Object>

The stored value, whatever its type, without resolving references.

The last entry with this key wins.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn string(&self, key: &Name) -> Option<&PdfString>

The string a String-typed entry holds, without resolving.

Source

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.

Source

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.

Source

pub fn number(&self, key: &Name, r: &impl Resolve) -> Option<f32>

The numeric value of an entry, coercing integers to f32.

Source

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.

Source

pub fn text(&self, key: &Name, r: &impl Resolve) -> Option<String>

An entry read as text — see Object::to_text.

Source

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.

Source

pub fn array(&self, key: &Name, r: &impl Resolve) -> Option<Array>

The array an entry holds, following one level of indirection.

Source

pub fn stream(&self, key: &Name, r: &impl Resolve) -> Option<Stream>

The stream an entry holds, following one level of indirection.

Source

pub fn reference(&self, key: &Name) -> Option<ObjRef>

The reference an entry holds, without resolving it.

Source

pub fn rect(&self, key: &Name, r: &impl Resolve) -> Rect

A rectangle read from a four-element array — see Array::as_rect.

Missing or malformed yields the zero rectangle, never None: PDF consumers of /MediaBox and friends all want a rectangle.

Source

pub fn matrix(&self, key: &Name, r: &impl Resolve) -> Affine

A transformation matrix read from a six-element array — see Array::as_matrix. Missing or malformed yields the identity.

Trait Implementations§

Source§

impl Clone for Dict

Source§

fn clone(&self) -> Dict

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dict

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Dict

Source§

fn default() -> Dict

Returns the “default value” for a type. Read more
Source§

impl From<Dict> for Object

Source§

fn from(v: Dict) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<(Name, Object)> for Dict

Source§

fn from_iter<I: IntoIterator<Item = (Name, Object)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a> IntoIterator for &'a Dict

Source§

type Item = &'a (Name, Object)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, (Name, Object)>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Dict

Source§

fn eq(&self, other: &Dict) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Dict

Auto Trait Implementations§

§

impl Freeze for Dict

§

impl RefUnwindSafe for Dict

§

impl Send for Dict

§

impl Sync for Dict

§

impl Unpin for Dict

§

impl UnsafeUnpin for Dict

§

impl UnwindSafe for Dict

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.