// Workspace: weaponry // File: weaponry/src/interfaces.rs //! Interface module holding all necesary traits //! A good amount of effort is made to keep the code polymorphic enough to allow //! handling of the whole crafting process when the times comes #![allow(dead_code)] use std::{path::Path, rc::Rc}; use more_fields::FieldsAsSlice; use serde::de::DeserializeOwned; use crate::error::{EquipementError, FatalEquipementError}; /// Trait representing a view table /// /// These table exist to allow to link several point of interest to a RecipeCollection /// Please note that while FinalRepresentations and ViewTable are two separate traits, /// they are always tied together as a view table will alway take specific assumptions /// on the data present inside therefore not allowing many variations on the underlying /// implementation of the representation pub trait ViewTable { /// The inner type of element that one would be peeking into /// /// Usually the final representation of an Item type InterestElementList: RefinedList; /// Give a list of interesting element indicises to further analyse fn index_of_interest_points(&self) -> Vec; /// Interrogate the FinalRepresentation to find interesting elements fn find_points_of_interest(representation: &Self::InterestElementList) -> Self; // An alternative, non Self::Typed way of doing since there is only one usage for now // fn find_points_of_interest(&self, representation: &impl FinalRepresentation); } /// Trait implemented by any struct holding the final representation of a RecipeCollection /// /// This trait is rather placeholder, reminding the user to give only objects that are indeed /// final representations and contains all important fields calculated /// /// While it is possible to enforce from_recipe_collection, doing so would require the viewtable /// lock itseld to a particular recipe. /// While it is the case as of now, matters are not yet set in stone pub trait RefinedList { // Must implement From, is it worth adding as a prerequite type ? // type OriginCollection: Into; } /// Trait implemented for the final collection of a recipe, allowing it to be combined with parts /// /// A recipe list will be combined with a part list to be updated and generate all combinaisons, so on and so forth pub trait RecipeCollection where for<'a> &'a <::Recipes as Combinable>::Inner: std::ops::Add, { /// The type of the inner recipes, this type must be combinable type Recipes: Combinable; /// A blanket implementation giving all combinaisons of recipes with another RecipeCollection /// /// This method doesn't make use of parallel iterators as the workload is way faster than would be the memory dispatch fn combine_all(&mut self, other: &G) where G: RecipeCollection, { self.assign_inner( self.ref_inner() .iter() .flat_map(|inner_recipes| { other .ref_inner() .iter() .filter(|&external_recipes| external_recipes.backward_filter(inner_recipes)) .map(|external_recipes: &Self::Recipes| { inner_recipes.add_ref_to_self(external_recipes) }) .collect::>() }) .collect::>(), ); } /// Assign the inner value from an owned value, used for blanket implementations fn assign_inner(&mut self, inner: Vec); /// Gives access to a reference of the inner list /// /// This allows to only clone when needed fn ref_inner(&self) -> &Vec; /// Create a collection from a vector fn from_vectored(list: Vec) -> Self; } /// Trait that ensure that two elements of a recipe are combinable, /// /// Being combinable means it can be coerced into elements that implement add pub trait Combinable where for<'a> &'a ::Inner: std::ops::Add, { /// The inner type of the combined elements, this type must implement Add<&Inner> type Inner; /// Give access to a reference of the underlying data to be combined /// /// The designed combinaison method is std::ops::Add fn combinable_part(&self) -> &Self::Inner; /// Add an element to self and generate a new Self /// /// As the signature implies, it will not mutate self not rhs and will generate a new value from the operation /// This pushes the actual requirement to &Inner : Add<&Inner> fn add_ref_to_self(&self, rhs: &T) -> Self where T: Combinable; /// Filter to decide if the addition should be done /// /// /// Please note that this comparison is done from the new element perspective, /// asking it if it will allow itself to be added as There is as of now no forward filtering /// Most filters will defer an internal FilterFunction into the Inner to do all inner working, /// but the implementation specifics are left open fn backward_filter(&self, lhs: &T) -> bool where T: Combinable; } /// Type alias represinting the inner function parameter // #![feature(trait_alias)] pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool; /// Type alias representing a filter pub(super) type FilterType = Box>>; #[derive(Clone)] /// A generic Filtering function pub(crate) struct FilterFunction(pub(super) FilterType); /// This element can be converted to tex pub trait ToTex { /// Export the object to a Tex string fn to_tex(&self) -> String; } /// This element can be converted to a csv for further analysis pub trait ToCSV { /// Convert this object into a csv fn write_csv(&self, path: &Path) -> csv::Result<()>; } /// Garanties that a struct can be generated from a database #[deprecated] pub trait DbSerializable { /// Serialize the object from a database fn from_database() -> Self; } /// A generic database+connection, handling all the deserialization of the inner types pub trait RecipeDatabase { /// Create the database from a file fn new(origin: &str) -> Result where Self: std::marker::Sized; /// Serialize a vector of objects of the given type in the specified table with an optional filter of the underlying database /// The FROM type is the original data, directly deserialized from the table fn serialize_vec_from_table( &self, table: &str, filter: Option<&str>, ) -> std::result::Result, FatalEquipementError>; /// Initialize an empty database & table fn initialize_empty_table( &self, table: &str, ) -> Result<(), FatalEquipementError>; } /// Trait implemented by any struct aiming to be a public recipe pub trait FixedRecipe { /// Struct representing the database type, most likely a string type Database; /// Struct representing the keylist type, most likely a usize type KeyList; /// Get the list of elements corresponding to the recipe /// /// These elements will either be iteratively fetched from the database or scoured through in the data depending on the scenario fn get_elements() -> Vec<(Self::Database, Self::KeyList)>; } /// Trait ensuring a status report can be inferred from the structure pub trait Reportable { /// Get a detailled inner status report with as many useful informations as possible /// /// This report should give detailled information about the inner data, without showing the actual inner data fn get_report(&self) -> String; } /// A recipe full stack scenario, from loading database through view table generation /// /// The inner working while somehow abstracted are very tied together and tied to the impl pub trait Scenario { /// The inner database type type DatabaseType: RecipeDatabase; /// The type of viewtable that must be returned by the scenario // Could also use dyn dispatch but since all of it is so tied down... type ViewTableType: ViewTable; /// The type of the final recipe list type FinalListType: RefinedList; /// Inner configuration type Config; /// Create a Scenario from a given config fn new_with_config(config: Self::Config) -> Result where Self: Sized; // /// Load all database into intermediate representation // fn load_from_database() -> Result // where // Self: std::marker::Sized; // /// Configure the scenario to the user's needs // fn configure_scenario(&mut self, config: Self::Config) -> Result<(), FatalEquipementError>; // All these steps should be implemented but not required // enforcing them helps writing new impls more easily but is not really nice in any way // // /// Process the inner representations and get a list of results // fn process_inner_representation(&self) -> Result<(), FatalEquipementError>; //Result<(), EquipementError> // /// Process inner recipes into a Final list // /// // /// This will generate the recipe list using the prefered method of the Scenario (Fixed Recipes or Cross Recipes or Whatever) // fn process_recipes(&self); // /// Process the recipes into a Refined Recipes // fn refine_recipes(&self); /// Generate the final list type fn generate_refined_recipes(&self) -> Result; /// Gather the selected view tables /// All the inner generation is done gradually and populate all section to finally generate the views fn generate_view_tables(&self, equipement_list: Self::FinalListType) -> Self::ViewTableType; }