From 755cc2a40d58ed96ef1fe70f9126be060de929e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 25 Mar 2021 20:50:42 -0300 Subject: [PATCH] Refactoring and Clippy warnings --- src/cli.rs | 17 +++--- src/compressors/bzip.rs | 6 +-- src/compressors/gzip.rs | 2 +- src/compressors/lzma.rs | 14 ++--- src/compressors/tar.rs | 38 ++++++-------- src/compressors/zip.rs | 10 ++-- src/evaluator.rs | 113 ++++++++++++++++++---------------------- src/main.rs | 36 +++++-------- src/utils.rs | 5 +- 9 files changed, 107 insertions(+), 134 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 8d355e2..21a68ed 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -33,7 +33,7 @@ pub fn clap_app<'a, 'b>() -> clap::App<'a, 'b> { .after_help( "ouch infers what to based on the extensions of the input files and output file received. Examples: `ouch -i movies.tar.gz classes.zip -o Videos/` in order to decompress files into a folder. - `ouch -i headers/ sources/ Makefile -o my-project.tar.gz` + `ouch -i headers/ sources/ Makefile -o my-project.tar.gz` `ouch -i image{1..50}.jpeg -o images.zip` Please relate any issues or contribute at https://github.com/vrmiguel/ouch") .author("Vinícius R. Miguel") @@ -107,7 +107,7 @@ impl TryFrom> for Command { let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied let output_file_extension = Extension::new(output_file); - + let output_is_compressible = output_file_extension.is_ok(); if output_is_compressible { // The supplied output is compressible, so we'll compress our inputs to it @@ -127,14 +127,15 @@ impl TryFrom> for Command { let input_files = canonical_paths.map(Result::unwrap).collect(); - return Ok(Command { + Ok(Command { kind: CommandKind::Compression(input_files), output: Some(File { path: output_file.into(), contents_in_memory: None, + // extension: output_file_extension.ok(), extension: Some(output_file_extension.unwrap()) }), - }); + }) } else { // Output not supplied @@ -142,14 +143,14 @@ impl TryFrom> for Command { let input_files = process_decompressible_input(input_files)?; - return Ok(Command { + Ok(Command { kind: CommandKind::Decompression(input_files), output: Some(File { path: output_file.into(), contents_in_memory: None, extension: None }) - }); + }) } } else { // else: output file not supplied @@ -157,10 +158,10 @@ impl TryFrom> for Command { // Case 2: error let input_files = process_decompressible_input(input_files)?; - return Ok(Command { + Ok(Command { kind: CommandKind::Decompression(input_files), output: None, - }); + }) } } } diff --git a/src/compressors/bzip.rs b/src/compressors/bzip.rs index 16dd99f..e3854bf 100644 --- a/src/compressors/bzip.rs +++ b/src/compressors/bzip.rs @@ -20,7 +20,7 @@ impl BzipCompressor { }; println!("{}: compressed {:?} into memory ({} bytes)", "info".yellow(), &path, contents.len()); - + Ok(contents) } @@ -34,7 +34,7 @@ impl BzipCompressor { } }; - Ok(Self::compress_bytes(&*bytes)?) + Self::compress_bytes(&*bytes) } fn compress_bytes(bytes: &[u8]) -> OuchResult> { @@ -58,4 +58,4 @@ impl Compressor for BzipCompressor { ), } } -} \ No newline at end of file +} diff --git a/src/compressors/gzip.rs b/src/compressors/gzip.rs index 09bac81..0541946 100644 --- a/src/compressors/gzip.rs +++ b/src/compressors/gzip.rs @@ -42,7 +42,7 @@ impl GzipCompressor { } }; - Ok(Self::compress_bytes(file_contents)?) + Self::compress_bytes(file_contents) } pub fn compress_bytes(bytes_to_compress: Vec) -> OuchResult> { diff --git a/src/compressors/lzma.rs b/src/compressors/lzma.rs index 6c70c7c..be43877 100644 --- a/src/compressors/lzma.rs +++ b/src/compressors/lzma.rs @@ -42,9 +42,9 @@ impl LzmaCompressor { } }; - Ok(Self::compress_bytes(file_contents)?) + Self::compress_bytes(file_contents) } - + pub fn compress_bytes(bytes_to_compress: Vec) -> OuchResult> { let buffer = vec![]; let mut encoder = xz2::write::XzEncoder::new(buffer, 6); @@ -58,12 +58,8 @@ impl Compressor for LzmaCompressor { fn compress(&self, from: Entry) -> OuchResult> { let format = CompressionFormat::Lzma; match from { - Entry::Files(files) => Ok( - Self::compress_files(files, format)? - ), - Entry::InMemory(file) => Ok( - Self::compress_file_in_memory(file)? - ), + Entry::Files(files) => Self::compress_files(files, format), + Entry::InMemory(file) => Self::compress_file_in_memory(file), } } -} \ No newline at end of file +} diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 945f3d3..5088e97 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -4,32 +4,37 @@ use colored::Colorize; use tar::Builder; use walkdir::WalkDir; -use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File}; +use crate::{ + compressors::Compressor, + error::{Error, OuchResult}, + file::File, +}; use super::compressor::Entry; pub struct TarCompressor {} impl TarCompressor { - // TODO: implement this fn make_archive_from_memory(_input: File) -> OuchResult> { - println!("{}: .tar.tar and .zip.tar is currently unimplemented.", "error".red()); + println!( + "{}: .tar.tar and .zip.tar is currently unimplemented.", + "error".red() + ); Err(Error::InvalidZipArchive("")) } fn make_archive_from_files(input_filenames: Vec) -> OuchResult> { - - let change_dir_and_return_parent = |filename: &PathBuf| -> OuchResult { + let change_dir_and_return_parent = |filename: &PathBuf| -> OuchResult { let previous_location = env::current_dir()?; let parent = filename.parent().unwrap(); env::set_current_dir(parent)?; Ok(previous_location) }; - + let buf = Vec::new(); let mut b = Builder::new(buf); - + for filename in input_filenames { let previous_location = change_dir_and_return_parent(&filename)?; // Safe unwrap since this filename came from `fs::canonicalize`. @@ -44,25 +49,16 @@ impl TarCompressor { } env::set_current_dir(previous_location)?; } - + Ok(b.into_inner()?) } } impl Compressor for TarCompressor { fn compress(&self, from: Entry) -> OuchResult> { - match from { - Entry::Files(filenames) => { - Ok( - Self::make_archive_from_files(filenames)? - ) - }, - Entry::InMemory(file) => { - Ok( - Self::make_archive_from_memory(file)? - ) - } - } + Entry::Files(filenames) => Self::make_archive_from_files(filenames), + Entry::InMemory(file) => Self::make_archive_from_memory(file), + } } -} \ No newline at end of file +} diff --git a/src/compressors/zip.rs b/src/compressors/zip.rs index 4b4dff8..bd851f7 100644 --- a/src/compressors/zip.rs +++ b/src/compressors/zip.rs @@ -42,10 +42,10 @@ impl ZipCompressor { } }; - writer.write(&*input_bytes)?; + writer.write_all(&*input_bytes)?; + - let bytes = writer.finish().unwrap(); Ok(bytes.into_inner()) @@ -67,14 +67,14 @@ impl ZipCompressor { } writer .start_file( - entry_path.to_string_lossy(), + entry_path.to_string_lossy(), options )?; let file_bytes = std::fs::read(entry.path())?; - writer.write(&*file_bytes)?; + writer.write_all(&*file_bytes)?; } } - + let bytes = writer.finish().unwrap(); diff --git a/src/evaluator.rs b/src/evaluator.rs index d87c684..58cd544 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -3,29 +3,15 @@ use std::{ffi::OsStr, fs, io::Write, path::PathBuf}; use colored::Colorize; use crate::compressors::{ - Entry, - Compressor, - TarCompressor, - ZipCompressor, - GzipCompressor, - BzipCompressor, - LzmaCompressor + BzipCompressor, Compressor, Entry, GzipCompressor, LzmaCompressor, TarCompressor, ZipCompressor, }; use crate::decompressors::{ - Decompressor, - TarDecompressor, - ZipDecompressor, - GzipDecompressor, - BzipDecompressor, - LzmaDecompressor, - DecompressionResult + BzipDecompressor, DecompressionResult, Decompressor, GzipDecompressor, LzmaDecompressor, + TarDecompressor, ZipDecompressor, }; -use crate::extension::{ - Extension, - CompressionFormat -}; +use crate::extension::{CompressionFormat, Extension}; use crate::cli::{Command, CommandKind}; @@ -41,19 +27,21 @@ impl Evaluator { pub fn get_compressor( file: &File, ) -> error::OuchResult<(Option>, Box)> { - if file.extension.is_none() { - // This block *should* be unreachable - eprintln!( - "{}: reached Evaluator::get_decompressor without known extension.", - "internal error".red() - ); - return Err(error::Error::InvalidInput); - } - let extension = file.extension.clone().unwrap(); - + let extension = match &file.extension { + Some(extension) => extension.clone(), + None => { + // This block *should* be unreachable + eprintln!( + "{}: reached Evaluator::get_decompressor without known extension.", + "internal error".red() + ); + return Err(error::Error::InvalidInput); + } + }; + // Supported first compressors: // .tar and .zip - let first_compressor: Option> = match extension.first_ext { + let first_compressor: Option> = match extension.first_ext { Some(ext) => match ext { CompressionFormat::Tar => Some(Box::new(TarCompressor {})), @@ -70,8 +58,8 @@ impl Evaluator { // Supported second compressors: // any let second_compressor: Box = match extension.second_ext { - CompressionFormat::Tar => Box::new(TarCompressor {}), - CompressionFormat::Zip => Box::new(ZipCompressor {}), + CompressionFormat::Tar => Box::new(TarCompressor {}), + CompressionFormat::Zip => Box::new(ZipCompressor {}), CompressionFormat::Bzip => Box::new(BzipCompressor {}), CompressionFormat::Gzip => Box::new(GzipCompressor {}), CompressionFormat::Lzma => Box::new(LzmaCompressor {}), @@ -83,15 +71,17 @@ impl Evaluator { pub fn get_decompressor( file: &File, ) -> error::OuchResult<(Option>, Box)> { - if file.extension.is_none() { - // This block *should* be unreachable - eprintln!( - "{}: reached Evaluator::get_decompressor without known extension.", - "internal error".red() - ); - return Err(error::Error::InvalidInput); - } - let extension = file.extension.clone().unwrap(); + let extension = match &file.extension { + Some(extension) => extension.clone(), + None => { + // This block *should* be unreachable + eprintln!( + "{}: reached Evaluator::get_decompressor without known extension.", + "internal error".red() + ); + return Err(error::Error::InvalidInput); + } + }; let second_decompressor: Box = match extension.second_ext { CompressionFormat::Tar => Box::new(TarDecompressor {}), @@ -102,8 +92,7 @@ impl Evaluator { CompressionFormat::Lzma => Box::new(LzmaDecompressor {}), - CompressionFormat::Bzip => Box::new(BzipDecompressor {}) - + CompressionFormat::Bzip => Box::new(BzipDecompressor {}), }; let first_decompressor: Option> = match extension.first_ext { @@ -132,7 +121,7 @@ impl Evaluator { let mut filename = file_path .file_stem() - .unwrap_or(output_file_path.as_os_str()); + .unwrap_or_else(|| output_file_path.as_os_str()); if filename == "." { // I believe this is only possible when the supplied inout has a name // of the sort `.tar` or `.zip' and no output has been supplied. @@ -141,16 +130,20 @@ impl Evaluator { let filename = PathBuf::from(filename); - if decompressor.is_none() { - // There is no more processing to be done on the input file (or there is but currently unsupported) - // Therefore, we'll save what we have in memory into a file. + // If there is a decompressor to use, we'll create a file in-memory and decompress it + let decompressor = match decompressor { + Some(decompressor) => decompressor, + None => { + // There is no more processing to be done on the input file (or there is but currently unsupported) + // Therefore, we'll save what we have in memory into a file. - println!("{}: saving to {:?}.", "info".yellow(), filename); + println!("{}: saving to {:?}.", "info".yellow(), filename); - let mut f = fs::File::create(output_file_path.join(filename))?; - f.write_all(&bytes)?; - return Ok(()); - } + let mut f = fs::File::create(output_file_path.join(filename))?; + f.write_all(&bytes)?; + return Ok(()); + } + }; let file = File { path: filename, @@ -158,10 +151,6 @@ impl Evaluator { extension, }; - let decompressor = decompressor.unwrap(); - - // If there is a decompressor to use, we'll create a file in-memory and decompress it - let decompression_result = decompressor.decompress(file, output_file)?; if let DecompressionResult::FileInMemory(_) = decompression_result { // Should not be reachable. @@ -174,7 +163,6 @@ impl Evaluator { fn compress_files(files: Vec, mut output: File) -> error::OuchResult<()> { let (first_compressor, second_compressor) = Self::get_compressor(&output)?; - let output_path = output.path.clone(); let bytes = match first_compressor { @@ -187,17 +175,20 @@ impl Evaluator { entry = Entry::InMemory(output); second_compressor.compress(entry)? - }, + } None => { let entry = Entry::Files(files); second_compressor.compress(entry)? } }; - println!("{}: writing to {:?}. ({} bytes)", "info".yellow(), &output_path, bytes.len()); - fs::write( - output_path, - bytes)?; + println!( + "{}: writing to {:?}. ({} bytes)", + "info".yellow(), + &output_path, + bytes.len() + ); + fs::write(output_path, bytes)?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 29408f0..4a6f249 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,57 +1,47 @@ use std::convert::TryFrom; -use colored::Colorize; - mod cli; mod error; +mod evaluator; mod extension; mod file; mod test; -mod evaluator; mod utils; mod compressors; mod decompressors; -fn main() -> error::OuchResult<()>{ +fn main() -> error::OuchResult<()> { let print_error = |err| { println!("{}", err); }; + let matches = cli::get_matches(); - match cli::Command::try_from(matches) { - Ok(command) => { - match evaluator::Evaluator::evaluate(command) { - Ok(_) => {}, - Err(err) => print_error(err) - } - } - Err(err) => { - print_error(err) - } - } + cli::Command::try_from(matches) + .map(|command| evaluator::Evaluator::evaluate(command).unwrap_or_else(print_error)) + .unwrap_or_else(print_error); Ok(()) } // fn main() -> error::OuchResult<()> { - // use tar::{Builder}; // use walkdir::WalkDir; - +// // let mut b = Builder::new(Vec::new()); - +// // for entry in WalkDir::new("src") { // let entry = entry?; // let mut file = std::fs::File::open(entry.path())?; // b.append_file(entry.path(), &mut file)?; // } - +// // // let mut file = std::fs::File::open("Cargo.toml")?; // // b.append_file("Cargo.toml", &mut file)?; - +// // let bytes = b.into_inner()?; - +// // std::fs::write("daaaaamn.tar", bytes)?; - +// // Ok(()) -// } \ No newline at end of file +// } diff --git a/src/utils.rs b/src/utils.rs index 5c7225f..6f37830 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,8 +14,7 @@ where Ok(()) } -pub (crate) fn check_for_multiple_files(files: &Vec, format: &CompressionFormat) -> OuchResult<()> { - +pub (crate) fn check_for_multiple_files(files: &[PathBuf], format: &CompressionFormat) -> OuchResult<()> { if files.len() != 1 { eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "[ERROR]".red(), format, format); return Err(Error::InvalidInput); @@ -51,4 +50,4 @@ pub (crate) fn get_destination_path(dest: &Option) -> &Path { } None => Path::new("."), } -} \ No newline at end of file +}