Skip to main content

luaur_analysis/functions/
filter_map.rs

1use crate::functions::follow_type::follow_type_id;
2use crate::functions::get_type_alt_j::get_type_id;
3use crate::records::union_type::UnionType;
4use crate::type_aliases::type_id::TypeId;
5use crate::type_aliases::type_id_predicate::TypeIdPredicate;
6use alloc::vec::Vec;
7use std::collections::HashSet;
8
9pub fn filter_map(type_: TypeId, predicate: TypeIdPredicate) -> Vec<TypeId> {
10    let type_ = unsafe { follow_type_id(type_) };
11
12    unsafe {
13        if !get_type_id::<UnionType>(type_).is_null() {
14            let utv = get_type_id::<UnionType>(type_);
15            let mut options: HashSet<TypeId> = HashSet::new();
16
17            for &option in (*utv).options.iter() {
18                let followed_option = follow_type_id(option);
19                if let Some(out) = predicate(followed_option) {
20                    options.insert(out);
21                }
22            }
23
24            options.into_iter().collect()
25        } else if let Some(out) = predicate(type_) {
26            vec![out]
27        } else {
28            Vec::new()
29        }
30    }
31}