Add message when cannot open file for compressing

And simplified error definition
This commit is contained in:
João M. Bezerra 2021-10-07 15:11:54 -03:00
parent a739b5a482
commit 493f42dd64
2 changed files with 28 additions and 26 deletions

View File

@ -39,8 +39,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
.hint("")
.hint("Examples:")
.hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
.hint(format!(" ouch compress ... {}.zip", to_utf(&output_path)))
.into_owned();
.hint(format!(" ouch compress ... {}.zip", to_utf(&output_path)));
return Err(Error::with_reason(reason));
}
@ -68,8 +67,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
.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))
.into_owned();
.hint(format!(" To : {}", suggested_output_path));
return Err(Error::with_reason(reason));
}
@ -79,8 +77,7 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
.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)))
.into_owned();
.hint(format!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
return Err(Error::with_reason(reason));
}
@ -90,7 +87,13 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
return Ok(());
}
let output_file = fs::File::create(&output_path)?;
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))
})?;
// 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

View File

@ -70,19 +70,15 @@ impl FinalError {
Self { title: title.to_string(), details: vec![], hints: vec![] }
}
pub fn detail(&mut self, detail: impl ToString) -> &mut Self {
pub fn detail(mut self, detail: impl ToString) -> Self {
self.details.push(detail.to_string());
self
}
pub fn hint(&mut self, hint: impl ToString) -> &mut Self {
pub fn hint(mut self, hint: impl ToString) -> Self {
self.hints.push(hint.to_string());
self
}
pub fn into_owned(&mut self) -> Self {
std::mem::take(self)
}
}
impl fmt::Display for Error {
@ -92,8 +88,7 @@ impl fmt::Display for Error {
let error = FinalError::with_title(format!("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")
.into_owned();
.hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats");
error
}
@ -110,8 +105,7 @@ impl fmt::Display for Error {
Error::CompressingRootFolder => {
let error = 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.")
.into_owned();
.hint("Use a more appropriate tool for this, such as rsync.");
error
}
@ -122,8 +116,7 @@ impl fmt::Display for Error {
.hint(" - At least one input argument.")
.hint(" - The output argument.")
.hint("")
.hint("Example: `ouch compress image.png img.zip`")
.into_owned();
.hint("Example: `ouch compress image.png img.zip`");
error
}
@ -133,8 +126,7 @@ impl fmt::Display for Error {
.hint("You must provide:")
.hint(" - At least one input argument.")
.hint("")
.hint("Example: `ouch decompress imgs.tar.gz`")
.into_owned();
.hint("Example: `ouch decompress imgs.tar.gz`");
error
}
@ -143,16 +135,17 @@ impl fmt::Display for Error {
.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()))
.into_owned();
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()));
error
}
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()))
.into_owned(),
Error::CompressionTypo => FinalError::with_title("Possible typo detected").hint(format!(
"Did you mean '{}ouch compress{}'?",
magenta(),
reset()
)),
_err => {
todo!();
}
@ -202,3 +195,9 @@ impl From<oof::OofError> for Error {
Self::OofError(err)
}
}
impl From<FinalError> for Error {
fn from(err: FinalError) -> Self {
Self::Custom { reason: err }
}
}