Skip to main content

InMemoryPackageSource

Struct InMemoryPackageSource 

Source
pub struct InMemoryPackageSource { /* private fields */ }
Expand description

A DOCX package stored entirely in memory.

Useful for:

  • Unit and integration tests that don’t touch the file system
  • Constructing minimal DOCX packages programmatically
  • Verifying that higher-level code is decoupled from ZIP storage

§Example

use docx_templating::package::{InMemoryPackageSource, PackageSource};

let mut source = InMemoryPackageSource::new();
source.add_part("word/document.xml", b"<root/>".to_vec());

assert!(source.contains_part("word/document.xml").unwrap());
assert_eq!(
    source.read_part("word/document.xml").unwrap().unwrap(),
    b"<root/>"
);

Implementations§

Source§

impl InMemoryPackageSource

Source

pub fn new() -> Self

Create an empty in-memory package.

Source

pub fn add_part(&mut self, name: impl Into<String>, data: Vec<u8>)

Add or replace a part with the given raw bytes.

name should use forward slashes and must not start with /.

Source

pub fn remove_part(&mut self, name: &str) -> Option<Vec<u8>>

Remove a part, returning its data if it existed.

Source

pub fn len(&self) -> usize

Return the number of parts currently in this package.

Source

pub fn is_empty(&self) -> bool

Return true if this package contains no parts.

Trait Implementations§

Source§

impl Debug for InMemoryPackageSource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for InMemoryPackageSource

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PackageSource for InMemoryPackageSource

Source§

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

Return the names of all parts in this package. Read more
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.
Source§

fn raw_archive_bytes(&self) -> Option<&[u8]>

Return the raw bytes of the underlying archive, if available. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.