mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-05 02:55:31 +00:00
Fix CLI parsing of decompression commands & early Compressor work
This commit is contained in:
parent
52afe3afd8
commit
9429fd8d67
@ -66,7 +66,7 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult<Command> {
|
||||
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<clap::ArgMatches<'static>> for Command {
|
||||
|
||||
Ok(input_files
|
||||
.map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap()))
|
||||
.map(File::from)
|
||||
.collect::<Vec<_>>())
|
||||
};
|
||||
|
||||
@ -114,12 +115,11 @@ impl TryFrom<clap::ArgMatches<'static>> 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<clap::ArgMatches<'static>> 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,
|
||||
|
22
src/compressors/compressor.rs
Normal file
22
src/compressors/compressor.rs
Normal 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
0
src/compressors/mod.rs
Normal file
@ -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<dyn Decompressor> = match extension.second_ext {
|
||||
CompressionFormat::Tar => Box::new(TarDecompressor {}),
|
||||
|
@ -31,7 +31,6 @@ pub fn get_extension_from_filename(filename: &str) -> Option<(&str, &str)> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<CompressionFormat> 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<CompressionFormat, error::Error> {
|
||||
|
||||
// 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<CompressionFormat, error::Error>
|
||||
"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)
|
||||
}
|
||||
}
|
||||
|
@ -18,12 +18,12 @@ pub struct File {
|
||||
pub extension: Option<Extension>
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
@ -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(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user