mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-08 12:35:41 +00:00
Minor formatting and naming changes
This commit is contained in:
parent
588359e9e5
commit
fc2e38a182
@ -33,13 +33,13 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
||||
let formats = extension::extensions_from_path(&output_path);
|
||||
|
||||
if formats.is_empty() {
|
||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail("You shall supply the compression format via the extension.")
|
||||
.hint("Try adding something like .tar.gz or .zip to the output file.")
|
||||
.hint("")
|
||||
.hint("Examples:")
|
||||
.hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
|
||||
.hint(format!(" ouch compress ... {}.zip", to_utf(&output_path)));
|
||||
.hint(f!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
|
||||
.hint(f!(" ouch compress ... {}.zip", to_utf(&output_path)));
|
||||
|
||||
return Err(Error::with_reason(reason));
|
||||
}
|
||||
@ -61,23 +61,23 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
||||
let mut suggested_output_path = output_path.clone();
|
||||
suggested_output_path.replace_range(empty_range, ".tar");
|
||||
|
||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail("You are trying to compress multiple files.")
|
||||
.detail(format!("The compression format '{}' cannot receive multiple files.", &formats[0]))
|
||||
.detail(f!("The compression format '{}' cannot receive multiple files.", &formats[0]))
|
||||
.detail("The only supported formats that archive files into an archive are .tar and .zip.")
|
||||
.hint(format!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
|
||||
.hint(format!("From: {}", output_path))
|
||||
.hint(format!(" To : {}", suggested_output_path));
|
||||
.hint(f!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
|
||||
.hint(f!("From: {}", output_path))
|
||||
.hint(f!(" To : {}", suggested_output_path));
|
||||
|
||||
return Err(Error::with_reason(reason));
|
||||
}
|
||||
|
||||
if let Some(format) = formats.iter().skip(1).position(|format| matches!(format, Tar | Zip)) {
|
||||
let reason = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail(format!("Found the format '{}' in an incorrect position.", format))
|
||||
.detail(format!("{} can only be used at the start of the file extension.", format))
|
||||
.hint(format!("If you wish to compress multiple files, start the extension with {}.", format))
|
||||
.hint(format!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
|
||||
let reason = FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail(f!("Found the format '{}' in an incorrect position.", format))
|
||||
.detail(f!("{} can only be used at the start of the file extension.", format))
|
||||
.hint(f!("If you wish to compress multiple files, start the extension with {}.", format))
|
||||
.hint(f!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
|
||||
|
||||
return Err(Error::with_reason(reason));
|
||||
}
|
||||
@ -88,12 +88,11 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
|
||||
}
|
||||
|
||||
let output_file = fs::File::create(&output_path).map_err(|err| {
|
||||
FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail(format!("Could not open file '{}' for writing.", to_utf(&output_path)))
|
||||
.detail(format!("Error: {}.", err))
|
||||
FinalError::with_title(f!("Cannot compress to '{}'.", to_utf(&output_path)))
|
||||
.detail(f!("Could not open file '{}' for writing.", to_utf(&output_path)))
|
||||
.detail(f!("Error: {}.", err))
|
||||
})?;
|
||||
|
||||
// let output_file = fs::File::create(&output_path)?;
|
||||
let compress_result = compress_files(files, formats, output_file, flags);
|
||||
|
||||
// If any error occurred, delete incomplete file
|
||||
|
50
src/error.rs
50
src/error.rs
@ -43,7 +43,7 @@ pub struct FinalError {
|
||||
}
|
||||
|
||||
impl Display for FinalError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// Title
|
||||
writeln!(f, "{}[ERROR]{} {}", red(), reset(), self.title)?;
|
||||
|
||||
@ -79,73 +79,69 @@ impl FinalError {
|
||||
self.hints.push(hint.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
// // In case we want to use mutable references in the future
|
||||
// pub fn detail_ref(&mut self, detail: impl ToString) -> Self;
|
||||
// pub fn hint_ref(&mut self, detail: impl ToString) -> Self;
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let err = match self {
|
||||
Error::MissingExtensionError(filename) => {
|
||||
let error = FinalError::with_title(format!("Cannot compress to {:?}", filename))
|
||||
FinalError::with_title(f!("Cannot compress to {:?}", filename))
|
||||
.detail("Ouch could not detect the compression format")
|
||||
.hint("Use a supported format extension, like '.zip' or '.tar.gz'")
|
||||
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats");
|
||||
|
||||
error
|
||||
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats")
|
||||
}
|
||||
Error::WalkdirError { reason } => FinalError::with_title(reason),
|
||||
Error::FileNotFound(file) => {
|
||||
let error = if file == Path::new("") {
|
||||
FinalError::with_title("file not found!")
|
||||
} else {
|
||||
FinalError::with_title(format!("file {:?} not found!", file))
|
||||
FinalError::with_title(f!("file {:?} not found!", file))
|
||||
};
|
||||
|
||||
error
|
||||
}
|
||||
Error::CompressingRootFolder => {
|
||||
let error = FinalError::with_title("It seems you're trying to compress the root folder.")
|
||||
FinalError::with_title("It seems you're trying to compress the root folder.")
|
||||
.detail("This is unadvisable since ouch does compressions in-memory.")
|
||||
.hint("Use a more appropriate tool for this, such as rsync.");
|
||||
|
||||
error
|
||||
.hint("Use a more appropriate tool for this, such as rsync.")
|
||||
}
|
||||
Error::MissingArgumentsForCompression => {
|
||||
let error = FinalError::with_title("Could not compress")
|
||||
FinalError::with_title("Could not compress")
|
||||
.detail("The compress command requires at least 2 arguments")
|
||||
.hint("You must provide:")
|
||||
.hint(" - At least one input argument.")
|
||||
.hint(" - The output argument.")
|
||||
.hint("")
|
||||
.hint("Example: `ouch compress image.png img.zip`");
|
||||
|
||||
error
|
||||
.hint("Example: `ouch compress image.png img.zip`")
|
||||
}
|
||||
Error::MissingArgumentsForDecompression => {
|
||||
let error = FinalError::with_title("Could not decompress")
|
||||
FinalError::with_title("Could not decompress")
|
||||
.detail("The compress command requires at least one argument")
|
||||
.hint("You must provide:")
|
||||
.hint(" - At least one input argument.")
|
||||
.hint("")
|
||||
.hint("Example: `ouch decompress imgs.tar.gz`");
|
||||
|
||||
error
|
||||
.hint("Example: `ouch decompress imgs.tar.gz`")
|
||||
}
|
||||
Error::InternalError => {
|
||||
let error = FinalError::with_title("InternalError :(")
|
||||
FinalError::with_title("InternalError :(")
|
||||
.detail("This should not have happened")
|
||||
.detail("It's probably our fault")
|
||||
.detail("Please help us improve by reporting the issue at:")
|
||||
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()));
|
||||
|
||||
error
|
||||
.detail(f!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()))
|
||||
}
|
||||
Error::OofError(err) => FinalError::with_title(err),
|
||||
Error::IoError { reason } => FinalError::with_title(reason),
|
||||
Error::CompressionTypo => FinalError::with_title("Possible typo detected").hint(format!(
|
||||
"Did you mean '{}ouch compress{}'?",
|
||||
magenta(),
|
||||
reset()
|
||||
)),
|
||||
Error::CompressionTypo => {
|
||||
FinalError::with_title("Possible typo detected").hint(f!(
|
||||
"Did you mean '{}ouch compress{}'?",
|
||||
magenta(),
|
||||
reset()
|
||||
))
|
||||
}
|
||||
_err => {
|
||||
todo!();
|
||||
}
|
||||
|
@ -4,6 +4,9 @@
|
||||
//! 1. It's required by `main.rs`, or
|
||||
//! 2. It's required by some integration tests at tests/ folder.
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
// Public modules
|
||||
pub mod cli;
|
||||
pub mod commands;
|
||||
@ -14,7 +17,6 @@ pub mod archive;
|
||||
mod dialogs;
|
||||
mod error;
|
||||
mod extension;
|
||||
mod macros;
|
||||
mod utils;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
@ -1,5 +1,13 @@
|
||||
use crate::NO_COLOR_IS_SET;
|
||||
|
||||
// f!() is an alias to f!()
|
||||
#[macro_export]
|
||||
macro_rules! f {
|
||||
{ $($tokens:tt)* } => {
|
||||
format!( $($tokens)* )
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($writer:expr, $($arg:tt)*) => {
|
||||
|
@ -31,18 +31,22 @@ impl fmt::Display for OofError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// TODO: implement proper debug messages
|
||||
match self {
|
||||
OofError::FlagValueConflict { flag, previous_value, new_value } => write!(
|
||||
f,
|
||||
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
|
||||
flag.long, previous_value, new_value
|
||||
),
|
||||
OofError::FlagValueConflict { flag, previous_value, new_value } => {
|
||||
write!(
|
||||
f,
|
||||
"CLI flag value conflicted for flag '--{}', previous: {:?}, new: {:?}.",
|
||||
flag.long, previous_value, new_value
|
||||
)
|
||||
}
|
||||
OofError::InvalidUnicode(flag) => write!(f, "{:?} is not valid Unicode.", flag),
|
||||
OofError::UnknownShortFlag(ch) => write!(f, "Unknown argument '-{}'", ch),
|
||||
OofError::MisplacedShortArgFlagError(ch) => write!(
|
||||
f,
|
||||
"Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.",
|
||||
ch
|
||||
),
|
||||
OofError::MisplacedShortArgFlagError(ch) => {
|
||||
write!(
|
||||
f,
|
||||
"Invalid placement of `-{}`.\nOnly the last letter in a sequence of short flags can take values.",
|
||||
ch
|
||||
)
|
||||
}
|
||||
OofError::MissingValueToFlag(flag) => write!(f, "Flag {} takes value but none was supplied.", flag),
|
||||
OofError::DuplicatedFlag(flag) => write!(f, "Duplicated usage of {}.", flag),
|
||||
OofError::UnknownLongFlag(flag) => write!(f, "Unknown argument '--{}'", flag),
|
||||
|
@ -26,7 +26,7 @@ pub struct Flag {
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Flag {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self.short {
|
||||
Some(short_flag) => write!(f, "-{}/--{}", short_flag, self.long),
|
||||
None => write!(f, "--{}", self.long),
|
||||
|
@ -47,7 +47,7 @@ pub fn user_wants_to_overwrite(path: &Path, flags: &oof::Flags) -> crate::Result
|
||||
}
|
||||
|
||||
pub fn to_utf(os_str: impl AsRef<OsStr>) -> String {
|
||||
let text = format!("{:?}", os_str.as_ref());
|
||||
let text = f!("{:?}", os_str.as_ref());
|
||||
text.trim_matches('"').to_string()
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ impl Bytes {
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Bytes {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let num = self.bytes;
|
||||
debug_assert!(num >= 0.0);
|
||||
if num < 1_f64 {
|
||||
@ -135,7 +135,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_pretty_bytes_formatting() {
|
||||
fn format_bytes(bytes: u64) -> String {
|
||||
format!("{}", Bytes::new(bytes))
|
||||
f!("{}", Bytes::new(bytes))
|
||||
}
|
||||
let b = 1;
|
||||
let kb = b * 1000;
|
||||
|
Loading…
x
Reference in New Issue
Block a user