Skip to content

Commit 51574b7

Browse files
authored
chore: make some assert statements more idiomatic (#85)
1 parent 3a46322 commit 51574b7

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/get_object.nr

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7070

7171
let (exists, key_index) = self.key_exists_impl_var(key);
7272
let entry: JSONEntry = self.json_entries_packed[key_index].into();
73-
assert(
74-
(entry.entry_type - END_OBJECT_TOKEN as Field) * exists as Field == 0,
75-
"get_object: entry exists but is not an object!",
76-
);
73+
if exists {
74+
assert_eq(
75+
entry.entry_type,
76+
END_OBJECT_TOKEN as Field,
77+
"get_object: entry exists but is not an object!",
78+
);
79+
}
7780

7881
let result = self.update_json_object(entry, key_index);
7982

@@ -94,8 +97,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9497
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
9598
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key);
9699
let entry: JSONEntry = self.json_entries_packed[key_index].into();
97-
assert(
98-
entry.entry_type == END_OBJECT_TOKEN as Field,
100+
assert_eq(
101+
entry.entry_type,
102+
END_OBJECT_TOKEN as Field,
99103
"get_object: entry exists but is not an object!",
100104
);
101105

@@ -107,8 +111,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
107111
* @description returns an Option<JSON> where, if the object exists, the JSON object will have the requested object as its root value
108112
**/
109113
pub(crate) fn get_object_from_array(self, array_index: Field) -> Option<Self> {
110-
assert(
111-
self.layer_type_of_root == ARRAY_LAYER,
114+
assert_eq(
115+
self.layer_type_of_root,
116+
ARRAY_LAYER,
112117
"can only acceess array elements from array",
113118
);
114119

@@ -141,8 +146,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
141146
* @description will revert if the object does not exist
142147
**/
143148
pub(crate) fn get_object_from_array_unchecked(self, array_index: Field) -> Self {
144-
assert(
145-
self.layer_type_of_root == ARRAY_LAYER,
149+
assert_eq(
150+
self.layer_type_of_root,
151+
ARRAY_LAYER,
146152
"can only acceess array elements from array",
147153
);
148154

@@ -154,8 +160,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
154160
let entry_index = (parent_entry.child_pointer + array_index);
155161

156162
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
157-
assert(
158-
entry.entry_type == END_OBJECT_TOKEN as Field,
163+
assert_eq(
164+
entry.entry_type,
165+
END_OBJECT_TOKEN as Field,
159166
"get_object_from_array_unchecked: entry exists but is not an object!",
160167
);
161168

src/get_string.nr

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,13 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
152152
key: BoundedVec<u8, KeyBytes>,
153153
) -> Option<BoundedVec<u8, StringBytes>> {
154154
let (exists, entry) = self.get_json_entry_var(key);
155-
assert(
156-
(entry.entry_type - STRING_TOKEN as Field) * exists as Field == 0,
157-
"get_string: entry exists but is not a string!",
158-
);
155+
if exists {
156+
assert_eq(
157+
entry.entry_type,
158+
STRING_TOKEN as Field,
159+
"get_string: entry exists but is not a string!",
160+
);
161+
}
159162
let mut parsed_string: [u8; StringBytes] = self.extract_string_entry(entry);
160163
let parsed_string: BoundedVec<u8, StringBytes> = process_escape_sequences(
161164
BoundedVec::from_parts_unchecked(parsed_string, entry.json_length as u32),
@@ -176,8 +179,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
176179
key: BoundedVec<u8, KeyBytes>,
177180
) -> BoundedVec<u8, StringBytes> {
178181
let entry = self.get_json_entry_unchecked_var(key);
179-
assert(
180-
entry.entry_type == STRING_TOKEN as Field,
182+
assert_eq(
183+
entry.entry_type,
184+
STRING_TOKEN as Field,
181185
"get_string_unchecked: entry exists but is not a string!",
182186
);
183187
let parsed_string = BoundedVec::from_parts_unchecked(
@@ -195,8 +199,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
195199
self,
196200
array_index: Field,
197201
) -> Option<BoundedVec<u8, StringBytes>> {
198-
assert(
199-
self.layer_type_of_root == ARRAY_LAYER,
202+
assert_eq(
203+
self.layer_type_of_root,
204+
ARRAY_LAYER,
200205
"can only acceess array elements from array",
201206
);
202207

@@ -235,9 +240,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
235240
self,
236241
array_index: Field,
237242
) -> BoundedVec<u8, StringBytes> {
238-
assert(
239-
self.layer_type_of_root == ARRAY_LAYER,
240-
"can only acceess array elements from array",
243+
assert_eq(
244+
self.layer_type_of_root,
245+
ARRAY_LAYER,
246+
"can only access array elements from array",
241247
);
242248

243249
let parent_entry: JSONEntry =
@@ -249,8 +255,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
249255

250256
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
251257

252-
assert(
253-
entry.entry_type == STRING_TOKEN as Field,
258+
assert_eq(
259+
entry.entry_type,
260+
STRING_TOKEN as Field,
254261
"get_string_from_array_unchecked: entry exists but is not a string!",
255262
);
256263

@@ -375,8 +382,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
375382
self,
376383
array_index: Field,
377384
) -> Option<JSONValue<StringBytes>> {
378-
assert(
379-
self.layer_type_of_root == ARRAY_LAYER,
385+
assert_eq(
386+
self.layer_type_of_root,
387+
ARRAY_LAYER,
380388
"can only acceess array elements from array",
381389
);
382390

@@ -420,8 +428,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
420428
self,
421429
array_index: Field,
422430
) -> JSONValue<StringBytes> {
423-
assert(
424-
self.layer_type_of_root == ARRAY_LAYER,
431+
assert_eq(
432+
self.layer_type_of_root,
433+
ARRAY_LAYER,
425434
"can only acceess array elements from array",
426435
);
427436

@@ -434,8 +443,9 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
434443

435444
let entry: JSONEntry = self.json_entries_packed[cast_num_to_u32(entry_index)].into();
436445

437-
assert(
438-
entry.entry_type == STRING_TOKEN as Field,
446+
assert_eq(
447+
entry.entry_type,
448+
STRING_TOKEN as Field,
439449
"get_string_from_array_unchecked: entry exists but is not a string!",
440450
);
441451

0 commit comments

Comments
 (0)