From 155fca45264896a5450ac9ce3df6f8fd3ebc2b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Sun, 21 Mar 2021 00:53:54 -0300 Subject: [PATCH] refactor: New File struct and switch to use Extension --- src/cli.rs | 35 ++++++++++++++++++++------- src/evaluator.rs | 34 +++++++++++++------------- src/{extensions.rs => extension.rs} | 12 +++++++++- src/file.rs | 28 +++++++++++++++++----- src/main.rs | 27 ++++++++++----------- src/test.rs | 37 ++++++++++++++++++++++------- 6 files changed, 117 insertions(+), 56 deletions(-) rename src/{extensions.rs => extension.rs} (94%) diff --git a/src/cli.rs b/src/cli.rs index 5be83bf..0b595a7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use clap::{Arg, Values}; use colored::Colorize; use crate::error; -use crate::extensions::CompressionFormat; +use crate::extension::{Extension, CompressionFormat}; use crate::file::File; #[derive(PartialEq, Eq, Debug)] @@ -15,7 +15,7 @@ pub enum CommandKind { ), Decompression( // Files to be decompressed and their extensions - Vec<(PathBuf, CompressionFormat)>, + Vec, ), } @@ -70,7 +70,6 @@ impl TryFrom> for Command { for file in input_files.clone() { if let (file, Err(_)) = file { - // eprintln!("{}: file '{}' is not decompressible.", "error".red(), file); return Err(error::Error::InputsMustHaveBeenDecompressible(file.into())); } } @@ -91,9 +90,11 @@ impl TryFrom> for Command { if output_was_supplied { let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied - let output_file_extension = CompressionFormat::try_from(output_file); + 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 + println!( "{}: trying to compress input files into '{}'", "info".yellow(), @@ -102,13 +103,22 @@ impl TryFrom> for Command { let input_files = input_files.map(PathBuf::from).collect(); + // return Ok(Command { + // kind: CommandKind::Compression(input_files), + // output: Some(File::WithExtension(( + // output_file.into(), + // output_file_extension.unwrap(), + // ))), + // }); + return Ok(Command { kind: CommandKind::Compression(input_files), - output: Some(File::WithExtension(( - output_file.into(), - output_file_extension.unwrap(), - ))), + output: Some(File { + path: output_file.into(), + extension: Some(output_file_extension.unwrap()) + }), }); + } else { // Checking if input files are decompressible @@ -119,9 +129,15 @@ impl TryFrom> for Command { "info".yellow(), output_file ); + + let input_files = input_files.into_iter().map(File::from).collect(); + return Ok(Command { kind: CommandKind::Decompression(input_files), - output: Some(File::WithoutExtension(output_file.into())), + output: Some(File { + path: output_file.into(), + extension: None + }) }); } } else { @@ -129,6 +145,7 @@ impl TryFrom> for Command { // Case 1: all input files are decompressible // Case 2: error let input_files = process_decompressible_input(input_files)?; + let input_files = input_files.into_iter().map(File::from).collect(); return Ok(Command { kind: CommandKind::Decompression(input_files), output: None, diff --git a/src/evaluator.rs b/src/evaluator.rs index e250f68..1f5eea1 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -2,7 +2,7 @@ use std::{convert::TryFrom, path::{PathBuf}}; use colored::Colorize; -use crate::{cli::{Command, CommandKind}, error, extensions::CompressionFormat, file::File}; +use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File}; pub struct Evaluator { command: Command, @@ -26,27 +26,27 @@ impl Evaluator { } } - fn handle_decompression(files_to_decompress: &[(PathBuf, CompressionFormat)], output_file: &Option) { - for (filename, extension) in files_to_decompress { - // println!("file: {:?}, extension: {:?}", filename, extension); + fn handle_decompression(files_to_decompress: &[File], output_file: &Option) { + // for (filename, extension) in files_to_decompress { + // // println!("file: {:?}, extension: {:?}", filename, extension); - // TODO: actually decompress anything ;-; + // // TODO: actually decompress anything ;-; - // Once decompressed, check if the file can be decompressed further - // e.g.: "foobar.tar.gz" -> "foobar.tar" + // // Once decompressed, check if the file can be decompressed further + // // e.g.: "foobar.tar.gz" -> "foobar.tar" - let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into(); - match CompressionFormat::try_from(filename) { - Ok(extension) => { - println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension); - }, - Err(err) => { - continue; - } - } - } + // let filename: &PathBuf = &filename.as_path().file_stem().unwrap().into(); + // match CompressionFormat::try_from(filename) { + // Ok(extension) => { + // println!("{}: attempting to decompress {:?}, ext: {:?}", "info".yellow(), filename, extension); + // }, + // Err(err) => { + // continue; + // } + // } + // } } pub fn evaluate(&mut self) { diff --git a/src/extensions.rs b/src/extension.rs similarity index 94% rename from src/extensions.rs rename to src/extension.rs index a0ee06e..ff22cf1 100644 --- a/src/extensions.rs +++ b/src/extension.rs @@ -6,11 +6,21 @@ use CompressionFormat::*; /// Represents the extension of a file, but only really caring about /// compression formats (and .tar). /// Ex.: Extension::new("file.tar.gz") == Extension { first_ext: Some(Tar), second_ext: Gzip } -struct Extension { +#[derive(Debug, PartialEq, Eq)] +pub struct Extension { first_ext: Option, second_ext: CompressionFormat } +impl From for Extension { + fn from(second_ext: CompressionFormat) -> Self { + Self { + first_ext: None, + second_ext + } + } +} + impl Extension { pub fn new(filename: &str) -> error::OuchResult { let ext_from_str = |ext| { diff --git a/src/file.rs b/src/file.rs index ead7b68..327e1b9 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,9 +1,25 @@ -use std::path::PathBuf; +use std::{path::PathBuf, process::Command}; -use crate::extensions::CompressionFormat; +use crate::extension::{CompressionFormat, Extension}; -#[derive(PartialEq, Eq, Debug)] -pub enum File { - WithExtension((PathBuf, CompressionFormat)), - WithoutExtension(PathBuf), + +#[derive(Debug, PartialEq, Eq)] +pub struct File { + /// File's (relative) path + pub path: PathBuf, + /// Note: extension here might be a misleading name since + /// we don't really care about any extension other than supported compression ones. + /// + /// So, for example, if a file has pathname "image.jpeg", it does have a JPEG extension but will + /// be represented as a None over here since that's not an extension we're particularly interested in + pub extension: Option +} + +impl From<(PathBuf, CompressionFormat)> for File { + fn from((path, format): (PathBuf, CompressionFormat)) -> Self { + Self { + path, + extension: Some(Extension::from(format)), + } + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 716579a..8977925 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,23 @@ -use std::{convert::TryFrom, ffi::OsStr, path::Path}; +use std::{convert::TryFrom}; use colored::Colorize; mod cli; mod error; -mod extensions; +mod extension; mod file; mod test; mod evaluator; fn main() { - // let matches = cli::get_matches(); - // match cli::Command::try_from(matches) { - // Ok(command) => { - // let mut eval = evaluator::Evaluator::new(command); - // eval.evaluate(); - // } - // Err(err) => { - // print!("{}: {}\n", "error".red(), err); - // } - // } - - dbg!(extensions::get_extension_from_filename("file")); - // dbg!(get_extension_from_filename("file.zip")); + let matches = cli::get_matches(); + match cli::Command::try_from(matches) { + Ok(command) => { + let mut eval = evaluator::Evaluator::new(command); + eval.evaluate(); + } + Err(err) => { + print!("{}: {}\n", "error".red(), err); + } + } } diff --git a/src/test.rs b/src/test.rs index 0db972a..e6364a7 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,11 +1,12 @@ #[cfg(test)] mod cli { - use crate::cli::clap_app; + use crate::{cli::clap_app, extension}; use crate::cli::Command; use crate::cli::CommandKind::*; use crate::error::OuchResult; - use crate::extensions::CompressionFormat::*; + use crate::extension::CompressionFormat::*; + use crate::extension::Extension; use crate::file::File; use std::convert::TryFrom; @@ -17,8 +18,16 @@ mod cli { assert_eq!( command_from_matches, Command { - kind: Decompression(vec![("file.zip".into(), Zip,),],), - output: Some(File::WithoutExtension("folder".into())), + kind: Decompression(vec![ + File { + path: "file.zip".into(), + extension: Some(Extension::from(Zip)) + } + ]), + output: Some(File { + path: "folder".into(), + extension: None + }), } ); @@ -34,8 +43,14 @@ mod cli { command_from_matches, Command { kind: Decompression(vec![ - ("file.zip".into(), Zip,), - ("file.tar".into(), Tar,), + File { + path: "file.zip".into(), + extension: Some(Extension::from(Zip)) + }, + File { + path: "file.tar".into(), + extension: Some(Extension::from(Tar)) + } ],), output: None, } @@ -65,7 +80,13 @@ mod cli { "file2.jpeg".into(), "file3.ok".into() ]), - output: Some(File::WithExtension(("file.tar".into(), Tar))) + // output: Some(File::WithExtension(("file.tar".into(), Extension::from(Tar)))) + output: Some( + File { + path: "file.tar".into(), + extension: Some(Extension::from(Tar)) + } + ), } ); @@ -101,7 +122,7 @@ mod cli_errors { #[cfg(test)] mod extension_extraction { use crate::error::OuchResult; - use crate::extensions::CompressionFormat; + use crate::extension::CompressionFormat; use std::{convert::TryFrom, path::PathBuf, str::FromStr}; #[test]