Adds a new impl in the interface
This commit is contained in:
parent
bfe9285b43
commit
f044d26a10
|
|
@ -5,7 +5,7 @@
|
|||
//! Should dump whole database in a vec of structs from weaponry_structs
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::{error::FatalEquipementError, interfaces::RecipeDatabase};
|
||||
use crate::{error::EquipementError, interfaces::RecipeDatabase};
|
||||
use rusqlite::{self, Connection};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
|
|
@ -16,13 +16,6 @@ pub struct EquipementDatabase {
|
|||
}
|
||||
|
||||
impl EquipementDatabase {
|
||||
/// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory.
|
||||
fn new(filename: Option<String>) -> Result<Self, FatalEquipementError> {
|
||||
Ok(Self {
|
||||
conn: filename.map_or_else(Connection::open_in_memory, Connection::open)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Public function to get data from db. Returns the result as a vec with structs
|
||||
pub fn get_data_from_fields<T>(
|
||||
&self,
|
||||
|
|
@ -32,16 +25,10 @@ impl EquipementDatabase {
|
|||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let gen_query = {
|
||||
let fields = fields;
|
||||
let table_name = table_name;
|
||||
format!("SELECT {} FROM {};", fields.join(", "), table_name)
|
||||
};
|
||||
let mut query_result = self.conn.prepare(&gen_query)?;
|
||||
let res = serde_rusqlite::from_rows::<T>(query_result.query([])?)
|
||||
let mut query_result = self.conn.prepare(&generate_query(fields, table_name))?;
|
||||
Ok(serde_rusqlite::from_rows::<T>(query_result.query([])?)
|
||||
.filter_map(|s| s.ok())
|
||||
.collect();
|
||||
Ok(res)
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,6 +43,13 @@ impl RecipeDatabase for EquipementDatabase {
|
|||
) -> Result<Vec<T>, crate::error::EquipementError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Checks if database exists, and returns a rusqlite::Connection. If not, opens database in memory.
|
||||
fn new(filename: &str) -> Result<Self, EquipementError> {
|
||||
Ok(EquipementDatabase {
|
||||
conn: Connection::open(filename)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate a SELECT query with fields generated from a struct. Returns a String
|
||||
|
|
@ -77,14 +71,19 @@ mod test {
|
|||
name: String,
|
||||
}
|
||||
|
||||
fn set_up() -> Result<EquipementDatabase, FatalEquipementError> {
|
||||
let equip_db = EquipementDatabase::new(None)?;
|
||||
equip_db.conn.execute("CREATE TABLE IF NOT EXISTS TestStruct (id INTEGER UNIQUE, name TEXT, PRIMARY KEY('id' AUTOINCREMENT));",())?;
|
||||
equip_db.conn.execute(
|
||||
"INSERT INTO TestStruct (id, name) VALUES (1, 'dupont'), (2, 'durand');",
|
||||
(),
|
||||
)?;
|
||||
Ok(equip_db)
|
||||
fn mock_set_up() -> EquipementDatabase {
|
||||
let equip_db = EquipementDatabase {
|
||||
conn: Connection::open_in_memory().unwrap(),
|
||||
};
|
||||
equip_db.conn.execute("CREATE TABLE IF NOT EXISTS TestStruct (id INTEGER UNIQUE, name TEXT, PRIMARY KEY('id' AUTOINCREMENT));",()).unwrap();
|
||||
equip_db
|
||||
.conn
|
||||
.execute(
|
||||
"INSERT INTO TestStruct (id, name) VALUES (1, 'dupont'), (2, 'durand');",
|
||||
(),
|
||||
)
|
||||
.unwrap();
|
||||
equip_db
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -110,7 +109,7 @@ mod test {
|
|||
#[ignore]
|
||||
#[test]
|
||||
fn test_get_data() {
|
||||
let conn = set_up().unwrap();
|
||||
let conn = mock_set_up();
|
||||
let result = conn
|
||||
.get_data_from_fields::<TestStruct>(&TestStruct::FIELD_NAMES_AS_ARRAY, "TestStruct")
|
||||
.unwrap();
|
||||
|
|
|
|||
|
|
@ -5,27 +5,29 @@
|
|||
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
/// Unrecoverable Error, all these error cannot gracefully fail and must return to the User for further handling
|
||||
pub enum FatalEquipementError {
|
||||
#[error("Rusqlite Error")]
|
||||
RusqliteConnection(#[from] rusqlite::Error),
|
||||
}
|
||||
// #[derive(Error, Debug)]
|
||||
// /// Unrecoverable Error, all these error cannot gracefully fail and must return to the User for further handling
|
||||
// pub enum FatalEquipementError {
|
||||
// #[error("Rusqlite failed to connect to the given path {0}")]
|
||||
// RusqliteConnection(#[from] rusqlite::Error),
|
||||
// }
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
/// Recoverable error, all these error exist to signal a retry or another path that should be taken by the program
|
||||
pub enum TrivialEquipementError {
|
||||
#[error("Very simple error")]
|
||||
Oops,
|
||||
}
|
||||
// #[derive(Error, Debug)]
|
||||
// /// Recoverable error, all these error exist to signal a retry or another path that should be taken by the program
|
||||
// pub enum TrivialEquipementError {
|
||||
// #[error("Very simple error")]
|
||||
// Oops,
|
||||
// }
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
/// An equipement error, either unrecoverable, indicating a that the execution
|
||||
/// flow must be stopped and only an user input can solve the issue
|
||||
pub enum EquipementError {
|
||||
#[error("An unrecoverable error occured: {0}")]
|
||||
Unrecoverable(#[from] FatalEquipementError),
|
||||
// #[error("An unrecoverable error occured: {0}")]
|
||||
// Unrecoverable(#[from] FatalEquipementError),
|
||||
|
||||
#[error("A recoverable error occured but wasn't caught by the programming: {0}")]
|
||||
Recoverable(#[from] TrivialEquipementError),
|
||||
// #[error("A recoverable error occured but wasn't caught by the programming: {0}")]
|
||||
// Recoverable(#[from] TrivialEquipementError),
|
||||
#[error("Rusqlite failed to connect to the given path {0}")]
|
||||
RusqliteConnection(#[from] rusqlite::Error),
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue