@@ -6,58 +6,43 @@ use crate::json::JSONValue;
6
6
use crate::json_entry::JSONEntry ;
7
7
use crate::utils::cast_num_to_u32 ;
8
8
9
- /**
10
- * @brief getter methods for extracting array types out of a JSON struct
11
- **/
9
+ /// getter methods for extracting array types out of a JSON struct
12
10
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
- **/
11
+ /// Updates the JSON object to point to a specific array entry
16
12
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
13
self .root_id = entry .id ;
19
14
self .layer_type_of_root = ARRAY_LAYER ;
20
15
self .root_index_in_transcript = key_index ;
21
16
self
22
17
}
23
-
24
- /**
25
- * @brief if the root JSON is an array, return its length
26
- **/
18
+ /// if the root JSON is an array, return its length
27
19
pub (crate ) fn get_length (self ) -> u32 {
28
20
assert (self .layer_type_of_root == ARRAY_LAYER , "can only get length of an array type" );
29
21
let parent_entry : JSONEntry =
30
22
self .json_entries_packed [self .root_index_in_transcript ].into ();
31
23
parent_entry .num_children as u32
32
24
}
33
25
34
- /**
35
- * @brief if the root JSON is an object, extract an array given by `key`
36
- * @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
37
- **/
26
+ /// if the root JSON is an object, extract an array given by `key`
27
+ ///
28
+ /// returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
38
29
pub (crate ) fn get_array <let KeyBytes : u32 >(self , key : [u8 ; KeyBytes ]) -> Option <Self > {
39
30
self .get_array_var (BoundedVec ::from_parts_unchecked (key , KeyBytes ))
40
31
}
41
32
42
- /**
43
- * @brief if the root JSON is an object, extract an array given by `key`
44
- * @description will revert if the array does not exist
45
- **/
33
+ /// if the root JSON is an object, extract an array given by `key`
34
+ ///
35
+ /// will revert if the array does not exist
46
36
pub (crate ) fn get_array_unchecked <let KeyBytes : u32 >(self , key : [u8 ; KeyBytes ]) -> Self {
47
37
self .get_array_unchecked_var (BoundedVec ::from_parts_unchecked (key , KeyBytes ))
48
38
}
49
39
50
- /**
51
- * @brief same as `get_array` for where the key length may be less than KeyBytes
52
- **/
40
+ /// same as `get_array` for where the key length may be less than KeyBytes
53
41
pub (crate ) fn get_array_var <let KeyBytes : u32 >(
54
42
self ,
55
43
key : BoundedVec <u8 , KeyBytes >,
56
44
) -> Option <Self > {
57
- assert (self .layer_type_of_root != ARRAY_LAYER , "cannot extract array elements via a key" );
58
- let (exists , key_index ) = self .key_exists_impl_var (key );
59
- let entry : JSONEntry = self .json_entries_packed [key_index ].into ();
60
- // TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
45
+ let (exists , entry , key_index ) = self .get_json_entry_var (key );
61
46
if !exists {
62
47
assert_eq (
63
48
entry .entry_type ,
@@ -75,9 +60,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
75
60
}
76
61
}
77
62
78
- /**
79
- * @brief same as `get_array_unchecked` for where the key length may be less than KeyBytes
80
- **/
63
+ /// same as `get_array_unchecked` for where the key length may be less than KeyBytes
81
64
pub (crate ) fn get_array_unchecked_var <let KeyBytes : u32 >(
82
65
self ,
83
66
key : BoundedVec <u8 , KeyBytes >,
@@ -90,22 +73,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
90
73
self .update_json_array (entry , key_index )
91
74
}
92
75
93
- /**
94
- * @brief if the root JSON is an array, extract an array given by the position of the target in the source array
95
- * @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
96
- **/
76
+ /// if the root JSON is an array, extract an array given by the position of the target in the source array
77
+ ///
78
+ /// returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
97
79
pub (crate ) fn get_array_from_array (self , array_index : Field ) -> Option <Self > {
98
- assert (
99
- self .layer_type_of_root == ARRAY_LAYER ,
100
- "can only acceess array elements from array" ,
101
- );
102
-
103
- let parent_entry : JSONEntry =
104
- self .json_entries_packed [self .root_index_in_transcript ].into ();
105
- let valid = lt_field_16_bit (array_index , parent_entry .num_children );
106
- let entry_index = (parent_entry .child_pointer + array_index ) * valid as Field ;
107
-
108
- let entry : JSONEntry = self .json_entries_packed [cast_num_to_u32 (entry_index )].into ();
80
+ let (entry , valid , entry_index ) = self .get_entry_from_array (array_index );
109
81
if valid {
110
82
assert_eq (
111
83
entry .entry_type ,
@@ -114,7 +86,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
114
86
);
115
87
}
116
88
117
- let result = self .update_json_array (entry , cast_num_to_u32 ( entry_index ) );
89
+ let result = self .update_json_array (entry , entry_index );
118
90
119
91
if valid {
120
92
Option ::some (result )
@@ -123,33 +95,15 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
123
95
}
124
96
}
125
97
126
- /**
127
- * @brief if the root JSON is an array, extract an array given by the position of the target in the source array
128
- * @description will revert if the array does not exist
129
- **/
98
+ /// if the root JSON is an array, extract an array given by the position of the target in the source array
99
+ ///
100
+ /// will revert if the array does not exist
130
101
pub (crate ) fn get_array_from_array_unchecked (self , array_index : Field ) -> Self {
131
- assert (
132
- self .layer_type_of_root == ARRAY_LAYER ,
133
- "can only acceess array elements from array" ,
134
- );
135
-
136
- let parent_entry : JSONEntry =
137
- self .json_entries_packed [self .root_index_in_transcript ].into ();
138
- let valid = lt_field_16_bit (array_index , parent_entry .num_children );
139
- assert (valid , "array overflow" );
140
- let entry_index = (parent_entry .child_pointer + array_index );
141
- let entry : JSONEntry = self .json_entries_packed [cast_num_to_u32 (entry_index )].into ();
142
- assert (
143
- entry .entry_type == END_ARRAY_TOKEN as Field ,
144
- "get_array_from_array_unchecked: entry exists but is not an array!" ,
145
- );
146
-
147
- self .update_json_array (entry , cast_num_to_u32 (entry_index ))
102
+ let result = self .get_array_from_array (array_index );
103
+ result .expect (f"array index out of bounds" )
148
104
}
149
105
150
- /**
151
- * @brief if the root is an array, map over the array values, applying `fn f` to each value
152
- **/
106
+ /// if the root is an array, map over the array values, applying `fn f` to each value
153
107
fn map <U , let MaxElements : u32 , let MaxElementBytes : u32 >(
154
108
self ,
155
109
f : fn (JSONValue <MaxElementBytes >) -> U ,
0 commit comments