···67676868 /// Compress an input file or pipe to a gzip archive
6969 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result {
7070- if let CmprssOutput::Path(out_path) = &output {
7171- if out_path.is_dir() {
7272- bail!(
7373- "Gzip does not support compressing to a directory. Please specify an output file."
7474- );
7575- }
7070+ if let CmprssOutput::Path(out_path) = &output
7171+ && out_path.is_dir()
7272+ {
7373+ bail!(
7474+ "Gzip does not support compressing to a directory. Please specify an output file."
7575+ );
7676 }
7777 if let CmprssInput::Path(input_paths) = &input {
7878 for x in input_paths {
+10-12
src/backends/lz4.rs
···41414242 /// Compress an input file or pipe to a lz4 archive
4343 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result {
4444- if let CmprssOutput::Path(out_path) = &output {
4545- if out_path.is_dir() {
4646- bail!(
4747- "LZ4 does not support compressing to a directory. Please specify an output file."
4848- );
4949- }
4444+ if let CmprssOutput::Path(out_path) = &output
4545+ && out_path.is_dir()
4646+ {
4747+ bail!(
4848+ "LZ4 does not support compressing to a directory. Please specify an output file."
4949+ );
5050 }
5151 if let CmprssInput::Path(input_paths) = &input {
5252 for x in input_paths {
···98989999 /// Extract a lz4 archive to an output file or pipe
100100 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result {
101101- if let CmprssOutput::Path(out_path) = &output {
102102- if out_path.is_dir() {
103103- bail!(
104104- "LZ4 does not support extracting to a directory. Please specify an output file."
105105- );
106106- }
101101+ if let CmprssOutput::Path(out_path) = &output
102102+ && out_path.is_dir()
103103+ {
104104+ bail!("LZ4 does not support extracting to a directory. Please specify an output file.");
107105 }
108106109107 let mut file_size = None;
+12-12
src/backends/zstd.rs
···86868787 /// Compress an input file or pipe to a zstd archive
8888 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result {
8989- if let CmprssOutput::Path(out_path) = &output {
9090- if out_path.is_dir() {
9191- bail!(
9292- "Zstd does not support compressing to a directory. Please specify an output file."
9393- );
9494- }
8989+ if let CmprssOutput::Path(out_path) = &output
9090+ && out_path.is_dir()
9191+ {
9292+ bail!(
9393+ "Zstd does not support compressing to a directory. Please specify an output file."
9494+ );
9595 }
9696 if let CmprssInput::Path(input_paths) = &input {
9797 for x in input_paths {
···143143144144 /// Extract a zstd archive to an output file or pipe
145145 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result {
146146- if let CmprssOutput::Path(out_path) = &output {
147147- if out_path.is_dir() {
148148- bail!(
149149- "Zstd does not support extracting to a directory. Please specify an output file."
150150- );
151151- }
146146+ if let CmprssOutput::Path(out_path) = &output
147147+ && out_path.is_dir()
148148+ {
149149+ bail!(
150150+ "Zstd does not support extracting to a directory. Please specify an output file."
151151+ );
152152 }
153153154154 let mut file_size = None;
+49-49
src/main.rs
···134134 // Check if output is a directory - this is likely an extraction
135135 if output.is_dir() {
136136 // Try to determine compressor from the input file's extension(s)
137137- if let Some(input_path) = input.first() {
138138- if let Some(guessed_compressor) = get_compressor_from_filename(input_path) {
139139- return (Some(guessed_compressor), Action::Extract);
140140- }
137137+ if let Some(input_path) = input.first()
138138+ && let Some(guessed_compressor) = get_compressor_from_filename(input_path)
139139+ {
140140+ return (Some(guessed_compressor), Action::Extract);
141141 }
142142 }
143143···247247248248 // Process the io_list, check if there is an output first
249249 let mut io_list = common_args.io_list.clone();
250250- if output.is_none() {
251251- if let Some(possible_output) = common_args.io_list.last() {
252252- let path = Path::new(possible_output);
253253- if !path.try_exists()? {
254254- // Use the given path if it doesn't exist
255255- output = Some(path);
256256- io_list.pop();
257257- } else if path.is_dir() {
258258- match action {
259259- Action::Compress => {
260260- // A directory can potentially be a target output location or
261261- // an input, for now assume it is an input.
262262- }
263263- Action::Extract => {
264264- // Can extract to a directory, and it wouldn't make any sense as an input
265265- output = Some(path);
266266- io_list.pop();
267267- }
268268- _ => {
269269- // TODO: don't know if this is an input or output, assume we're compressing this directory
270270- // This does cause problems for inferencing "cat archive.tar | cmprss tar ."
271271- // Probably need to add some special casing
272272- }
273273- };
274274- } else {
275275- // TODO: check for scenarios where we want to append to an existing archive
276276- }
250250+ if output.is_none()
251251+ && let Some(possible_output) = common_args.io_list.last()
252252+ {
253253+ let path = Path::new(possible_output);
254254+ if !path.try_exists()? {
255255+ // Use the given path if it doesn't exist
256256+ output = Some(path);
257257+ io_list.pop();
258258+ } else if path.is_dir() {
259259+ match action {
260260+ Action::Compress => {
261261+ // A directory can potentially be a target output location or
262262+ // an input, for now assume it is an input.
263263+ }
264264+ Action::Extract => {
265265+ // Can extract to a directory, and it wouldn't make any sense as an input
266266+ output = Some(path);
267267+ io_list.pop();
268268+ }
269269+ _ => {
270270+ // TODO: don't know if this is an input or output, assume we're compressing this directory
271271+ // This does cause problems for inferencing "cat archive.tar | cmprss tar ."
272272+ // Probably need to add some special casing
273273+ }
274274+ };
275275+ } else {
276276+ // TODO: check for scenarios where we want to append to an existing archive
277277 }
278278 }
279279···498498 Ok(())
499499}
500500501501+fn main() {
502502+ let args = CmprssArgs::parse();
503503+ match args.format {
504504+ Some(Format::Tar(a)) => command(Some(Box::new(Tar::new(&a))), &a.common_args),
505505+ Some(Format::Gzip(a)) => command(Some(Box::new(Gzip::new(&a))), &a.common_args),
506506+ Some(Format::Xz(a)) => command(Some(Box::new(Xz::new(&a))), &a.common_args),
507507+ Some(Format::Bzip2(a)) => command(Some(Box::new(Bzip2::new(&a))), &a.common_args),
508508+ Some(Format::Zip(a)) => command(Some(Box::new(Zip::new(&a))), &a.common_args),
509509+ Some(Format::Zstd(a)) => command(Some(Box::new(Zstd::new(&a))), &a.common_args),
510510+ Some(Format::Lz4(a)) => command(Some(Box::new(Lz4::new(&a))), &a.common_args),
511511+ _ => command(None, &args.base_args),
512512+ }
513513+ .unwrap_or_else(|e| {
514514+ eprintln!("ERROR(cmprss): {}", e);
515515+ std::process::exit(1);
516516+ });
517517+}
518518+501519#[cfg(test)]
502520mod tests {
503521 use super::*;
···601619 assert_eq!(compressor_extension("file.tar"), Some("tar".into()));
602620 }
603621}
604604-605605-fn main() {
606606- let args = CmprssArgs::parse();
607607- match args.format {
608608- Some(Format::Tar(a)) => command(Some(Box::new(Tar::new(&a))), &a.common_args),
609609- Some(Format::Gzip(a)) => command(Some(Box::new(Gzip::new(&a))), &a.common_args),
610610- Some(Format::Xz(a)) => command(Some(Box::new(Xz::new(&a))), &a.common_args),
611611- Some(Format::Bzip2(a)) => command(Some(Box::new(Bzip2::new(&a))), &a.common_args),
612612- Some(Format::Zip(a)) => command(Some(Box::new(Zip::new(&a))), &a.common_args),
613613- Some(Format::Zstd(a)) => command(Some(Box::new(Zstd::new(&a))), &a.common_args),
614614- Some(Format::Lz4(a)) => command(Some(Box::new(Lz4::new(&a))), &a.common_args),
615615- _ => command(None, &args.base_args),
616616- }
617617- .unwrap_or_else(|e| {
618618- eprintln!("ERROR(cmprss): {}", e);
619619- std::process::exit(1);
620620- });
621621-}
+6-8
src/utils.rs
···205205 // If the file has no extension, return the current directory
206206 if let Some(ext) = in_path.extension() {
207207 // If the file has the extension for this type, return the filename without the extension
208208- if let Some(ext_str) = ext.to_str() {
209209- if ext_str == self.extension() {
210210- if let Some(stem) = in_path.file_stem() {
211211- if let Some(stem_str) = stem.to_str() {
212212- return stem_str.to_string();
213213- }
214214- }
215215- }
208208+ if let Some(ext_str) = ext.to_str()
209209+ && ext_str == self.extension()
210210+ && let Some(stem) = in_path.file_stem()
211211+ && let Some(stem_str) = stem.to_str()
212212+ {
213213+ return stem_str.to_string();
216214 }
217215 }
218216 "archive".to_string()