From 78d5f435ee120edabbba102c70a5316bf2b6d85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Tue, 6 Apr 2021 20:14:33 -0300 Subject: [PATCH] Minor style changes --- src/cli.rs | 6 +---- src/compressors/bzip.rs | 2 +- src/compressors/compressor.rs | 6 ----- src/compressors/gzip.rs | 2 +- src/compressors/lzma.rs | 2 +- src/compressors/mod.rs | 1 + src/compressors/tar.rs | 2 +- src/compressors/zip.rs | 2 +- src/decompressors/mod.rs | 13 +++++------ src/decompressors/tar.rs | 2 +- src/decompressors/to_memory.rs | 12 +++++----- src/decompressors/zip.rs | 2 +- src/evaluator.rs | 40 +++++++++++++++++----------------- src/extension.rs | 15 +++++-------- src/main.rs | 2 ++ src/utils.rs | 9 +++++++- 16 files changed, 54 insertions(+), 64 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index ef54fe3..0eb4cc5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,8 +7,6 @@ use std::{ use oof::{arg_flag, flag}; -pub const VERSION: &str = "0.1.5"; - #[derive(PartialEq, Eq, Debug)] pub enum Command { /// Files to be compressed @@ -29,7 +27,6 @@ pub enum Command { pub struct CommandInfo { pub command: Command, pub flags: oof::Flags, - // pub config: Config, // From .TOML, maybe, in the future } /// Calls parse_args_and_flags_from using std::env::args_os ( argv ) @@ -41,7 +38,6 @@ pub fn parse_args() -> crate::Result { pub struct ParsedArgs { pub command: Command, pub flags: oof::Flags, - // pub program_called: OsString, // Useful? } fn canonicalize<'a, P>(path: P) -> crate::Result @@ -54,7 +50,7 @@ where if !path.as_ref().exists() { Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))) } else { - eprintln!("{} {}", "[ERROR]", io_err); + eprintln!("[ERROR] {}", io_err); Err(crate::Error::IoError) } } diff --git a/src/compressors/bzip.rs b/src/compressors/bzip.rs index 28afd51..43fea59 100644 --- a/src/compressors/bzip.rs +++ b/src/compressors/bzip.rs @@ -10,7 +10,7 @@ use crate::{ utils::{check_for_multiple_files, ensure_exists}, }; -pub struct BzipCompressor {} +pub struct BzipCompressor; impl BzipCompressor { fn compress_files(files: Vec, format: CompressionFormat) -> crate::Result> { diff --git a/src/compressors/compressor.rs b/src/compressors/compressor.rs index 77ff2b8..7773dce 100644 --- a/src/compressors/compressor.rs +++ b/src/compressors/compressor.rs @@ -2,12 +2,6 @@ use std::path::PathBuf; use crate::file::File; -// pub enum CompressionResult { -// ZipArchive(Vec), -// TarArchive(Vec), -// FileInMemory(Vec) -// } - pub enum Entry<'a> { Files(Vec), InMemory(File<'a>), diff --git a/src/compressors/gzip.rs b/src/compressors/gzip.rs index a390971..6824d2c 100644 --- a/src/compressors/gzip.rs +++ b/src/compressors/gzip.rs @@ -10,7 +10,7 @@ use crate::{ utils::{check_for_multiple_files, ensure_exists}, }; -pub struct GzipCompressor {} +pub struct GzipCompressor; impl GzipCompressor { pub fn compress_files( diff --git a/src/compressors/lzma.rs b/src/compressors/lzma.rs index 71d93c1..5517730 100644 --- a/src/compressors/lzma.rs +++ b/src/compressors/lzma.rs @@ -10,7 +10,7 @@ use crate::{ utils::{check_for_multiple_files, ensure_exists}, }; -pub struct LzmaCompressor {} +pub struct LzmaCompressor; impl LzmaCompressor { pub fn compress_files( diff --git a/src/compressors/mod.rs b/src/compressors/mod.rs index f4c73bb..ecfed8b 100644 --- a/src/compressors/mod.rs +++ b/src/compressors/mod.rs @@ -1,3 +1,4 @@ +//! This module contains the Compressor trait and an implementor for each format. mod bzip; mod compressor; mod gzip; diff --git a/src/compressors/tar.rs b/src/compressors/tar.rs index 5de2537..0e0c417 100644 --- a/src/compressors/tar.rs +++ b/src/compressors/tar.rs @@ -7,7 +7,7 @@ use walkdir::WalkDir; use super::compressor::Entry; use crate::{compressors::Compressor, file::File, utils}; -pub struct TarCompressor {} +pub struct TarCompressor; impl TarCompressor { // TODO: implement this diff --git a/src/compressors/zip.rs b/src/compressors/zip.rs index be9f1fe..1d814d5 100644 --- a/src/compressors/zip.rs +++ b/src/compressors/zip.rs @@ -8,7 +8,7 @@ use walkdir::WalkDir; use super::compressor::Entry; use crate::{compressors::Compressor, file::File, utils}; -pub struct ZipCompressor {} +pub struct ZipCompressor; impl ZipCompressor { // TODO: this function does not seem to be working correctly ;/ diff --git a/src/decompressors/mod.rs b/src/decompressors/mod.rs index 689109e..c996f41 100644 --- a/src/decompressors/mod.rs +++ b/src/decompressors/mod.rs @@ -1,3 +1,5 @@ +//! This module contains the Decompressor trait and an implementor for each format. + mod decompressor; mod tar; mod to_memory; @@ -5,11 +7,6 @@ mod zip; pub use decompressor::{DecompressionResult, Decompressor}; -// These decompressors only decompress to memory, -// unlike {Tar, Zip}Decompressor which are capable of -// decompressing directly to storage -pub use self::{ - tar::TarDecompressor, - to_memory::{BzipDecompressor, GzipDecompressor, LzmaDecompressor}, - zip::ZipDecompressor, -}; +pub use self::to_memory::{BzipDecompressor, GzipDecompressor, LzmaDecompressor}; +// The .tar and .zip decompressors are capable of decompressing directly to storage +pub use self::{tar::TarDecompressor, zip::ZipDecompressor}; diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 5461b08..d3374d3 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -11,7 +11,7 @@ use super::decompressor::{DecompressionResult, Decompressor}; use crate::{bytes::Bytes, dialogs::Confirmation, file::File, utils}; #[derive(Debug)] -pub struct TarDecompressor {} +pub struct TarDecompressor; impl TarDecompressor { fn unpack_files(from: File, into: &Path, flags: &oof::Flags) -> crate::Result> { diff --git a/src/decompressors/to_memory.rs b/src/decompressors/to_memory.rs index d7af622..3a3f47f 100644 --- a/src/decompressors/to_memory.rs +++ b/src/decompressors/to_memory.rs @@ -6,14 +6,12 @@ use std::{ use colored::Colorize; use super::decompressor::{DecompressionResult, Decompressor}; -use crate::bytes::Bytes; -use crate::utils; -use crate::{extension::CompressionFormat, file::File}; +use crate::{bytes::Bytes, extension::CompressionFormat, file::File, utils}; -struct DecompressorToMemory {} -pub struct GzipDecompressor {} -pub struct LzmaDecompressor {} -pub struct BzipDecompressor {} +struct DecompressorToMemory; +pub struct GzipDecompressor; +pub struct LzmaDecompressor; +pub struct BzipDecompressor; fn get_decoder<'a>( format: CompressionFormat, diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index dcf1329..e3d2cb9 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -19,7 +19,7 @@ fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { } } -pub struct ZipDecompressor {} +pub struct ZipDecompressor; impl ZipDecompressor { fn check_for_comments(file: &ZipFile) { diff --git a/src/evaluator.rs b/src/evaluator.rs index 6093dc5..e90b400 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -8,7 +8,7 @@ use colored::Colorize; use crate::{ bytes::Bytes, - cli::{Command, VERSION}, + cli::Command, compressors::{ BzipCompressor, Compressor, Entry, GzipCompressor, LzmaCompressor, TarCompressor, ZipCompressor, @@ -23,7 +23,7 @@ use crate::{ utils, }; -pub struct Evaluator {} +pub struct Evaluator; type BoxedCompressor = Box; type BoxedDecompressor = Box; @@ -48,11 +48,11 @@ impl Evaluator { // .tar and .zip let first_compressor: Option> = match extension.first_ext { Some(ext) => match ext { - CompressionFormat::Tar => Some(Box::new(TarCompressor {})), - CompressionFormat::Zip => Some(Box::new(ZipCompressor {})), - CompressionFormat::Bzip => Some(Box::new(BzipCompressor {})), - CompressionFormat::Gzip => Some(Box::new(GzipCompressor {})), - CompressionFormat::Lzma => Some(Box::new(LzmaCompressor {})), + CompressionFormat::Tar => Some(Box::new(TarCompressor)), + CompressionFormat::Zip => Some(Box::new(ZipCompressor)), + CompressionFormat::Bzip => Some(Box::new(BzipCompressor)), + CompressionFormat::Gzip => Some(Box::new(GzipCompressor)), + CompressionFormat::Lzma => Some(Box::new(LzmaCompressor)), }, None => None, }; @@ -60,11 +60,11 @@ 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::Bzip => Box::new(BzipCompressor {}), - CompressionFormat::Gzip => Box::new(GzipCompressor {}), - CompressionFormat::Lzma => Box::new(LzmaCompressor {}), + 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), }; Ok((first_compressor, second_compressor)) @@ -86,17 +86,17 @@ impl Evaluator { }; let second_decompressor: Box = match extension.second_ext { - CompressionFormat::Tar => Box::new(TarDecompressor {}), - CompressionFormat::Zip => Box::new(ZipDecompressor {}), - CompressionFormat::Gzip => Box::new(GzipDecompressor {}), - CompressionFormat::Lzma => Box::new(LzmaDecompressor {}), - CompressionFormat::Bzip => Box::new(BzipDecompressor {}), + CompressionFormat::Tar => Box::new(TarDecompressor), + CompressionFormat::Zip => Box::new(ZipDecompressor), + CompressionFormat::Gzip => Box::new(GzipDecompressor), + CompressionFormat::Lzma => Box::new(LzmaDecompressor), + CompressionFormat::Bzip => Box::new(BzipDecompressor), }; let first_decompressor: Option> = match extension.first_ext { Some(ext) => match ext { - CompressionFormat::Tar => Some(Box::new(TarDecompressor {})), - CompressionFormat::Zip => Some(Box::new(ZipDecompressor {})), + CompressionFormat::Tar => Some(Box::new(TarDecompressor)), + CompressionFormat::Zip => Some(Box::new(ZipDecompressor)), _other => None, }, None => None, @@ -282,7 +282,7 @@ impl Evaluator { #[inline] fn version_message() { - println!("ouch {}", VERSION); + println!("ouch {}", crate::VERSION); } fn help_message() { diff --git a/src/extension.rs b/src/extension.rs index 43e0e1a..6ca7e4d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -86,16 +86,11 @@ impl Extension { #[derive(Clone, PartialEq, Eq, Debug)] /// Accepted extensions for input and output pub enum CompressionFormat { - // .gz - Gzip, - // .bz - Bzip, - // .lzma - Lzma, - // .tar (technically not a compression extension, but will do for now) - Tar, - // .zip - Zip, + Gzip, // .gz + Bzip, // .bz + Lzma, // .lzma + Tar, // .tar (technically not a compression extension, but will do for now) + Zip, // .zip } fn extension_from_os_str(ext: &OsStr) -> Result { diff --git a/src/main.rs b/src/main.rs index 984f191..784e59b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ mod file; mod test; mod utils; +pub const VERSION: &str = "0.1.5"; + use error::{Error, Result}; use evaluator::Evaluator; diff --git a/src/utils.rs b/src/utils.rs index 3b6d4d1..1953ccf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -41,7 +41,14 @@ pub(crate) fn check_for_multiple_files( format: &CompressionFormat, ) -> crate::Result<()> { 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); + 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(crate::Error::InvalidInput); }