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]]
name = "ouch"
version = "0.1.1"
version = "0.1.2"
dependencies = [
"bzip2 0.4.2",
"clap",

View File

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

View File

@ -3,17 +3,17 @@
`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 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Compression | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ |
| Compression | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
## How does it work?
`ouch` infers commands from the extensions of its command-line options.
```
ouch 0.1.1
ouch 0.1.2
Vinícius R. Miguel
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> {
clap::App::new("ouch")
.version("0.1.1")
.version("0.1.2")
.about("ouch is a unified compression & decompression utility")
.after_help(
"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 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};
@ -11,10 +11,7 @@ pub struct BzipCompressor {}
impl BzipCompressor {
fn compress_files(files: Vec<PathBuf>, format: CompressionFormat) -> OuchResult<Vec<u8>> {
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);
}
check_for_multiple_files(&files, &format)?;
let path = &files[0];
ensure_exists(path)?;
let contents = {

View File

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

View File

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

View File

@ -12,20 +12,10 @@ pub struct TarCompressor {}
impl TarCompressor {
// TODO: this function does not seem to be working correctly ;/
fn make_archive_from_memory(input: File) -> OuchResult<Vec<u8>> {
let _contents = match input.contents_in_memory {
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![])
// TODO: implement this
fn make_archive_from_memory(_input: File) -> OuchResult<Vec<u8>> {
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>> {

View File

@ -24,10 +24,6 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::InvalidInput => write!(
f,
"When `-o/--output` is omitted, all input files should be compressed files."
),
Error::MissingExtensionError(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 => {
write!(f, "file not found!")
}
err => {
_err => {
// 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 crate::{error::OuchResult, file::File};
use crate::{error::{Error, OuchResult}, extension::CompressionFormat, file::File};
pub (crate) fn ensure_exists<'a, P>(path: P) -> OuchResult<()>
where
@ -13,6 +13,16 @@ where
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<()> {
if !path.exists() {
println!(