laminapdf-model 0.7.0

Typed views of a PDF document in Rust: pages, outline, document info, page labels, name trees and the structure tree, read tolerantly.
Documentation
//! The version a file declares and the standards it claims to meet: PDF/A (ISO 19005), PDF/UA
//! (ISO 14289) and PDF/X (ISO 15930). A claim is what the file says, never a verdict: nothing
//! here checks that the file meets the standard.

use laminapdf_cos::Result;

use crate::document::Document;
use crate::xmp::{PacketState, PdfaClaim};

/// The version a file declares and the standards it claims ([`Document::conformance`]). Each
/// claim is claimed, not verified.
///
/// Added in 0.6.0.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[non_exhaustive]
pub struct ConformanceClaims {
    /// The `%PDF-x.y` header's version; `None` without a header.
    pub header_version: Option<String>,
    /// The catalog's `/Version` (ISO 32000-2 ยง7.7.2), as written; `None` when absent.
    pub catalog_version: Option<String>,
    /// The PDF/A claim ([`Packet::pdfa_claim`](crate::xmp::Packet::pdfa_claim)).
    pub pdfa: Option<PdfaClaim>,
    /// The PDF/UA part claimed (`pdfuaid:part`).
    pub pdfua: Option<u8>,
    /// The PDF/X version claimed (`GTS_PDFXVersion`: in the XMP metadata for PDF/X-4 and later,
    /// in the Info dictionary for earlier parts), as written (`PDF/X-4`, `PDF/X-1a:2003`): the
    /// XMP metadata's when it has one, else the Info dictionary's.
    pub pdfx: Option<String>,
    /// The Info dictionary's `GTS_PDFXVersion` when the XMP metadata claims another: the file
    /// contradicts itself, and neither is taken for the other.
    pub pdfx_info_conflict: Option<String>,
}

impl Document {
    /// The version the file declares and the standards it claims: the header's and the
    /// catalog's versions, and the PDF/A, PDF/UA and PDF/X claims. A packet that doesn't parse
    /// claims nothing.
    ///
    /// # Errors
    /// Reading errors (the catalog, the metadata stream).
    ///
    /// Added in 0.6.0.
    pub fn conformance(&self) -> Result<ConformanceClaims> {
        let catalog = self.catalog()?;
        let catalog_version = catalog
            .get_name("Version")
            .map(|name| String::from_utf8_lossy(name).into_owned());
        let mut out = ConformanceClaims {
            header_version: self.header_version().map(str::to_owned),
            catalog_version,
            ..ConformanceClaims::default()
        };
        if let PacketState::Parsed(packet) = self.xmp()? {
            out.pdfa = packet.pdfa_claim();
            out.pdfua = packet.pdfua_part();
            out.pdfx = packet.pdfx_version();
        }
        let info = match self.info()? {
            Some(info) => info
                .text("GTS_PDFXVersion")?
                .map(|v| v.trim().to_owned())
                .filter(|v| !v.is_empty()),
            None => None,
        };
        match (&out.pdfx, info) {
            (None, info) => out.pdfx = info,
            (Some(xmp), Some(info)) if *xmp != info => out.pdfx_info_conflict = Some(info),
            _ => {}
        }
        Ok(out)
    }
}