From 77d7613967f2717b85cfc49a08cb52f32fd1f6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Mon, 22 Mar 2021 03:44:56 -0300 Subject: [PATCH] WIP refactor --- src/cli.rs | 2 ++ src/decompressors/tar.rs | 13 +++++-------- src/decompressors/zip.rs | 1 - src/evaluator.rs | 24 ++++++++++-------------- src/file.rs | 6 +++++- src/main.rs | 4 ++-- src/test.rs | 5 +++++ 7 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 6571c55..80c1722 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -115,6 +115,7 @@ impl TryFrom> for Command { kind: CommandKind::Compression(input_files), output: Some(File { path: output_file.into(), + contents: None, extension: Some(output_file_extension.unwrap()) }), }); @@ -130,6 +131,7 @@ impl TryFrom> for Command { kind: CommandKind::Decompression(input_files), output: Some(File { path: output_file.into(), + contents: None, extension: None }) }); diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 913bdeb..eee2f9d 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -1,10 +1,7 @@ -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::{fs, io::{Cursor, Read}, path::{Path, PathBuf}}; use colored::Colorize; -use tar; +use tar::{self, Archive}; use crate::{error::OuchResult, utils}; use crate::file::File; @@ -15,12 +12,12 @@ pub struct TarDecompressor {} impl TarDecompressor { - fn unpack_files(from: &Path, into: &Path) -> OuchResult> { + fn unpack_files(from: &File, into: &Path) -> OuchResult> { println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from); let mut files_unpacked = vec![]; - let file = fs::File::open(from)?; + let file = fs::File::open(&from.path)?; let mut archive = tar::Archive::new(file); for file in archive.entries()? { @@ -50,7 +47,7 @@ impl Decompressor for TarDecompressor { utils::create_path_if_non_existent(destination_path)?; - let files_unpacked = Self::unpack_files(&from.path, destination_path)?; + let files_unpacked = Self::unpack_files(&from, destination_path)?; Ok(DecompressionResult::FilesUnpacked(files_unpacked)) } diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index c3ce796..c7f5fff 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -23,7 +23,6 @@ impl ZipDecompressor { let mut unpacked_files = vec![]; - // placeholder return println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from); let file = fs::File::open(from)?; diff --git a/src/evaluator.rs b/src/evaluator.rs index c1a7fbe..9016535 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -20,13 +20,7 @@ pub struct Evaluator { } impl Evaluator { - // todo: remove this? - pub fn new(command: Command) -> Self { - Self { command } - } - fn get_decompressor( - &self, file: &File, ) -> error::OuchResult<(Option>, Box)> { if file.extension.is_none() { @@ -95,17 +89,17 @@ impl Evaluator { Ok(()) } - fn decompress_file(&self, file: &File) -> error::OuchResult<()> { - let output_file = &self.command.output; - let (first_decompressor, second_decompressor) = self.get_decompressor(file)?; + fn decompress_file(file: File, output: &Option) -> error::OuchResult<()> { + // let output_file = &command.output; + let (first_decompressor, second_decompressor) = Self::get_decompressor(&file)?; - let decompression_result = second_decompressor.decompress(file, output_file)?; + let decompression_result = second_decompressor.decompress(&file, output)?; match decompression_result { DecompressionResult::FileInMemory(bytes) => { // We'll now decompress a file currently in memory. // This will currently happen in the case of .bz, .xz and .lzma - Self::decompress_file_in_memory(bytes, file, first_decompressor, output_file)?; + Self::decompress_file_in_memory(bytes, &file, first_decompressor, output)?; } DecompressionResult::FilesUnpacked(files) => { // If the file's last extension was an archival method, @@ -121,8 +115,10 @@ impl Evaluator { Ok(()) } - pub fn evaluate(&mut self) -> error::OuchResult<()> { - match &self.command.kind { + pub fn evaluate(command: Command) -> error::OuchResult<()> { + let output = command.output.clone(); + + match command.kind { CommandKind::Compression(files_to_compress) => { for _file in files_to_compress { todo!(); @@ -130,7 +126,7 @@ impl Evaluator { } CommandKind::Decompression(files_to_decompress) => { for file in files_to_decompress { - self.decompress_file(file)?; + Self::decompress_file(file, &output)?; } } } diff --git a/src/file.rs b/src/file.rs index 7066934..3b50a6c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -3,10 +3,13 @@ use std::path::PathBuf; use crate::extension::{CompressionFormat, Extension}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct File { /// File's (relative) path pub path: PathBuf, + /// The bytes that compose the file. + /// Only used when the whole file is kept in-memory + pub contents: Option>, /// Note: extension here might be a misleading name since /// we don't really care about any extension other than supported compression ones. /// @@ -19,6 +22,7 @@ impl From<(PathBuf, CompressionFormat)> for File { fn from((path, format): (PathBuf, CompressionFormat)) -> Self { Self { path, + contents: None, extension: Some(Extension::from(format)), } } diff --git a/src/main.rs b/src/main.rs index ab67756..e873960 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,8 @@ fn main() -> OuchResult<()>{ let matches = cli::get_matches(); match cli::Command::try_from(matches) { Ok(command) => { - let mut eval = evaluator::Evaluator::new(command); - match eval.evaluate() { + // let mut eval = evaluator::Evaluator::new(); + match evaluator::Evaluator::evaluate(command) { Ok(_) => {}, Err(err) => print_error(err) } diff --git a/src/test.rs b/src/test.rs index 401991a..7703215 100644 --- a/src/test.rs +++ b/src/test.rs @@ -21,11 +21,13 @@ mod cli { kind: Decompression(vec![ File { path: "file.zip".into(), + contents: None, extension: Some(Extension::from(Zip)) } ]), output: Some(File { path: "folder".into(), + contents: None, extension: None }), } @@ -45,10 +47,12 @@ mod cli { kind: Decompression(vec![ File { path: "file.zip".into(), + contents: None, extension: Some(Extension::from(Zip)) }, File { path: "file.tar".into(), + contents: None, extension: Some(Extension::from(Tar)) } ],), @@ -84,6 +88,7 @@ mod cli { output: Some( File { path: "file.tar".into(), + contents: None, extension: Some(Extension::from(Tar)) } ),