diff --git a/src/decompressors/mod.rs b/src/decompressors/mod.rs index 8f9fa4b..4edba7c 100644 --- a/src/decompressors/mod.rs +++ b/src/decompressors/mod.rs @@ -4,3 +4,4 @@ mod zip; pub use decompressor::Decompressor; pub use self::tar::TarDecompressor; +pub use self::zip::ZipDecompressor; \ No newline at end of file diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 7787fc6..1bc1ec8 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -6,7 +6,7 @@ use std::{ use colored::Colorize; use tar; -use crate::error::OuchResult; +use crate::{error::OuchResult, utils}; use crate::file::File; use super::decompressor::Decompressor; @@ -15,23 +15,6 @@ pub struct TarDecompressor {} impl TarDecompressor { - fn create_path_if_non_existent(path: &Path) -> OuchResult<()> { - if !path.exists() { - println!( - "{}: attempting to create folder {:?}.", - "info".yellow(), - &path - ); - std::fs::create_dir_all(path)?; - println!( - "{}: directory {:#?} created.", - "info".yellow(), - fs::canonicalize(&path)? - ); - } - Ok(()) - } - fn unpack_files(from: &Path, into: &Path) -> OuchResult> { let mut files_unpacked = vec![]; @@ -55,17 +38,9 @@ impl TarDecompressor { 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 - assert_eq!(output.extension, None); + let destination_path = utils::get_destination_path(into); - Path::new(&output.path) - } - None => Path::new("."), - }; - - Self::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)?; diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index 20e094d..0cdf48d 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -6,15 +6,30 @@ use std::{ use colored::Colorize; use zip; -use crate::error::OuchResult; +use crate::{error::{self, OuchResult}, utils}; use crate::file::File; use super::decompressor::Decompressor; pub struct ZipDecompressor {} +impl ZipDecompressor { + fn unpack_files(from: &Path, into: &Path) -> OuchResult> { + // placeholder return + Err(error::Error::IOError) + } +} + + impl Decompressor for ZipDecompressor { fn decompress(&self, from: &File, into: &Option) -> OuchResult> { - todo!() + let destination_path = utils::get_destination_path(into); + + utils::create_path_if_non_existent(destination_path)?; + + let files_unpacked = Self::unpack_files(&from.path, destination_path)?; + + // placeholder return + Err(error::Error::IOError) } } \ No newline at end of file diff --git a/src/evaluator.rs b/src/evaluator.rs index 90336f0..b992705 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -2,8 +2,9 @@ use colored::Colorize; use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File}; -use crate::decompressors::TarDecompressor; use crate::decompressors::Decompressor; +use crate::decompressors::TarDecompressor; +use crate::decompressors::ZipDecompressor; pub struct Evaluator { command: Command, @@ -29,6 +30,9 @@ impl Evaluator { CompressionFormat::Tar => { Box::new(TarDecompressor{}) }, + // CompressionFormat::Zip => { + // Box::new(ZipDecompressor{}) + // } _ => { todo!() } diff --git a/src/extension.rs b/src/extension.rs index a279041..d4880a9 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -12,6 +12,26 @@ pub struct Extension { pub second_ext: CompressionFormat } +pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> { + let path = Path::new(filename); + + let ext = path + .extension() + .and_then(OsStr::to_str)?; + + let previous_extension = path + .file_stem() + .and_then(OsStr::to_str) + .and_then(get_extension_from_filename); + + if let Some((_, prev)) = previous_extension { + Some((prev, ext)) + } else { + Some(("", ext)) + } +} + + impl From for Extension { fn from(second_ext: CompressionFormat) -> Self { Self { @@ -67,25 +87,6 @@ impl Extension { } } -pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> { - let path = Path::new(filename); - - let ext = path - .extension() - .and_then(OsStr::to_str)?; - - let previous_extension = path - .file_stem() - .and_then(OsStr::to_str) - .and_then(get_extension_from_filename); - - if let Some((_, prev)) = previous_extension { - Some((prev, ext)) - } else { - Some(("", ext)) - } -} - #[derive(Clone, PartialEq, Eq, Debug)] /// Accepted extensions for input and output pub enum CompressionFormat { diff --git a/src/file.rs b/src/file.rs index 327e1b9..7066934 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, process::Command}; +use std::path::PathBuf; use crate::extension::{CompressionFormat, Extension}; diff --git a/src/main.rs b/src/main.rs index fca88cf..2b2bac5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ -use std::{convert::TryFrom, fs::File, path::{Path, PathBuf}}; +use std::convert::TryFrom; use colored::Colorize; -use error::{Error, OuchResult}; -use tar::Archive; mod cli; mod error; @@ -10,9 +8,11 @@ mod extension; mod file; mod test; mod evaluator; - +mod utils; mod decompressors; +use error::OuchResult; + fn main() -> OuchResult<()>{ let matches = cli::get_matches(); match cli::Command::try_from(matches) { diff --git a/src/test.rs b/src/test.rs index e6364a7..401991a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,7 +1,7 @@ #[cfg(test)] mod cli { - use crate::{cli::clap_app, extension}; + use crate::cli::clap_app; use crate::cli::Command; use crate::cli::CommandKind::*; use crate::error::OuchResult; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..82babb9 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,33 @@ +use std::{fs, path::Path}; + +use colored::Colorize; +use crate::{error::OuchResult, file::File}; + +pub (crate) fn create_path_if_non_existent(path: &Path) -> OuchResult<()> { + if !path.exists() { + println!( + "{}: attempting to create folder {:?}.", + "info".yellow(), + &path + ); + std::fs::create_dir_all(path)?; + println!( + "{}: directory {:#?} created.", + "info".yellow(), + fs::canonicalize(&path)? + ); + } + Ok(()) +} + +pub (crate) fn get_destination_path(dest: &Option) -> &Path { + match dest { + Some(output) => { + // Must be None according to the way command-line arg. parsing in Ouch works + assert_eq!(output.extension, None); + + Path::new(&output.path) + } + None => Path::new("."), + } +} \ No newline at end of file