WIP refactor

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-22 03:44:56 -03:00
parent 90ad9d8f8a
commit 77d7613967
7 changed files with 29 additions and 26 deletions

View File

@ -115,6 +115,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
kind: CommandKind::Compression(input_files), kind: CommandKind::Compression(input_files),
output: Some(File { output: Some(File {
path: output_file.into(), path: output_file.into(),
contents: None,
extension: Some(output_file_extension.unwrap()) extension: Some(output_file_extension.unwrap())
}), }),
}); });
@ -130,6 +131,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
kind: CommandKind::Decompression(input_files), kind: CommandKind::Decompression(input_files),
output: Some(File { output: Some(File {
path: output_file.into(), path: output_file.into(),
contents: None,
extension: None extension: None
}) })
}); });

View File

@ -1,10 +1,7 @@
use std::{ use std::{fs, io::{Cursor, Read}, path::{Path, PathBuf}};
fs,
path::{Path, PathBuf},
};
use colored::Colorize; use colored::Colorize;
use tar; use tar::{self, Archive};
use crate::{error::OuchResult, utils}; use crate::{error::OuchResult, utils};
use crate::file::File; use crate::file::File;
@ -15,12 +12,12 @@ pub struct TarDecompressor {}
impl TarDecompressor { impl TarDecompressor {
fn unpack_files(from: &Path, into: &Path) -> OuchResult<Vec<PathBuf>> { fn unpack_files(from: &File, into: &Path) -> OuchResult<Vec<PathBuf>> {
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from); println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from);
let mut files_unpacked = vec![]; 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); let mut archive = tar::Archive::new(file);
for file in archive.entries()? { for file in archive.entries()? {
@ -50,7 +47,7 @@ impl Decompressor for TarDecompressor {
utils::create_path_if_non_existent(destination_path)?; 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)) Ok(DecompressionResult::FilesUnpacked(files_unpacked))
} }

View File

@ -23,7 +23,6 @@ impl ZipDecompressor {
let mut unpacked_files = vec![]; let mut unpacked_files = vec![];
// placeholder return
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from); println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), from);
let file = fs::File::open(from)?; let file = fs::File::open(from)?;

View File

@ -20,13 +20,7 @@ pub struct Evaluator {
} }
impl Evaluator { impl Evaluator {
// todo: remove this?
pub fn new(command: Command) -> Self {
Self { command }
}
fn get_decompressor( fn get_decompressor(
&self,
file: &File, file: &File,
) -> error::OuchResult<(Option<Box<dyn Decompressor>>, Box<dyn Decompressor>)> { ) -> error::OuchResult<(Option<Box<dyn Decompressor>>, Box<dyn Decompressor>)> {
if file.extension.is_none() { if file.extension.is_none() {
@ -95,17 +89,17 @@ impl Evaluator {
Ok(()) Ok(())
} }
fn decompress_file(&self, file: &File) -> error::OuchResult<()> { fn decompress_file(file: File, output: &Option<File>) -> error::OuchResult<()> {
let output_file = &self.command.output; // let output_file = &command.output;
let (first_decompressor, second_decompressor) = self.get_decompressor(file)?; 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 { match decompression_result {
DecompressionResult::FileInMemory(bytes) => { DecompressionResult::FileInMemory(bytes) => {
// We'll now decompress a file currently in memory. // We'll now decompress a file currently in memory.
// This will currently happen in the case of .bz, .xz and .lzma // 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) => { DecompressionResult::FilesUnpacked(files) => {
// If the file's last extension was an archival method, // If the file's last extension was an archival method,
@ -121,8 +115,10 @@ impl Evaluator {
Ok(()) Ok(())
} }
pub fn evaluate(&mut self) -> error::OuchResult<()> { pub fn evaluate(command: Command) -> error::OuchResult<()> {
match &self.command.kind { let output = command.output.clone();
match command.kind {
CommandKind::Compression(files_to_compress) => { CommandKind::Compression(files_to_compress) => {
for _file in files_to_compress { for _file in files_to_compress {
todo!(); todo!();
@ -130,7 +126,7 @@ impl Evaluator {
} }
CommandKind::Decompression(files_to_decompress) => { CommandKind::Decompression(files_to_decompress) => {
for file in files_to_decompress { for file in files_to_decompress {
self.decompress_file(file)?; Self::decompress_file(file, &output)?;
} }
} }
} }

View File

@ -3,10 +3,13 @@ use std::path::PathBuf;
use crate::extension::{CompressionFormat, Extension}; use crate::extension::{CompressionFormat, Extension};
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct File { pub struct File {
/// File's (relative) path /// File's (relative) path
pub path: PathBuf, pub path: PathBuf,
/// The bytes that compose the file.
/// Only used when the whole file is kept in-memory
pub contents: Option<Vec<u8>>,
/// Note: extension here might be a misleading name since /// Note: extension here might be a misleading name since
/// we don't really care about any extension other than supported compression ones. /// 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 { fn from((path, format): (PathBuf, CompressionFormat)) -> Self {
Self { Self {
path, path,
contents: None,
extension: Some(Extension::from(format)), extension: Some(Extension::from(format)),
} }
} }

View File

@ -20,8 +20,8 @@ fn main() -> OuchResult<()>{
let matches = cli::get_matches(); let matches = cli::get_matches();
match cli::Command::try_from(matches) { match cli::Command::try_from(matches) {
Ok(command) => { Ok(command) => {
let mut eval = evaluator::Evaluator::new(command); // let mut eval = evaluator::Evaluator::new();
match eval.evaluate() { match evaluator::Evaluator::evaluate(command) {
Ok(_) => {}, Ok(_) => {},
Err(err) => print_error(err) Err(err) => print_error(err)
} }

View File

@ -21,11 +21,13 @@ mod cli {
kind: Decompression(vec![ kind: Decompression(vec![
File { File {
path: "file.zip".into(), path: "file.zip".into(),
contents: None,
extension: Some(Extension::from(Zip)) extension: Some(Extension::from(Zip))
} }
]), ]),
output: Some(File { output: Some(File {
path: "folder".into(), path: "folder".into(),
contents: None,
extension: None extension: None
}), }),
} }
@ -45,10 +47,12 @@ mod cli {
kind: Decompression(vec![ kind: Decompression(vec![
File { File {
path: "file.zip".into(), path: "file.zip".into(),
contents: None,
extension: Some(Extension::from(Zip)) extension: Some(Extension::from(Zip))
}, },
File { File {
path: "file.tar".into(), path: "file.tar".into(),
contents: None,
extension: Some(Extension::from(Tar)) extension: Some(Extension::from(Tar))
} }
],), ],),
@ -84,6 +88,7 @@ mod cli {
output: Some( output: Some(
File { File {
path: "file.tar".into(), path: "file.tar".into(),
contents: None,
extension: Some(Extension::from(Tar)) extension: Some(Extension::from(Tar))
} }
), ),