Skip to content

Commit

Permalink
feat: use Cow<'static, str> in FinalError (ouch-org#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrmiguel committed Jan 16, 2022
1 parent 4b2d634 commit bf9e6b7
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//!
//! All usage errors will pass throught the Error enum, a lot of them in the Error::Custom.

use std::fmt::{self, Display};
use std::{
borrow::Cow,
fmt::{self, Display},
};

use crate::utils::colors::*;

Expand Down Expand Up @@ -35,15 +38,18 @@ pub enum Error {
/// Alias to std's Result with ouch's Error
pub type Result<T> = std::result::Result<T, Error>;

/// A string either heap-allocated or located in static storage
pub type CowStr = Cow<'static, str>;

/// Pretty final error message for end users, crashing the program after display.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FinalError {
/// Should be made of just one line, appears after the "\[ERROR\]" part
title: String,
title: CowStr,
/// Shown as a unnumbered list in yellow
details: Vec<String>,
details: Vec<CowStr>,
/// Shown as green at the end to give hints on how to work around this error, if it's fixable
hints: Vec<String>,
hints: Vec<CowStr>,
}

impl Display for FinalError {
Expand Down Expand Up @@ -87,41 +93,45 @@ impl Display for FinalError {
impl FinalError {
/// Only constructor
#[must_use]
pub fn with_title(title: impl ToString) -> Self {
Self { title: title.to_string(), details: vec![], hints: vec![] }
pub fn with_title(title: impl Into<CowStr>) -> Self {
Self { title: title.into(), details: vec![], hints: vec![] }
}

/// Add one detail line, can have multiple
#[must_use]
pub fn detail(mut self, detail: impl ToString) -> Self {
self.details.push(detail.to_string());
pub fn detail(mut self, detail: impl Into<CowStr>) -> Self {
self.details.push(detail.into());
self
}

/// Add one hint line, can have multiple
#[must_use]
pub fn hint(mut self, hint: impl ToString) -> Self {
self.hints.push(hint.to_string());
pub fn hint(mut self, hint: impl Into<CowStr>) -> Self {
self.hints.push(hint.into());
self
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let err = match self {
Error::WalkdirError { reason } => FinalError::with_title(reason),
Error::NotFound { error_title } => FinalError::with_title(error_title).detail("File not found"),
Error::WalkdirError { reason } => FinalError::with_title(reason.to_string()),
Error::NotFound { error_title } => FinalError::with_title(error_title.to_string()).detail("File not found"),
Error::CompressingRootFolder => {
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::IoError { reason } => FinalError::with_title(reason),
Error::Lz4Error { reason } => FinalError::with_title(reason),
Error::AlreadyExists { error_title } => FinalError::with_title(error_title).detail("File already exists"),
Error::InvalidZipArchive(reason) => FinalError::with_title("Invalid zip archive").detail(reason),
Error::PermissionDenied { error_title } => FinalError::with_title(error_title).detail("Permission denied"),
Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(reason),
Error::IoError { reason } => FinalError::with_title(reason.to_string()),
Error::Lz4Error { reason } => FinalError::with_title(reason.to_string()),
Error::AlreadyExists { error_title } => {
FinalError::with_title(error_title.to_string()).detail("File already exists")
}
Error::InvalidZipArchive(reason) => FinalError::with_title("Invalid zip archive").detail(*reason),
Error::PermissionDenied { error_title } => {
FinalError::with_title(error_title.to_string()).detail("Permission denied")
}
Error::UnsupportedZipArchive(reason) => FinalError::with_title("Unsupported zip archive").detail(*reason),
Error::Custom { reason } => reason.clone(),
};

Expand Down

0 comments on commit bf9e6b7

Please sign in to comment.