Skip to content

Commit

Permalink
add error database
Browse files Browse the repository at this point in the history
  • Loading branch information
AmineZouitine committed Oct 13, 2022
1 parent f522750 commit 6a35c61
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 46 deletions.
85 changes: 51 additions & 34 deletions src/data_manager.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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.
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,
Expand All @@ -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<T: FromSql>(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<TrashItem> {
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::<TrashItem>::new();

Expand Down Expand Up @@ -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![
Expand All @@ -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) {
Expand Down
25 changes: 16 additions & 9 deletions src/database_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
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::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,
Expand Down

0 comments on commit 6a35c61

Please sign in to comment.