From f52275068bb3a6add4c0a5975902b918f0d2984a Mon Sep 17 00:00:00 2001 From: AmineZouitine Date: Thu, 13 Oct 2022 20:10:33 +0200 Subject: [PATCH] add database errors --- src/{error_manager.rs => argument_errors.rs} | 0 src/database_errors.rs | 52 ++++++++++++++++++++ src/main.rs | 5 +- src/trash_manager.rs | 2 +- 4 files changed, 56 insertions(+), 3 deletions(-) rename src/{error_manager.rs => argument_errors.rs} (100%) create mode 100644 src/database_errors.rs diff --git a/src/error_manager.rs b/src/argument_errors.rs similarity index 100% rename from src/error_manager.rs rename to src/argument_errors.rs diff --git a/src/database_errors.rs b/src/database_errors.rs new file mode 100644 index 0000000..cff16c3 --- /dev/null +++ b/src/database_errors.rs @@ -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 {} diff --git a/src/main.rs b/src/main.rs index ded551f..6b54b0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/trash_manager.rs b/src/trash_manager.rs index de7c0ae..592d22c 100644 --- a/src/trash_manager.rs +++ b/src/trash_manager.rs @@ -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,