From 837a6a6a572194d3da8a61467696ea390ce31725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Sun, 21 Mar 2021 15:23:00 -0300 Subject: [PATCH] Get Decompressors as Trait Objects, start working on Zip decompressor --- src/decompressors/decompressor.rs | 10 +++++++++- src/decompressors/mod.rs | 7 ++++++- src/decompressors/tar.rs | 12 ++++++++---- src/decompressors/zip.rs | 20 ++++++++++++++++++++ src/evaluator.rs | 28 ++++++++++++++++++---------- 5 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 src/decompressors/zip.rs diff --git a/src/decompressors/decompressor.rs b/src/decompressors/decompressor.rs index 20b1051..c160216 100644 --- a/src/decompressors/decompressor.rs +++ b/src/decompressors/decompressor.rs @@ -1 +1,9 @@ -/// This file should/could store a Decompressor trait \ No newline at end of file +use std::path::PathBuf; + +use crate::{error::OuchResult, file::File}; + +/// This file should/could store a Decompressor trait + +pub trait Decompressor { + fn decompress(&self, from: &File, into: &Option) -> OuchResult>; +} \ No newline at end of file diff --git a/src/decompressors/mod.rs b/src/decompressors/mod.rs index 8e97713..8f9fa4b 100644 --- a/src/decompressors/mod.rs +++ b/src/decompressors/mod.rs @@ -1 +1,6 @@ -pub mod tar; \ No newline at end of file +mod decompressor; +mod tar; +mod zip; + +pub use decompressor::Decompressor; +pub use self::tar::TarDecompressor; diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 88d2a50..7787fc6 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -9,9 +9,11 @@ use tar; use crate::error::OuchResult; use crate::file::File; -pub struct Decompressor {} +use super::decompressor::Decompressor; -impl Decompressor { +pub struct TarDecompressor {} + +impl TarDecompressor { fn create_path_if_non_existent(path: &Path) -> OuchResult<()> { if !path.exists() { @@ -49,8 +51,10 @@ impl Decompressor { Ok(files_unpacked) } +} - pub fn decompress(from: &File, into: &Option) -> OuchResult> { +impl Decompressor for TarDecompressor { + fn decompress(&self, from: &File, into: &Option) -> OuchResult> { let destination_path = match into { Some(output) => { // Must be None according to the way command-line arg. parsing in Ouch works @@ -67,4 +71,4 @@ impl Decompressor { Ok(files_unpacked) } -} +} \ No newline at end of file diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs new file mode 100644 index 0000000..20e094d --- /dev/null +++ b/src/decompressors/zip.rs @@ -0,0 +1,20 @@ +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use colored::Colorize; +use zip; + +use crate::error::OuchResult; +use crate::file::File; + +use super::decompressor::Decompressor; + +pub struct ZipDecompressor {} + +impl Decompressor for ZipDecompressor { + fn decompress(&self, from: &File, into: &Option) -> OuchResult> { + todo!() + } +} \ No newline at end of file diff --git a/src/evaluator.rs b/src/evaluator.rs index ff4af4c..90336f0 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -2,7 +2,8 @@ use colored::Colorize; use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File}; -use crate::decompressors::tar; +use crate::decompressors::TarDecompressor; +use crate::decompressors::Decompressor; pub struct Evaluator { command: Command, @@ -17,26 +18,33 @@ impl Evaluator { } } - fn decompress_file(&self, file: &File) -> error::OuchResult<()> { - println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), file.path); + fn get_decompressor(&self, file: &File) -> error::OuchResult> { if file.extension.is_none() { // This block *should* be unreachable - eprintln!("{}: reached Evaluator::decompress_file without known extension.", "internal error".red()); + eprintln!("{}: reached Evaluator::get_decompressor without known extension.", "internal error".red()); return Err(error::Error::InvalidInput); } let extension = file.extension.clone().unwrap(); - let output_file = &self.command.output; - - match extension.second_ext { + let decompressor = match extension.second_ext { CompressionFormat::Tar => { - let _ = tar::Decompressor::decompress(file, output_file)?; + Box::new(TarDecompressor{}) }, _ => { todo!() } - } + }; - // TODO: decompress first extension + + Ok(decompressor) + } + + fn decompress_file(&self, file: &File) -> error::OuchResult<()> { + println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), file.path); + let output_file = &self.command.output; + let decompressor = self.get_decompressor(file)?; + let files_unpacked = decompressor.decompress(file, output_file)?; + + // TODO: decompress the first extension if it exists Ok(()) }