ravenous-tools/weaponry/src/weaponry_structs/equipement_scenario.rs

132 lines
4.7 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/weaponry_structs/equipement_scenario.rs
//! Implementations of a full scenario,
//!
//! These implementation must take care of everything from database retrieval to view exporting,
//! however all they mostly do is glue together all the parts designed in the interface,
//! and therefore mostly serves as a TO-DO list
//!
use crate::{
error::FatalEquipementError,
interfaces::{RecipeCollection, RecipeDatabase, Scenario},
weaponry_structs::{
Equipement, InitialEquipementList, RefinedEquipementList,
equipement_impl_database::EquipementDatabase,
equipement_structs::{
DatabaseEquipementStats, EqFinishing, EqMaterial, EqNature, EqShape,
InnerEquipementStats,
},
},
};
/// Base Equipement Scenario config
pub struct ConfigEquipementScenario {
/// Table Name
pub table_name: String,
/// Database path
pub db_path: String,
// TODO[2]: Add something smart here to take care of the selection process
/// Wether the shapes are selected
pub shape: Option<()>,
/// Wether the materials are selected, and how many iteration there are
pub materials: Option<usize>,
/// Wether the natures are selected
pub nature: Option<()>,
/// Wether the finishing touches are selected
pub finishing: Option<()>,
}
/// A simple cross recipe scenario
#[allow(clippy::missing_docs_in_private_items)]
pub struct CrossRecipeScenario {
// TODO: Should obv be inside the config
database: EquipementDatabase,
config: ConfigEquipementScenario,
}
impl CrossRecipeScenario {
/// Automatically deserialize into an equipement
// TODO: Add to trait
// TODO: Add an optionnal initial filter ?
fn deserde_into_equipement<T>(
&self,
name: &str,
) -> Result<InitialEquipementList, FatalEquipementError>
where
T: TryFrom<InnerEquipementStats> + From<InnerEquipementStats> + Into<Equipement>,
{
// TODO: This likely has too high a complexity and should either be a blanket implementation
// : or require the inner workings to implement methods to easily allow this deserialisation
Ok(InitialEquipementList::from_vectored(
self.database
.serialize_vec_from_table::<DatabaseEquipementStats>(
&self.config.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::<Result<Vec<_>, FatalEquipementError>>()?,
))
}
/// Generate all the operations to be processed with the current config
// TODO: Defer this to a custom type in the config some sort of OperationSelector
fn generate_selected_operation(
&self,
) -> Result<Vec<InitialEquipementList>, FatalEquipementError> {
let mut operations = Vec::with_capacity(8);
if self.config.shape.is_some() {
operations.push(self.deserde_into_equipement::<EqShape>("Shape")?);
}
if let Some(qt) = self.config.materials {
for _ in 0..qt {
operations.push(self.deserde_into_equipement::<EqMaterial>("Materials")?);
}
}
if self.config.nature.is_some() {
operations.push(self.deserde_into_equipement::<EqNature>("Nature")?);
}
if self.config.finishing.is_some() {
operations.push(self.deserde_into_equipement::<EqFinishing>("Finishing")?);
}
if operations.is_empty() {
return Err(FatalEquipementError::CLISelection(
"No step was selected for generation, please select at least one step".into(),
));
};
Ok(operations)
}
}
impl Scenario for CrossRecipeScenario {
type DatabaseType = EquipementDatabase;
type FinalListType = RefinedEquipementList;
type Config = ConfigEquipementScenario;
fn new_with_config(config: Self::Config) -> Result<Self, FatalEquipementError>
where
Self: Sized,
{
Ok(Self {
database: Self::DatabaseType::new(&config.db_path)?,
config,
})
}
fn generate_refined_recipes(&self) -> Result<Self::FinalListType, FatalEquipementError> {
let operations = self.generate_selected_operation()?;
let mut opertation_it = operations.into_iter();
let mut refined_list = opertation_it.next().unwrap();
opertation_it.for_each(|operation| refined_list.combine_all(&operation));
Ok(refined_list.into())
}
}