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

[SPELL::STD] As an example, we implement a log spell

parent b85f4ab8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -8,12 +8,23 @@ use std::collections::HashMap; ...@@ -8,12 +8,23 @@ use std::collections::HashMap;
pub type SpellBuilderFunction = fn(SpellArguments) -> Result<Box<dyn Spell>, Error>; pub type SpellBuilderFunction = fn(SpellArguments) -> Result<Box<dyn Spell>, Error>;
/// The spell factory, you have to register the spell before trying to create them. /// The spell factory, you have to register the spell before trying to create them.
#[derive(Debug, Default)] #[derive(Debug)]
pub struct SpellFactory { pub struct SpellFactory {
/// The dispatch map, to get a builder by an associated spell name. /// The dispatch map, to get a builder by an associated spell name.
dispatch: HashMap<&'static str, SpellBuilderFunction>, dispatch: HashMap<&'static str, SpellBuilderFunction>,
} }
impl Default for SpellFactory {
fn default() -> Self {
use crate::spell::std as spell;
let mut this = Self { dispatch: Default::default() };
this.register::<spell::Log>().expect("error in std spells");
this
}
}
impl SpellFactory { impl SpellFactory {
/// Get the list of all registered spells. /// Get the list of all registered spells.
pub fn get_registered_spells(&self) -> impl Iterator<Item = &'static str> { pub fn get_registered_spells(&self) -> impl Iterator<Item = &'static str> {
......
//! The definition of Spells in the Grimoire engine. //! The definition of Spells in the Grimoire engine.
mod factory;
mod arguments; mod arguments;
mod factory;
mod traits; mod traits;
pub use factory::*; /// Standard spells.
pub mod std {
mod log;
pub use log::Log;
}
pub use arguments::*; pub use arguments::*;
pub use factory::*;
pub use traits::*; pub use traits::*;
use crate::prelude::*;
#[derive(Debug)]
pub struct Log {
level: String,
messages: Vec<String>,
}
impl Spell for Log {
fn cast(&self, state: GrimoireState) -> Result<GrimoireState, GrimoireError> {
let (heading, complementary) = match self.messages.split_first() {
Some((heading, complementary)) => (heading, complementary),
None => return Ok(state),
};
let logger = |ident: bool, str: &str| {
let ident = ident.then_some("\t").unwrap_or_default();
match self.level.as_str() {
"error" => log::error!(target: "grimoire", "{ident}{str}"),
_ => log::info!(target: "grimoire", "{ident}{str}"),
}
};
logger(false, heading);
complementary
.iter()
.for_each(|complementary| logger(true, complementary));
Ok(state)
}
}
impl TryFrom<SpellArguments> for Log {
type Error = GrimoireError;
fn try_from(mut args: SpellArguments) -> Result<Self, Self::Error> {
let level = args
.remove_as("level", GrimoireType::Identifier)?
.to_string();
args.has_named()
.then_some(())
.ok_or(GrimoireError::Unexpected("too much named arguments"))?;
let messages = args
.into_iter()
.filter_map(|(key, msg)| key.is_none().then(|| msg.to_string()))
.collect();
Ok(Self { level, messages })
}
}
impl BuildableSpell for Log {
fn name() -> &'static str {
"log"
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter