diff --git a/src/commands.rs b/src/commands.rs index 9086bb6..92a34b4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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 diff --git a/src/error.rs b/src/error.rs index 8c4f33c..82f8424 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 for Error { Self::OofError(err) } } + +impl From for Error { + fn from(err: FinalError) -> Self { + Self::Custom { reason: err } + } +}