Skip to main content

DocxAssetProvider

Struct DocxAssetProvider 

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

Provides streaming access to binary assets stored inside a DOCX ZIP archive.

Use this alongside DocxReader when you need to resolve embedded images. DocxReader emits Event::Image with an asset_id of the form zip://word/media/image1.png; pass that asset_id to DocxAssetProvider to stream the raw bytes.

§Example

use docspec_docx_reader::DocxAssetProvider;
use docspec_core::AssetProvider;

let provider = DocxAssetProvider::from_path("document.docx")?;
let mut buf = Vec::new();
if let Some(result) = provider.stream_to("zip://word/media/image1.png", &mut buf) {
    result?;
}

Provides streaming access to binary assets stored inside a DOCX ZIP archive.

Holds the docx ZIP file open until dropped. Uses internal Mutex to serialize concurrent ZIP reads. Not Clone — use Arc<DocxAssetProvider> to share.

Implementations§

Source§

impl DocxAssetProvider

Source

pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>

Creates a DocxAssetProvider from a file path.

Opens the DOCX ZIP file and reads [Content_Types].xml to build the content type lookup table.

§Errors

Returns Error::Io if the file cannot be opened, or Error::Parse if the file is not a valid ZIP archive.

Source

pub fn from_reader<R: Read + Seek + Send + 'static>(reader: R) -> Result<Self>

Creates a DocxAssetProvider from any Read + Seek + Send source.

The source must be positioned at the start of a valid DOCX (ZIP) archive. Reads [Content_Types].xml to build the content type lookup table.

§Errors

Returns Error::Parse if the input is not a valid ZIP archive, or Error::Io for I/O failures when reading [Content_Types].xml.

Trait Implementations§

Source§

impl AssetProvider for DocxAssetProvider

Source§

fn content_type(&self, asset_id: &str) -> Option<Cow<'_, str>>

Returns the MIME content type for an asset ID with a zip:// scheme prefix.

Strips the zip:// prefix and looks up the path in [Content_Types].xml. Returns None if the scheme is not zip:// or if no content type is registered for the path.

Source§

fn stream_to( &self, asset_id: &str, writer: &mut dyn Write, ) -> Option<Result<u64>>

Streams the asset bytes at asset_id to writer.

Strips the zip:// prefix, acquires the archive mutex, locates the ZIP entry, and copies bytes via io::copy — never buffers the full asset. Returns:

  • None if asset_id does not start with zip://
  • None if the mutex is poisoned
  • None if the entry is not found in the archive
  • Some(Ok(n)) on success with n bytes written
  • Some(Err(_)) on I/O error during copy

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.