-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmod.rs
More file actions
384 lines (366 loc) · 18 KB
/
mod.rs
File metadata and controls
384 lines (366 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use dojo_types::schema::Ty;
use starknet_crypto::Felt;
use torii_proto::{
Clause, ComparisonOperator, KeysClause, LogicalOperator, MemberValue, PatternMatching,
};
pub mod activity;
pub mod aggregation;
pub mod contract;
pub mod entity;
pub mod error;
pub mod event;
pub mod event_message;
pub mod token;
pub mod token_balance;
pub mod token_transfer;
pub mod transaction;
pub(crate) fn match_entity(
id: Felt,
keys: &[Felt],
updated_model: &Option<Ty>,
clause: &Clause,
) -> bool {
match clause {
Clause::HashedKeys(hashed_keys) => hashed_keys.is_empty() || hashed_keys.contains(&id),
Clause::Keys(clause) => {
// Check model matching if specified in the clause
if !clause.models.is_empty() {
if let Some(updated_model) = &updated_model {
let name = updated_model.name();
// Split name into namespace and model parts
let (namespace, name) = name.split_once('-').unwrap_or(("", &name));
// Check if any model clause matches
if !clause.models.iter().any(|clause_model| {
if clause_model.is_empty() {
return true;
}
let (clause_namespace, clause_model) =
clause_model.split_once('-').unwrap_or((clause_model, ""));
// Match namespace and model name according to rules:
// - Empty or * namespace matches any namespace
// - Empty or * model matches any model in the specified namespace
(clause_namespace.is_empty()
|| clause_namespace == "*"
|| clause_namespace == namespace)
&& (clause_model.is_empty()
|| clause_model == "*"
|| clause_model == name)
}) {
return false;
}
} else {
// No model available but models specified in clause
return false;
}
}
// Check key pattern matching
if clause.pattern_matching == PatternMatching::FixedLen
&& keys.len() != clause.keys.len()
{
return false;
}
// Check if all keys match the pattern
keys.iter().enumerate().all(|(idx, key)| {
match clause.keys.get(idx) {
// Specific key requirement at this position
Some(Some(sub_key)) => key == sub_key,
// No specific requirement (None or position beyond clause.keys)
_ => true,
}
})
}
Clause::Member(member_clause) => {
let updated_model = match updated_model {
Some(model) => model,
None => return false, // No model to match against
};
// Check if model name matches
if updated_model.name() != member_clause.model {
return false;
}
// Check for array indexing syntax like "field[0]"
let (member_path, array_index) = if let Some(start) = member_clause.member.find('[') {
if let Some(end) = member_clause.member.find(']') {
let field = &member_clause.member[..start];
let index_str = &member_clause.member[start + 1..end];
if let Ok(index) = index_str.parse::<usize>() {
(field, Some(index))
} else {
(member_clause.member.as_str(), None)
}
} else {
(member_clause.member.as_str(), None)
}
} else {
(member_clause.member.as_str(), None)
};
// Split the member path
let parts = member_path.split('.').collect::<Vec<&str>>();
// Traverse the model structure to find the target member
let mut current_ty = updated_model.clone();
for (idx, part) in parts.iter().enumerate() {
match current_ty {
Ty::Struct(struct_ty) => {
// Find the member with matching name
if let Some(member) = struct_ty.children.iter().find(|c| c.name == *part) {
current_ty = member.ty.clone();
} else {
return false; // Member not found
}
}
Ty::Tuple(tuple_ty) => {
// Access tuple element by index
if let Ok(index) = part.parse::<usize>() {
if let Some(ty) = tuple_ty.get(index) {
current_ty = ty.clone();
} else {
return false; // Index out of bounds
}
} else {
return false; // Invalid index
}
}
Ty::Enum(enum_ty) => {
let is_last_part = idx == parts.len() - 1;
if is_last_part {
// If it's the last part, compare the enum option's name
let option = match enum_ty.option() {
Ok(opt) => opt,
Err(_) => return false, // No enum option selected
};
return match (member_clause.operator.clone(), &member_clause.value) {
(ComparisonOperator::Eq, MemberValue::String(value)) => {
option.name == *value
}
(ComparisonOperator::Neq, MemberValue::String(value)) => {
option.name != *value
}
(ComparisonOperator::In, MemberValue::List(values)) => {
values.iter().any(|v| match v {
MemberValue::String(s) => option.name == *s,
_ => false,
})
}
(ComparisonOperator::NotIn, MemberValue::List(values)) => {
!values.iter().any(|v| match v {
MemberValue::String(s) => option.name == *s,
_ => false,
})
}
_ => false, // Other operators don't make sense for enum names
};
} else {
// Navigate to the selected enum option
if let Some(option_idx) =
enum_ty.options.iter().position(|o| o.name == *part)
{
if Some(option_idx as u8) == enum_ty.option {
current_ty = enum_ty.options[option_idx].ty.clone();
} else {
return false; // Option not selected
}
} else {
return false; // Option not found
}
}
}
Ty::Array(array_ty) | Ty::FixedSizeArray((array_ty, _)) => {
// If we have array indexing, extract the element
if let Some(index) = array_index {
if let Some(element) = array_ty.get(index) {
current_ty = element.clone();
} else {
return false; // Index out of bounds
}
} else {
// Array types without indexing cannot be navigated further
return false;
}
}
Ty::ByteArray(_) | Ty::Primitive(_) => {
// These types cannot be navigated further
return false;
}
}
}
// After navigating the path, compare the final type with the clause value
match current_ty {
Ty::Primitive(primitive) => {
match (member_clause.operator.clone(), &member_clause.value) {
(ComparisonOperator::Eq, MemberValue::Primitive(value)) => {
primitive == *value
}
(ComparisonOperator::Neq, MemberValue::Primitive(value)) => {
primitive != *value
}
(ComparisonOperator::Gt, MemberValue::Primitive(value)) => {
primitive > *value
}
(ComparisonOperator::Gte, MemberValue::Primitive(value)) => {
primitive >= *value
}
(ComparisonOperator::Lt, MemberValue::Primitive(value)) => {
primitive < *value
}
(ComparisonOperator::Lte, MemberValue::Primitive(value)) => {
primitive <= *value
}
(ComparisonOperator::In, MemberValue::List(values)) => {
values.iter().any(|v| match v {
MemberValue::Primitive(p) => primitive == *p,
_ => false,
})
}
(ComparisonOperator::NotIn, MemberValue::List(values)) => {
!values.iter().any(|v| match v {
MemberValue::Primitive(p) => primitive == *p,
_ => false,
})
}
_ => false,
}
}
Ty::ByteArray(string) => {
match (member_clause.operator.clone(), &member_clause.value) {
(ComparisonOperator::Eq, MemberValue::String(value)) => string == *value,
(ComparisonOperator::Neq, MemberValue::String(value)) => string != *value,
(ComparisonOperator::Gt, MemberValue::String(value)) => string > *value,
(ComparisonOperator::Gte, MemberValue::String(value)) => string >= *value,
(ComparisonOperator::Lt, MemberValue::String(value)) => string < *value,
(ComparisonOperator::Lte, MemberValue::String(value)) => string <= *value,
(ComparisonOperator::In, MemberValue::List(values)) => {
values.iter().any(|v| match v {
MemberValue::String(s) => string == *s,
_ => false,
})
}
(ComparisonOperator::NotIn, MemberValue::List(values)) => {
!values.iter().any(|v| match v {
MemberValue::String(s) => string == *s,
_ => false,
})
}
_ => false,
}
}
Ty::Enum(enum_ty) => {
// Compare the enum option's name
let option = match enum_ty.option() {
Ok(opt) => opt,
Err(_) => return false, // No enum option selected
};
match (member_clause.operator.clone(), &member_clause.value) {
(ComparisonOperator::Eq, MemberValue::String(value)) => {
option.name == *value
}
(ComparisonOperator::Neq, MemberValue::String(value)) => {
option.name != *value
}
(ComparisonOperator::In, MemberValue::List(values)) => {
values.iter().any(|v| match v {
MemberValue::String(s) => option.name == *s,
_ => false,
})
}
(ComparisonOperator::NotIn, MemberValue::List(values)) => {
!values.iter().any(|v| match v {
MemberValue::String(s) => option.name == *s,
_ => false,
})
}
_ => false,
}
}
Ty::Array(array_ty) | Ty::FixedSizeArray((array_ty, _)) => {
// Array operations on whole array (only when no indexing was used)
match (member_clause.operator.clone(), &member_clause.value) {
(ComparisonOperator::Contains, MemberValue::Primitive(value)) => array_ty
.iter()
.any(|elem| matches!(elem, Ty::Primitive(p) if p == value)),
(ComparisonOperator::Contains, MemberValue::String(value)) => array_ty
.iter()
.any(|elem| matches!(elem, Ty::ByteArray(s) if s == value)),
(ComparisonOperator::ContainsAll, MemberValue::List(values)) => {
values.iter().all(|search_val| {
array_ty.iter().any(|elem| match (elem, search_val) {
(Ty::Primitive(p), MemberValue::Primitive(v)) => p == v,
(Ty::ByteArray(s), MemberValue::String(v)) => s == v,
_ => false,
})
})
}
(ComparisonOperator::ContainsAny, MemberValue::List(values)) => {
values.iter().any(|search_val| {
array_ty.iter().any(|elem| match (elem, search_val) {
(Ty::Primitive(p), MemberValue::Primitive(v)) => p == v,
(Ty::ByteArray(s), MemberValue::String(v)) => s == v,
_ => false,
})
})
}
(ComparisonOperator::ArrayLengthEq, MemberValue::Primitive(value)) => value
.as_u32()
.is_some_and(|len| array_ty.len() == len as usize),
(ComparisonOperator::ArrayLengthGt, MemberValue::Primitive(value)) => value
.as_u32()
.is_some_and(|len| array_ty.len() > len as usize),
(ComparisonOperator::ArrayLengthLt, MemberValue::Primitive(value)) => value
.as_u32()
.is_some_and(|len| array_ty.len() < len as usize),
_ => false,
}
}
Ty::Struct(_) | Ty::Tuple(_) => {
// These types are not directly comparable to a MemberValue
false
}
}
}
Clause::Composite(composite_clause) => match composite_clause.operator {
LogicalOperator::And => composite_clause
.clauses
.iter()
.all(|c| match_entity(id, keys, updated_model, c)),
LogicalOperator::Or => composite_clause
.clauses
.iter()
.any(|c| match_entity(id, keys, updated_model, c)),
},
}
}
pub(crate) fn match_keys(keys: &[Felt], clauses: &[KeysClause]) -> bool {
// Check if the subscriber is interested in this entity
// If we have a clause of hashed keys, then check that the id of the entity
// is in the list of hashed keys.
// If we have a clause of keys, then check that the key pattern of the entity
// matches the key pattern of the subscriber.
if !clauses.is_empty()
&& !clauses.iter().any(|clause| {
// if the key pattern doesnt match our subscribers key pattern, skip
// ["", "0x0"] would match with keys ["0x...", "0x0", ...]
if clause.pattern_matching == PatternMatching::FixedLen
&& keys.len() != clause.keys.len()
{
return false;
}
keys.iter().enumerate().all(|(idx, key)| {
// this is going to be None if our key pattern overflows the subscriber
// key pattern in this case we should skip
let sub_key = clause.keys.get(idx);
match sub_key {
// the key in the subscriber must match the key of the entity
// athis index
Some(Some(sub_key)) => key == sub_key,
// otherwise, if we have no key we should automatically match.
// or.. we overflowed the subscriber key pattern
// but we're in VariableLen pattern matching
// so we should match all next keys
_ => true,
}
})
})
{
return false;
}
true
}