From b1da31499de4145b1f77296cbea0c637e6f866bf Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 9 Feb 2020 12:10:35 +0100 Subject: componentwrapper parameters are now directly usable in creation macro expansion --- src/componentwrapper.rs | 126 +++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 60 deletions(-) (limited to 'src/componentwrapper.rs') diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 23e104b..f89f44f 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -8,84 +8,90 @@ use crate::parameter::{Parameter, ParameterType}; macro_rules! components { - ($($comp: ident [$($paramname: ident : $paramtype: ident),*] $paramlist: ident {$creation: expr});*) => { + ($($comp: ident ($($paramname: ident : $paramtype: ident),*) {$creation: expr});*) => { - #[derive(Clone)] - pub enum ComponentWrapper{ - $( - $comp($comp), - )* - } + #[derive(Clone)] + pub enum ComponentWrapper{ + $( + $comp($comp), + )* + } - impl ComponentWrapper { + impl ComponentWrapper { - pub fn build<'a>(&self, builder: LazyBuilder<'a>) -> LazyBuilder<'a> { - match self.clone() { - $( - Self::$comp(c) => builder.with(c), - )* + pub fn build<'a>(&self, builder: LazyBuilder<'a>) -> LazyBuilder<'a> { + match self.clone() { + $( + Self::$comp(c) => builder.with(c), + )* + } } - } - - pub fn load_component(comptype: ComponentType, parameters_: HashMap<&str, Parameter>) -> Option { - match comptype { - $( - ComponentType::$comp => Some(Self::$comp({ - #[allow(unused_mut)] - let mut $paramlist = parameters_; - $creation - })), - )* + pub fn load_component(comptype: ComponentType, mut parameters: HashMap<&str, Parameter>) -> Option { + + match comptype { + $( + + ComponentType::$comp => Some(Self::$comp({ + $( + let $paramname = match parameters.remove(stringify!($paramname))? { + Parameter::$paramtype(p) => p, + _ => {return None} + }; + )* + $creation + })), + )* + } } } - } - - #[derive(Debug, PartialEq, Eq, Clone, Copy)] - pub enum ComponentType { - $( - $comp, - )* - } - - impl ComponentType { - pub fn from_str(typename: &str) -> Option{ - match typename { - $( - stringify!($comp) => Some(Self::$comp), - )* - _ => None - } + #[derive(Debug, PartialEq, Eq, Clone, Copy)] + pub enum ComponentType { + $( + $comp, + )* } - pub fn parameters(&self) -> HashMap<&str, ParameterType> { - match self { - $( - Self::$comp => { - #[allow(unused_mut)] - let mut h = HashMap::new(); - $( - h.insert(stringify!($paramname), ParameterType::$paramtype); - )* - h - }, - )* + impl ComponentType { + + pub fn from_str(typename: &str) -> Option{ + match typename { + $( + stringify!($comp) => Some(Self::$comp), + )* + _ => None + } + } + + pub fn parameters(&self) -> HashMap<&str, ParameterType> { + match self { + $( + Self::$comp => { + #[allow(unused_mut)] + let mut h = HashMap::new(); + $( + h.insert(stringify!($paramname), ParameterType::$paramtype); + )* + h + }, + )* + } } } } -}} +} components!( - Visible [sprite: String, height: Float] parameters { + Visible (sprite: String, height: Float) { Visible { - sprite: parameters.remove("sprite")?.as_string()?, - height: parameters.remove("height")?.as_f64()? + sprite, + height } }; - Blocking [] _p {Blocking}; - Floor [] _p {Floor}; - Player [name: String] parameters {Player::new(parameters.remove("name")?.as_string()?)} + Blocking () {Blocking}; + Floor () {Floor}; + Player (name: String) {Player::new(name.to_string())} ); pub type PreEntity = Vec; -- cgit