Skip to content

Commit

Permalink
add database errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AmineZouitine committed Oct 13, 2022
1 parent a26bac2 commit f522750
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
File renamed without changes.
52 changes: 52 additions & 0 deletions src/database_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use core::fmt;
use std::error::Error;

use colored::Colorize;

use crate::trash_item::{TrashItem};

#[derive(Debug)]
pub enum RmtDataBaseErrors {
DataBaseCreation,
SelectAllElements,
GetCellElement(u8),
DeleteElementById(i32),
InsertTrashItem(TrashItem),
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(trash_item) => format!("Impossible to insert the trashItem {}.", trash_item.to_string().red().bold()),
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 {}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ pub mod config;
pub mod config_manager;
pub mod data_manager;
pub mod display_manager;
pub mod error_manager;
pub mod argument_errors;
pub mod input_manager;
pub mod structure_manager;
pub mod trash_item;
pub mod trash_manager;
pub mod database_errors;
use arguments_manager::ArgumentsManager;
use clap::Parser;
use colored::Colorize;

use crate::error_manager::RmtArgumentErrors;
use crate::argument_errors::RmtArgumentErrors;

fn main() {
let is_test = false;
Expand Down
2 changes: 1 addition & 1 deletion src/trash_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::arguments_manager::ArgumentsManager;
use crate::display_manager;
use crate::error_manager::RmtArgumentErrors;
use crate::argument_errors::RmtArgumentErrors;
use crate::structure_manager::{self, get_element_name, get_element_path, get_home_directory_path};
use crate::{
config::Config, data_manager, structure_manager::get_trash_directory_path,
Expand Down

0 comments on commit f522750

Please sign in to comment.