Skip to content

Commit 3a46322

Browse files
chore: add helper function to remove duplicated state changes (#83)
Co-authored-by: Tom French <[email protected]>
1 parent 2d90375 commit 3a46322

File tree

2 files changed

+40
-74
lines changed

2 files changed

+40
-74
lines changed

src/get_array.nr

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use crate::utils::cast_num_to_u32;
1010
* @brief getter methods for extracting array types out of a JSON struct
1111
**/
1212
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
13+
/**
14+
* @brief Updates the JSON object to point to a specific array entry
15+
**/
16+
pub(crate) fn update_json_array(mut self, entry: JSONEntry, key_index: u32) -> Self {
17+
self.layer_type_of_root = cast_num_to_u32(entry.parent_index);
18+
self.root_id = entry.id;
19+
self.layer_type_of_root = ARRAY_LAYER;
20+
self.root_index_in_transcript = key_index;
21+
self
22+
}
1323

1424
/**
1525
* @brief if the root JSON is an array, return its length
@@ -34,14 +44,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
3444
"key does not describe an object",
3545
);
3646

37-
let mut r = self;
38-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
39-
r.root_id = entry.id;
40-
r.layer_type_of_root = ARRAY_LAYER;
41-
r.root_index_in_transcript = key_index;
47+
let result = self.update_json_array(entry, key_index);
4248

4349
if exists {
44-
Option::some(r)
50+
Option::some(result)
4551
} else {
4652
Option::none()
4753
}
@@ -57,13 +63,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
5763
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index(key);
5864
assert(entry.entry_type == END_ARRAY_TOKEN as Field, "key does not describe an object");
5965

60-
let mut r = self;
61-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
62-
r.root_id = entry.id;
63-
r.layer_type_of_root = ARRAY_LAYER;
64-
r.root_index_in_transcript = key_index;
65-
66-
r
66+
self.update_json_array(entry, key_index)
6767
}
6868

6969
/**
@@ -85,14 +85,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
8585
);
8686
}
8787

88-
let mut r = self;
89-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
90-
r.root_id = entry.id;
91-
r.layer_type_of_root = ARRAY_LAYER;
92-
r.root_index_in_transcript = key_index;
88+
let result = self.update_json_array(entry, key_index);
9389

9490
if exists {
95-
Option::some(r)
91+
Option::some(result)
9692
} else {
9793
Option::none()
9894
}
@@ -110,13 +106,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
110106
let (entry, key_index) = self.get_json_entry_unchecked_with_key_index_var(key);
111107
assert(entry.entry_type == END_ARRAY_TOKEN as Field, "key does not describe an object");
112108

113-
let mut r = self;
114-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
115-
r.root_id = entry.id;
116-
r.layer_type_of_root = ARRAY_LAYER;
117-
r.root_index_in_transcript = key_index;
118-
119-
r
109+
self.update_json_array(entry, key_index)
120110
}
121111

122112
/**
@@ -143,14 +133,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
143133
);
144134
}
145135

146-
let mut r = self;
147-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
148-
r.root_id = entry.id;
149-
r.layer_type_of_root = ARRAY_LAYER;
150-
r.root_index_in_transcript = cast_num_to_u32(entry_index);
136+
let result = self.update_json_array(entry, cast_num_to_u32(entry_index));
151137

152138
if valid {
153-
Option::some(r)
139+
Option::some(result)
154140
} else {
155141
Option::none()
156142
}
@@ -177,12 +163,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
177163
"get_array_from_array_unchecked: entry exists but is not an array!",
178164
);
179165

180-
let mut r = self;
181-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
182-
r.root_id = entry.id;
183-
r.layer_type_of_root = ARRAY_LAYER;
184-
r.root_index_in_transcript = cast_num_to_u32(entry_index);
185-
r
166+
self.update_json_array(entry, cast_num_to_u32(entry_index))
186167
}
187168

188169
/**

src/get_object.nr

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ use crate::utils::cast_num_to_u32;
99
* @brief getter methods for extracting object types out of a JSON struct
1010
**/
1111
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
12+
/**
13+
* @brief Updates the JSON object to point to a specific object entry
14+
**/
15+
pub(crate) fn update_json_object(mut self, entry: JSONEntry, key_index: u32) -> Self {
16+
self.layer_type_of_root = cast_num_to_u32(entry.parent_index);
17+
self.root_id = entry.id;
18+
self.layer_type_of_root = OBJECT_LAYER;
19+
self.root_index_in_transcript = key_index;
20+
self
21+
}
1222

1323
/**
1424
* @brief if the root JSON is an object, extract a child object given by `key`
@@ -24,13 +34,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
2434
"get_object: entry exists but is not an object!",
2535
);
2636

27-
let mut r = self;
28-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
29-
r.root_id = entry.id;
30-
r.layer_type_of_root = OBJECT_LAYER;
31-
r.root_index_in_transcript = key_index;
37+
let result = self.update_json_object(entry, key_index);
38+
3239
if exists {
33-
Option::some(r)
40+
Option::some(result)
3441
} else {
3542
Option::none()
3643
}
@@ -49,12 +56,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
4956
"get_object: entry exists but is not an object!",
5057
);
5158

52-
let mut r = self;
53-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
54-
r.root_id = entry.id;
55-
r.layer_type_of_root = OBJECT_LAYER;
56-
r.root_index_in_transcript = key_index;
57-
r
59+
self.update_json_object(entry, key_index)
5860
}
5961

6062
/**
@@ -73,13 +75,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7375
"get_object: entry exists but is not an object!",
7476
);
7577

76-
let mut r = self;
77-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
78-
r.root_id = entry.id;
79-
r.layer_type_of_root = OBJECT_LAYER;
80-
r.root_index_in_transcript = key_index;
78+
let result = self.update_json_object(entry, key_index);
79+
8180
if exists {
82-
Option::some(r)
81+
Option::some(result)
8382
} else {
8483
Option::none()
8584
}
@@ -100,12 +99,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
10099
"get_object: entry exists but is not an object!",
101100
);
102101

103-
let mut r = self;
104-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
105-
r.root_id = entry.id;
106-
r.layer_type_of_root = OBJECT_LAYER;
107-
r.root_index_in_transcript = key_index;
108-
r
102+
self.update_json_object(entry, key_index)
109103
}
110104

111105
/**
@@ -133,14 +127,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
133127
);
134128
}
135129

136-
let mut r = self;
137-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
138-
r.root_id = entry.id;
139-
r.layer_type_of_root = OBJECT_LAYER;
140-
r.root_index_in_transcript = cast_num_to_u32(entry_index);
130+
let result = self.update_json_object(entry, cast_num_to_u32(entry_index));
141131

142132
if valid {
143-
Option::some(r)
133+
Option::some(result)
144134
} else {
145135
Option::none()
146136
}
@@ -169,12 +159,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
169159
"get_object_from_array_unchecked: entry exists but is not an object!",
170160
);
171161

172-
let mut r = self;
173-
r.layer_type_of_root = cast_num_to_u32(entry.parent_index);
174-
r.root_id = entry.id;
175-
r.layer_type_of_root = OBJECT_LAYER;
176-
r.root_index_in_transcript = cast_num_to_u32(entry_index);
177-
r
162+
self.update_json_object(entry, cast_num_to_u32(entry_index))
178163
}
179164
}
180165

0 commit comments

Comments
 (0)