Skip to main content

PackageSource

Trait PackageSource 

Source
pub trait PackageSource {
    // Required methods
    fn part_names(&self) -> Result<Vec<String>>;
    fn contains_part(&self, name: &str) -> Result<bool>;
    fn read_part(&self, name: &str) -> Result<Option<Vec<u8>>>;

    // Provided method
    fn raw_archive_bytes(&self) -> Option<&[u8]> { ... }
}
Expand description

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 /.

Required Methods§

Source

fn part_names(&self) -> Result<Vec<String>>

Return the names of all parts in this package.

Implementations must filter out unsafe entry names before returning.

Source

fn contains_part(&self, name: &str) -> Result<bool>

Return true if the named part exists in this package.

Source

fn read_part(&self, name: &str) -> Result<Option<Vec<u8>>>

Read the raw bytes of a part, or None if the part does not exist.

Provided Methods§

Source

fn raw_archive_bytes(&self) -> Option<&[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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§