87 lines
2.4 KiB
Rust
87 lines
2.4 KiB
Rust
|
use mlua::IntoLua;
|
||
|
|
||
|
#[derive(Clone,Copy)]
|
||
|
pub struct Enum(u32);
|
||
|
pub struct EnumItems;
|
||
|
pub struct EnumItem{
|
||
|
name:String
|
||
|
}
|
||
|
|
||
|
impl Into<rbx_types::Enum> for Enum{
|
||
|
fn into(self)->rbx_types::Enum{
|
||
|
rbx_types::Enum::from_u32(self.0)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl EnumItem{
|
||
|
const fn new(name:String)->Self{
|
||
|
Self{name}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Error>{
|
||
|
globals.set("Enum",EnumItems)
|
||
|
}
|
||
|
|
||
|
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),
|
||
|
},
|
||
|
None=>mlua::Value::Nil.into_lua(lua),
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
impl<'lua> mlua::FromLua<'lua> for EnumItem{
|
||
|
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||
|
match value{
|
||
|
mlua::Value::UserData(ud)=>ud.take()?,
|
||
|
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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)|{
|
||
|
let db=rbx_reflection_database::get();
|
||
|
match db.enums.get(val.as_str()){
|
||
|
Some(_)=>EnumItem::new(val).into_lua(lua),
|
||
|
None=>mlua::Value::Nil.into_lua(lua),
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
impl<'lua> mlua::FromLua<'lua> for EnumItems{
|
||
|
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
|
||
|
match value{
|
||
|
mlua::Value::UserData(ud)=>ud.take()?,
|
||
|
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl mlua::UserData for Enum{
|
||
|
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
|
||
|
}
|
||
|
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<Self>{
|
||
|
match value{
|
||
|
mlua::Value::UserData(ud)=>ud.take()?,
|
||
|
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
|
||
|
}
|
||
|
}
|
||
|
}
|