1
- use crate::_comparison_tools::lt::lt_field_16_bit ;
2
- use crate::enums::Layer::ARRAY_LAYER ;
3
1
use crate::enums::Token::LITERAL_TOKEN ;
4
2
use crate::json::JSON ;
5
- use crate::json_entry::JSONEntry ;
6
3
use crate::utils::cast_num_to_u32 ;
7
4
8
5
global MAX_LITERAL_LENGTH_AS_STRING : u32 = 5 ;
9
6
global LITERAL_OFFSET_SHIFT : [Field ; 6 ] =
10
7
[0x10000000000 , 0x100000000 , 0x1000000 , 0x10000 , 0x100 , 1 ];
11
8
12
- /**
13
- * @brief a JSON "literal" type has 3 states: "true", "false", "null".
14
- * As such we can't directly convert to a bool and cover all possible cases
15
- **/
9
+ /// a JSON "literal" type has 3 states: "true", "false", "null".
10
+ /// As such we can't directly convert to a bool and cover all possible cases
16
11
pub struct JSONLiteral {
17
12
pub(crate) value : Field ,
18
13
}
@@ -39,9 +34,7 @@ impl JSONLiteral {
39
34
}
40
35
}
41
36
42
- /**
43
- * @brief given an input array, return a JSONLiteral depending on whether the array says "true", "false" or "null"
44
- **/
37
+ /// given an input array, return a JSONLiteral depending on whether the array says "true", "false" or "null"
45
38
fn extract_literal_from_array (
46
39
arr : [u8 ; MAX_LITERAL_LENGTH_AS_STRING ],
47
40
json_length : Field ,
@@ -64,37 +57,29 @@ fn extract_literal_from_array(
64
57
JSONLiteral ::new (is_true , is_false , is_null )
65
58
}
66
59
67
- /**
68
- * @brief getter methods for extracting literal values out of a JSON struct
69
- **/
60
+ /// getter methods for extracting literal values out of a JSON struct
70
61
impl <let NumBytes : u32 , let NumPackedFields : u32 , let MaxNumTokens : u32 , let MaxNumValues : u32 , let MaxKeyFields : u32 > JSON <NumBytes , NumPackedFields , MaxNumTokens , MaxNumValues , MaxKeyFields > {
71
62
72
- /**
73
- * @brief if the root JSON is an object, extract a literal value given by `key`
74
- * @description returns an Option<JSONLiteral> which will be null if the literal does not exist
75
- **/
63
+ /// if the root JSON is an object, extract a literal value given by `key`
64
+ ///
65
+ /// returns an Option<JSONLiteral> which will be null if the literal does not exist
76
66
pub (crate ) fn get_literal <let KeyBytes : u32 >(self , key : [u8 ; KeyBytes ]) -> Option <JSONLiteral > {
77
67
self .get_literal_var (BoundedVec ::from_parts_unchecked (key , KeyBytes ))
78
68
}
79
69
80
- /**
81
- * @brief if the root JSON is an object, extract a literal value given by `key`
82
- * @description will revert if the literal does not exist
83
- **/
70
+ /// if the root JSON is an object, extract a literal value given by `key`
71
+ ///
72
+ /// will revert if the literal does not exist
84
73
fn get_literal_unchecked <let KeyBytes : u32 >(self , key : [u8 ; KeyBytes ]) -> JSONLiteral {
85
74
self .get_literal_unchecked_var (BoundedVec ::from_parts_unchecked (key , KeyBytes ))
86
75
}
87
76
88
- /**
89
- * @brief same as `get_literal` for where the key length may be less than KeyBytes
90
- **/
77
+ /// same as `get_literal` for where the key length may be less than KeyBytes
91
78
pub (crate ) fn get_literal_var <let KeyBytes : u32 >(
92
79
self ,
93
80
key : BoundedVec <u8 , KeyBytes >,
94
81
) -> Option <JSONLiteral > {
95
- assert (self .layer_type_of_root != ARRAY_LAYER , "cannot extract array elements via a key" );
96
-
97
- let (exists , entry ) = self .get_json_entry_var (key );
82
+ let (exists , entry , _ ) = self .get_json_entry_var (key );
98
83
assert (
99
84
(entry .entry_type - LITERAL_TOKEN as Field ) * exists as Field == 0 ,
100
85
"get_literal_var: entry exists but is not a literal!" ,
@@ -109,15 +94,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
109
94
}
110
95
}
111
96
112
- /**
113
- * @brief same as `get_literal_unchecked` for where the key length may be less than KeyBytes
114
- **/
97
+ /// same as `get_literal_unchecked` for where the key length may be less than KeyBytes
115
98
pub (crate ) fn get_literal_unchecked_var <let KeyBytes : u32 >(
116
99
self ,
117
100
key : BoundedVec <u8 , KeyBytes >,
118
101
) -> JSONLiteral {
119
- assert (self .layer_type_of_root != ARRAY_LAYER , "cannot extract array elements via a key" );
120
-
121
102
let entry = self .get_json_entry_unchecked_var (key );
122
103
assert (
123
104
entry .entry_type == LITERAL_TOKEN as Field ,
@@ -129,23 +110,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
129
110
extract_literal_from_array (parsed_string , entry .json_length )
130
111
}
131
112
132
- /**
133
- * @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
134
- * @description returns an Option<JSONLiteral> which will be null if the literal does not exist
135
- **/
136
- fn get_literal_from_array (self , array_index : Field ) -> Option <JSONLiteral > {
137
- assert (
138
- self .layer_type_of_root == ARRAY_LAYER ,
139
- "can only acceess array elements from array" ,
140
- );
141
-
142
- let parent_entry : JSONEntry =
143
- self .json_entries_packed [self .root_index_in_transcript ].into ();
144
-
145
- let valid = lt_field_16_bit (array_index , parent_entry .num_children );
146
- let entry_index = (parent_entry .child_pointer + array_index ) * valid as Field ;
147
-
148
- let entry : JSONEntry = self .json_entries_packed [cast_num_to_u32 (entry_index )].into ();
113
+ /// if the root JSON is an array, extract a literal given by the position of the target in the source array
114
+ ///
115
+ /// returns an Option<JSONLiteral> which will be null if the literal does not exist
116
+ pub fn get_literal_from_array (self , array_index : Field ) -> Option <JSONLiteral > {
117
+ let (entry , valid , _ ) = self .get_entry_from_array (array_index );
149
118
if valid {
150
119
assert_eq (
151
120
entry .entry_type ,
@@ -165,32 +134,11 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
165
134
}
166
135
}
167
136
168
- /**
169
- * @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
170
- * @description will revert if the literal does not exist
171
- **/
137
+ /// if the root JSON is an array, extract a literal given by the position of the target in the source array
138
+ ///
139
+ /// will revert if the literal does not exist
172
140
fn get_literal_from_array_unchecked (self , array_index : Field ) -> JSONLiteral {
173
- assert (
174
- self .layer_type_of_root == ARRAY_LAYER ,
175
- "can only acceess array elements from array" ,
176
- );
177
-
178
- let parent_entry : JSONEntry =
179
- self .json_entries_packed [self .root_index_in_transcript ].into ();
180
-
181
- let valid = lt_field_16_bit (array_index , parent_entry .num_children );
182
- assert (valid , "array overflow" );
183
- let entry_index = (parent_entry .child_pointer + array_index );
184
-
185
- let entry : JSONEntry = self .json_entries_packed [cast_num_to_u32 (entry_index )].into ();
186
-
187
- assert (
188
- entry .entry_type == LITERAL_TOKEN as Field ,
189
- "get_literal_from_array_unchecked: entry exists but is not a literal!" ,
190
- );
191
-
192
- let mut parsed_string : [u8 ; MAX_LITERAL_LENGTH_AS_STRING ] =
193
- self .extract_string_entry (entry );
194
- extract_literal_from_array (parsed_string , entry .json_length )
141
+ let result = self .get_literal_from_array (array_index );
142
+ result .expect (f"array index out of bounds" )
195
143
}
196
144
}
0 commit comments