ClassFunction exceptional simplification

This commit is contained in:
Quaternions 2024-10-06 10:41:42 -07:00
parent 082de122b1
commit 726ffeca21

View File

@ -1,4 +1,4 @@
use std::collections::{hash_map::Entry,HashMap};
use std::collections::HashMap;
use mlua::{FromLua,FromLuaMulti,IntoLua,IntoLuaMulti};
use rbx_types::Ref;
@ -384,43 +384,21 @@ struct ClassFunctions{
>
}
impl ClassFunctions{
/// Someone please rewrite this, all it's supposed to do is
/// return self.classes[class][index] or create the function in the hashmap and then return it
fn get_or_create_class_function(&mut self,lua:&mlua::Lua,class:&str,index:&str)->mlua::Result<Option<mlua::Function>>{
// Use get_entry to get the &'static str key of the database
// Use get_entry to get the &'static str keys of the database
// and use it as a key for the classes hashmap
let f=match CLASS_FUNCTION_DATABASE.get_entry(class){
Some((&static_class_str,class_functions))=>{
match self.classes.entry(static_class_str){
Entry::Occupied(mut occupied_entry)=>{
match class_functions.get_entry(index){
Some((&static_index_str,function_pointer))=>{
match occupied_entry.get_mut().entry(static_index_str){
Entry::Occupied(occupied_entry)=>{
Some(occupied_entry.get().clone())
},
Entry::Vacant(vacant_entry)=>{
Some(vacant_entry.insert(lua.create_function(function_pointer)?).clone())
},
}
},
None=>None,
}
},
Entry::Vacant(vacant_entry)=>{
match class_functions.get_entry(index){
Some((&static_index_str,function_pointer))=>{
let mut h=HashMap::new();
h.entry(static_index_str).or_insert(lua.create_function(function_pointer)?);
vacant_entry.insert(h).get(static_index_str).map(|f|f.clone())
},
None=>None,
}
},
}
},
None=>None,
};
Ok(f)
if let Some((&static_class_str,class_functions))=CLASS_FUNCTION_DATABASE.get_entry(class){
if let Some((&static_index_str,function_pointer))=class_functions.get_entry(index){
return Ok(Some(
self.classes.entry(static_class_str)
.or_insert_with(||HashMap::new())
.entry(static_index_str)
.or_insert(lua.create_function(function_pointer)?)
.clone()
))
}
}
return Ok(None)
}
}