diff --git a/src/runner/enum.rs b/src/runner/enum.rs index b508f7e..27f2df0 100644 --- a/src/runner/enum.rs +++ b/src/runner/enum.rs @@ -3,8 +3,8 @@ use mlua::IntoLua; #[derive(Clone,Copy)] pub struct Enum(u32); pub struct EnumItems; -pub struct EnumItem{ - name:String +pub struct EnumItem<'a>{ + ed:&'a rbx_reflection::EnumDescriptor<'a>, } impl Into for Enum{ @@ -13,9 +13,9 @@ impl Into for Enum{ } } -impl EnumItem{ - const fn new(name:String)->Self{ - Self{name} +impl<'a> EnumItem<'a>{ + const fn new(ed:&'a rbx_reflection::EnumDescriptor)->Self{ + Self{ed} } } @@ -23,32 +23,28 @@ pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Er globals.set("Enum",EnumItems) } -impl mlua::UserData for EnumItem{ +impl mlua::UserData for EnumItem<'_>{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ } fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ - methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(Self,String)|{ - let db=rbx_reflection_database::get(); - match db.enums.get(this.name.as_str()){ - Some(e)=>match e.items.get(val.as_str()){ - Some(&id)=>Enum(id).into_lua(lua), - None=>mlua::Value::Nil.into_lua(lua), - }, + methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'lua>,mlua::String)|{ + match this.ed.items.get(val.to_str()?){ + Some(&id)=>Enum(id).into_lua(lua), None=>mlua::Value::Nil.into_lua(lua), } }); } } -type_from_lua_userdata!(EnumItem); +type_from_lua_userdata_lua_lifetime!(EnumItem); impl mlua::UserData for EnumItems{ fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){ } fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){ - methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,String)|{ + methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,mlua::String)|{ let db=rbx_reflection_database::get(); - match db.enums.get(val.as_str()){ - Some(_)=>EnumItem::new(val).into_lua(lua), + match db.enums.get(val.to_str()?){ + Some(ed)=>EnumItem::new(ed).into_lua(lua), None=>mlua::Value::Nil.into_lua(lua), } }); diff --git a/src/runner/macros.rs b/src/runner/macros.rs index b935225..8121af8 100644 --- a/src/runner/macros.rs +++ b/src/runner/macros.rs @@ -10,3 +10,15 @@ macro_rules! type_from_lua_userdata{ } }; } +macro_rules! type_from_lua_userdata_lua_lifetime{ + ($asd:ident)=>{ + impl<'lua> mlua::FromLua<'lua> for $asd<'lua>{ + 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))), + } + } + } + }; +}