156 lines
5.9 KiB
Rust
156 lines
5.9 KiB
Rust
// Workspace: weaponry
|
|
// File: weaponry/src/weaponry_structs/equipement_impl_database.rs
|
|
//! All database specific implementations
|
|
|
|
use super::EquipementSerializedCategory;
|
|
use super::equipement_fields::{WeaponBaseShape, WeaponBaseSize};
|
|
use super::equipement_structs::{DatabaseEquipementStats, InnerEquipementStats};
|
|
use crate::error::FatalEquipementError;
|
|
use crate::interfaces::{FixedRecipe, RecipeDatabase};
|
|
use rusqlite::Connection;
|
|
|
|
/// Struct for database connection.
|
|
#[derive(Debug)]
|
|
pub struct EquipementDatabase {
|
|
/// Connection parameter for rusqlite
|
|
pub conn: rusqlite::Connection,
|
|
}
|
|
|
|
impl RecipeDatabase for EquipementDatabase {
|
|
type DeserialisationType = DatabaseEquipementStats;
|
|
type InnerType = InnerEquipementStats;
|
|
|
|
fn new(filename: &str) -> Result<Self, FatalEquipementError> {
|
|
Ok(EquipementDatabase {
|
|
conn: Connection::open(filename)?,
|
|
})
|
|
}
|
|
|
|
fn get_connection(&self) -> &Connection {
|
|
&self.conn
|
|
}
|
|
|
|
#[cfg_attr(feature = "dev-analysis", coverage(off))] // INCORRECT: Error with line report
|
|
fn serialize_into_intermediate<T>(
|
|
&self,
|
|
table: &str,
|
|
filter: Option<&str>,
|
|
) -> Result<impl Iterator<Item = Result<T, FatalEquipementError>> + '_, FatalEquipementError>
|
|
where
|
|
T: From<Self::InnerType>,
|
|
{
|
|
// Start with Self::DatabaseType
|
|
Ok(self
|
|
.serialize_vec_from_table::<Self::DeserialisationType>(table, filter)?
|
|
.into_iter()
|
|
.map(|db_element| {
|
|
Self::InnerType::try_from(db_element).map(|inner_eq| T::from(inner_eq))
|
|
}))
|
|
}
|
|
}
|
|
#[allow(missing_docs)]
|
|
pub struct EquipementFixedRecipe {
|
|
pub conn: rusqlite::Connection,
|
|
}
|
|
|
|
#[allow(missing_docs)]
|
|
pub struct MockRecipe {}
|
|
|
|
// TODO[1] : Add fixed recipes
|
|
impl FixedRecipe for EquipementFixedRecipe {
|
|
type KeyList = MockRecipe;
|
|
|
|
fn get_elements_from_table(
|
|
&self,
|
|
table: &str,
|
|
) -> Result<Vec<Self::KeyList>, FatalEquipementError> {
|
|
let _ = table;
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
// TODO[3]: Migrate ASAP from this manual implementation
|
|
/// Macro converting a value using serde and mapping an in house error
|
|
macro_rules! from_serde {
|
|
($value: expr) => {
|
|
serde_json::from_str(&$value).map_err(|error| FatalEquipementError::SerdeJSONAt {
|
|
line: line!().to_string(),
|
|
file: file!().to_string(),
|
|
parsed_value: $value.to_string(),
|
|
error,
|
|
})
|
|
};
|
|
}
|
|
|
|
// #[allow(clippy::useless_conversion)]
|
|
impl TryFrom<DatabaseEquipementStats> for InnerEquipementStats {
|
|
type Error = FatalEquipementError;
|
|
|
|
fn try_from(value: DatabaseEquipementStats) -> Result<Self, Self::Error> {
|
|
let (category_base, category_size): SubCategories =
|
|
get_sub_categories_from_serde(&value.category)?;
|
|
Ok(Self {
|
|
id: vec![value.id], // Special case where manual is better than serde
|
|
name: vec![value.name],
|
|
adjective: value.adjective,
|
|
rarity: value.rarity, // Generic case, into(), serde or cast are equivalent
|
|
cost: value.cost,
|
|
difficulty: value.difficulty,
|
|
// tier: value.tier,
|
|
tier: 0,
|
|
damage: value.damage,
|
|
p_damage: value.p_damage,
|
|
precision: value.precision,
|
|
rmax: value.rmax,
|
|
armor_break: value.armor_break,
|
|
armor_pierce: value.armor_pierce,
|
|
antimaterial: value.antimaterial,
|
|
damage_type: from_serde!(&value.damage_type)?, // Serde and derive are godsent
|
|
protection: value.protection,
|
|
integrity: value.integrity,
|
|
torque_two_handed: from_serde!(&value.torque_two_handed)?,
|
|
torque_one_handed: from_serde!(&value.torque_one_handed)?,
|
|
dodge: value.dodge,
|
|
parry: value.parry,
|
|
speed: value.speed,
|
|
stealth: value.stealth,
|
|
// macro_category: from_serde!(&value.category)?,
|
|
// target: from_serde!(&value.target)?,
|
|
special: from_serde!(&value.special)?,
|
|
action_cost: value.action_cost,
|
|
reload_cost: from_serde!(&value.reload_cost)?,
|
|
range: value.range as i16,
|
|
agility_bonus: value.agility_bonus,
|
|
category_base,
|
|
category_size,
|
|
target_category: from_serde!(value.target)?,
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A type englobing the three subcategores
|
|
type SubCategories = (Option<WeaponBaseShape>, Option<WeaponBaseSize>);
|
|
|
|
/// Infer all subcategories from a deserialized DatabaseCategory
|
|
#[cfg_attr(feature = "dev-analysis", coverage(off))] // A test would literally be a copy paste of this match
|
|
fn get_sub_categories_from_serde(category: &str) -> Result<SubCategories, FatalEquipementError> {
|
|
Ok(if let Some(category) = from_serde!(category)? {
|
|
match category {
|
|
EquipementSerializedCategory::Staves => (Some(WeaponBaseShape::Staves), None),
|
|
EquipementSerializedCategory::Blunt => (Some(WeaponBaseShape::Blunt), None),
|
|
EquipementSerializedCategory::Slash => (Some(WeaponBaseShape::Slash), None),
|
|
EquipementSerializedCategory::Ranged => (Some(WeaponBaseShape::Ranged), None),
|
|
EquipementSerializedCategory::Exotic => (Some(WeaponBaseShape::Exotic), None),
|
|
EquipementSerializedCategory::Armors => (Some(WeaponBaseShape::Armors), None),
|
|
EquipementSerializedCategory::Shields => (Some(WeaponBaseShape::Shields), None),
|
|
EquipementSerializedCategory::None => (Some(WeaponBaseShape::None), None),
|
|
EquipementSerializedCategory::Light => (None, Some(WeaponBaseSize::Light)),
|
|
EquipementSerializedCategory::Medium => (None, Some(WeaponBaseSize::Medium)),
|
|
EquipementSerializedCategory::Heavy => (None, Some(WeaponBaseSize::Heavy)),
|
|
EquipementSerializedCategory::SuperHeavy => (None, Some(WeaponBaseSize::SuperHeavy)),
|
|
}
|
|
} else {
|
|
(None, None)
|
|
})
|
|
}
|