diff --git a/src/data_manager.rs b/src/data_manager.rs index 3d31f28..824df83 100644 --- a/src/data_manager.rs +++ b/src/data_manager.rs @@ -1,5 +1,7 @@ -use crate::structure_manager; +use std::process::exit; + use crate::trash_item::TrashItem; +use crate::{database_errors::RmtDataBaseErrors, structure_manager}; use rusqlite::{params, types::FromSql, Connection, Row}; // Create the database and the table to save information about deleted elements. @@ -7,10 +9,9 @@ pub fn setup_data_base(is_test: bool) -> Connection { let connection = structure_manager::create_data_base_file(is_test); let table_name = structure_manager::get_data_base_table_name(is_test); - connection - .execute( - &format!( - "CREATE TABLE IF NOT EXISTS {} ( + let stmt_result = connection.execute( + &format!( + "CREATE TABLE IF NOT EXISTS {} ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, hash NOT NULL UNIQUE, @@ -20,29 +21,41 @@ pub fn setup_data_base(is_test: bool) -> Connection { compression_size INTEGER, is_folder INTEGER NOT NULL )", - table_name - ), - [], - ) - .unwrap_or_else(|_| panic!("Unable to execute creation of {} table", table_name)); - - connection + table_name + ), + [], + ); + + match stmt_result { + Ok(_) => connection, + Err(_) => { + println!("{}", RmtDataBaseErrors::DataBaseCreation); + exit(1); + } + } } fn get(row: &Row, index: usize) -> T { - let element: T = row - .get(index) - .unwrap_or_else(|_| panic!("Get a {} not valid", index)); - element + match row.get(index) { + Ok(element) => element, + Err(_) => { + println!("{}", RmtDataBaseErrors::GetCellElement(index)); + exit(1); + } + } } // Find all elements on the table and convert them to TrashItems pub fn find_all_trash_items(connection: &Connection, is_test: bool) -> Vec { let table_name = structure_manager::get_data_base_table_name(is_test); - let mut stmt = connection - .prepare(&format!("SELECT * FROM {}", table_name)) - .expect("Cannot select every element in database"); + let mut stmt = match connection.prepare(&format!("SELECT * FROM {}", table_name)) { + Ok(stmt) => stmt, + Err(_) => { + println!("{}", RmtDataBaseErrors::SelectAllElements); + exit(1); + } + }; let mut trash_items = Vec::::new(); @@ -88,7 +101,7 @@ pub fn delete_trash_item_by_id(connection: &Connection, is_test: bool, id: i32) pub fn insert_trash_item(connection: &Connection, trash_item: &TrashItem, is_test: bool) { let table_name = structure_manager::get_data_base_table_name(is_test); - connection + let stmt_result = connection .execute( &format!("INSERT INTO {} (name, hash, path, date, real_size, compression_size, is_folder) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", table_name), params![ @@ -100,25 +113,29 @@ pub fn insert_trash_item(connection: &Connection, trash_item: &TrashItem, is_tes trash_item.compression_size, trash_item.is_folder ], - ) - .unwrap_or_else(|_| panic!("Unable to do insert element request with this values : {:?}", - trash_item)); + ); + + match stmt_result { + Ok(_) => (), + Err(_) => println!("{}", RmtDataBaseErrors::InsertTrashItem), + }; } pub fn delete_trash_item(connection: &Connection, trash_item_id: i32, is_test: bool) { let table_name = structure_manager::get_data_base_table_name(is_test); - connection - .execute( - &format!("DELETE FROM {} WHERE id = (?1)", table_name), - params![trash_item_id], - ) - .unwrap_or_else(|_| { - panic!( - "Unable to do delete element request with this id : {}", - trash_item_id - ) - }); + let stmt_result = connection.execute( + &format!("DELETE FROM {} WHERE id = (?1)", table_name), + params![trash_item_id], + ); + + match stmt_result { + Ok(_) => (), + Err(_) => { + println!("{}", RmtDataBaseErrors::DeleteElementById(trash_item_id)); + exit(1); + } + } } pub fn delete_all_trash_item(connection: &Connection, is_test: bool) { diff --git a/src/database_errors.rs b/src/database_errors.rs index cff16c3..dd32992 100644 --- a/src/database_errors.rs +++ b/src/database_errors.rs @@ -3,27 +3,34 @@ use std::error::Error; use colored::Colorize; -use crate::trash_item::{TrashItem}; +use crate::trash_item::TrashItem; #[derive(Debug)] pub enum RmtDataBaseErrors { DataBaseCreation, SelectAllElements, - GetCellElement(u8), + GetCellElement(usize), DeleteElementById(i32), - InsertTrashItem(TrashItem), - DeleteAllElement - + 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(trash_item) => format!("Impossible to insert the trashItem {}.", trash_item.to_string().red().bold()), + 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(), } } diff --git a/src/main.rs b/src/main.rs index 6b54b0a..35e57cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,14 @@ +pub mod argument_errors; pub mod arguments_manager; pub mod config; pub mod config_manager; pub mod data_manager; +pub mod database_errors; pub mod display_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; diff --git a/src/trash_manager.rs b/src/trash_manager.rs index 592d22c..c1c9702 100644 --- a/src/trash_manager.rs +++ b/src/trash_manager.rs @@ -1,6 +1,6 @@ +use crate::argument_errors::RmtArgumentErrors; use crate::arguments_manager::ArgumentsManager; use crate::display_manager; -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,