Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: separate compression into cargo features #47

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ default = ["remote"]
toml-serde = ["toml", "serde"]
json-serde = ["serde_json", "serde"]
remote = ["reqwest"]
compression = ["compression-tar", "compression-zip"]
compression-tar = ["flate2", "tar", "xz2"]
compression-zip = ["zip"]

[dependencies]
image = "0.24.5"
Expand All @@ -18,15 +21,15 @@ reqwest = { version = "0.11.13", optional = true, features = ["json"] }
thiserror = "1.0.37"
url = "2.3.1"
miette = "5.6.0"
camino = "1.1.4"

toml = { version = "0.5.9", optional = true }
serde_json = { version = "1.0.95", optional = true }
serde = { version = "1.0.159", optional = true, features = ["derive"] }
camino = "1.1.4"
tar = "0.4.38"
zip = "0.6.4"
flate2 = "1.0.25"
xz2 = "0.1.7"
tar = { version = "0.4.38", optional = true }
zip = { version = "0.6.4", optional = true }
flate2 = { version = "1.0.25", optional = true }
xz2 = { version = "0.1.7", optional = true }

[dev-dependencies]
assert_fs = "1"
Expand Down
29 changes: 17 additions & 12 deletions src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

use crate::error::*;
use camino::Utf8Path;
use flate2::{write::ZlibEncoder, Compression, GzBuilder};
use std::{
fs::{self, DirEntry},
io::BufReader,
};
use xz2::write::XzEncoder;
use zip::ZipWriter;
use std::fs;

/// Internal tar-file compression algorithms
#[cfg(feature = "compression-tar")]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum CompressionImpl {
/// .gz
Expand All @@ -21,11 +16,15 @@ pub(crate) enum CompressionImpl {
Zstd,
}

#[cfg(feature = "compression-tar")]
pub(crate) fn tar_dir(
src_path: &Utf8Path,
dest_path: &Utf8Path,
compression: &CompressionImpl,
) -> Result<()> {
use flate2::{write::ZlibEncoder, Compression, GzBuilder};
use xz2::write::XzEncoder;

// Set up the archive/compression
// The contents of the zip (e.g. a tar)
let dir_name = src_path.file_name().unwrap();
Expand Down Expand Up @@ -154,7 +153,10 @@ pub(crate) fn tar_dir(
Ok(())
}

#[cfg(feature = "compression-zip")]
pub(crate) fn zip_dir(src_path: &Utf8Path, dest_path: &Utf8Path) -> Result<()> {
use zip::ZipWriter;

// Set up the archive/compression
let final_zip_file = match fs::File::create(dest_path) {
Ok(file) => file,
Expand Down Expand Up @@ -204,21 +206,24 @@ pub(crate) fn zip_dir(src_path: &Utf8Path, dest_path: &Utf8Path) -> Result<()> {

/// Copies a file into a provided `ZipWriter`. Mostly factored out so that we can bunch up
/// a bunch of `std::io::Error`s without having to individually handle them.
#[cfg(feature = "compression-zip")]
fn copy_into_zip(
entry: std::result::Result<DirEntry, std::io::Error>,
zip: &mut ZipWriter<fs::File>,
entry: std::result::Result<std::fs::DirEntry, std::io::Error>,
zip: &mut zip::ZipWriter<fs::File>,
) -> std::result::Result<(), std::io::Error> {
use std::io::{self, BufReader};
use zip::{write::FileOptions, CompressionMethod};

let entry = entry?;
if entry.file_type()?.is_file() {
let options =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
let options = FileOptions::default().compression_method(CompressionMethod::Stored);
let file = fs::File::open(entry.path())?;
let mut buf = BufReader::new(file);
let file_name = entry.file_name();
// FIXME: ...don't do this lossy conversion?
let utf8_file_name = file_name.to_string_lossy();
zip.start_file(utf8_file_name.clone(), options)?;
std::io::copy(&mut buf, zip)?;
io::copy(&mut buf, zip)?;
} else {
todo!("implement zip subdirs! (or was this a symlink?)");
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use std::path::PathBuf;

#[cfg(any(feature = "compression-zip", feature = "compression-tar"))]
pub(crate) mod compression;
pub(crate) mod error;
pub(crate) mod local;
Expand Down
19 changes: 11 additions & 8 deletions src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::path::{Path, PathBuf};

use camino::{Utf8Path, Utf8PathBuf};

use crate::compression::{tar_dir, zip_dir, CompressionImpl};
use crate::error::*;

/// A local asset contains a path on the local filesystem and its contents
Expand Down Expand Up @@ -278,44 +277,48 @@ impl LocalAsset {
}

/// Creates a new .tar.gz file from a provided directory
#[cfg(any(feature = "compression", feature = "compression-tar"))]
pub fn tar_gz_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
crate::compression::tar_dir(
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Gzip,
&crate::compression::CompressionImpl::Gzip,
)
}

/// Creates a new .tar.xz file from a provided directory
#[cfg(any(feature = "compression", feature = "compression-tar"))]
pub fn tar_xz_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
crate::compression::tar_dir(
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Xzip,
&crate::compression::CompressionImpl::Xzip,
)
}

/// Creates a new .tar.zstd file from a provided directory
#[cfg(any(feature = "compression", feature = "compression-tar"))]
pub fn tar_zstd_dir(
origin_dir: impl AsRef<Utf8Path>,
dest_dir: impl AsRef<Utf8Path>,
) -> Result<()> {
tar_dir(
crate::compression::tar_dir(
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
&CompressionImpl::Zstd,
&crate::compression::CompressionImpl::Zstd,
)
}

/// Creates a new .zip file from a provided directory
#[cfg(any(feature = "compression", feature = "compression-zip"))]
pub fn zip_dir(origin_dir: impl AsRef<Utf8Path>, dest_dir: impl AsRef<Utf8Path>) -> Result<()> {
zip_dir(
crate::compression::zip_dir(
Utf8Path::new(origin_dir.as_ref()),
Utf8Path::new(dest_dir.as_ref()),
)
Expand Down