Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 15601e54 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

Just trying an API for the Spell/SpellFactory

parent baedc9e3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -366,6 +366,7 @@ dependencies = [
"log",
"paste",
"serde",
"thiserror",
]
[[package]]
......
......@@ -15,3 +15,4 @@ paste.workspace = true
serde.workspace = true
hashbrown.workspace = true
log.workspace = true
thiserror.workspace = true
#[derive(Debug, thiserror::Error)]
pub enum SpellError {}
pub mod spell;
pub mod state;
pub mod error;
use crate::{error::SpellError, state::State};
pub trait Spell {
fn cast(&self, state: State) -> Result<State, SpellError>;
fn uncast(&self, state: State) -> Result<State, SpellError>;
}
pub struct SpellArguments {}
pub trait BuildableSpell: Spell + TryFrom<SpellArguments, Error = SpellError> {
fn name() -> &'static str;
fn aliases() -> &'static [&'static str] {
&[]
}
fn name_list() -> impl Iterator<Item = &'static str> {
[Self::name()]
.into_iter()
.chain(Self::aliases().iter().copied())
}
fn create(arguments: SpellArguments) -> Result<Box<dyn Spell>, SpellError>;
}
type SpellBuilderFunction = fn(SpellArguments) -> Result<Box<dyn Spell>, SpellError>;
#[derive(Debug, Default)]
pub struct SpellFactory {
dispatch: hashbrown::HashMap<&'static str, SpellBuilderFunction>,
}
impl SpellFactory {
pub fn register<Spell: BuildableSpell + 'static>(&mut self) -> Result<(), SpellError> {
Spell::name_list()
.find(|spell_name| self.dispatch.contains_key(spell_name))
.map(|_name| -> Result<(), SpellError> { todo!("error") })
.unwrap_or(Ok(()))?;
Spell::name_list().for_each(|name| _ = self.dispatch.insert(name, Spell::create));
Ok(())
}
pub fn try_build_spell(
&self,
name: &str,
arguments: SpellArguments,
) -> Result<Box<dyn Spell>, SpellError> {
let builder_function = self.dispatch.get(name).ok_or_else(|| todo!("error"))?;
builder_function(arguments)
}
}
pub struct State {
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter