mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
Get Decompressors as Trait Objects, start working on Zip decompressor
This commit is contained in:
parent
8d36120690
commit
837a6a6a57
@ -1 +1,9 @@
|
|||||||
/// This file should/could store a Decompressor trait
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use crate::{error::OuchResult, file::File};
|
||||||
|
|
||||||
|
/// This file should/could store a Decompressor trait
|
||||||
|
|
||||||
|
pub trait Decompressor {
|
||||||
|
fn decompress(&self, from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>>;
|
||||||
|
}
|
@ -1 +1,6 @@
|
|||||||
pub mod tar;
|
mod decompressor;
|
||||||
|
mod tar;
|
||||||
|
mod zip;
|
||||||
|
|
||||||
|
pub use decompressor::Decompressor;
|
||||||
|
pub use self::tar::TarDecompressor;
|
||||||
|
@ -9,9 +9,11 @@ use tar;
|
|||||||
use crate::error::OuchResult;
|
use crate::error::OuchResult;
|
||||||
use crate::file::File;
|
use crate::file::File;
|
||||||
|
|
||||||
pub struct Decompressor {}
|
use super::decompressor::Decompressor;
|
||||||
|
|
||||||
impl Decompressor {
|
pub struct TarDecompressor {}
|
||||||
|
|
||||||
|
impl TarDecompressor {
|
||||||
|
|
||||||
fn create_path_if_non_existent(path: &Path) -> OuchResult<()> {
|
fn create_path_if_non_existent(path: &Path) -> OuchResult<()> {
|
||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
@ -49,8 +51,10 @@ impl Decompressor {
|
|||||||
|
|
||||||
Ok(files_unpacked)
|
Ok(files_unpacked)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn decompress(from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>> {
|
impl Decompressor for TarDecompressor {
|
||||||
|
fn decompress(&self, from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>> {
|
||||||
let destination_path = match into {
|
let destination_path = match into {
|
||||||
Some(output) => {
|
Some(output) => {
|
||||||
// Must be None according to the way command-line arg. parsing in Ouch works
|
// Must be None according to the way command-line arg. parsing in Ouch works
|
||||||
@ -67,4 +71,4 @@ impl Decompressor {
|
|||||||
|
|
||||||
Ok(files_unpacked)
|
Ok(files_unpacked)
|
||||||
}
|
}
|
||||||
}
|
}
|
20
src/decompressors/zip.rs
Normal file
20
src/decompressors/zip.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
use colored::Colorize;
|
||||||
|
use zip;
|
||||||
|
|
||||||
|
use crate::error::OuchResult;
|
||||||
|
use crate::file::File;
|
||||||
|
|
||||||
|
use super::decompressor::Decompressor;
|
||||||
|
|
||||||
|
pub struct ZipDecompressor {}
|
||||||
|
|
||||||
|
impl Decompressor for ZipDecompressor {
|
||||||
|
fn decompress(&self, from: &File, into: &Option<File>) -> OuchResult<Vec<PathBuf>> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,8 @@
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File};
|
use crate::{cli::{Command, CommandKind}, error, extension::CompressionFormat, file::File};
|
||||||
use crate::decompressors::tar;
|
use crate::decompressors::TarDecompressor;
|
||||||
|
use crate::decompressors::Decompressor;
|
||||||
|
|
||||||
pub struct Evaluator {
|
pub struct Evaluator {
|
||||||
command: Command,
|
command: Command,
|
||||||
@ -17,26 +18,33 @@ impl Evaluator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decompress_file(&self, file: &File) -> error::OuchResult<()> {
|
fn get_decompressor(&self, file: &File) -> error::OuchResult<Box<dyn Decompressor>> {
|
||||||
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), file.path);
|
|
||||||
if file.extension.is_none() {
|
if file.extension.is_none() {
|
||||||
// This block *should* be unreachable
|
// This block *should* be unreachable
|
||||||
eprintln!("{}: reached Evaluator::decompress_file without known extension.", "internal error".red());
|
eprintln!("{}: reached Evaluator::get_decompressor without known extension.", "internal error".red());
|
||||||
return Err(error::Error::InvalidInput);
|
return Err(error::Error::InvalidInput);
|
||||||
}
|
}
|
||||||
let extension = file.extension.clone().unwrap();
|
let extension = file.extension.clone().unwrap();
|
||||||
let output_file = &self.command.output;
|
let decompressor = match extension.second_ext {
|
||||||
|
|
||||||
match extension.second_ext {
|
|
||||||
CompressionFormat::Tar => {
|
CompressionFormat::Tar => {
|
||||||
let _ = tar::Decompressor::decompress(file, output_file)?;
|
Box::new(TarDecompressor{})
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// TODO: decompress first extension
|
|
||||||
|
Ok(decompressor)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decompress_file(&self, file: &File) -> error::OuchResult<()> {
|
||||||
|
println!("{}: attempting to decompress {:?}", "ouch".bright_blue(), file.path);
|
||||||
|
let output_file = &self.command.output;
|
||||||
|
let decompressor = self.get_decompressor(file)?;
|
||||||
|
let files_unpacked = decompressor.decompress(file, output_file)?;
|
||||||
|
|
||||||
|
// TODO: decompress the first extension if it exists
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user