-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from AmineZouitine/error_handling
Error handling
- Loading branch information
Showing
5 changed files
with
200 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use core::fmt; | ||
use std::error::Error; | ||
|
||
use colored::Colorize; | ||
|
||
#[derive(Debug)] | ||
pub enum RmtArgumentErrors { | ||
InvalidNumberOfArguments(usize), | ||
InvalidDirFlags { | ||
folder_name: String, | ||
element_in_folder: usize, | ||
}, | ||
InvalidFillFolderFlags { | ||
folder_name: String, | ||
}, | ||
InvalidEmptyFolderFlags { | ||
folder_name: String, | ||
}, | ||
InvalidElementName { | ||
element_name: String, | ||
}, | ||
} | ||
|
||
impl RmtArgumentErrors { | ||
fn error_message(&self) -> String { | ||
match self { | ||
RmtArgumentErrors::InvalidNumberOfArguments(args_number) => format!( | ||
"The number of arguments ({}) is not valid.", | ||
args_number.to_string().red().bold(), | ||
), | ||
RmtArgumentErrors::InvalidDirFlags { folder_name, element_in_folder } => format!("You cannot delete {} folder with the {} flags because there is {} elements inside, you should use {} flags instead.", folder_name.red().bold(), "-d".red().bold().green(), element_in_folder.to_string().red().bold(), "-r".green().bold()), | ||
RmtArgumentErrors::InvalidEmptyFolderFlags { folder_name } => format!("You cannot delete {} folder without using {} or {} flags.", folder_name.red().bold(), "-r".green().bold(), "-d".green().bold()), | ||
RmtArgumentErrors::InvalidFillFolderFlags { folder_name } => format!("You cannot delete {} folder without using {} flags (do no use {} flags because your directory isn't empty).", folder_name.red().bold(), "-r".green().bold(), "-d".green().bold()), | ||
RmtArgumentErrors::InvalidElementName { element_name} => format!("you cannot destroy your {} because it doesn't exist (use the {} option to stop getting this warning).", element_name.red().bold(), "-f".green().bold()) | ||
} | ||
} | ||
|
||
fn default_help_message() -> String { | ||
format!( | ||
"Use the {} option to get more details on how to use {}.", | ||
"rmt --help".bold().green(), | ||
"rmt".bold() | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for RmtArgumentErrors { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"{}: {}\n{}", | ||
"Error".red().bold(), | ||
self.error_message(), | ||
RmtArgumentErrors::default_help_message().italic() | ||
) | ||
} | ||
} | ||
|
||
impl Error for RmtArgumentErrors {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use core::fmt; | ||
use std::error::Error; | ||
|
||
use colored::Colorize; | ||
|
||
use crate::trash_item::TrashItem; | ||
|
||
#[derive(Debug)] | ||
pub enum RmtDataBaseErrors { | ||
DataBaseCreation, | ||
SelectAllElements, | ||
GetCellElement(usize), | ||
DeleteElementById(i32), | ||
InsertTrashItem, | ||
DeleteAllElement, | ||
} | ||
|
||
impl RmtDataBaseErrors { | ||
fn error_message(&self) -> String { | ||
match self { | ||
RmtDataBaseErrors::DataBaseCreation => "Impossible to create the database.".to_string(), | ||
RmtDataBaseErrors::SelectAllElements => { | ||
"Impossible to select all the elements.".to_string() | ||
} | ||
RmtDataBaseErrors::GetCellElement(index) => format!( | ||
"Impossible to retrieve the cell with index {}.", | ||
index.to_string().red().bold() | ||
), | ||
RmtDataBaseErrors::DeleteElementById(id) => format!( | ||
"Impossible to delete the element at index {}.", | ||
id.to_string().red().bold() | ||
), | ||
RmtDataBaseErrors::InsertTrashItem => format!("Impossible to insert the trashItem.",), | ||
RmtDataBaseErrors::DeleteAllElement => "Impossible to delete all elements.".to_string(), | ||
} | ||
} | ||
|
||
fn default_help_message() -> String { | ||
format!( | ||
"If you haven't touched the {} file yourself, please open an issue on: {} with as many details as possible.", | ||
".trash_rmt".red().bold(), | ||
"https://github.com/AmineZouitine/rmt.rs".bold().green() | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for RmtDataBaseErrors { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"{}: {}\n{}", | ||
"Error".red().bold(), | ||
self.error_message(), | ||
RmtDataBaseErrors::default_help_message().italic() | ||
) | ||
} | ||
} | ||
|
||
impl Error for RmtDataBaseErrors {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters