summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-09 12:10:35 +0100
committertroido <troido@protonmail.com>2020-02-09 12:10:35 +0100
commitb1da31499de4145b1f77296cbea0c637e6f866bf (patch)
tree38756f28100add668d589f1ac25307c027ed2e28 /src
parentf13c592c80f33b8ba5afeac2ede6594a65f26344 (diff)
componentwrapper parameters are now directly usable in creation macro expansion
Diffstat (limited to 'src')
-rw-r--r--src/componentwrapper.rs126
-rw-r--r--src/parameter.rs30
2 files changed, 66 insertions, 90 deletions
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<Self> {
- 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<Self> {
+
+ 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<ComponentType>{
- 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<ComponentType>{
+ 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<ComponentWrapper>;
diff --git a/src/parameter.rs b/src/parameter.rs
index 41137bb..33eee25 100644
--- a/src/parameter.rs
+++ b/src/parameter.rs
@@ -46,41 +46,11 @@ impl Parameter {
};
Self::from_typed_json(typ, val)
}
-
- pub fn as_str(&self) -> Option<&str> {
- if let Self::String(str) = self {
- Some(str)
- } else {
- None
- }
- }
-
- pub fn as_string(&self) -> Option<String> {
- Some(self.as_str()?.to_string())
- }
-
- #[allow(dead_code)]
- pub fn as_i64(&self) -> Option<i64> {
- if let Self::Int(num) = self {
- Some(*num)
- } else {
- None
- }
- }
-
- pub fn as_f64(&self) -> Option<f64> {
- if let Self::Float(num) = self {
- Some(*num)
- } else {
- None
- }
- }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParameterType {
String,
-// Pos,
Int,
Float
}