Skip to main content

DuplicateKeyPolicy

Enum DuplicateKeyPolicy 

Source
#[non_exhaustive]
pub enum DuplicateKeyPolicy { Refuse, KeepLast, KeepFirst, }
Expand description

What to do when one dictionary names the same key twice (§7.3.7).

§The file that forced this, and why the strict answer was too expensive

An operator’s 46 KB drawing opens in Acrobat and was refused whole by pdfcer: its document catalog carries /PageMode twice, with different values —

… /PageMode /UseOC /PageLayout /SinglePage /PageLabels << … >>
  /PageMode /UseOutlines /Outlines 58 0 R /Metadata 59 0 R >>

One repeated key in one object cost the whole document, on a file every other reader opens without comment. That is the wrong trade for a malformed entry in an otherwise sound file: pdfcer’s fail-clean posture is about never silently doing the wrong thing, and a counted, disclosed choice is not silent.

§Why this is a POLICY and not simply a relaxation

The spec’s own rule and pdfcer’s obligation are different questions. The rule is recorded at ParseErrorKind::DuplicateDictKey; what a reader may do about a violation is not stated, which makes the winner pdfcer’s choice to make and to disclose — this project’s standing shape for a spec ambiguity (a setting, a documented default, never a silent decision).

Self::Refuse stays the type’s Default so every caller that did not ask — fuzz targets, the recovery confirmation pass, any future one — keeps the strict behaviour it was written against. The document loader opts in; the parser does not opt in for it.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Refuse

Refuse the object with ParseErrorKind::DuplicateDictKey.

The default, and the right answer for a caller whose job is to decide whether a file is well-formed rather than to open it.

§

KeepLast

Keep the last occurrence, and count it.

§Why last rather than first

Nothing in the standard prescribes either — see ParseErrorKind::DuplicateDictKey for the sourcing — so this is pdfcer’s choice, and it is made on observed reader behaviour: qpdf, pdf.js and pdfium all keep the last occurrence, and none of the three refuses the file. qpdf even says so out loud — “dictionary has duplicated key <key>; last occurrence overrides earlier ones” — which is the same disclosure pdfcer now makes.

CORRECTED BEFORE SHIPPING. This paragraph first argued from §7.5.6 — “every other override-by-repetition in PDF is last-wins; an incremental update’s newer definition supersedes the older one” — and that is an analogy dressed as a citation. §7.5.6 orders objects across incremental updates, an ordering the standard deliberately makes meaningful; §7.3.7’s own preceding sentence says the ordering of a dictionary’s entries “shall be ignored”. Citing it here would have borrowed authority from a clause that says the opposite thing about this exact structure.

The distinction is not pedantry: ISO’s one resolved duplicate-key erratum (#3, inline images) picked the winner by content, and its submission rejected first/last positional logic by name, for precisely that reason.

The alternative is defensible and is not silently unavailable: a caller wanting the first value has Self::KeepFirst, and every decision is recorded with BOTH values so a shell can offer the operator the swap. What is NOT on offer is pdfcer choosing quietly.

§

KeepFirst

Keep the first occurrence, and count it.

The other half of the operator’s choice. A shell that shows “this file names /PageMode twice — UseOC or UseOutlines?” re-loads under this policy when the operator picks the earlier value; nothing else in the document changes.

Not the default only because Self::KeepLast matches every other override-by-repetition in PDF; on the evidence available neither is wrong, which is exactly why both ship.

Trait Implementations§

Source§

impl Clone for DuplicateKeyPolicy

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 Copy for DuplicateKeyPolicy

Source§

impl Debug for DuplicateKeyPolicy

Source§

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

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

impl Default for DuplicateKeyPolicy

Source§

fn default() -> Self

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

impl Eq for DuplicateKeyPolicy

Source§

impl PartialEq for DuplicateKeyPolicy

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 DuplicateKeyPolicy

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> Same for T

Source§

type Output = T

Should always be Self
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.