event_trigger_action_system/
constructors.rs1use crate::TriggerCondition;
2use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
3
4pub fn none<Event>() -> TriggerCondition<Event> {
6 TriggerCondition::None
7}
8
9pub fn never<Event>() -> TriggerCondition<Event> {
11 TriggerCondition::Never
12}
13
14pub fn event_count<Event>(event: Event, required: usize) -> TriggerCondition<Event> {
16 TriggerCondition::EventCount { event, required }
17}
18
19pub fn gt<Event>(reference_event: Event) -> TriggerCondition<Event> {
21 TriggerCondition::Greater { reference_event }
22}
23
24pub fn geq<Event>(reference_event: Event) -> TriggerCondition<Event> {
26 TriggerCondition::GreaterOrEqual { reference_event }
27}
28
29pub fn eq<Event>(reference_event: Event) -> TriggerCondition<Event> {
31 TriggerCondition::Equal { reference_event }
32}
33
34pub fn leq<Event>(reference_event: Event) -> TriggerCondition<Event> {
36 TriggerCondition::LessOrEqual { reference_event }
37}
38
39pub fn lt<Event>(reference_event: Event) -> TriggerCondition<Event> {
41 TriggerCondition::Less { reference_event }
42}
43
44pub fn and<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
46 TriggerCondition::And { conditions }
47}
48
49pub fn or<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
51 TriggerCondition::Or { conditions }
52}
53
54pub fn sequence<Event>(conditions: Vec<TriggerCondition<Event>>) -> TriggerCondition<Event> {
60 TriggerCondition::Sequence { conditions }
61}
62
63pub 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}