deduplicate more find code

This commit is contained in:
Quaternions 2024-10-04 21:08:28 -07:00
parent 3a716373db
commit 7b8f091ab3

View File

@ -29,6 +29,13 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
full_name full_name
} }
pub fn find_first_child<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,name:&str)->Option<&'a rbx_dom_weak::Instance>{
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.name==name)
}
pub fn find_first_descendant<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,name:&str)->Option<&'a rbx_dom_weak::Instance>{
dom.descendants_of(instance.referent()).find(|&inst|inst.name==name)
}
pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{ pub fn find_first_child_of_class<'a>(dom:&'a rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance,class:&str)->Option<&'a rbx_dom_weak::Instance>{
instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class) instance.children().iter().filter_map(|&r|dom.get_by_ref(r)).find(|inst|inst.class==class)
} }
@ -171,10 +178,8 @@ impl Instance{
let instance=this.get(dom)?; let instance=this.get(dom)?;
Ok( Ok(
match search_descendants.unwrap_or(false){ match search_descendants.unwrap_or(false){
true=>dom.descendants_of(this.referent()).find(|inst|inst.name==name_str), true=>find_first_descendant(dom,instance,name_str),
false=>instance.children().iter().filter_map(|&r| false=>find_first_child(dom,instance,name_str),
dom.get_by_ref(r)
).find(|inst|inst.name==name_str),
} }
.map(|instance| .map(|instance|
Instance::new(instance.referent()) Instance::new(instance.referent())
@ -192,7 +197,9 @@ impl Instance{
true=>find_first_descendant_of_class(dom,this.get(dom)?,class_str), true=>find_first_descendant_of_class(dom,this.get(dom)?,class_str),
false=>find_first_child_of_class(dom,this.get(dom)?,class_str), false=>find_first_child_of_class(dom,this.get(dom)?,class_str),
} }
.map(|inst|(Instance::new(inst.referent()))) .map(|instance|
Instance::new(instance.referent())
)
) )
}) })
}); });
@ -232,13 +239,9 @@ impl Instance{
} }
//find a child with a matching name //find a child with a matching name
Ok( Ok(
instance.children() find_first_child(dom,instance,index_str)
.iter() .map(|instance|Instance::new(instance.referent()))
.find(|&&r| .into_lua(lua)
dom.get_by_ref(r)
.is_some_and(|instance|instance.name==index_str)
)
.map(|&referent|Instance::new(referent)).into_lua(lua)
) )
}) })
}); });
@ -320,12 +323,8 @@ impl DataModel{
dom(lua,|dom|{ dom(lua,|dom|{
match service.as_str(){ match service.as_str(){
"Lighting"=>{ "Lighting"=>{
let referent=dom.root() let referent=find_first_child_of_class(dom,dom.root(),"Lighting")
.children() .map(|instance|instance.referent())
.iter()
.find(|&&c|
dom.get_by_ref(c).is_some_and(|c|c.class=="Lighting")
).map(|r|*r)
.unwrap_or_else(|| .unwrap_or_else(||
dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting")) dom.insert(dom.root_ref(),InstanceBuilder::new("Lighting"))
); );