···6868 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> {
6969 if let CmprssOutput::Path(out_path) = &output {
7070 if out_path.is_dir() {
7171- return cmprss_error("Gzip does not support compressing to a directory. Please specify an output file.");
7171+ return cmprss_error(
7272+ "Gzip does not support compressing to a directory. Please specify an output file.",
7373+ );
7274 }
7375 }
7476 if let CmprssInput::Path(input_paths) = &input {
+6-2
src/backends/lz4.rs
···4242 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> {
4343 if let CmprssOutput::Path(out_path) = &output {
4444 if out_path.is_dir() {
4545- return cmprss_error("LZ4 does not support compressing to a directory. Please specify an output file.");
4545+ return cmprss_error(
4646+ "LZ4 does not support compressing to a directory. Please specify an output file.",
4747+ );
4648 }
4749 }
4850 if let CmprssInput::Path(input_paths) = &input {
···100102 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> {
101103 if let CmprssOutput::Path(out_path) = &output {
102104 if out_path.is_dir() {
103103- return cmprss_error("LZ4 does not support extracting to a directory. Please specify an output file.");
105105+ return cmprss_error(
106106+ "LZ4 does not support extracting to a directory. Please specify an output file.",
107107+ );
104108 }
105109 }
106110
+12-12
src/backends/pipeline.rs
···251251 last_compressor.compress(current_thread_input, output)?;
252252253253 for handle in handles {
254254- handle.join().map_err(|_| {
255255- io::Error::new(io::ErrorKind::Other, "Compression thread panicked")
256256- })??;
254254+ handle
255255+ .join()
256256+ .map_err(|_| io::Error::other("Compression thread panicked"))??;
257257 }
258258 Ok(())
259259 }
···302302303303 let final_output = match output {
304304 CmprssOutput::Path(ref p) => {
305305- if last_extractor.default_extracted_target() == ExtractedTarget::DIRECTORY {
306306- if !p.exists() {
307307- std::fs::create_dir_all(p)?;
308308- }
309309- // If it's a directory, the tar extractor (usually innermost) will handle it.
310310- // The path provided should be the target directory.
305305+ if last_extractor.default_extracted_target() == ExtractedTarget::DIRECTORY
306306+ && !p.exists()
307307+ {
308308+ std::fs::create_dir_all(p)?;
311309 }
310310+ // If it's a directory, the tar extractor (usually innermost) will handle it.
311311+ // The path provided should be the target directory.
312312 // Always pass the path; the backend decides how to use it.
313313 CmprssOutput::Path(p.clone())
314314 }
···319319 last_extractor.extract(current_thread_input, final_output)?;
320320321321 for handle in handles {
322322- handle.join().map_err(|_| {
323323- io::Error::new(io::ErrorKind::Other, "Extraction thread panicked")
324324- })??;
322322+ handle
323323+ .join()
324324+ .map_err(|_| io::Error::other("Extraction thread panicked"))??;
325325 }
326326 Ok(())
327327 }
+5-11
src/backends/zip.rs
···117117 }
118118 let file = File::open(&paths[0])?;
119119 let mut archive = ZipArchive::new(file)?;
120120- archive
121121- .extract(out_dir)
122122- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))
120120+ archive.extract(out_dir).map_err(io::Error::other)
123121 }
124122 CmprssInput::Pipe(mut pipe) => {
125123 // Create a temporary file to store the zip content
···133131134132 // Extract from the temporary file
135133 let mut archive = ZipArchive::new(temp_file)?;
136136- archive
137137- .extract(out_dir)
138138- .map_err(|e| io::Error::new(io::ErrorKind::Other, e))
139139- }
140140- CmprssInput::Reader(_) => {
141141- return cmprss_error(
142142- "Cannot extract from a reader input for zip (requires seekable input)",
143143- );
134134+ archive.extract(out_dir).map_err(io::Error::other)
144135 }
136136+ CmprssInput::Reader(_) => cmprss_error(
137137+ "Cannot extract from a reader input for zip (requires seekable input)",
138138+ ),
145139 }
146140 }
147141 CmprssOutput::Pipe(_) => cmprss_error("zip extraction to stdout is not supported"),
+6-2
src/backends/zstd.rs
···9090 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> {
9191 if let CmprssOutput::Path(out_path) = &output {
9292 if out_path.is_dir() {
9393- return cmprss_error("Zstd does not support compressing to a directory. Please specify an output file.");
9393+ return cmprss_error(
9494+ "Zstd does not support compressing to a directory. Please specify an output file.",
9595+ );
9496 }
9597 }
9698 if let CmprssInput::Path(input_paths) = &input {
···148150 fn extract(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> {
149151 if let CmprssOutput::Path(out_path) = &output {
150152 if out_path.is_dir() {
151151- return cmprss_error("Zstd does not support extracting to a directory. Please specify an output file.");
153153+ return cmprss_error(
154154+ "Zstd does not support extracting to a directory. Please specify an output file.",
155155+ );
152156 }
153157 }
154158
+69-123
src/main.rs
···5555 match input {
5656 CmprssInput::Path(paths) => match paths.first() {
5757 Some(path) => Ok(path),
5858- None => Err(io::Error::new(
5959- io::ErrorKind::Other,
6060- "error: no input specified",
6161- )),
5858+ None => Err(io::Error::other("error: no input specified")),
6259 },
6360 CmprssInput::Pipe(_) => Ok(Path::new("archive")),
6461 CmprssInput::Reader(_) => Ok(Path::new("piped_data")),
···234231 match get_path(in_file) {
235232 Some(path) => inputs.push(path),
236233 None => {
237237- return Err(io::Error::new(
238238- io::ErrorKind::Other,
239239- "Specified input path does not exist",
240240- ));
234234+ return Err(io::Error::other("Specified input path does not exist"));
241235 }
242236 }
243237 }
···247241 let path = Path::new(output);
248242 if path.try_exists()? && !path.is_dir() {
249243 // Output path exists, bail out
250250- return Err(io::Error::new(
251251- io::ErrorKind::Other,
252252- "Specified output path already exists",
253253- ));
244244+ return Err(io::Error::other("Specified output path already exists"));
254245 }
255246 Some(path)
256247 }
···295286 if let Some(path) = get_path(input) {
296287 inputs.push(path);
297288 } else {
298298- return Err(io::Error::new(
299299- io::ErrorKind::Other,
300300- "Specified input path does not exist",
301301- ));
289289+ return Err(io::Error::other("Specified input path does not exist"));
302290 }
303291 }
304292···311299 {
312300 CmprssInput::Pipe(std::io::stdin())
313301 } else {
314314- return Err(io::Error::new(io::ErrorKind::Other, "No specified input"));
302302+ return Err(io::Error::other("No specified input"));
315303 }
316304 }
317305 false => CmprssInput::Path(inputs),
···328316 } else {
329317 match action {
330318 Action::Compress => {
331331- if compressor.is_none() {
332332- return Err(io::Error::new(
333333- io::ErrorKind::Other,
334334- "Must specify a compressor",
335335- ));
336336- }
319319+ let c = compressor
320320+ .as_ref()
321321+ .ok_or_else(|| io::Error::other("Must specify a compressor"))?;
337322 CmprssOutput::Path(PathBuf::from(
338338- compressor
339339- .as_ref()
340340- .unwrap()
341341- .default_compressed_filename(get_input_filename(&cmprss_input)?),
323323+ c.default_compressed_filename(get_input_filename(&cmprss_input)?),
342324 ))
343325 }
344326 Action::Extract => {
345327 if compressor.is_none() {
346328 compressor =
347329 get_compressor_from_filename(get_input_filename(&cmprss_input)?);
348348- if compressor.is_none() {
349349- return Err(io::Error::new(
350350- io::ErrorKind::Other,
351351- "Must specify a compressor",
352352- ));
353353- }
354330 }
331331+ let c = compressor
332332+ .as_ref()
333333+ .ok_or_else(|| io::Error::other("Must specify a compressor"))?;
355334 CmprssOutput::Path(PathBuf::from(
356356- compressor
357357- .as_ref()
358358- .unwrap()
359359- .default_extracted_filename(get_input_filename(&cmprss_input)?),
335335+ c.default_extracted_filename(get_input_filename(&cmprss_input)?),
360336 ))
361337 }
362338 Action::Unknown => {
363363- if compressor.is_none() {
364364- // Can still work if the input is an archive
365365- compressor =
366366- get_compressor_from_filename(get_input_filename(&cmprss_input)?);
367367- if compressor.is_none() {
368368- return Err(io::Error::new(
369369- io::ErrorKind::Other,
370370- "Must specify a compressor",
371371- ));
372372- }
373373- action = Action::Extract;
374374- CmprssOutput::Path(PathBuf::from(
375375- compressor
376376- .as_ref()
377377- .unwrap()
378378- .default_extracted_filename(get_input_filename(&cmprss_input)?),
379379- ))
380380- } else {
339339+ if let Some(ref c) = compressor {
381340 // We know the compressor, does the input have the same extension?
382341 if let Some(compressor_from_input) =
383342 get_compressor_from_filename(get_input_filename(&cmprss_input)?)
384343 {
385385- if compressor.as_ref().unwrap().name()
386386- == compressor_from_input.name()
387387- {
344344+ if c.name() == compressor_from_input.name() {
388345 action = Action::Extract;
389389- CmprssOutput::Path(PathBuf::from(
390390- compressor.as_ref().unwrap().default_extracted_filename(
391391- get_input_filename(&cmprss_input)?,
392392- ),
393393- ))
346346+ CmprssOutput::Path(PathBuf::from(c.default_extracted_filename(
347347+ get_input_filename(&cmprss_input)?,
348348+ )))
394349 } else {
395350 action = Action::Compress;
396351 CmprssOutput::Path(PathBuf::from(
397397- compressor.as_ref().unwrap().default_compressed_filename(
398398- get_input_filename(&cmprss_input)?,
399399- ),
352352+ c.default_compressed_filename(get_input_filename(
353353+ &cmprss_input,
354354+ )?),
400355 ))
401356 }
402357 } else {
403358 action = Action::Compress;
404404- CmprssOutput::Path(PathBuf::from(
405405- compressor.as_ref().unwrap().default_compressed_filename(
406406- get_input_filename(&cmprss_input)?,
407407- ),
408408- ))
359359+ CmprssOutput::Path(PathBuf::from(c.default_compressed_filename(
360360+ get_input_filename(&cmprss_input)?,
361361+ )))
409362 }
363363+ } else {
364364+ // Can still work if the input is an archive
365365+ compressor =
366366+ get_compressor_from_filename(get_input_filename(&cmprss_input)?);
367367+ let c = compressor
368368+ .as_ref()
369369+ .ok_or_else(|| io::Error::other("Must specify a compressor"))?;
370370+ action = Action::Extract;
371371+ CmprssOutput::Path(PathBuf::from(
372372+ c.default_extracted_filename(get_input_filename(&cmprss_input)?),
373373+ ))
410374 }
411375 }
412376 }
···426390 Action::Extract => {
427391 if let CmprssInput::Path(paths) = &cmprss_input {
428392 if paths.len() != 1 {
429429- return Err(io::Error::new(
430430- io::ErrorKind::Other,
431431- "Expected a single archive to extract",
432432- ));
393393+ return Err(io::Error::other("Expected a single archive to extract"));
433394 }
434395 compressor = get_compressor_from_filename(paths.first().unwrap());
435396 }
···441402 action = Action::Extract;
442403443404 if compressor.is_none() {
444444- return Err(io::Error::new(
445445- io::ErrorKind::Other,
446446- format!(
447447- "Couldn't determine how to extract {:?}",
448448- paths.first().unwrap()
449449- ),
450450- ));
405405+ return Err(io::Error::other(format!(
406406+ "Couldn't determine how to extract {:?}",
407407+ paths.first().unwrap()
408408+ )));
451409 }
452410 } else {
453411 let (guessed_compressor, guessed_action) =
···457415 }
458416 }
459417 (CmprssInput::Path(paths), CmprssOutput::Pipe(_)) => {
460460- if compressor.is_none() {
418418+ if let Some(ref c) = compressor {
419419+ if let Some(input_c) = get_compressor_from_filename(paths.first().unwrap())
420420+ {
421421+ if c.name() == input_c.name() {
422422+ action = Action::Extract;
423423+ } else {
424424+ action = Action::Compress;
425425+ }
426426+ } else {
427427+ action = Action::Compress;
428428+ }
429429+ } else {
461430 if paths.len() != 1 {
462462- return Err(io::Error::new(
463463- io::ErrorKind::Other,
431431+ return Err(io::Error::other(
464432 "Expected a single input file for piping to stdout",
465433 ));
466434 }
···468436 if compressor.is_some() {
469437 action = Action::Extract;
470438 } else {
471471- return Err(io::Error::new(
472472- io::ErrorKind::Other,
473473- "Can't guess compressor to use",
474474- ));
475475- }
476476- } else if let Some(c) = get_compressor_from_filename(paths.first().unwrap()) {
477477- if compressor.as_ref().unwrap().name() == c.name() {
478478- action = Action::Extract;
479479- } else {
480480- action = Action::Compress;
439439+ return Err(io::Error::other("Can't guess compressor to use"));
481440 }
482482- } else {
483483- action = Action::Compress;
484441 }
485442 }
486443 (CmprssInput::Pipe(_), CmprssOutput::Path(path)) => {
487487- if compressor.is_none() {
444444+ if let Some(ref c) = compressor {
445445+ if get_compressor_from_filename(path)
446446+ .is_some_and(|pc| c.name() == pc.name())
447447+ {
448448+ action = Action::Compress;
449449+ } else {
450450+ action = Action::Extract;
451451+ }
452452+ } else {
488453 compressor = get_compressor_from_filename(path);
489454 if compressor.is_some() {
490455 action = Action::Compress;
491456 } else {
492492- return Err(io::Error::new(
493493- io::ErrorKind::Other,
494494- "Can't guess compressor to use",
495495- ));
457457+ return Err(io::Error::other("Can't guess compressor to use"));
496458 }
497497- } else if compressor.as_ref().unwrap().name()
498498- == get_compressor_from_filename(path).unwrap().name()
499499- {
500500- action = Action::Compress;
501501- } else {
502502- action = Action::Extract;
503459 }
504460 }
505461 (CmprssInput::Pipe(_), CmprssOutput::Pipe(_)) => {
···520476 }
521477 }
522478523523- if compressor.is_none() {
524524- return Err(io::Error::new(
525525- io::ErrorKind::Other,
526526- "Could not determine compressor to use",
527527- ));
528528- }
479479+ let compressor =
480480+ compressor.ok_or_else(|| io::Error::other("Could not determine compressor to use"))?;
529481 if action == Action::Unknown {
530530- return Err(io::Error::new(
531531- io::ErrorKind::Other,
532532- "Could not determine action to take",
533533- ));
482482+ return Err(io::Error::other("Could not determine action to take"));
534483 }
535484536485 Ok(Job {
537537- compressor: compressor.unwrap(),
486486+ compressor,
538487 input: cmprss_input,
539488 output: cmprss_output,
540489 action,
···548497 Action::Compress => job.compressor.compress(job.input, job.output)?,
549498 Action::Extract => job.compressor.extract(job.input, job.output)?,
550499 _ => {
551551- return Err(io::Error::new(
552552- io::ErrorKind::Other,
553553- "Unknown action requested",
554554- ));
500500+ return Err(io::Error::other("Unknown action requested"));
555501 }
556502 };
557503