docx-templating 1.0.0

Rust library for DOCX templating with variable replacement, conditionals, and loops
Documentation
//! DOCX package (ZIP/OPC) handling.
//!
//! This module provides abstract, safe access to DOCX files as ZIP archives.
//!
//! ## Traits
//!
//! - [`PackageSource`] — read-only access to package parts
//! - [`PackageWriter`] — write access to package parts
//!
//! ## Implementations
//!
//! | Type                   | Description                                   |
//! |------------------------|-----------------------------------------------|
//! | [`ZipPackageSource`]   | File/reader-backed ZIP source                 |
//! | [`ZipPackageWriter`]   | File/writer-backed ZIP output                 |
//! | [`InMemoryPackageSource`] | In-memory source for tests                 |
//!
//! ## Security
//!
//! All part names from external sources are validated by
//! [`validation::is_safe_part_name`] before use. Path traversal attempts
//! (e.g. `../etc/passwd`) are rejected with [`PackageError::UnsupportedEntry`].

pub mod memory_source;
pub mod source;
pub mod validation;
pub mod writer;
pub mod zip_source;
pub mod zip_writer;

pub use memory_source::InMemoryPackageSource;
pub use source::PackageSource;
pub use writer::PackageWriter;
pub use zip_source::ZipPackageSource;
pub use zip_writer::ZipPackageWriter;

/// Package-layer error type.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PackageError {
    /// Required DOCX part is missing from the archive.
    #[error("missing required part: {0}")]
    MissingRequiredPart(String),

    /// ZIP entry name is unsafe (path traversal, absolute path, etc.).
    #[error("unsupported ZIP entry: {0}")]
    UnsupportedEntry(String),

    /// ZIP entry decompressed size exceeds the configured limit.
    #[error("entry '{name}' decompressed size {size} bytes exceeds the {limit}-byte limit")]
    EntrySizeExceeded {
        /// The entry name that was too large.
        name: String,
        /// Reported uncompressed size of the entry.
        size: u64,
        /// The limit that was exceeded.
        limit: u64,
    },

    /// ZIP archive structure is invalid or unreadable.
    #[error("ZIP archive is malformed")]
    MalformedArchive,
}