Skip to main content

luaur_analysis/type_aliases/
predicate.rs

1//! Node: `cxx:TypeAlias:Luau.Analysis:Analysis/include/Luau/Predicate.h:22:predicate`
2//! Source: `Analysis/include/Luau/Predicate.h:22` (hand-ported)
3use crate::records::and_predicate::AndPredicate;
4use crate::records::eq_predicate::EqPredicate;
5use crate::records::is_a_predicate::IsAPredicate;
6use crate::records::not_predicate::NotPredicate;
7use crate::records::or_predicate::OrPredicate;
8use crate::records::truthy_predicate::TruthyPredicate;
9use crate::records::type_guard_predicate::TypeGuardPredicate;
10
11#[derive(Debug, Clone)]
12pub enum Predicate {
13    Truthy(TruthyPredicate),
14    IsA(IsAPredicate),
15    TypeGuard(TypeGuardPredicate),
16    Eq(EqPredicate),
17    And(AndPredicate),
18    Or(OrPredicate),
19    Not(NotPredicate),
20}
21
22impl Predicate {
23    /// C++ `v.index()` — the member's position in the Variant<...> list.
24    pub fn index(&self) -> i32 {
25        match self {
26            Predicate::Truthy(_) => 0,
27            Predicate::IsA(_) => 1,
28            Predicate::TypeGuard(_) => 2,
29            Predicate::Eq(_) => 3,
30            Predicate::And(_) => 4,
31            Predicate::Or(_) => 5,
32            Predicate::Not(_) => 6,
33        }
34    }
35}
36
37/// `get_if<T>(&v)` — the Rust shape of C++ overload-on-T over this variant.
38pub trait PredicateMember: Sized {
39    fn get_if(v: &Predicate) -> Option<&Self>;
40    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self>;
41}
42
43impl PredicateMember for TruthyPredicate {
44    fn get_if(v: &Predicate) -> Option<&Self> {
45        match v {
46            Predicate::Truthy(x) => Some(x),
47            _ => None,
48        }
49    }
50    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
51        match v {
52            Predicate::Truthy(x) => Some(x),
53            _ => None,
54        }
55    }
56}
57
58impl PredicateMember for IsAPredicate {
59    fn get_if(v: &Predicate) -> Option<&Self> {
60        match v {
61            Predicate::IsA(x) => Some(x),
62            _ => None,
63        }
64    }
65    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
66        match v {
67            Predicate::IsA(x) => Some(x),
68            _ => None,
69        }
70    }
71}
72
73impl PredicateMember for TypeGuardPredicate {
74    fn get_if(v: &Predicate) -> Option<&Self> {
75        match v {
76            Predicate::TypeGuard(x) => Some(x),
77            _ => None,
78        }
79    }
80    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
81        match v {
82            Predicate::TypeGuard(x) => Some(x),
83            _ => None,
84        }
85    }
86}
87
88impl PredicateMember for EqPredicate {
89    fn get_if(v: &Predicate) -> Option<&Self> {
90        match v {
91            Predicate::Eq(x) => Some(x),
92            _ => None,
93        }
94    }
95    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
96        match v {
97            Predicate::Eq(x) => Some(x),
98            _ => None,
99        }
100    }
101}
102
103impl PredicateMember for AndPredicate {
104    fn get_if(v: &Predicate) -> Option<&Self> {
105        match v {
106            Predicate::And(x) => Some(x),
107            _ => None,
108        }
109    }
110    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
111        match v {
112            Predicate::And(x) => Some(x),
113            _ => None,
114        }
115    }
116}
117
118impl PredicateMember for OrPredicate {
119    fn get_if(v: &Predicate) -> Option<&Self> {
120        match v {
121            Predicate::Or(x) => Some(x),
122            _ => None,
123        }
124    }
125    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
126        match v {
127            Predicate::Or(x) => Some(x),
128            _ => None,
129        }
130    }
131}
132
133impl PredicateMember for NotPredicate {
134    fn get_if(v: &Predicate) -> Option<&Self> {
135        match v {
136            Predicate::Not(x) => Some(x),
137            _ => None,
138        }
139    }
140    fn get_if_mut(v: &mut Predicate) -> Option<&mut Self> {
141        match v {
142            Predicate::Not(x) => Some(x),
143            _ => None,
144        }
145    }
146}