teo_runtime/action/
action.rs1use std::ops::{BitAnd, BitOr, BitXor, Neg, Not};
2use std::slice::Iter;
3use serde::Serialize;
4use super::const_values::*;
5use teo_result::Error;
6use teo_result::Result;
7
8#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
9pub struct Action(pub u32);
10
11impl BitOr for Action {
12
13 type Output = Action;
14
15 fn bitor(self, rhs: Self) -> Self::Output {
16 Self(self.0 | rhs.0)
17 }
18}
19
20impl BitAnd for Action {
21
22 type Output = Action;
23
24 fn bitand(self, rhs: Self) -> Self::Output {
25 Self(self.0 & rhs.0)
26 }
27}
28
29impl BitXor for Action {
30
31 type Output = Action;
32
33 fn bitxor(self, rhs: Self) -> Self::Output {
34 Self(self.0 & rhs.0)
35 }
36}
37
38impl Not for Action {
39
40 type Output = Action;
41
42 fn not(self) -> Self::Output {
43 Self(!self.0)
44 }
45}
46
47impl Neg for Action {
48
49 type Output = Action;
50
51 fn neg(self) -> Self::Output {
52 let restore_name_bits = !self.contains_name_bits();
53 let restore_entry_nested_bits = !self.contains_position_bits();
54 let restore_single_many_bits = !self.contains_amount_bits();
55 let mut value = !self;
56 if restore_name_bits {
57 value = value & NOT_ALL_NAMES;
58 }
59 if restore_entry_nested_bits {
60 value = value & NOT_ENTRY_NESTED;
61 }
62 if restore_single_many_bits {
63 value = value & NOT_SINGLE_MANY;
64 }
65 value
66 }
67}
68
69pub const CREATE: Action = Action(CREATE_U32);
70pub const UPDATE: Action = Action(UPDATE_U32);
71pub const DELETE: Action = Action(DELETE_U32);
72pub const COPY: Action = Action(COPY_U32);
73pub const FIND: Action = Action(FIND_U32);
74pub const FIRST: Action = Action(FIRST_U32);
75pub const CONNECT: Action = Action(CONNECT_U32);
76pub const DISCONNECT: Action = Action(DISCONNECT_U32);
77pub const SET: Action = Action(SET_U32);
78pub const JOIN: Action = Action(JOIN_U32);
79pub const COUNT: Action = Action(COUNT_U32);
80pub const AGGREGATE: Action = Action(AGGREGATE_U32);
81pub const GROUP_BY: Action = Action(GROUP_BY_U32);
82pub const CODE_NAME: Action = Action(CODE_NAME_U32);
83
84pub const UPSERT: Action = Action(UPSERT_U32);
85pub const CONNECT_OR_CREATE: Action = Action(CONNECT_OR_CREATE_U32);
86pub const JOIN_CREATE: Action = Action(JOIN_CREATE_U32);
87pub const JOIN_DELETE: Action = Action(JOIN_DELETE_U32);
88pub const FIND_FIRST: Action = Action(FIND_FIRST_U32);
89
90pub const ENTRY: Action = Action(ENTRY_U32);
91pub const NESTED: Action = Action(NESTED_U32);
92pub const CODE_POSITION: Action = Action(CODE_POSITION_U32);
93
94pub const SINGLE: Action = Action(SINGLE_U32);
95pub const MANY: Action = Action(MANY_U32);
96pub const CODE_AMOUNT: Action = Action(CODE_AMOUNT_U32);
97
98const ALL_NAMES: Action = Action(ALL_NAMES_U32);
99const ALL_POSITIONS: Action = Action(ALL_POSITIONS_U32);
100const ALL_AMOUNTS: Action = Action(ALL_AMOUNTS_U32);
101
102const NOT_ALL_NAMES: Action = Action(NOT_ALL_NAMES_U32);
103const NOT_ENTRY_NESTED: Action = Action(NOT_ENTRY_NESTED_U32);
104const NOT_SINGLE_MANY: Action = Action(NOT_SINGLE_MANY_U32);
105
106pub(crate) const FIND_UNIQUE_HANDLER: Action = Action(FIND_UNIQUE_HANDLER_U32);
107pub(crate) const FIND_FIRST_HANDLER: Action = Action(FIND_FIRST_HANDLER_U32);
108pub(crate) const FIND_MANY_HANDLER: Action = Action(FIND_MANY_HANDLER_U32);
109pub(crate) const CREATE_HANDLER: Action = Action(CREATE_HANDLER_U32);
110pub(crate) const UPDATE_HANDLER: Action = Action(UPDATE_HANDLER_U32);
111pub(crate) const COPY_HANDLER: Action = Action(COPY_HANDLER_U32);
112pub(crate) const UPSERT_HANDLER: Action = Action(UPSERT_HANDLER_U32);
113pub(crate) const DELETE_HANDLER: Action = Action(DELETE_HANDLER_U32);
114pub(crate) const CREATE_MANY_HANDLER: Action = Action(CREATE_MANY_HANDLER_U32);
115pub(crate) const UPDATE_MANY_HANDLER: Action = Action(UPDATE_MANY_HANDLER_U32);
116pub(crate) const COPY_MANY_HANDLER: Action = Action(COPY_MANY_HANDLER_U32);
117pub(crate) const DELETE_MANY_HANDLER: Action = Action(DELETE_MANY_HANDLER_U32);
118pub(crate) const COUNT_HANDLER: Action = Action(COUNT_HANDLER_U32);
119pub(crate) const AGGREGATE_HANDLER: Action = Action(AGGREGATE_HANDLER_U32);
120pub(crate) const GROUP_BY_HANDLER: Action = Action(GROUP_BY_HANDLER_U32);
121
122pub(crate) const NESTED_CREATE_ACTION: Action = Action(NESTED_CREATE_ACTION_U32);
123pub(crate) const NESTED_UPDATE_ACTION: Action = Action(NESTED_UPDATE_ACTION_U32);
124pub(crate) const NESTED_UPSERT_ACTION: Action = Action(NESTED_UPSERT_ACTION_U32);
125pub(crate) const NESTED_DELETE_ACTION: Action = Action(NESTED_DELETE_ACTION_U32);
126pub(crate) const NESTED_COPY_ACTION: Action = Action(NESTED_COPY_ACTION_U32);
127pub(crate) const NESTED_CONNECT_OR_CREATE_ACTION: Action = Action(NESTED_CONNECT_OR_CREATE_ACTION_U32);
128pub(crate) const NESTED_CONNECT_ACTION: Action = Action(NESTED_CONNECT_ACTION_U32);
129pub(crate) const NESTED_DISCONNECT_ACTION: Action = Action(NESTED_DISCONNECT_ACTION_U32);
130pub(crate) const NESTED_SET_ACTION: Action = Action(NESTED_SET_ACTION_U32);
131pub(crate) const NESTED_CREATE_MANY_ACTION: Action = Action(NESTED_CREATE_MANY_ACTION_U32);
132pub(crate) const NESTED_UPDATE_MANY_ACTION: Action = Action(NESTED_UPDATE_MANY_ACTION_U32);
133pub(crate) const NESTED_DELETE_MANY_ACTION: Action = Action(NESTED_DELETE_MANY_ACTION_U32);
134
135impl Default for Action {
136
137 fn default() -> Self {
138 Self::empty()
139 }
140}
141
142impl Action {
143
144 pub(crate) fn empty() -> Self {
145 Self(0)
146 }
147
148 pub(crate) fn is_empty(&self) -> bool {
149 self.0 == 0
150 }
151
152 pub(crate) fn from_name(name: &str) -> Result<Self> {
153 Ok(match name {
154 "create" => CREATE,
155 "update" => UPDATE,
156 "delete" => DELETE,
157 "copy" => COPY,
158 "find" => FIND,
159 "connect" => CONNECT,
160 "disconnect" => DISCONNECT,
161 "set" => SET,
162 "join" => JOIN,
163 "first" => FIRST,
164 "count" => COUNT,
165 "aggregate" => AGGREGATE,
166 "groupBy" => GROUP_BY,
167 "codeName" => CODE_NAME,
168 "entry" => ENTRY,
169 "nested" => NESTED,
170 "codePosition" => CODE_POSITION,
171 "single" => SINGLE,
172 "many" => MANY,
173 "codeAmount" => CODE_AMOUNT,
174 _ => Err(Error::new(format!("Unrecognized action name '{}'", name)))?
175 })
176 }
177
178 pub(crate) fn nested_from_name(name: &str) -> Result<Self> {
179 Ok(match name {
180 "create" => NESTED_CREATE_ACTION,
181 "update" => NESTED_UPDATE_ACTION,
182 "upsert" => NESTED_UPSERT_ACTION,
183 "delete" => NESTED_DELETE_ACTION,
184 "copy" => NESTED_COPY_ACTION,
185 "connect" => NESTED_CONNECT_ACTION,
186 "connectOrCreate" => NESTED_CONNECT_OR_CREATE_ACTION,
187 "disconnect" => NESTED_DISCONNECT_ACTION,
188 "set" => NESTED_SET_ACTION,
189 "createMany" => NESTED_CREATE_ACTION,
190 "updateMany" => NESTED_UPDATE_MANY_ACTION,
191 "deleteMany" => NESTED_DELETE_MANY_ACTION,
192 _ => Err(Error::new(format!("Unrecognized nested action name '{}'", name)))?
193 })
194 }
195
196 pub(crate) fn finalized(&self) -> Self {
197 let mut value = *self;
198 if !self.contains_name_bits() {
199 value = value | ALL_NAMES;
200 }
201 if !self.contains_position_bits() {
202 value = value | ALL_POSITIONS;
203 }
204 if !self.contains_amount_bits() {
205 value = value | ALL_AMOUNTS;
206 }
207 value
208 }
209
210 pub(crate) fn redirect(&self, action: Action) -> Self {
211 let mut result = *self;
212 let new_names_bits = action & ALL_NAMES;
213 if new_names_bits.0 != 0 {
214 result = (result & !ALL_NAMES) | new_names_bits;
215 }
216 let new_position_bits = action & ALL_POSITIONS;
217 if new_position_bits.0 != 0 {
218 result = (result & !ALL_POSITIONS) | new_position_bits;
219 }
220 let new_amount_bits = action & ALL_AMOUNTS;
221 if new_amount_bits.0 != 0 {
222 result = (result & !ALL_AMOUNTS) | new_amount_bits;
223 }
224 result
225 }
226
227 #[inline(always)]
228 fn contains_name_bits(&self) -> bool {
229 (*self & ALL_NAMES).0 != 0
230 }
231
232 #[inline(always)]
233 fn contains_position_bits(&self) -> bool {
234 (*self & ALL_POSITIONS).0 != 0
235 }
236
237 #[inline(always)]
238 fn contains_amount_bits(&self) -> bool {
239 (*self & ALL_AMOUNTS).0 != 0
240 }
241
242 pub(crate) fn passes(&self, matchers: &Vec<Action>) -> bool {
243 for matcher in matchers {
244 let copy = self.finalized();
245 let finalized_matcher = matcher.finalized();
246 let result = finalized_matcher & copy;
247 return ((result & ALL_NAMES).0 != 0) &&
248 ((result & ALL_POSITIONS).0 != 0) &&
249 ((result & ALL_AMOUNTS).0 != 0)
250 }
251 false
252 }
253
254 pub fn builtin_handlers() -> Iter<'static, Action> {
255 static HANDLER_TYPES: [Action; 15] = [
256 FIND_UNIQUE_HANDLER,
257 FIND_FIRST_HANDLER,
258 FIND_MANY_HANDLER,
259 CREATE_HANDLER,
260 UPDATE_HANDLER,
261 UPSERT_HANDLER,
262 COPY_HANDLER,
263 DELETE_HANDLER,
264 CREATE_MANY_HANDLER,
265 UPDATE_MANY_HANDLER,
266 COPY_MANY_HANDLER,
267 DELETE_MANY_HANDLER,
268 COUNT_HANDLER,
269 AGGREGATE_HANDLER,
270 GROUP_BY_HANDLER,
271 ];
272 HANDLER_TYPES.iter()
273 }
274
275 pub fn as_handler_str(&self) -> &'static str {
276 match *self {
277 FIND_UNIQUE_HANDLER => "findUnique",
278 FIND_FIRST_HANDLER => "findFirst",
279 FIND_MANY_HANDLER => "findMany",
280 CREATE_HANDLER => "create",
281 UPDATE_HANDLER => "update",
282 COPY_HANDLER => "copy",
283 UPSERT_HANDLER => "upsert",
284 DELETE_HANDLER => "delete",
285 CREATE_MANY_HANDLER => "createMany",
286 UPDATE_MANY_HANDLER => "updateMany",
287 COPY_MANY_HANDLER => "copyMany",
288 DELETE_MANY_HANDLER => "deleteMany",
289 COUNT_HANDLER => "count",
290 AGGREGATE_HANDLER => "aggregate",
291 GROUP_BY_HANDLER => "groupBy",
292 _ => unreachable!()
293 }
294 }
295}
296
297impl From<u32> for Action {
298
299 fn from(value: u32) -> Self {
300 Self(value)
301 }
302}
303
304impl From<Action> for u32 {
305
306 fn from(value: Action) -> Self {
307 value.0
308 }
309}
310
311impl From<i32> for Action {
312
313 fn from(value: i32) -> Self {
314 Self(value as u32)
315 }
316}
317
318impl From<Action> for i32 {
319
320 fn from(value: Action) -> Self {
321 value.0 as i32
322 }
323}