diff --git a/src/runner/cframe.rs b/src/runner/cframe.rs index 3c66cef..8942578 100644 --- a/src/runner/cframe.rs +++ b/src/runner/cframe.rs @@ -76,11 +76,5 @@ impl mlua::UserData for CFrame{ ); } } -impl<'lua> mlua::FromLua<'lua> for CFrame{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take()?, - other=>Err(mlua::Error::runtime(format!("Expected CFrame got {:?}",other))), - } - } -} + +type_from_lua_userdata!(CFrame); diff --git a/src/runner/color3.rs b/src/runner/color3.rs index f6a162d..71899e8 100644 --- a/src/runner/color3.rs +++ b/src/runner/color3.rs @@ -40,11 +40,4 @@ impl mlua::UserData for Color3{ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ } } -impl<'lua> mlua::FromLua<'lua> for Color3{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take()?, - other=>Err(mlua::Error::runtime(format!("Expected Color3 got {:?}",other))), - } - } -} +type_from_lua_userdata!(Color3); diff --git a/src/runner/enum.rs b/src/runner/enum.rs index 0fcda7a..b508f7e 100644 --- a/src/runner/enum.rs +++ b/src/runner/enum.rs @@ -39,14 +39,7 @@ impl mlua::UserData for EnumItem{ }); } } -impl<'lua> mlua::FromLua<'lua> for EnumItem{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take()?, - other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), - } - } -} +type_from_lua_userdata!(EnumItem); impl mlua::UserData for EnumItems{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ @@ -61,14 +54,7 @@ impl mlua::UserData for EnumItems{ }); } } -impl<'lua> mlua::FromLua<'lua> for EnumItems{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take()?, - other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), - } - } -} +type_from_lua_userdata!(EnumItems); impl mlua::UserData for Enum{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ @@ -76,11 +62,4 @@ impl mlua::UserData for Enum{ fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){ } } -impl<'lua> mlua::FromLua<'lua> for Enum{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take()?, - other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))), - } - } -} +type_from_lua_userdata!(Enum); diff --git a/src/runner/instance.rs b/src/runner/instance.rs index c81d874..666a8db 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -54,14 +54,7 @@ macro_rules! class{ self.referent } } - impl<'lua> mlua::FromLua<'lua> for $class{ - fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->mlua::Result{ - match value{ - mlua::Value::UserData(ud)=>ud.take(), - other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($class),other))), - } - } - } + type_from_lua_userdata!($class); }; } macro_rules! class_composition{ diff --git a/src/runner/macros.rs b/src/runner/macros.rs new file mode 100644 index 0000000..b935225 --- /dev/null +++ b/src/runner/macros.rs @@ -0,0 +1,12 @@ +macro_rules! type_from_lua_userdata{ + ($asd:ident)=>{ + impl<'lua> mlua::FromLua<'lua> for $asd{ + fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result{ + match value{ + mlua::Value::UserData(ud)=>ud.take(), + other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))), + } + } + } + }; +} diff --git a/src/runner/mod.rs b/src/runner/mod.rs index abec22e..de4859f 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,3 +1,5 @@ +#[macro_use] +mod macros; mod runner; mod r#enum; diff --git a/src/runner/vector3.rs b/src/runner/vector3.rs index 78244e5..bff57a6 100644 --- a/src/runner/vector3.rs +++ b/src/runner/vector3.rs @@ -73,11 +73,4 @@ impl mlua::UserData for Vector3{ } } -impl<'lua> mlua::FromLua<'lua> for Vector3{ - fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult{ - match value{ - mlua::Value::UserData(ud)=>ud.take(), - other=>Err(mlua::Error::runtime(format!("Expected Vector3 got {:?}",other))), - } - } -} +type_from_lua_userdata!(Vector3);