Skip to main content

CreateRequest

Struct CreateRequest 

Source
pub struct CreateRequest<'a> { /* private fields */ }
Expand description

Creates an archive from files and directories.

Holds the CreationConfig and an optional ProgressCallback; the destination is chosen by the terminal method: a file (create), a writer (write) or memory (to_bytes).

§Errors

All terminals return an error if:

  • a source does not exist or cannot be read
  • the configuration is invalid
  • writing the archive fails

create also returns an error if the output is refused (see OutputFile) or its format cannot be determined. Sources are validated before the output is published, so a failed creation leaves no archive.

§Examples

use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::OutputFile;

let dir = tempfile::tempdir()?;
std::fs::write(dir.path().join("a.txt"), b"hello")?;

let config = CreationConfig::default();
let output = dir.path().join("out.tar.gz");
let report = CreateRequest::new(&config).create(
    &OutputFile::create_new(&output),
    &[dir.path().join("a.txt")],
)?;
assert_eq!(report.files_added, 1);
assert!(output.exists());

let missing = dir.path().join("missing.tar.gz");
assert!(
    CreateRequest::new(&config)
        .create(
            &OutputFile::create_new(&missing),
            &[dir.path().join("nope")]
        )
        .is_err()
);
assert!(!missing.exists());

Implementations§

Source§

impl<'a> CreateRequest<'a>

Source

pub const fn new(config: &'a CreationConfig) -> Self

A creation under config with no progress callback.

Source

pub fn with_progress(self, progress: &'a mut dyn ProgressCallback) -> Self

Reports progress to progress.

Source

pub fn create<P: AsRef<Path>>( self, output: &OutputFile, sources: &[P], ) -> Result<CreationReport>

Creates the archive at output from sources.

CreationReport::bytes_compressed is the size of the archive file.

§Errors

See CreateRequest.

Source

pub fn write<P: AsRef<Path>>( self, writer: ArchiveWriter<'_>, sources: &[P], ) -> Result<CreationReport>

Creates the archive from sources into writer.

The writer fixes the format. CreationReport::bytes_compressed is the number of bytes written to it.

§Errors

See CreateRequest.

§Examples
use exarch_core::ArchiveWriter;
use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::formats::CompressionCodec;

let src = tempfile::tempdir()?;
std::fs::write(src.path().join("a.txt"), b"hello")?;

let mut buf = Vec::new();
let config = CreationConfig::default();
let report = CreateRequest::new(&config).write(
    ArchiveWriter::tar(&mut buf, Some(CompressionCodec::Gzip)),
    &[src.path()],
)?;
assert_eq!(report.bytes_compressed, buf.len() as u64);
Source

pub fn to_bytes<P: AsRef<Path>>( self, format: CreatableFormat, sources: &[P], ) -> Result<(Vec<u8>, CreationReport)>

Creates an archive of format from sources and returns its bytes.

The whole archive is held in memory, so use create for large ones. Because the finished ZIP can be read back, a ZIP result carries the same warning as create when default extraction would refuse an entry as a zip bomb.

§Errors

See CreateRequest.

§Examples
use exarch_core::CreateRequest;
use exarch_core::CreationConfig;
use exarch_core::formats::detect::CreatableFormat;

let src = tempfile::tempdir()?;
std::fs::write(src.path().join("a.txt"), b"hello")?;

let config = CreationConfig::default();
let (bytes, report) =
    CreateRequest::new(&config).to_bytes(CreatableFormat::TarGz, &[src.path()])?;
assert_eq!(report.bytes_compressed, bytes.len() as u64);

Trait Implementations§

Source§

impl<'a> Debug for CreateRequest<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for CreateRequest<'a>

§

impl<'a> !Sync for CreateRequest<'a>

§

impl<'a> !UnwindSafe for CreateRequest<'a>

§

impl<'a> Freeze for CreateRequest<'a>

§

impl<'a> Send for CreateRequest<'a>

§

impl<'a> Unpin for CreateRequest<'a>

§

impl<'a> UnsafeUnpin for CreateRequest<'a>

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.