diff --git a/src/cli.rs b/src/cli.rs index 1da9d12..69cb256 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -66,7 +66,7 @@ impl TryFrom> for Command { fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult { let process_decompressible_input = |input_files: Values| { 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() { if let (file, Err(_)) = file { @@ -76,6 +76,7 @@ impl TryFrom> for Command { Ok(input_files .map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap())) + .map(File::from) .collect::>()) }; @@ -114,12 +115,11 @@ impl TryFrom> for Command { }); } else { + // Output not supplied // Checking if input files are decompressible 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: Some(File { @@ -134,7 +134,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/compressors/compressor.rs b/src/compressors/compressor.rs new file mode 100644 index 0000000..2530085 --- /dev/null +++ b/src/compressors/compressor.rs @@ -0,0 +1,22 @@ +use std::path::PathBuf; + +use crate::{error::OuchResult, file::File}; + +pub enum CompressionResult { + FilesUnpacked(Vec), + FileInMemory(Vec) +} + +pub trait Compressor { + fn compress(&self, from: Vec, into: &Option) -> OuchResult; +} + +// +// +// +// +// +// +// +// +// \ No newline at end of file diff --git a/src/compressors/mod.rs b/src/compressors/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/evaluator.rs b/src/evaluator.rs index 644875f..d9cedb9 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -30,7 +30,7 @@ impl Evaluator { ); return Err(error::Error::InvalidInput); } - let extension = Extension::new(&file.path.to_str().unwrap())?; + let extension = file.extension.clone().unwrap(); let second_decompressor: Box = match extension.second_ext { CompressionFormat::Tar => Box::new(TarDecompressor {}), diff --git a/src/extension.rs b/src/extension.rs index 38e682c..ee4f86d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -31,7 +31,6 @@ pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> { } } - impl From for Extension { fn from(second_ext: CompressionFormat) -> Self { Self { @@ -48,7 +47,7 @@ impl Extension { "zip" => Ok(Zip), "tar" => Ok(Tar), "gz" => Ok(Gzip), - "bz" => Ok(Bzip), + "bz" | "bz2" => Ok(Bzip), "lz" | "lzma" => Ok(Lzma), other => Err(error::Error::UnknownExtensionError(other.into())), } @@ -103,7 +102,8 @@ pub enum CompressionFormat { } fn extension_from_os_str(ext: &OsStr) -> Result { - + // let ext = Path::new(ext); + let ext = match ext.to_str() { Some(str) => str, None => return Err(error::Error::InvalidUnicode), @@ -113,8 +113,8 @@ fn extension_from_os_str(ext: &OsStr) -> Result "zip" => Ok(Zip), "tar" => Ok(Tar), "gz" => Ok(Gzip), - "bz" => Ok(Bzip), - "lzma" => Ok(Lzma), + "bz" | "bz2" => Ok(Bzip), + "lzma" | "lz" => Ok(Lzma), other => Err(error::Error::UnknownExtensionError(other.into())), } } @@ -130,7 +130,6 @@ impl TryFrom<&PathBuf> for CompressionFormat { return Err(error::Error::MissingExtensionError(String::new())); } }; - extension_from_os_str(ext) } } diff --git a/src/file.rs b/src/file.rs index 3b50a6c..78b87a5 100644 --- a/src/file.rs +++ b/src/file.rs @@ -18,12 +18,12 @@ pub struct File { pub extension: Option } -impl From<(PathBuf, CompressionFormat)> for File { - fn from((path, format): (PathBuf, CompressionFormat)) -> Self { +impl From<(PathBuf, Extension)> for File { + fn from((path, format): (PathBuf, Extension)) -> Self { Self { path, contents: None, - extension: Some(Extension::from(format)), + extension: Some(format), } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 511aef2..8a2f942 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ mod file; mod test; mod evaluator; mod utils; + +mod compressors; mod decompressors; use error::OuchResult; @@ -20,7 +22,6 @@ fn main() -> OuchResult<()>{ let matches = cli::get_matches(); match cli::Command::try_from(matches) { Ok(command) => { - // let mut eval = evaluator::Evaluator::new(); match evaluator::Evaluator::evaluate(command) { Ok(_) => {}, Err(err) => print_error(err) @@ -31,7 +32,5 @@ fn main() -> OuchResult<()>{ } } - // let extension = dbg!(Extension::new("file.tar.gz")); - Ok(()) }