docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! `PackageSource` trait — abstract read access to a DOCX/OPC package.

use crate::Result;

/// Read-only access to a DOCX package (ZIP/OPC archive).
///
/// Implemented by [`ZipPackageSource`] for file/stream-backed packages and by
/// [`InMemoryPackageSource`] for in-memory packages used in tests.
///
/// Higher-level layers depend on this trait rather than any concrete ZIP type,
/// which keeps the templating engine independent of storage format.
///
/// # Contract
///
/// - `part_names` returns only **safe** part names (no path traversal).
/// - `read_part` returns `None` for unknown parts, never an error for "not found".
/// - Part names use forward slashes as separators and do NOT start with `/`.
///
/// [`ZipPackageSource`]: crate::package::ZipPackageSource
/// [`InMemoryPackageSource`]: crate::package::InMemoryPackageSource
pub trait PackageSource {
    /// Return the names of all parts in this package.
    ///
    /// Implementations must filter out unsafe entry names before returning.
    fn part_names(&self) -> Result<Vec<String>>;

    /// Return `true` if the named part exists in this package.
    fn contains_part(&self, name: &str) -> Result<bool>;

    /// Read the raw bytes of a part, or `None` if the part does not exist.
    fn read_part(&self, name: &str) -> Result<Option<Vec<u8>>>;

    /// Return the raw bytes of the underlying archive, if available.
    ///
    /// Returning `Some` allows [`ZipPackageWriter`] to copy parts verbatim
    /// without decompressing and recompressing them. The default returns
    /// `None`, which causes a safe fallback to decompress + recompress.
    ///
    /// [`ZipPackageWriter`]: crate::package::ZipPackageWriter
    fn raw_archive_bytes(&self) -> Option<&[u8]> {
        None
    }
}