Tranforming deserde_into_equipment into function

This commit is contained in:
Frédéric CAMPO 2025-02-22 00:02:45 +01:00
parent 7f98215bc2
commit fde34be744
Signed by: llowin
GPG Key ID: 19EACF0560706F99
2 changed files with 55 additions and 30 deletions

View File

@ -8,7 +8,7 @@
//!
#![allow(dead_code)]
use crate::error::FatalEquipementError;
#[allow(unused_imports)]
use crate::interfaces::Reportable;
use crate::weaponry_structs::InitialEquipementList;
@ -17,14 +17,15 @@ use crate::weaponry_structs::equipement_structs::EqFinishing;
use crate::weaponry_structs::equipement_structs::EqMaterial;
use crate::weaponry_structs::equipement_structs::EqNature;
use crate::weaponry_structs::equipement_structs::EqShape;
use crate::weaponry_structs::equipement_structs::InnerEquipementStats;
use crate::{
error::EquipementError,
interfaces::{RecipeCollection, RecipeDatabase, Scenario, ViewTable},
};
use super::Equipement;
use super::equipement_advanced_repr::RefinedEquipementList;
use super::equipement_impl_database::EquipementDatabase;
use super::equipement_structs::InnerEquipementStats;
/// Mock config
pub struct ConfigMock {}
@ -50,29 +51,30 @@ pub struct CrossRecipeScenario {
config: ConfigMock,
}
/// Automatically deserialize into an equipement
// TODO: This macro only exists because no blanket implementation or function does this *relatively* easy step
// TODO: This place the place where all new impl and simplifications will be done
// TODO: Streamline this so no macro is needed
macro_rules! deserde_into_equipement {
( $db:expr, $name:expr, $type:ident) => {
InitialEquipementList::from_vectored(
$db.serialize_vec_from_table::<DatabaseEquipementStats>(
"Equipements",
Some(&format!("META_CATEGORY LIKE '{}'", $name)),
// None,
)?
.into_iter()
// TODO: This is the lamest way to init this data
.map(|v| {
$type {
stats: InnerEquipementStats::try_from(v).unwrap(),
}
.into()
})
.collect::<Vec<_>>(),
)
};
impl CrossRecipeScenario {
/// Automatically deserialize into an equipement
fn deserde_into_equipement<T>(
&self,
name: &str,
) -> Result<InitialEquipementList, FatalEquipementError>
where
T: TryFrom<InnerEquipementStats> + From<InnerEquipementStats> + Into<Equipement>,
{
Ok(InitialEquipementList::from_vectored(
self.database
.serialize_vec_from_table::<DatabaseEquipementStats>(
&name,
Some(&format!("META_CATEGORY LIKE '{}'", name)),
)?
.into_iter()
.map(|v| {
InnerEquipementStats::try_from(v)
.map(|w| T::from(w))
.map(|v| v.into())
})
.collect::<Result<Vec<_>, FatalEquipementError>>()?,
))
}
}
impl Scenario for CrossRecipeScenario {
@ -94,11 +96,11 @@ impl Scenario for CrossRecipeScenario {
fn generate_refined_recipes(&self) -> Result<Self::FinalListType, EquipementError> {
// TODO: Depending on config, a different filter might be given here
let eq_shape = deserde_into_equipement!(self.database, "Shape", EqShape);
let eq_material = deserde_into_equipement!(self.database, "Materials", EqMaterial);
let eq_nature = deserde_into_equipement!(self.database, "Nature", EqNature);
let eq_shape = self.deserde_into_equipement::<EqShape>("Shape")?;
let eq_material = self.deserde_into_equipement::<EqMaterial>("Materials")?;
let eq_nature = self.deserde_into_equipement::<EqNature>("Nature")?;
#[allow(unused_variables)]
let eq_finishing = deserde_into_equipement!(self.database, "Finishing", EqFinishing);
let eq_finishing = self.deserde_into_equipement::<EqFinishing>("Finishing")?;
// println!(
// "{}\n{}\n{}\n{}",
@ -121,7 +123,6 @@ impl Scenario for CrossRecipeScenario {
// TODO: The finishes are interesting if they are configured or at least tamed a little
// TODO: As a high cost finish cannot hope to compete with a low one
// TODO: and therefore need a special metric and fixed entry data to be mesured
Ok(f_list.into())
}

View File

@ -231,6 +231,12 @@ pub struct EqShape {
pub(super) stats: InnerEquipementStats,
}
impl From<InnerEquipementStats> for EqShape {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// Material used for the equipement, all equipements are crafted with 1-2 materials
#[derive(Default)]
pub struct EqMaterial {
@ -238,6 +244,12 @@ pub struct EqMaterial {
pub(super) stats: InnerEquipementStats,
}
impl From<InnerEquipementStats> for EqMaterial {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// Nature of the equipement, some natures are reserved and some can be chained
///
/// Deciding on the combinaison can be tricky an will need an extra field
@ -247,6 +259,12 @@ pub struct EqNature {
pub(super) stats: InnerEquipementStats,
}
impl From<InnerEquipementStats> for EqNature {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
/// Finishing touches, since all of them have a cost, they are treated in a special fashion
#[derive(Default)]
pub struct EqFinishing {
@ -254,6 +272,12 @@ pub struct EqFinishing {
pub(super) stats: InnerEquipementStats,
}
impl From<InnerEquipementStats> for EqFinishing {
fn from(value: InnerEquipementStats) -> Self {
Self { stats: value }
}
}
#[cfg(test)]
mod test {
use super::{