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<W: Write + Seek> CdxWriter<W>
impl<W: Write + Seek> CdxWriter<W>
Sourcepub fn new(writer: W) -> Result<Self>
pub fn new(writer: W) -> Result<Self>
Create a new writer wrapping any Write + Seek destination.
§Errors
Returns an error if initialization fails.
Sourcepub fn write_manifest(&mut self, manifest: &Manifest) -> Result<()>
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
Sourcepub fn write_file(
&mut self,
path: &str,
data: &[u8],
compression: CompressionMethod,
) -> Result<()>
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
Sourcepub fn write_file_hashed(
&mut self,
path: &str,
data: &[u8],
compression: CompressionMethod,
algorithm: HashAlgorithm,
) -> Result<DocumentId>
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.
Sourcepub fn write_phantoms(&mut self, phantoms: &PhantomClusters) -> Result<()>
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.
Sourcepub fn add_directory(&mut self, path: &str) -> Result<()>
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.
Sourcepub fn manifest_written(&self) -> bool
pub fn manifest_written(&self) -> bool
Check if the manifest has been written.
Sourcepub fn files_written(&self) -> &[String]
pub fn files_written(&self) -> &[String]
Get the list of files that have been written.