Fix CLI parsing of decompression commands & early Compressor work

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-22 23:39:08 -03:00
parent 52afe3afd8
commit 9429fd8d67
7 changed files with 37 additions and 17 deletions

View File

@ -66,7 +66,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult<Command> { fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult<Command> {
let process_decompressible_input = |input_files: Values| { let process_decompressible_input = |input_files: Values| {
let input_files = let input_files =
input_files.map(|filename| (filename, CompressionFormat::try_from(filename))); input_files.map(|filename| (filename, Extension::new(filename)));
for file in input_files.clone() { for file in input_files.clone() {
if let (file, Err(_)) = file { if let (file, Err(_)) = file {
@ -76,6 +76,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
Ok(input_files Ok(input_files
.map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap())) .map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap()))
.map(File::from)
.collect::<Vec<_>>()) .collect::<Vec<_>>())
}; };
@ -114,12 +115,11 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
}); });
} else { } else {
// Output not supplied
// Checking if input files are decompressible // Checking if input files are decompressible
let input_files = process_decompressible_input(input_files)?; let input_files = process_decompressible_input(input_files)?;
let input_files = input_files.into_iter().map(File::from).collect();
return Ok(Command { return Ok(Command {
kind: CommandKind::Decompression(input_files), kind: CommandKind::Decompression(input_files),
output: Some(File { output: Some(File {
@ -134,7 +134,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
// Case 1: all input files are decompressible // Case 1: all input files are decompressible
// Case 2: error // Case 2: error
let input_files = process_decompressible_input(input_files)?; let input_files = process_decompressible_input(input_files)?;
let input_files = input_files.into_iter().map(File::from).collect();
return Ok(Command { return Ok(Command {
kind: CommandKind::Decompression(input_files), kind: CommandKind::Decompression(input_files),
output: None, output: None,

View File

@ -0,0 +1,22 @@
use std::path::PathBuf;
use crate::{error::OuchResult, file::File};
pub enum CompressionResult {
FilesUnpacked(Vec<PathBuf>),
FileInMemory(Vec<u8>)
}
pub trait Compressor {
fn compress(&self, from: Vec<File>, into: &Option<File>) -> OuchResult<DecompressionResult>;
}
//
//
//
//
//
//
//
//
//

0
src/compressors/mod.rs Normal file
View File

View File

@ -30,7 +30,7 @@ impl Evaluator {
); );
return Err(error::Error::InvalidInput); return Err(error::Error::InvalidInput);
} }
let extension = Extension::new(&file.path.to_str().unwrap())?; let extension = file.extension.clone().unwrap();
let second_decompressor: Box<dyn Decompressor> = match extension.second_ext { let second_decompressor: Box<dyn Decompressor> = match extension.second_ext {
CompressionFormat::Tar => Box::new(TarDecompressor {}), CompressionFormat::Tar => Box::new(TarDecompressor {}),

View File

@ -31,7 +31,6 @@ pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> {
} }
} }
impl From<CompressionFormat> for Extension { impl From<CompressionFormat> for Extension {
fn from(second_ext: CompressionFormat) -> Self { fn from(second_ext: CompressionFormat) -> Self {
Self { Self {
@ -48,7 +47,7 @@ impl Extension {
"zip" => Ok(Zip), "zip" => Ok(Zip),
"tar" => Ok(Tar), "tar" => Ok(Tar),
"gz" => Ok(Gzip), "gz" => Ok(Gzip),
"bz" => Ok(Bzip), "bz" | "bz2" => Ok(Bzip),
"lz" | "lzma" => Ok(Lzma), "lz" | "lzma" => Ok(Lzma),
other => Err(error::Error::UnknownExtensionError(other.into())), other => Err(error::Error::UnknownExtensionError(other.into())),
} }
@ -103,6 +102,7 @@ pub enum CompressionFormat {
} }
fn extension_from_os_str(ext: &OsStr) -> Result<CompressionFormat, error::Error> { fn extension_from_os_str(ext: &OsStr) -> Result<CompressionFormat, error::Error> {
// let ext = Path::new(ext);
let ext = match ext.to_str() { let ext = match ext.to_str() {
Some(str) => str, Some(str) => str,
@ -113,8 +113,8 @@ fn extension_from_os_str(ext: &OsStr) -> Result<CompressionFormat, error::Error>
"zip" => Ok(Zip), "zip" => Ok(Zip),
"tar" => Ok(Tar), "tar" => Ok(Tar),
"gz" => Ok(Gzip), "gz" => Ok(Gzip),
"bz" => Ok(Bzip), "bz" | "bz2" => Ok(Bzip),
"lzma" => Ok(Lzma), "lzma" | "lz" => Ok(Lzma),
other => Err(error::Error::UnknownExtensionError(other.into())), other => Err(error::Error::UnknownExtensionError(other.into())),
} }
} }
@ -130,7 +130,6 @@ impl TryFrom<&PathBuf> for CompressionFormat {
return Err(error::Error::MissingExtensionError(String::new())); return Err(error::Error::MissingExtensionError(String::new()));
} }
}; };
extension_from_os_str(ext) extension_from_os_str(ext)
} }
} }

View File

@ -18,12 +18,12 @@ pub struct File {
pub extension: Option<Extension> pub extension: Option<Extension>
} }
impl From<(PathBuf, CompressionFormat)> for File { impl From<(PathBuf, Extension)> for File {
fn from((path, format): (PathBuf, CompressionFormat)) -> Self { fn from((path, format): (PathBuf, Extension)) -> Self {
Self { Self {
path, path,
contents: None, contents: None,
extension: Some(Extension::from(format)), extension: Some(format),
} }
} }
} }

View File

@ -9,6 +9,8 @@ mod file;
mod test; mod test;
mod evaluator; mod evaluator;
mod utils; mod utils;
mod compressors;
mod decompressors; mod decompressors;
use error::OuchResult; use error::OuchResult;
@ -20,7 +22,6 @@ 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();
match evaluator::Evaluator::evaluate(command) { match evaluator::Evaluator::evaluate(command) {
Ok(_) => {}, Ok(_) => {},
Err(err) => print_error(err) Err(err) => print_error(err)
@ -31,7 +32,5 @@ fn main() -> OuchResult<()>{
} }
} }
// let extension = dbg!(Extension::new("file.tar.gz"));
Ok(()) Ok(())
} }