diff --git a/weaponry/src/weaponry_structs/equipement_scenario.rs b/weaponry/src/weaponry_structs/equipement_scenario.rs index 66a4e70..5e11109 100644 --- a/weaponry/src/weaponry_structs/equipement_scenario.rs +++ b/weaponry/src/weaponry_structs/equipement_scenario.rs @@ -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::( - "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::>(), - ) - }; +impl CrossRecipeScenario { + /// Automatically deserialize into an equipement + fn deserde_into_equipement( + &self, + name: &str, + ) -> Result + where + T: TryFrom + From + Into, + { + Ok(InitialEquipementList::from_vectored( + self.database + .serialize_vec_from_table::( + &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::, FatalEquipementError>>()?, + )) + } } impl Scenario for CrossRecipeScenario { @@ -94,11 +96,11 @@ impl Scenario for CrossRecipeScenario { fn generate_refined_recipes(&self) -> Result { // 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::("Shape")?; + let eq_material = self.deserde_into_equipement::("Materials")?; + let eq_nature = self.deserde_into_equipement::("Nature")?; #[allow(unused_variables)] - let eq_finishing = deserde_into_equipement!(self.database, "Finishing", EqFinishing); + let eq_finishing = self.deserde_into_equipement::("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()) } diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index ca67614..4e92388 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -231,6 +231,12 @@ pub struct EqShape { pub(super) stats: InnerEquipementStats, } +impl From 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 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 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 for EqFinishing { + fn from(value: InnerEquipementStats) -> Self { + Self { stats: value } + } +} + #[cfg(test)] mod test { use super::{