From 1852e96192927e5cdc873eade213a69e08739c1f Mon Sep 17 00:00:00 2001 From: Allison Lo <117852723+allilo1@users.noreply.github.com> Date: Fri, 3 May 2024 17:19:30 -0700 Subject: [PATCH 1/3] Prelim changes to write_dir Remove old code and fix match statement Edit arg names and use PathBuf and Path Fix path ordering Fix enum names Add clap as a dev dependency Pin clap version --- Cargo.toml | 1 + examples/write_dir.rs | 127 ++++++++++++++++++++++++++---------------- 2 files changed, 81 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 510df9ca2..ce9aea7b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ bencher = "0.1.5" getrandom = "0.2.5" walkdir = "2.3.2" time = { version = "0.3.7", features = ["formatting", "macros"] } +clap = { version = "=4.4.18", features = ["derive"] } [features] aes-crypto = [ "aes", "constant_time_eq", "hmac", "pbkdf2", "sha1" ] diff --git a/examples/write_dir.rs b/examples/write_dir.rs index 3b043528f..9124438fd 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -1,3 +1,4 @@ +use clap::{Parser, ValueEnum}; use std::io::prelude::*; use std::io::{Seek, Write}; use std::iter::Iterator; @@ -5,58 +6,90 @@ use zip::result::ZipError; use zip::write::FileOptions; use std::fs::File; -use std::path::Path; +use std::path::{Path, PathBuf}; use walkdir::{DirEntry, WalkDir}; -fn main() { - std::process::exit(real_main()); +#[derive(Parser)] +#[command(about, long_about = None)] +struct Args { + // Source directory + source: PathBuf, + // Destination zipfile + destination: PathBuf, + // Compression method + #[arg(value_enum)] + compression_method: CompressionMethod, } -const METHOD_STORED: Option = Some(zip::CompressionMethod::Stored); - -#[cfg(any( - feature = "deflate", - feature = "deflate-miniz", - feature = "deflate-zlib" -))] -const METHOD_DEFLATED: Option = Some(zip::CompressionMethod::Deflated); -#[cfg(not(any( - feature = "deflate", - feature = "deflate-miniz", - feature = "deflate-zlib" -)))] -const METHOD_DEFLATED: Option = None; - -#[cfg(feature = "bzip2")] -const METHOD_BZIP2: Option = Some(zip::CompressionMethod::Bzip2); -#[cfg(not(feature = "bzip2"))] -const METHOD_BZIP2: Option = None; +#[derive(Clone, ValueEnum)] +enum CompressionMethod { + Stored, + Deflated, + DeflatedMiniz, + DeflatedZlib, + Bzip2, + Zstd, +} -#[cfg(feature = "zstd")] -const METHOD_ZSTD: Option = Some(zip::CompressionMethod::Zstd); -#[cfg(not(feature = "zstd"))] -const METHOD_ZSTD: Option = None; +fn main() { + std::process::exit(real_main()); +} fn real_main() -> i32 { - let args: Vec<_> = std::env::args().collect(); - if args.len() < 3 { - println!( - "Usage: {} ", - args[0] - ); - return 1; - } - - let src_dir = &*args[1]; - let dst_file = &*args[2]; - for &method in [METHOD_STORED, METHOD_DEFLATED, METHOD_BZIP2, METHOD_ZSTD].iter() { - if method.is_none() { - continue; - } - match doit(src_dir, dst_file, method.unwrap()) { - Ok(_) => println!("done: {src_dir} written to {dst_file}"), - Err(e) => println!("Error: {e:?}"), + let args = Args::parse(); + let src_dir = &args.source; + let dst_file = &args.destination; + let method = match args.compression_method { + CompressionMethod::Stored => zip::CompressionMethod::Stored, + CompressionMethod::Deflated => { + #[cfg(not(feature = "deflate"))] + { + println!("The `deflate` feature is not enabled"); + return 1; + } + #[cfg(feature = "deflate")] + zip::CompressionMethod::Deflated + }, + CompressionMethod::DeflatedMiniz => { + #[cfg(not(feature = "deflate-miniz"))] + { + println!("The `deflate-miniz` feature is not enabled"); + return 1; + } + #[cfg(feature = "deflate-miniz")] + zip::CompressionMethod::Deflated + }, + CompressionMethod::DeflatedZlib => { + #[cfg(not(feature = "deflate-zlib"))] + { + println!("The `deflate-zlib` feature is not enabled"); + return 1; + } + #[cfg(feature = "deflate-zlib")] + zip::CompressionMethod::Deflated + }, + CompressionMethod::Bzip2 => { + #[cfg(not(feature = "bzip2"))] + { + println!("The `bzip2` feature is not enabled"); + return 1; + } + #[cfg(feature = "bzip2")] + zip::CompressionMethod::Bzip2 + }, + CompressionMethod::Zstd => { + #[cfg(not(feature = "zstd"))] + { + println!("The `zstd` feature is not enabled"); + return 1; + } + #[cfg(feature = "zstd")] + zip::CompressionMethod::Zstd } + }; + match doit(src_dir, dst_file, method) { + Ok(_) => println!("done: {:?} written to {:?}", src_dir, dst_file), + Err(e) => println!("Error: {e:?}"), } 0 @@ -64,7 +97,7 @@ fn real_main() -> i32 { fn zip_dir( it: &mut dyn Iterator, - prefix: &str, + prefix: &Path, writer: T, method: zip::CompressionMethod, ) -> zip::result::ZipResult<()> @@ -105,8 +138,8 @@ where } fn doit( - src_dir: &str, - dst_file: &str, + src_dir: &Path, + dst_file: &Path, method: zip::CompressionMethod, ) -> zip::result::ZipResult<()> { if !Path::new(src_dir).is_dir() { From de95acc543d0f77ea13e860c5713941e2490c16c Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Fri, 3 May 2024 19:49:33 -0700 Subject: [PATCH 2/3] style: allow conditionally-unused variables in write_dir.rs --- examples/write_dir.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/write_dir.rs b/examples/write_dir.rs index ead79f8fe..32279b556 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -1,3 +1,4 @@ +#![allow(unused_variables)] use anyhow::Context; use clap::{Parser, ValueEnum}; use std::io::prelude::*; From 3ccaa3cc857ae01f0c0f93b6c4862a0add8b5674 Mon Sep 17 00:00:00 2001 From: Chris Hennick <4961925+Pr0methean@users.noreply.github.com> Date: Fri, 3 May 2024 19:51:23 -0700 Subject: [PATCH 3/3] style: cargo fmt --all & `#![allow(dead_code)]` --- examples/write_dir.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/write_dir.rs b/examples/write_dir.rs index 32279b556..d9cd7342b 100644 --- a/examples/write_dir.rs +++ b/examples/write_dir.rs @@ -1,4 +1,5 @@ #![allow(unused_variables)] +#![allow(dead_code)] use anyhow::Context; use clap::{Parser, ValueEnum}; use std::io::prelude::*; @@ -65,7 +66,7 @@ fn real_main() -> i32 { } #[cfg(feature = "deflate")] zip::CompressionMethod::Deflated - }, + } CompressionMethod::DeflatedMiniz => { #[cfg(not(feature = "deflate-miniz"))] { @@ -74,7 +75,7 @@ fn real_main() -> i32 { } #[cfg(feature = "deflate-miniz")] zip::CompressionMethod::Deflated - }, + } CompressionMethod::DeflatedZlib => { #[cfg(not(feature = "deflate-zlib"))] { @@ -83,7 +84,7 @@ fn real_main() -> i32 { } #[cfg(feature = "deflate-zlib")] zip::CompressionMethod::Deflated - }, + } CompressionMethod::Bzip2 => { #[cfg(not(feature = "bzip2"))] { @@ -92,7 +93,7 @@ fn real_main() -> i32 { } #[cfg(feature = "bzip2")] zip::CompressionMethod::Bzip2 - }, + } CompressionMethod::Zstd => { #[cfg(not(feature = "zstd"))] {