event_trigger_action_system/
constructors.rs

1use crate::TriggerCondition;
2use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
3
4/// Constructs a trigger condition that triggers immediately.
5pub fn none<Event>() -> TriggerCondition<Event> {
6    TriggerCondition::None
7}
8
9/// Constructs a trigger condition that never triggers.
10pub fn never<Event>() -> TriggerCondition<Event> {
11    TriggerCondition::Never
12}
13
14/// Constructs a trigger condition that triggers after the given event has been received the given amount of times.
15pub fn event_count<Event>(event: Event, required: usize) -> TriggerCondition<Event> {
16    TriggerCondition::EventCount { event, required }
17}
18
19/// Constructs a trigger condition that triggers after an event has been received that is greater than the reference event.
20pub fn gt<Event>(reference_event: Event) -> TriggerCondition<Event> {
21    TriggerCondition::Greater { reference_event }
22}
23
24/// Constructs a trigger condition that triggers after an event has been received that is greater than or equal to the reference event.
25pub fn geq<Event>(reference_event: Event) -> TriggerCondition<Event> {
26    TriggerCondition::GreaterOrEqual { reference_event }
27}
28
29/// Constructs a trigger condition that triggers after an event has been received that is equal to the reference event.
30pub fn eq<Event>(reference_event: Event) -> TriggerCondition<Event> {
31    TriggerCondition::Equal { reference_event }
32}
33
34/// Constructs a trigger condition that triggers after an event has been received that is less than or equal to the reference event.
35pub fn leq<Event>(reference_event: Event) -> TriggerCondition<Event> {
36    TriggerCondition::LessOrEqual { reference_event }
37}
38
39/// Constructs a trigger condition that triggers after an event has been received that is less than the reference event.
40pub fn lt<Event>(reference_event: Event) -> TriggerCondition<Event> {
41    TriggerCondition::Less { reference_event }
42}
43
44/// Constructs a trigger condition that triggers after all given conditions have triggered.
45pub fn and<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
46    TriggerCondition::And { conditions }
47}
48
49/// Constructs a trigger condition that triggers after any of the given conditions have triggered.
50pub fn or<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
51    TriggerCondition::Or { conditions }
52}
53
54/// Constructs a trigger condition that triggers after all given conditions have triggered in sequence.
55///
56/// This works by having a pointer to the current active condition in the sequence.
57/// Whenever a condition is fulfilled, the pointer gets moved to the right.
58/// If it has visited all conditions, then this condition triggers.
59pub fn sequence<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
60    TriggerCondition::Sequence { conditions }
61}
62
63/// Constructs a trigger condition that triggers after the given amount of given trigger conditions have triggered.
64pub fn any_n<Event>(conditions: Vec<TriggerCondition<Event>>, n: usize) -> TriggerCondition<Event> {
65    TriggerCondition::AnyN { conditions, n }
66}
67
68impl<Event: Clone> BitAndAssign for TriggerCondition<Event> {
69    fn bitand_assign(&mut self, rhs: Self) {
70        *self = self.clone() & rhs;
71    }
72}
73
74impl<Event: Clone> BitOrAssign for TriggerCondition<Event> {
75    fn bitor_assign(&mut self, rhs: Self) {
76        *self = self.clone() | rhs;
77    }
78}
79
80impl<Event> BitAnd for TriggerCondition<Event> {
81    type Output = TriggerCondition<Event>;
82
83    fn bitand(self, rhs: Self) -> Self::Output {
84        match (self, rhs) {
85            (
86                TriggerCondition::And {
87                    conditions: mut conditions_self,
88                },
89                TriggerCondition::And {
90                    conditions: mut conditions_rhs,
91                },
92            ) => {
93                conditions_self.append(&mut conditions_rhs);
94                TriggerCondition::And {
95                    conditions: conditions_self,
96                }
97            }
98            (
99                TriggerCondition::And {
100                    conditions: mut conditions_self,
101                },
102                rhs,
103            ) => {
104                conditions_self.push(rhs);
105                TriggerCondition::And {
106                    conditions: conditions_self,
107                }
108            }
109            (
110                lhs,
111                TriggerCondition::And {
112                    conditions: mut conditions_rhs,
113                },
114            ) => {
115                conditions_rhs.push(lhs);
116                TriggerCondition::And {
117                    conditions: conditions_rhs,
118                }
119            }
120            (lhs, rhs) => {
121                let conditions = vec![lhs, rhs];
122                TriggerCondition::And { conditions }
123            }
124        }
125    }
126}
127
128impl<Event> BitOr for TriggerCondition<Event> {
129    type Output = TriggerCondition<Event>;
130
131    fn bitor(self, rhs: Self) -> Self::Output {
132        match (self, rhs) {
133            (
134                TriggerCondition::Or {
135                    conditions: mut conditions_self,
136                },
137                TriggerCondition::Or {
138                    conditions: mut conditions_rhs,
139                },
140            ) => {
141                conditions_self.append(&mut conditions_rhs);
142                TriggerCondition::Or {
143                    conditions: conditions_self,
144                }
145            }
146            (
147                TriggerCondition::Or {
148                    conditions: mut conditions_self,
149                },
150                rhs,
151            ) => {
152                conditions_self.push(rhs);
153                TriggerCondition::Or {
154                    conditions: conditions_self,
155                }
156            }
157            (
158                lhs,
159                TriggerCondition::Or {
160                    conditions: mut conditions_rhs,
161                },
162            ) => {
163                conditions_rhs.push(lhs);
164                TriggerCondition::Or {
165                    conditions: conditions_rhs,
166                }
167            }
168            (lhs, rhs) => {
169                let conditions = vec![lhs, rhs];
170                TriggerCondition::Or { conditions }
171            }
172        }
173    }
174}