1
- use crate::getters ;
2
- use dep::noir_sort ;
3
1
use crate::json_entry::JSONEntry ;
4
- use crate::redux::JSON ;
5
- use crate::keymap ;
6
- use crate::lt:: {lt_field_8_bit , lt_field_16_bit , assert_lt_240_bit , assert_gt_240_bit };
7
- use crate::redux_tables:: {
8
- OBJECT_LAYER , ARRAY_LAYER , NUMERIC_TOKEN , LITERAL_TOKEN , STRING_TOKEN , BEGIN_OBJECT_TOKEN ,
9
- BEGIN_ARRAY_TOKEN , ASCII_TO_NUMBER , ESCAPE_SEQUENCE_END_CHARS , ESCAPE_SEQUENCE_START_CHARS ,
10
- ESCAPE_SEQUENCE_REPLACEMENT
11
- };
12
- use crate::keyhash::Hasher ;
2
+ use crate::json::JSON ;
3
+ use crate::lt::lt_field_16_bit ;
4
+ use crate::json_tables:: {OBJECT_LAYER , ARRAY_LAYER , BEGIN_ARRAY_TOKEN };
13
5
use crate::keyhash::get_keyhash ;
14
- use crate::slice_field::slice_fields ;
15
6
use crate::getters::JSONValue ;
16
7
8
+ /**
9
+ * @brief getter methods for extracting array types out of a JSON struct
10
+ **/
17
11
impl <let NumBytes : u32 , let NumPackedFields : u16 , let MaxNumTokens : u16 , let MaxNumValues : u16 > JSON <NumBytes ,NumPackedFields , MaxNumTokens , MaxNumValues > {
18
12
13
+ /**
14
+ * @brief if the root JSON is an array, return its length
15
+ **/
19
16
fn get_length (self ) -> u32 {
20
17
assert (self .layer_context == ARRAY_LAYER , "can only get length of an array type" );
21
- let parent_entry = JSONEntry :: from_field ( self .packed_json_entries [self .layer_index_in_transcript ]);
18
+ let parent_entry : JSONEntry = self .packed_json_entries [self .layer_index_in_transcript ]. into ( );
22
19
parent_entry .num_children as u32
23
20
}
24
21
22
+ /**
23
+ * @brief if the root JSON is an object, extract an array given by `key`
24
+ * @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
25
+ **/
25
26
fn get_array <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ]) -> Option <Self > {
26
27
assert (self .layer_context != ARRAY_LAYER , "cannot extract array elements via a key" );
27
28
let (exists , key_index ) = self .key_exists_impl (key , KeyBytes );
28
- let entry : JSONEntry = JSONEntry ::from_field (self .packed_json_entries [key_index ]);
29
-
29
+ let entry : JSONEntry = self .packed_json_entries [key_index ].into ();
30
30
assert (
31
31
(entry .entry_type - BEGIN_ARRAY_TOKEN ) * exists as Field == 0 , "key does not describe an object"
32
32
);
@@ -40,12 +40,14 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
40
40
Option { _is_some : exists , _value : r }
41
41
}
42
42
43
+ /**
44
+ * @brief if the root JSON is an object, extract an array given by `key`
45
+ * @description will revert if the array does not exist
46
+ **/
43
47
fn get_array_unchecked <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ]) -> Self {
44
48
assert (self .layer_context != ARRAY_LAYER , "cannot extract array elements via a key" );
45
49
46
- let key_index = self .key_exists_impl_unchecked (key , KeyBytes );
47
- let entry : JSONEntry = JSONEntry ::from_field (self .packed_json_entries [key_index ]);
48
-
50
+ let (entry , key_index ) = self .get_json_entry_unchecked_with_key_index_var (key , KeyBytes );
49
51
assert (entry .entry_type == BEGIN_ARRAY_TOKEN , "key does not describe an object" );
50
52
51
53
let mut r = self ;
@@ -57,51 +59,57 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
57
59
r
58
60
}
59
61
60
- fn get_array_unchecked_var <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ], key_length : u16 ) -> Self {
62
+ /**
63
+ * @brief same as `get_array` for where the key length may be less than KeyBytes
64
+ **/
65
+ fn get_array_var <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ], key_length : u16 ) -> Option <Self > {
61
66
assert (self .layer_context != ARRAY_LAYER , "cannot extract array elements via a key" );
62
-
63
- let key_index = self .key_exists_impl_unchecked (key , key_length );
64
- let entry : JSONEntry = JSONEntry ::from_field (self .packed_json_entries [key_index ]);
65
-
66
- assert (entry .entry_type == BEGIN_ARRAY_TOKEN , "key does not describe an object" );
67
+ let (exists , key_index ) = self .key_exists_impl (key , key_length );
68
+ let entry : JSONEntry = self .packed_json_entries [key_index ].into ();
69
+ // TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
70
+ assert (
71
+ (entry .entry_type - BEGIN_ARRAY_TOKEN ) * exists as Field == 0 , "key does not describe an object"
72
+ );
67
73
68
74
let mut r = self ;
69
75
r .layer_id = entry .parent_index ;
70
76
r .root_id = entry .id ;
71
77
r .layer_context = ARRAY_LAYER ;
72
78
r .layer_index_in_transcript = key_index ;
73
79
74
- r
80
+ Option { _is_some : exists , _value : r }
75
81
}
76
- fn get_array_var <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ], key_length : u16 ) -> Option <Self > {
82
+
83
+ /**
84
+ * @brief same as `get_array_unchecked` for where the key length may be less than KeyBytes
85
+ **/
86
+ fn get_array_unchecked_var <let KeyBytes : u16 >(self , key : [u8 ; KeyBytes ], key_length : u16 ) -> Self {
77
87
assert (self .layer_context != ARRAY_LAYER , "cannot extract array elements via a key" );
78
- let (exists , key_index ) = self .key_exists_impl (key , key_length );
79
- let entry : JSONEntry = JSONEntry ::from_field (self .packed_json_entries [key_index ]);
80
88
81
- // TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
82
- assert (
83
- (entry .entry_type - BEGIN_ARRAY_TOKEN ) * exists as Field == 0 , "key does not describe an object"
84
- );
89
+ let (entry , key_index ) = self .get_json_entry_unchecked_with_key_index_var (key , key_length );
90
+ assert (entry .entry_type == BEGIN_ARRAY_TOKEN , "key does not describe an object" );
85
91
86
92
let mut r = self ;
87
93
r .layer_id = entry .parent_index ;
88
94
r .root_id = entry .id ;
89
95
r .layer_context = ARRAY_LAYER ;
90
96
r .layer_index_in_transcript = key_index ;
91
97
92
- Option { _is_some : exists , _value : r }
98
+ r
93
99
}
94
100
101
+ /**
102
+ * @brief if the root JSON is an array, extract an array given by the position of the target in the source array
103
+ * @description returns an Option<JSON> where, if the array exists, the JSON object will have the requested array as its root value
104
+ **/
95
105
fn get_array_from_array (self , array_index : Field ) -> Option <Self > {
96
106
assert (self .layer_context == ARRAY_LAYER , "can only acceess array elements from array" );
97
107
98
- let parent_entry = JSONEntry ::from_field (self .packed_json_entries [self .layer_index_in_transcript ]);
99
-
108
+ let parent_entry : JSONEntry = self .packed_json_entries [self .layer_index_in_transcript ].into ();
100
109
let valid = lt_field_16_bit (array_index , parent_entry .num_children );
101
110
let entry_index = (parent_entry .child_pointer + array_index ) * valid as Field ;
102
111
103
- let entry = JSONEntry ::from_field (self .packed_json_entries [entry_index ]);
104
-
112
+ let entry : JSONEntry = self .packed_json_entries [entry_index ].into ();
105
113
assert (
106
114
(entry .entry_type - BEGIN_ARRAY_TOKEN ) * valid as Field == 0 , "get_object_from_array: entry exists but is not an object!"
107
115
);
@@ -115,17 +123,19 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
115
123
Option { _is_some : valid , _value : r }
116
124
}
117
125
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
+ **/
118
130
fn get_array_from_array_unchecked (self , array_index : Field ) -> Self {
119
131
assert (self .layer_context == ARRAY_LAYER , "can only acceess array elements from array" );
120
132
121
- let parent_entry = JSONEntry ::from_field (self .packed_json_entries [self .layer_index_in_transcript ]);
122
-
133
+ let parent_entry : JSONEntry = self .packed_json_entries [self .layer_index_in_transcript ].into ();
123
134
let valid = lt_field_16_bit (array_index , parent_entry .num_children );
124
135
assert (valid , "array overflow" );
125
136
let entry_index = (parent_entry .child_pointer + array_index );
126
137
127
- let entry = JSONEntry ::from_field (self .packed_json_entries [entry_index ]);
128
-
138
+ let entry : JSONEntry = self .packed_json_entries [entry_index ].into ();
129
139
assert (
130
140
entry .entry_type == BEGIN_ARRAY_TOKEN , "get_array_from_array_unchecked: entry exists but is not an array!"
131
141
);
@@ -138,22 +148,20 @@ impl<let NumBytes: u32, let NumPackedFields: u16, let MaxNumTokens: u16, let Max
138
148
r
139
149
}
140
150
141
- fn map < U , let MaxElements : u32 , let MaxElementBytes : u32 >(
142
- self ,
143
- f : fn ( JSONValue < MaxElementBytes >) -> U
144
- ) -> [U ; MaxElements ] where U : std::default::Default {
151
+ /**
152
+ * @brief if the root is an array, map over the array values, applying `fn f` to each value
153
+ **/
154
+ fn map < U , let MaxElements : u32 , let MaxElementBytes : u32 >( self , f : fn ( JSONValue < MaxElementBytes >) -> U ) -> [U ; MaxElements ] where U : std::default::Default {
145
155
assert (self .layer_context == ARRAY_LAYER , "can only call map on an array" );
146
156
147
- let entry = JSONEntry ::from_field (self .packed_json_entries [self .layer_index_in_transcript ]);
148
-
157
+ let entry : JSONEntry = self .packed_json_entries [self .layer_index_in_transcript ].into ();
149
158
let num_children = entry .num_children ;
150
159
let mut r : [U ; MaxElements ] = [U ::default (); MaxElements ];
151
160
152
161
for i in 0 ..MaxElements {
153
162
let valid = lt_field_16_bit (i as Field , num_children );
154
163
let entry_index = (entry .child_pointer + i as Field ) * valid as Field ;
155
- let child_entry = JSONEntry ::from_field (self .packed_json_entries [entry_index ]);
156
-
164
+ let child_entry : JSONEntry = self .packed_json_entries [entry_index ].into ();
157
165
let mut parsed_string : [u8 ; MaxElementBytes ] = [0 ; MaxElementBytes ];
158
166
for j in 0 ..MaxElementBytes {
159
167
let byte_valid = lt_field_16_bit (j as Field , child_entry .json_length );
@@ -209,9 +217,9 @@ fn test_array() {
209
217
210
218
let E = first .get_object_from_array_unchecked (4 );
211
219
212
- let entry_maybe = JSONEntry :: from_field ( E .packed_json_entries [E .layer_index_in_transcript ]);
220
+ let entry_maybe : JSONEntry = E .packed_json_entries [E .layer_index_in_transcript ]. into ( );
213
221
println (f"entry = { entry_maybe} " );
214
- let child = JSONEntry :: from_field ( E .packed_json_entries [entry_maybe .child_pointer ]);
222
+ let child : JSONEntry = E .packed_json_entries [entry_maybe .child_pointer ]. into ( );
215
223
println (f"target? = { child} " );
216
224
println (f"{ E} " );
217
225
let E_A = E .get_array_unchecked ("bar" .as_bytes ());
0 commit comments