Update README, slightly reduce code repetition

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-24 18:35:01 -03:00
parent 6eee06a51a
commit 9ea18659e5
10 changed files with 38 additions and 45 deletions

2
Cargo.lock generated
View File

@ -189,7 +189,7 @@ dependencies = [
[[package]] [[package]]
name = "ouch" name = "ouch"
version = "0.1.1" version = "0.1.2"
dependencies = [ dependencies = [
"bzip2 0.4.2", "bzip2 0.4.2",
"clap", "clap",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ouch" name = "ouch"
version = "0.1.1" version = "0.1.2"
authors = ["Vinícius Rodrigues Miguel <vrmiguel99@gmail.com>"] authors = ["Vinícius Rodrigues Miguel <vrmiguel99@gmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

View File

@ -3,17 +3,17 @@
`ouch` is the Obvious Unified Compression (and decompression) Helper. `ouch` is the Obvious Unified Compression (and decompression) Helper.
| Supported formats | .tar | .zip | .tar.{.lz,.gz, .bz} | .zip.{.lz, .gz, .bz, .bz2} | .bz | .gz | .lz, .lzma | | Supported formats | .tar | .zip | .tar.{.lz*,.gz, .bz} | .zip.{.lz*, .gz, .bz*} | .bz | .gz | .lz, .lzma |
|-------------------|------|------|------------------------------|------------------------------|-----|-----|------------| |-------------------|------|------|------------------------------|------------------------------|-----|-----|------------|
| Decompression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Decompression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Compression | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ | | Compression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
## How does it work? ## How does it work?
`ouch` infers commands from the extensions of its command-line options. `ouch` infers commands from the extensions of its command-line options.
``` ```
ouch 0.1.1 ouch 0.1.2
Vinícius R. Miguel Vinícius R. Miguel
ouch is a unified compression & decompression utility ouch is a unified compression & decompression utility

View File

@ -27,7 +27,7 @@ pub struct Command {
pub fn clap_app<'a, 'b>() -> clap::App<'a, 'b> { pub fn clap_app<'a, 'b>() -> clap::App<'a, 'b> {
clap::App::new("ouch") clap::App::new("ouch")
.version("0.1.1") .version("0.1.2")
.about("ouch is a unified compression & decompression utility") .about("ouch is a unified compression & decompression utility")
.after_help( .after_help(
"ouch infers what to based on the extensions of the input files and output file received. "ouch infers what to based on the extensions of the input files and output file received.

View File

@ -3,7 +3,7 @@ use std::{fs, io::Write, path::PathBuf};
use colored::Colorize; use colored::Colorize;
use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File};
use crate::utils::ensure_exists; use crate::utils::{ensure_exists, check_for_multiple_files};
use super::{Compressor, Entry}; use super::{Compressor, Entry};
@ -11,10 +11,7 @@ pub struct BzipCompressor {}
impl BzipCompressor { impl BzipCompressor {
fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> { fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> {
if files.len() != 1 { check_for_multiple_files(&files, &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);
}
let path = &files[0]; let path = &files[0];
ensure_exists(path)?; ensure_exists(path)?;
let contents = { let contents = {

View File

@ -2,8 +2,11 @@ use std::{fs, io::Write, path::PathBuf};
use colored::Colorize; use colored::Colorize;
use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; use crate::{error::OuchResult, extension::CompressionFormat, file::File};
use crate::utils::ensure_exists; use crate::utils::{
ensure_exists,
check_for_multiple_files
};
use super::{Compressor, Entry}; use super::{Compressor, Entry};
@ -11,10 +14,7 @@ pub struct GzipCompressor {}
impl GzipCompressor { impl GzipCompressor {
pub fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> { pub fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> {
if files.len() != 1 { check_for_multiple_files(&files, &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);
}
let path = &files[0]; let path = &files[0];
ensure_exists(path)?; ensure_exists(path)?;

View File

@ -2,8 +2,11 @@ use std::{fs, io::Write, path::PathBuf};
use colored::Colorize; use colored::Colorize;
use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File}; use crate::{error::{OuchResult}, extension::CompressionFormat, file::File};
use crate::utils::ensure_exists; use crate::utils::{
ensure_exists,
check_for_multiple_files
};
use super::{Compressor, Entry}; use super::{Compressor, Entry};
@ -11,10 +14,7 @@ pub struct LzmaCompressor {}
impl LzmaCompressor { impl LzmaCompressor {
pub fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> { pub fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> {
if files.len() != 1 { check_for_multiple_files(&files, &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);
}
let path = &files[0]; let path = &files[0];
ensure_exists(path)?; ensure_exists(path)?;

View File

@ -12,20 +12,10 @@ pub struct TarCompressor {}
impl TarCompressor { impl TarCompressor {
// TODO: this function does not seem to be working correctly ;/ // 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());
let _contents = match input.contents_in_memory { Err(Error::InvalidZipArchive(""))
Some(bytes) => bytes,
None => {
eprintln!("{}: reached TarCompressor::make_archive_from_memory without known content.", "internal error".red());
return Err(Error::InvalidInput);
}
};
println!("todo");
Ok(vec![])
} }
fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> { fn make_archive_from_files(input_filenames: Vec<PathBuf>) -> OuchResult<Vec<u8>> {

View File

@ -24,10 +24,6 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Error::InvalidInput => write!(
f,
"When `-o/--output` is omitted, all input files should be compressed files."
),
Error::MissingExtensionError(filename) => { Error::MissingExtensionError(filename) => {
write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename) write!(f, "cannot compress to \'{}\', likely because it has an unsupported (or missing) extension.", filename)
}, },
@ -38,9 +34,9 @@ impl fmt::Display for Error {
Error::FileNotFound => { Error::FileNotFound => {
write!(f, "file not found!") write!(f, "file not found!")
} }
err => { _err => {
// TODO // TODO
write!(f, "todo: missing description for error {:?}", err) write!(f, "")
} }
} }
} }

View File

@ -1,7 +1,7 @@
use std::{fs, path::Path}; use std::{fs, path::{Path, PathBuf}};
use colored::Colorize; use colored::Colorize;
use crate::{error::OuchResult, file::File}; use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File};
pub (crate) fn ensure_exists<'a, P>(path: P) -> OuchResult<()> pub (crate) fn ensure_exists<'a, P>(path: P) -> OuchResult<()>
where where
@ -13,6 +13,16 @@ where
Ok(()) Ok(())
} }
pub (crate) fn check_for_multiple_files(files: &Vec<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);
}
Ok(())
}
pub (crate) fn create_path_if_non_existent(path: &Path) -> OuchResult<()> { pub (crate) fn create_path_if_non_existent(path: &Path) -> OuchResult<()> {
if !path.exists() { if !path.exists() {
println!( println!(