Skip to content

Commit c34dac4

Browse files
committed
inline documentation
1 parent 96285c4 commit c34dac4

File tree

5 files changed

+23
-110
lines changed

5 files changed

+23
-110
lines changed

README.md

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -97,88 +97,6 @@ fn process_schema(text: [u8; NUM_BYTES])
9797
- Functions taking `key: [u8; KeyBytes]` expect a fixed-length key known at compile time.
9898
- Functions with `_var` take `key: BoundedVec<u8, KeyBytes>` for variable-length keys up to `KeyBytes`.
9999

100-
### Object getters API
101-
102-
- **`get_object<let KeyBytes>(key: [u8; KeyBytes])``Option<JSON>`**: From an object root, returns `Some(JSON)` if `key` exists and is an object, `None` if `key` is missing; errors if `key` exists but is not an object, or if called when the current root is an array.
103-
104-
- **`get_object_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSON`**: From an object root, returns the object at `key`; errors otherwise.
105-
106-
- **`get_object_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSON>`**: Same as `get_object`, but the key can be variable-length up to `KeyBytes`.
107-
108-
- **`get_object_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSON`**: Same as `get_object_unchecked`, but with a variable-length key.
109-
110-
- **`get_object_from_array(index: Field)``Option<JSON>`**: When the current root is an array, returns the object at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element is not an object.
111-
112-
- **`get_object_from_array_unchecked(index: Field)``JSON`**: When the current root is an array, returns the object at `index`. Errors if `index` is out of bounds or the element is not an object.
113-
114-
### Array getters API
115-
116-
- **`get_length()``u32`**: Returns its length when the current root is an array; errors otherwise.
117-
118-
- **`get_array<let KeyBytes>(key: [u8; KeyBytes])``Option<JSON>`**: From an object root, returns Some(JSON) if key exists and is an array, None if key is missing; errors if key exists but is not an array, or if called when the current root is an array.
119-
120-
- **`get_array_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSON`**: From an object root, returns the array at key; errors otherwise.
121-
122-
- **`get_array_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSON>`**: Same as `get_array`, but the key can be variable-length up to `KeyBytes`.
123-
124-
- **`get_array_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSON`**: Same as `get_array_unchecked`, but with a variable-length key.
125-
126-
- **`get_array_from_array(index: Field)``Option<JSON>`**: When the current root is an array, returns the nested array at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element is not an array.
127-
128-
- **`get_array_from_array_unchecked(index: Field)``JSON`**: When the current root is an array, returns the nested array at `index`. Errors if `index` is out of bounds or the element is not an array.
129-
130-
### Literal getters API
131-
132-
Literals are the JSON keywords `true`, `false`, and `null`. They are represented by `JSONLiteral`, which supports:
133-
- `is_true()`, `is_false()`, `is_null()`
134-
- `to_bool()` (maps `true``true`, `false`/`null``false`)
135-
136-
- **`get_literal<let KeyBytes>(key: [u8; KeyBytes])``Option<JSONLiteral>`**: From an object root, returns `Some(JSONLiteral)` if `key` exists and is a literal; `None` if `key` is missing; errors if `key` exists but is not a literal, or if called when the current root is an array.
137-
138-
- **`get_literal_unchecked<let KeyBytes>(key: [u8; KeyBytes])``JSONLiteral`**: From an object root, returns the literal at `key`; errors if the key is missing, not a literal, or if called when the current root is an array.
139-
140-
- **`get_literal_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<JSONLiteral>`**: Same as `get_literal`, but accepts a variable-length key up to `KeyBytes`.
141-
142-
- **`get_literal_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``JSONLiteral`**: Same as `get_literal_unchecked`, with a variable-length key.
143-
144-
- **`get_literal_from_array(index: Field)``Option<JSONLiteral>`**: When the current root is an array, returns the literal at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a literal.
145-
146-
- **`get_literal_from_array_unchecked(index: Field)``JSONLiteral`**: When the current root is an array, returns the literal at `index`; errors if `index` is out of bounds or the element is not a literal.
147-
148-
### Number getters API
149-
150-
Numbers are parsed as unsigned integers and must fit into `u64` (no decimals).
151-
152-
- **`get_number<let KeyBytes>(key: [u8; KeyBytes])``Option<u64>`**: From an object root, returns `Some(u64)` if `key` exists and is a number; `None` if `key` is missing; errors if `key` exists but is not a number, or if called when the current root is an array.
153-
154-
- **`get_number_unchecked<let KeyBytes>(key: [u8; KeyBytes])``u64`**: From an object root, returns the number at `key`; errors if the key is missing, not a number, or if called when the current root is an array.
155-
156-
- **`get_number_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``Option<u64>`**: Same as `get_number`, but accepts a variable-length key up to `KeyBytes`.
157-
158-
- **`get_number_unchecked_var<let KeyBytes>(key: BoundedVec<u8, KeyBytes>)``u64`**: Same as `get_number_unchecked`, with a variable-length key.
159-
160-
- **`get_number_from_array(index: Field)``Option<u64>`**: When the current root is an array, returns the number at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a number.
161-
162-
- **`get_number_from_array_unchecked(index: Field)``u64`**: When the current root is an array, returns the number at `index`; errors if `index` is out of bounds or the element is not a number.
163-
164-
### String getters API
165-
166-
Strings are returned as `BoundedVec<u8, StringBytes>` where `StringBytes` is the maximum allowed length of the returned string.
167-
168-
- **`get_string<let KeyBytes, let StringBytes>(key: [u8; KeyBytes])``Option<BoundedVec<u8, StringBytes>>`**: From an object root, returns `Some(string)` if `key` exists and is a string; `None` if `key` is missing; errors if `key` exists but is not a string, or if called when the current root is an array.
169-
170-
- **`get_string_unchecked<let KeyBytes, let StringBytes>(key: [u8; KeyBytes])``BoundedVec<u8, StringBytes>`**: From an object root, returns the string at `key`; errors otherwise.
171-
172-
- **`get_string_var<let KeyBytes, let StringBytes>(key: BoundedVec<u8, KeyBytes>)``Option<BoundedVec<u8, StringBytes>>`**: Same as `get_string`, but accepts a variable-length key up to `KeyBytes`.
173-
174-
- **`get_string_unchecked_var<let KeyBytes, let StringBytes>(key: BoundedVec<u8, KeyBytes>)``BoundedVec<u8, StringBytes>`**: Same as `get_string_unchecked`, with a variable-length key.
175-
176-
- **`get_string_from_array<let StringBytes>(index: Field)``Option<BoundedVec<u8, StringBytes>>`**: When the current root is an array, returns the string at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a string.
177-
178-
- **`get_string_from_array_unchecked<let StringBytes>(index: Field)``BoundedVec<u8, StringBytes>`**: When the current root is an array, returns the string at `index`; errors if `index` is out of bounds or the element is not a string.
179-
180-
- **`get_string_from_path<let KeyBytes, let StringBytes, let PathDepth>(keys: [BoundedVec<u8, KeyBytes>; PathDepth])``Option<BoundedVec<u8, StringBytes>>`**: Traverses nested objects by the given keys; returns Some(string) if the final value exists and is a string, otherwise None.
181-
182100

