mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
Refactoring and Clippy warnings
This commit is contained in:
parent
e1da79c2ac
commit
755cc2a40d
13
src/cli.rs
13
src/cli.rs
@ -127,14 +127,15 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
|
||||
let input_files = canonical_paths.map(Result::unwrap).collect();
|
||||
|
||||
return Ok(Command {
|
||||
Ok(Command {
|
||||
kind: CommandKind::Compression(input_files),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
contents_in_memory: None,
|
||||
// extension: output_file_extension.ok(),
|
||||
extension: Some(output_file_extension.unwrap())
|
||||
}),
|
||||
});
|
||||
})
|
||||
|
||||
} else {
|
||||
// Output not supplied
|
||||
@ -142,14 +143,14 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
|
||||
let input_files = process_decompressible_input(input_files)?;
|
||||
|
||||
return Ok(Command {
|
||||
Ok(Command {
|
||||
kind: CommandKind::Decompression(input_files),
|
||||
output: Some(File {
|
||||
path: output_file.into(),
|
||||
contents_in_memory: None,
|
||||
extension: None
|
||||
})
|
||||
});
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// else: output file not supplied
|
||||
@ -157,10 +158,10 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||
// Case 2: error
|
||||
let input_files = process_decompressible_input(input_files)?;
|
||||
|
||||
return Ok(Command {
|
||||
Ok(Command {
|
||||
kind: CommandKind::Decompression(input_files),
|
||||
output: None,
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ impl BzipCompressor {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self::compress_bytes(&*bytes)?)
|
||||
Self::compress_bytes(&*bytes)
|
||||
}
|
||||
|
||||
fn compress_bytes(bytes: &[u8]) -> OuchResult<Vec<u8>> {
|
||||
|
@ -42,7 +42,7 @@ impl GzipCompressor {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self::compress_bytes(file_contents)?)
|
||||
Self::compress_bytes(file_contents)
|
||||
}
|
||||
|
||||
pub fn compress_bytes(bytes_to_compress: Vec<u8>) -> OuchResult<Vec<u8>> {
|
||||
|
@ -42,7 +42,7 @@ impl LzmaCompressor {
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self::compress_bytes(file_contents)?)
|
||||
Self::compress_bytes(file_contents)
|
||||
}
|
||||
|
||||
pub fn compress_bytes(bytes_to_compress: Vec<u8>) -> OuchResult<Vec<u8>> {
|
||||
@ -58,12 +58,8 @@ impl Compressor for LzmaCompressor {
|
||||
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
|
||||
let format = CompressionFormat::Lzma;
|
||||
match from {
|
||||
Entry::Files(files) => Ok(
|
||||
Self::compress_files(files, format)?
|
||||
),
|
||||
Entry::InMemory(file) => Ok(
|
||||
Self::compress_file_in_memory(file)?
|
||||
),
|
||||
Entry::Files(files) => Self::compress_files(files, format),
|
||||
Entry::InMemory(file) => Self::compress_file_in_memory(file),
|
||||
}
|
||||
}
|
||||
}
|
@ -4,22 +4,27 @@ use colored::Colorize;
|
||||
use tar::Builder;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::{compressors::Compressor, error::{Error, OuchResult}, file::File};
|
||||
use crate::{
|
||||
compressors::Compressor,
|
||||
error::{Error, OuchResult},
|
||||
file::File,
|
||||
};
|
||||
|
||||
use super::compressor::Entry;
|
||||
|
||||
pub struct TarCompressor {}
|
||||
|
||||
impl TarCompressor {
|
||||
|
||||
// TODO: implement this
|
||||
fn make_archive_from_memory(_input: File) -> OuchResult<Vec<u8>> {
|
||||
println!("{}: .tar.tar and .zip.tar is currently unimplemented.", "error".red());
|
||||
println!(
|
||||
"{}: .tar.tar and .zip.tar is currently unimplemented.",
|
||||
"error".red()
|
||||
);
|
||||
Err(Error::InvalidZipArchive(""))
|
||||
}
|
||||
|
||||
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {
|
||||
|
||||
let change_dir_and_return_parent = |filename: &PathBuf| -> OuchResult<PathBuf> {
|
||||
let previous_location = env::current_dir()?;
|
||||
let parent = filename.parent().unwrap();
|
||||
@ -51,18 +56,9 @@ impl TarCompressor {
|
||||
|
||||
impl Compressor for TarCompressor {
|
||||
fn compress(&self, from: Entry) -> OuchResult<Vec<u8>> {
|
||||
|
||||
match from {
|
||||
Entry::Files(filenames) => {
|
||||
Ok(
|
||||
Self::make_archive_from_files(filenames)?
|
||||
)
|
||||
},
|
||||
Entry::InMemory(file) => {
|
||||
Ok(
|
||||
Self::make_archive_from_memory(file)?
|
||||
)
|
||||
}
|
||||
Entry::Files(filenames) => Self::make_archive_from_files(filenames),
|
||||
Entry::InMemory(file) => Self::make_archive_from_memory(file),
|
||||
}
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ impl ZipCompressor {
|
||||
}
|
||||
};
|
||||
|
||||
writer.write(&*input_bytes)?;
|
||||
writer.write_all(&*input_bytes)?;
|
||||
|
||||
|
||||
|
||||
@ -71,7 +71,7 @@ impl ZipCompressor {
|
||||
options
|
||||
)?;
|
||||
let file_bytes = std::fs::read(entry.path())?;
|
||||
writer.write(&*file_bytes)?;
|
||||
writer.write_all(&*file_bytes)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,29 +3,15 @@ use std::{ffi::OsStr, fs, io::Write, path::PathBuf};
|
||||
use colored::Colorize;
|
||||
|
||||
use crate::compressors::{
|
||||
Entry,
|
||||
Compressor,
|
||||
TarCompressor,
|
||||
ZipCompressor,
|
||||
GzipCompressor,
|
||||
BzipCompressor,
|
||||
LzmaCompressor
|
||||
BzipCompressor, Compressor, Entry, GzipCompressor, LzmaCompressor, TarCompressor, ZipCompressor,
|
||||
};
|
||||
|
||||
use crate::decompressors::{
|
||||
Decompressor,
|
||||
TarDecompressor,
|
||||
ZipDecompressor,
|
||||
GzipDecompressor,
|
||||
BzipDecompressor,
|
||||
LzmaDecompressor,
|
||||
DecompressionResult
|
||||
BzipDecompressor, DecompressionResult, Decompressor, GzipDecompressor, LzmaDecompressor,
|
||||
TarDecompressor, ZipDecompressor,
|
||||
};
|
||||
|
||||
use crate::extension::{
|
||||
Extension,
|
||||
CompressionFormat
|
||||
};
|
||||
use crate::extension::{CompressionFormat, Extension};
|
||||
|
||||
use crate::cli::{Command, CommandKind};
|
||||
|
||||
@ -41,7 +27,9 @@ impl Evaluator {
|
||||
pub fn get_compressor(
|
||||
file: &File,
|
||||
) -> error::OuchResult<(Option<Box<dyn Compressor>>, Box<dyn Compressor>)> {
|
||||
if file.extension.is_none() {
|
||||
let extension = match &file.extension {
|
||||
Some(extension) => extension.clone(),
|
||||
None => {
|
||||
// This block *should* be unreachable
|
||||
eprintln!(
|
||||
"{}: reached Evaluator::get_decompressor without known extension.",
|
||||
@ -49,7 +37,7 @@ impl Evaluator {
|
||||
);
|
||||
return Err(error::Error::InvalidInput);
|
||||
}
|
||||
let extension = file.extension.clone().unwrap();
|
||||
};
|
||||
|
||||
// Supported first compressors:
|
||||
// .tar and .zip
|
||||
@ -83,7 +71,9 @@ impl Evaluator {
|
||||
pub fn get_decompressor(
|
||||
file: &File,
|
||||
) -> error::OuchResult<(Option<Box<dyn Decompressor>>, Box<dyn Decompressor>)> {
|
||||
if file.extension.is_none() {
|
||||
let extension = match &file.extension {
|
||||
Some(extension) => extension.clone(),
|
||||
None => {
|
||||
// This block *should* be unreachable
|
||||
eprintln!(
|
||||
"{}: reached Evaluator::get_decompressor without known extension.",
|
||||
@ -91,7 +81,7 @@ impl Evaluator {
|
||||
);
|
||||
return Err(error::Error::InvalidInput);
|
||||
}
|
||||
let extension = file.extension.clone().unwrap();
|
||||
};
|
||||
|
||||
let second_decompressor: Box<dyn Decompressor> = match extension.second_ext {
|
||||
CompressionFormat::Tar => Box::new(TarDecompressor {}),
|
||||
@ -102,8 +92,7 @@ impl Evaluator {
|
||||
|
||||
CompressionFormat::Lzma => Box::new(LzmaDecompressor {}),
|
||||
|
||||
CompressionFormat::Bzip => Box::new(BzipDecompressor {})
|
||||
|
||||
CompressionFormat::Bzip => Box::new(BzipDecompressor {}),
|
||||
};
|
||||
|
||||
let first_decompressor: Option<Box<dyn Decompressor>> = match extension.first_ext {
|
||||
@ -132,7 +121,7 @@ impl Evaluator {
|
||||
|
||||
let mut filename = file_path
|
||||
.file_stem()
|
||||
.unwrap_or(output_file_path.as_os_str());
|
||||
.unwrap_or_else(|| output_file_path.as_os_str());
|
||||
if filename == "." {
|
||||
// I believe this is only possible when the supplied inout has a name
|
||||
// of the sort `.tar` or `.zip' and no output has been supplied.
|
||||
@ -141,7 +130,10 @@ impl Evaluator {
|
||||
|
||||
let filename = PathBuf::from(filename);
|
||||
|
||||
if decompressor.is_none() {
|
||||
// If there is a decompressor to use, we'll create a file in-memory and decompress it
|
||||
let decompressor = match decompressor {
|
||||
Some(decompressor) => decompressor,
|
||||
None => {
|
||||
// There is no more processing to be done on the input file (or there is but currently unsupported)
|
||||
// Therefore, we'll save what we have in memory into a file.
|
||||
|
||||
@ -151,6 +143,7 @@ impl Evaluator {
|
||||
f.write_all(&bytes)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let file = File {
|
||||
path: filename,
|
||||
@ -158,10 +151,6 @@ impl Evaluator {
|
||||
extension,
|
||||
};
|
||||
|
||||
let decompressor = decompressor.unwrap();
|
||||
|
||||
// If there is a decompressor to use, we'll create a file in-memory and decompress it
|
||||
|
||||
let decompression_result = decompressor.decompress(file, output_file)?;
|
||||
if let DecompressionResult::FileInMemory(_) = decompression_result {
|
||||
// Should not be reachable.
|
||||
@ -174,7 +163,6 @@ impl Evaluator {
|
||||
fn compress_files(files: Vec<PathBuf>, mut output: File) -> error::OuchResult<()> {
|
||||
let (first_compressor, second_compressor) = Self::get_compressor(&output)?;
|
||||
|
||||
|
||||
let output_path = output.path.clone();
|
||||
|
||||
let bytes = match first_compressor {
|
||||
@ -187,17 +175,20 @@ impl Evaluator {
|
||||
entry = Entry::InMemory(output);
|
||||
|
||||
second_compressor.compress(entry)?
|
||||
},
|
||||
}
|
||||
None => {
|
||||
let entry = Entry::Files(files);
|
||||
second_compressor.compress(entry)?
|
||||
}
|
||||
};
|
||||
|
||||
println!("{}: writing to {:?}. ({} bytes)", "info".yellow(), &output_path, bytes.len());
|
||||
fs::write(
|
||||
output_path,
|
||||
bytes)?;
|
||||
println!(
|
||||
"{}: writing to {:?}. ({} bytes)",
|
||||
"info".yellow(),
|
||||
&output_path,
|
||||
bytes.len()
|
||||
);
|
||||
fs::write(output_path, bytes)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
32
src/main.rs
32
src/main.rs
@ -1,13 +1,11 @@
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
mod cli;
|
||||
mod error;
|
||||
mod evaluator;
|
||||
mod extension;
|
||||
mod file;
|
||||
mod test;
|
||||
mod evaluator;
|
||||
mod utils;
|
||||
|
||||
mod compressors;
|
||||
@ -17,41 +15,33 @@ fn main() -> error::OuchResult<()>{
|
||||
let print_error = |err| {
|
||||
println!("{}", err);
|
||||
};
|
||||
|
||||
let matches = cli::get_matches();
|
||||
match cli::Command::try_from(matches) {
|
||||
Ok(command) => {
|
||||
match evaluator::Evaluator::evaluate(command) {
|
||||
Ok(_) => {},
|
||||
Err(err) => print_error(err)
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
print_error(err)
|
||||
}
|
||||
}
|
||||
cli::Command::try_from(matches)
|
||||
.map(|command| evaluator::Evaluator::evaluate(command).unwrap_or_else(print_error))
|
||||
.unwrap_or_else(print_error);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// fn main() -> error::OuchResult<()> {
|
||||
|
||||
// use tar::{Builder};
|
||||
// use walkdir::WalkDir;
|
||||
|
||||
//
|
||||
// let mut b = Builder::new(Vec::new());
|
||||
|
||||
//
|
||||
// for entry in WalkDir::new("src") {
|
||||
// let entry = entry?;
|
||||
// let mut file = std::fs::File::open(entry.path())?;
|
||||
// b.append_file(entry.path(), &mut file)?;
|
||||
// }
|
||||
|
||||
//
|
||||
// // let mut file = std::fs::File::open("Cargo.toml")?;
|
||||
// // b.append_file("Cargo.toml", &mut file)?;
|
||||
|
||||
//
|
||||
// let bytes = b.into_inner()?;
|
||||
|
||||
//
|
||||
// std::fs::write("daaaaamn.tar", bytes)?;
|
||||
|
||||
//
|
||||
// Ok(())
|
||||
// }
|
@ -14,8 +14,7 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub (crate) fn check_for_multiple_files(files: &Vec<PathBuf>, format: &CompressionFormat) -> OuchResult<()> {
|
||||
|
||||
pub (crate) fn check_for_multiple_files(files: &[PathBuf], format: &CompressionFormat) -> OuchResult<()> {
|
||||
if files.len() != 1 {
|
||||
eprintln!("{}: cannot compress multiple files directly to {:#?}.\n Try using an intermediate archival method such as Tar.\n Example: filename.tar{}", "[ERROR]".red(), format, format);
|
||||
return Err(Error::InvalidInput);
|
||||
|
Loading…
x
Reference in New Issue
Block a user