Skip to main content

NameTree

Struct NameTree 

Source
pub struct NameTree {
    pub root: Dict,
}
Expand description

A name tree rooted at one category of the catalog’s /Names.

Fields§

§root: Dict

The tree’s root node.

Implementations§

Source§

impl NameTree

Source

pub fn open<R: Resolve>( catalog: &Dict, category: &Name, r: &R, ) -> Option<NameTree>

Opens catalog[/Names][<category>].

A missing link at either step means no tree, which is not the same as an empty one: a lookup against nothing and a lookup against an empty tree take different fallback paths at the caller.

use pdfrum_common::{Diagnostics, Limits};
use pdfrum_doc::NameTree;
use pdfrum_object::{Array, Dict, Name, NoResolve, Object, PdfString, names};

let dests = Dict::from_pairs([(
    Name::from("Names"),
    Object::Array(Array::of([
        Object::Str(PdfString::literal(b"intro")),
        Object::Array(Array::of([Object::Int(0), Object::Name(Name::from("Fit"))])),
    ])),
)]);
let catalog = Dict::from_pairs([(
    Name::from("Names"),
    Object::Dict(Dict::from_pairs([(Name::from("Dests"), Object::Dict(dests))])),
)]);

let tree = NameTree::open(&catalog, &Name::from("Dests"), &NoResolve)
    .expect("the catalog carries a `/Names /Dests` tree");
let (limits, mut diags) = (Limits::default(), Diagnostics::default());

// A missing link at either step is *no tree*, not an empty one.
assert!(NameTree::open(&Dict::default(), &Name::from("Dests"), &NoResolve).is_none());
Source

pub fn lookup<R: Resolve>( &self, key: &str, r: &R, limits: &Limits, diags: &mut Diagnostics, ) -> Option<Object>

Looks up one key’s value.

use pdfrum_common::{Diagnostics, Limits};
use pdfrum_doc::NameTree;
use pdfrum_object::{Array, Dict, Name, NoResolve, Object, PdfString, names};

let dests = Dict::from_pairs([(
    Name::from("Names"),
    Object::Array(Array::of([
        Object::Str(PdfString::literal(b"intro")),
        Object::Array(Array::of([Object::Int(0), Object::Name(Name::from("Fit"))])),
    ])),
)]);
let catalog = Dict::from_pairs([(
    Name::from("Names"),
    Object::Dict(Dict::from_pairs([(Name::from("Dests"), Object::Dict(dests))])),
)]);

let tree = NameTree::open(&catalog, &Name::from("Dests"), &NoResolve)
    .expect("the catalog carries a `/Names /Dests` tree");
let (limits, mut diags) = (Limits::default(), Diagnostics::default());

assert!(tree.lookup("intro", &NoResolve, &limits, &mut diags).is_some());
assert!(tree.lookup("absent", &NoResolve, &limits, &mut diags).is_none());
Source

pub fn lookup_by_index<R: Resolve>( &self, target: usize, r: &R, limits: &Limits, diags: &mut Diagnostics, ) -> Option<(String, Object)>

Looks up the index-th entry in tree order, returning its key and value.

Two differences from the by-name path: there is no cycle guard at all — a /Kids loop is stopped only by the depth cap — and /Limits is not consulted, because this is a pure in-order walk. A slot whose value will not resolve aborts the whole search rather than being skipped.

use pdfrum_common::{Diagnostics, Limits};
use pdfrum_doc::NameTree;
use pdfrum_object::{Array, Dict, Name, NoResolve, Object, PdfString, names};

let dests = Dict::from_pairs([(
    Name::from("Names"),
    Object::Array(Array::of([
        Object::Str(PdfString::literal(b"intro")),
        Object::Array(Array::of([Object::Int(0), Object::Name(Name::from("Fit"))])),
    ])),
)]);
let catalog = Dict::from_pairs([(
    Name::from("Names"),
    Object::Dict(Dict::from_pairs([(Name::from("Dests"), Object::Dict(dests))])),
)]);

let tree = NameTree::open(&catalog, &Name::from("Dests"), &NoResolve)
    .expect("the catalog carries a `/Names /Dests` tree");
let (limits, mut diags) = (Limits::default(), Diagnostics::default());

let (key, _value) = tree
    .lookup_by_index(0, &NoResolve, &limits, &mut diags)
    .expect("the tree has a first entry");
assert_eq!(key, "intro");
Source

pub fn count<R: Resolve>( &self, r: &R, limits: &Limits, diags: &mut Diagnostics, ) -> usize

How many entries the tree holds.

The visited set here is on dictionary identity, so an inline node counts too — a different guard from the by-name lookup’s.

use pdfrum_common::{Diagnostics, Limits};
use pdfrum_doc::NameTree;
use pdfrum_object::{Array, Dict, Name, NoResolve, Object, PdfString, names};

let dests = Dict::from_pairs([(
    Name::from("Names"),
    Object::Array(Array::of([
        Object::Str(PdfString::literal(b"intro")),
        Object::Array(Array::of([Object::Int(0), Object::Name(Name::from("Fit"))])),
    ])),
)]);
let catalog = Dict::from_pairs([(
    Name::from("Names"),
    Object::Dict(Dict::from_pairs([(Name::from("Dests"), Object::Dict(dests))])),
)]);

let tree = NameTree::open(&catalog, &Name::from("Dests"), &NoResolve)
    .expect("the catalog carries a `/Names /Dests` tree");
let (limits, mut diags) = (Limits::default(), Diagnostics::default());

assert_eq!(tree.count(&NoResolve, &limits, &mut diags), 1);

Trait Implementations§

Source§

impl Clone for NameTree

Source§

fn clone(&self) -> Self

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 NameTree

Source§

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

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

impl PartialEq for NameTree

Source§

fn eq(&self, other: &Self) -> 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 NameTree

Auto Trait Implementations§

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.