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