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("")
.hint("Examples:") .hint("Examples:")
.hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path))) .hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path)))
.hint(format!(" ouch compress ... {}.zip", to_utf(&output_path))) .hint(format!(" ouch compress ... {}.zip", to_utf(&output_path)));
.into_owned();
return Err(Error::with_reason(reason)); 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.") .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!("Try inserting '.tar' or '.zip' before '{}'.", &formats[0]))
.hint(format!("From: {}", output_path)) .hint(format!("From: {}", output_path))
.hint(format!(" To : {}", suggested_output_path)) .hint(format!(" To : {}", suggested_output_path));
.into_owned();
return Err(Error::with_reason(reason)); 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!("Found the format '{}' in an incorrect position.", format))
.detail(format!("{} can only be used at the start of the file extension.", 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!("If you wish to compress multiple files, start the extension with {}.", format))
.hint(format!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path))) .hint(format!("Otherwise, remove {} from '{}'.", format, to_utf(&output_path)));
.into_owned();
return Err(Error::with_reason(reason)); return Err(Error::with_reason(reason));
} }
@ -90,7 +87,13 @@ pub fn run(command: Command, flags: &oof::Flags) -> crate::Result<()> {
return Ok(()); 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); let compress_result = compress_files(files, formats, output_file, flags);
// If any error occurred, delete incomplete file // If any error occurred, delete incomplete file

View File

@ -70,19 +70,15 @@ impl FinalError {
Self { title: title.to_string(), details: vec![], hints: vec![] } 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.details.push(detail.to_string());
self 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.hints.push(hint.to_string());
self self
} }
pub fn into_owned(&mut self) -> Self {
std::mem::take(self)
}
} }
impl fmt::Display for Error { impl fmt::Display for Error {
@ -92,8 +88,7 @@ impl fmt::Display for Error {
let error = FinalError::with_title(format!("Cannot compress to {:?}", filename)) let error = FinalError::with_title(format!("Cannot compress to {:?}", filename))
.detail("Ouch could not detect the compression format") .detail("Ouch could not detect the compression format")
.hint("Use a supported format extension, like '.zip' or '.tar.gz'") .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") .hint("Check https://github.com/vrmiguel/ouch for a full list of supported formats");
.into_owned();
error error
} }
@ -110,8 +105,7 @@ impl fmt::Display for Error {
Error::CompressingRootFolder => { Error::CompressingRootFolder => {
let error = FinalError::with_title("It seems you're trying to compress the root folder.") 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.") .detail("This is unadvisable since ouch does compressions in-memory.")
.hint("Use a more appropriate tool for this, such as rsync.") .hint("Use a more appropriate tool for this, such as rsync.");
.into_owned();
error error
} }
@ -122,8 +116,7 @@ impl fmt::Display for Error {
.hint(" - At least one input argument.") .hint(" - At least one input argument.")
.hint(" - The output argument.") .hint(" - The output argument.")
.hint("") .hint("")
.hint("Example: `ouch compress image.png img.zip`") .hint("Example: `ouch compress image.png img.zip`");
.into_owned();
error error
} }
@ -133,8 +126,7 @@ impl fmt::Display for Error {
.hint("You must provide:") .hint("You must provide:")
.hint(" - At least one input argument.") .hint(" - At least one input argument.")
.hint("") .hint("")
.hint("Example: `ouch decompress imgs.tar.gz`") .hint("Example: `ouch decompress imgs.tar.gz`");
.into_owned();
error error
} }
@ -143,16 +135,17 @@ impl fmt::Display for Error {
.detail("This should not have happened") .detail("This should not have happened")
.detail("It's probably our fault") .detail("It's probably our fault")
.detail("Please help us improve by reporting the issue at:") .detail("Please help us improve by reporting the issue at:")
.detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan())) .detail(format!(" {}https://github.com/vrmiguel/ouch/issues ", cyan()));
.into_owned();
error error
} }
Error::OofError(err) => FinalError::with_title(err), Error::OofError(err) => FinalError::with_title(err),
Error::IoError { reason } => FinalError::with_title(reason), Error::IoError { reason } => FinalError::with_title(reason),
Error::CompressionTypo => FinalError::with_title("Possible typo detected") Error::CompressionTypo => FinalError::with_title("Possible typo detected").hint(format!(
.hint(format!("Did you mean '{}ouch compress{}'?", magenta(), reset())) "Did you mean '{}ouch compress{}'?",
.into_owned(), magenta(),
reset()
)),
_err => { _err => {
todo!(); todo!();
} }
@ -202,3 +195,9 @@ impl From<oof::OofError> for Error {
Self::OofError(err) Self::OofError(err)
} }
} }
impl From<FinalError> for Error {
fn from(err: FinalError) -> Self {
Self::Custom { reason: err }
}
}