Skip to main content

CdxWriter

Struct CdxWriter 

Source
pub struct CdxWriter<W: Write + Seek> { /* private fields */ }
Expand description

Writer for creating Codex document archives.

CdxWriter creates properly formatted .cdx files, ensuring the manifest is written first and all required structure is maintained.

§Example

use cdx_core::archive::{CdxWriter, CompressionMethod};

let mut writer = CdxWriter::create("output.cdx")?;

writer.write_manifest(&manifest)?;
writer.write_file("content/document.json", &content, CompressionMethod::Deflate)?;
writer.write_file("metadata/dublin-core.json", &metadata, CompressionMethod::Deflate)?;

writer.finish()?;

Implementations§

Source§

impl CdxWriter<BufWriter<File>>

Source

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

Create a new Codex document at the given file path.

§Errors

Returns an error if the file cannot be created.

Source§

impl CdxWriter<Cursor<Vec<u8>>>

Source

pub fn in_memory() -> Self

Create a new Codex document in memory.

§Panics

This function will not panic in practice, as initializing a ZipWriter on an in-memory buffer cannot fail.

Source§

impl<W: Write + Seek> CdxWriter<W>

Source

pub fn new(writer: W) -> Result<Self>

Create a new writer wrapping any Write + Seek destination.

§Errors

Returns an error if initialization fails.

Source

pub fn write_manifest(&mut self, manifest: &Manifest) -> Result<()>

Write the manifest to the archive.

This must be called before writing any other files, as the manifest must be the first file in the archive per the Codex specification.

§Errors

Returns an error if:

  • Writing fails
  • The manifest has already been written
Source

pub fn write_file( &mut self, path: &str, data: &[u8], compression: CompressionMethod, ) -> Result<()>

Write a file to the archive.

§Errors

Returns an error if:

  • The manifest has not been written yet
  • The path contains traversal patterns (security check)
  • Writing fails
  • A file with the same path already exists
Source

pub fn write_file_hashed( &mut self, path: &str, data: &[u8], compression: CompressionMethod, algorithm: HashAlgorithm, ) -> Result<DocumentId>

Write a file with automatic hash computation.

Returns the computed hash for inclusion in the manifest.

§Errors

Returns an error if writing fails.

Source

pub fn write_phantoms(&mut self, phantoms: &PhantomClusters) -> Result<()>

Write phantom clusters to the archive.

Phantom clusters are stored at phantoms/clusters.json and are not included in the content hash since they exist outside the core content boundary.

§Errors

Returns an error if writing fails.

Source

pub fn add_directory(&mut self, path: &str) -> Result<()>

Start a directory in the archive.

This is optional, as ZIP archives create directories implicitly, but can be useful for clarity.

§Errors

Returns an error if adding the directory fails.

Source

pub fn manifest_written(&self) -> bool

Check if the manifest has been written.

Source

pub fn files_written(&self) -> &[String]

Get the list of files that have been written.

Source

pub fn finish(self) -> Result<W>

Finish writing and close the archive.

§Errors

Returns an error if:

  • The manifest was not written
  • Finalizing the archive fails
Source

pub fn abort(self) -> W

Abort writing and return the underlying writer without finalizing.

The resulting archive will be invalid.

§Panics

Panics if the ZIP finalization fails, which should not happen for valid writer implementations.

Auto Trait Implementations§

§

impl<W> Freeze for CdxWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for CdxWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for CdxWriter<W>
where W: Send,

§

impl<W> Sync for CdxWriter<W>
where W: Sync,

§

impl<W> Unpin for CdxWriter<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for CdxWriter<W>
where W: UnsafeUnpin,

§

impl<W> !UnwindSafe for CdxWriter<W>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more