183101
# Edge Cases
184102

src/get_array.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
152152

153153
/**
154154
* @brief if the root JSON is an array, extract an array given by the position of the target in the source array
155-
* @description will revert if the array does not exist
155+
* @description When the current root is an array, returns the nested array at `index`. Errors if `index` is out of bounds or the element is not an array.
156156
**/
157157
pub fn get_array_from_array_unchecked(self, array_index: Field) -> Self {
158158
assert(

src/get_literal.nr

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ fn extract_literal_from_array(
6666

6767
/**
6868
* @brief getter methods for extracting literal values out of a JSON struct
69+
* @description iterals are the JSON keywords `true`, `false`, and `null`. They are represented by `JSONLiteral`, which supports: `is_true()`, `is_false()`, `is_null()`, `to_bool()` (maps `true` to `true`, `false`/`null` to `false`)
6970
**/
7071
impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let MaxNumValues: u32, let MaxKeyFields: u32> JSON<NumBytes, NumPackedFields, MaxNumTokens, MaxNumValues, MaxKeyFields> {
7172

7273
/**
7374
* @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+
* @description From an object root, returns `Some(JSONLiteral)` if `key` exists and is a literal; `None` if `key` is missing; errors if `key` exists but is not a literal, or if called when the current root is an array.
7576
**/
7677
pub fn get_literal<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<JSONLiteral> {
7778
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
@@ -93,7 +94,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9394

9495
/**
9596
* @brief if the root JSON is an object, extract a literal value given by `key`
96-
* @description will revert if the literal does not exist
97+
* @description From an object root, returns the literal at `key`; errors if the key is missing, not a literal, or if called when the current root is an array.
9798
**/
9899
pub fn get_literal_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> JSONLiteral {
99100
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
@@ -110,7 +111,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
110111
}
111112

112113
/**
113-
* @brief same as `get_literal` for where the key length may be less than KeyBytes
114+
* @brief Same as `get_literal`, but accepts a variable-length key up to `KeyBytes`.
114115
**/
115116
pub fn get_literal_var<let KeyBytes: u32>(
116117
self,
@@ -134,7 +135,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
134135
}
135136

136137
/**
137-
* @brief same as `get_literal_unchecked` for where the key length may be less than KeyBytes
138+
* @brief Same as `get_literal_unchecked`, with a variable-length key.
138139
**/
139140
pub fn get_literal_unchecked_var<let KeyBytes: u32>(
140141
self,
@@ -155,7 +156,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
155156

156157
/**
157158
* @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
158-
* @description returns an Option<JSONLiteral> which will be null if the literal does not exist
159+
* @description When the current root is an array, returns the literal at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a literal.
159160
**/
160161
fn get_literal_from_array(self, array_index: Field) -> Option<JSONLiteral> {
161162
assert(
@@ -191,7 +192,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
191192

192193
/**
193194
* @brief if the root JSON is an array, extract a literal given by the position of the target in the source array
194-
* @description will revert if the literal does not exist
195+
* @description When the current root is an array, returns the literal at `index`; errors if `index` is out of bounds or the element is not a literal.
195196
**/
196197
pub fn get_literal_from_array_unchecked(self, array_index: Field) -> JSONLiteral {
197198
assert(

src/get_number.nr

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
5252

5353
/**
5454
* @brief if the root JSON is an object, extract a numeric value given by `key`
55-
* @description returns an Option<u64> which will be null if the key does not exist
55+
* @description From an object root, returns `Some(u64)` if `key` exists and is a number; `None` if `key` is missing; errors if `key` exists but is not a number, or if called when the current root is an array.
5656
**/
5757
pub fn get_number<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> Option<u64> {
5858
let (exists, entry) = self.get_json_entry(key);
@@ -71,7 +71,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
7171

7272
/**
7373
* @brief if the root JSON is an object, extract a u64 value given by `key`
74-
* @description will revert if the number does not exist
74+
* @description From an object root, returns the number at `key`; errors if the key is missing, not a number, or if called when the current root is an array.
7575
**/
7676
pub fn get_number_unchecked<let KeyBytes: u32>(self, key: [u8; KeyBytes]) -> u64 {
7777
let entry = self.get_json_entry_unchecked(key);
@@ -85,7 +85,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
8585
}
8686

8787
/**
88-
* @brief same as `get_number` for where the key length may be less than KeyBytes
88+
* @brief Same as `get_number`, but accepts a variable-length key up to `KeyBytes`.
8989
**/
9090
pub fn get_number_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> Option<u64> {
9191
let (exists, entry) = self.get_json_entry_var(key);
@@ -103,7 +103,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
103103
}
104104

105105
/**
106-
* @brief same as `get_number_unchecked` for where the key length may be less than KeyBytes
106+
* @brief Same as `get_number_unchecked`, with a variable-length key.
107107
**/
108108
pub fn get_number_unchecked_var<let KeyBytes: u32>(self, key: BoundedVec<u8, KeyBytes>) -> u64 {
109109
let entry = self.get_json_entry_unchecked_var(key);
@@ -118,13 +118,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
118118

119119
/**
120120
* @brief if the root JSON is an array, extract a numeric value given by the position of the target in the source array
121-
* @description returns an Option<u64> which will be null if the number does not exist
121+
* @description When the current root is an array, returns the number at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a number.
122122
**/
123123
pub fn get_number_from_array(self, array_index: Field) -> Option<u64> {
124-
assert(
125-
self.layer_type_of_root == ARRAY_LAYER,
126-
"can only acceess array elements from array",
127-
);
124+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only access array elements from array");
128125

129126
let parent_entry: JSONEntry =
130127
self.json_entries_packed[self.root_index_in_transcript].into();
@@ -153,13 +150,10 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
153150

154151
/**
155152
* @brief if the root JSON is an array, extract a numeric value given by the position of the target in the source array
156-
* @description will revert if the number does not exist
153+
* @description When the current root is an array, returns the number at `index`; errors if `index` is out of bounds or the element is not a number.
157154
**/
158155
pub fn get_number_from_array_unchecked(self, array_index: Field) -> u64 {
159-
assert(
160-
self.layer_type_of_root == ARRAY_LAYER,
161-
"can only acceess array elements from array",
162-
);
156+
assert(self.layer_type_of_root == ARRAY_LAYER, "can only access array elements from array");
163157

164158
let parent_entry: JSONEntry =
165159
self.json_entries_packed[self.root_index_in_transcript].into();

src/get_string.nr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9898

9999
/**
100100
* @brief if the root JSON is an object, extract a string given by `key`
101-
* @description returns an Option<BoundedVec> which will be null if the key does not exist
101+
* @description From an object root, returns `Some(string)` if `key` exists and is a string; `None` if `key` is missing; errors if `key` exists but is not a string, or if called when the current root is an array.
102102
* @note the `StringBytes` parameter defines the maximum allowable length of the returned string
103103
**/
104104
pub fn get_string<let KeyBytes: u32, let StringBytes: u32>(
@@ -125,7 +125,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
125125

126126
/**
127127
* @brief if the root JSON is an object, extract a string given by `key`
128-
* @description will revert if the string does not exist
128+
* @description From an object root, returns the string at `key`; errors otherwise.
129129
* @note the `StringBytes` parameter defines the maximum allowable length of the returned string
130130
**/
131131
pub fn get_string_unchecked<let KeyBytes: u32, let StringBytes: u32>(
@@ -145,7 +145,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
145145
}
146146

147147
/**
148-
* @brief same as `get_string` for where the key length may be less than KeyBytes
148+
* @brief Same as `get_string`, but accepts a variable-length key up to `KeyBytes`.
149149
**/
150150
pub fn get_string_var<let KeyBytes: u32, let StringBytes: u32>(
151151
self,
@@ -169,7 +169,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
169169
}
170170

171171
/**
172-
* @brief same as `get_string_unchecked` for where the key length may be less than KeyBytes
172+
* @brief Same as `get_string_unchecked`, with a variable-length key.
173173
**/
174174
pub fn get_string_unchecked_var<let KeyBytes: u32, let StringBytes: u32>(
175175
self,
@@ -189,7 +189,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
189189

190190
/**
191191
* @brief if the root JSON is an array, extract a string given by the position of the target in the source array
192-
* @description returns an Option<BoundedVec> which will be null if the string does not exist
192+
* @description When the current root is an array, returns the string at `index` wrapped in `Option`. Returns `None` if `index` is out of bounds; errors if in-bounds but the element at `index` is not a string.
193193
**/
194194
pub fn get_string_from_array<let StringBytes: u32>(
195195
self,
@@ -229,7 +229,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
229229

230230
/**
231231
* @brief if the root JSON is an array, extract a string given by the position of the target in the source array
232-
* @description will revert if the string does not exist
232+
* @description When the current root is an array, returns the string at `index`; errors if `index` is out of bounds or the element is not a string.
233233
**/
234234
pub fn get_string_from_array_unchecked<let StringBytes: u32>(
235235
self,
@@ -262,7 +262,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
262262
}
263263

264264
/**
265-
* @brief if the root JSON is an object, get a nested string out of the JSON, which may be several keys deep
265+
* @brief Traverses nested objects by the given keys; returns Some(string) if the final value exists and is a string, otherwise None.
266266
**/
267267
pub fn get_string_from_path<let KeyBytes: u32, let StringBytes: u32, let PathDepth: u32>(
268268
self,

0 commit comments

Comments
 (0)