Skip to main content

docx_templating/package/
mod.rs

1//! DOCX package (ZIP/OPC) handling.
2//!
3//! This module provides abstract, safe access to DOCX files as ZIP archives.
4//!
5//! ## Traits
6//!
7//! - [`PackageSource`] — read-only access to package parts
8//! - [`PackageWriter`] — write access to package parts
9//!
10//! ## Implementations
11//!
12//! | Type                   | Description                                   |
13//! |------------------------|-----------------------------------------------|
14//! | [`ZipPackageSource`]   | File/reader-backed ZIP source                 |
15//! | [`ZipPackageWriter`]   | File/writer-backed ZIP output                 |
16//! | [`InMemoryPackageSource`] | In-memory source for tests                 |
17//!
18//! ## Security
19//!
20//! All part names from external sources are validated by
21//! [`validation::is_safe_part_name`] before use. Path traversal attempts
22//! (e.g. `../etc/passwd`) are rejected with [`PackageError::UnsupportedEntry`].
23
24pub mod memory_source;
25pub mod source;
26pub mod validation;
27pub mod writer;
28pub mod zip_source;
29pub mod zip_writer;
30
31pub use memory_source::InMemoryPackageSource;
32pub use source::PackageSource;
33pub use writer::PackageWriter;
34pub use zip_source::ZipPackageSource;
35pub use zip_writer::ZipPackageWriter;
36
37/// Package-layer error type.
38#[derive(Debug, thiserror::Error)]
39#[non_exhaustive]
40pub enum PackageError {
41    /// Required DOCX part is missing from the archive.
42    #[error("missing required part: {0}")]
43    MissingRequiredPart(String),
44
45    /// ZIP entry name is unsafe (path traversal, absolute path, etc.).
46    #[error("unsupported ZIP entry: {0}")]
47    UnsupportedEntry(String),
48
49    /// ZIP entry decompressed size exceeds the configured limit.
50    #[error("entry '{name}' decompressed size {size} bytes exceeds the {limit}-byte limit")]
51    EntrySizeExceeded {
52        /// The entry name that was too large.
53        name: String,
54        /// Reported uncompressed size of the entry.
55        size: u64,
56        /// The limit that was exceeded.
57        limit: u64,
58    },
59
60    /// ZIP archive structure is invalid or unreadable.
61    #[error("ZIP archive is malformed")]
62    MalformedArchive,
63}