1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Syntax-only formatter that retains every source token verbatim.
use Error;
use JsonDecodeLimits;
use JsonDecodeSession;
use JsonResource;
use ByteOutput;
use JsonPrettyError;
use JsonPrettyErrorKind;
use JsonPrettyOptions;
use Action;
use Plan;
use Planner;
use crateJsonLexicalScanError;
use crateJsonLexicalScanner;
use crateJsonNumberModel;
/// Validates and pretty-prints JSON while preserving source token spelling.
///
/// The complete input is scanned and the output plan is built before rendering.
/// Syntax, output-limit, and planning-capacity errors therefore produce no
/// output. Scalar token spellings are preserved verbatim, duplicate object
/// keys are accepted, numbers are checked for JSON syntax only, and no JCS
/// canonicalization is applied. Scanning continues through the full input so
/// syntax errors retain their existing precedence even after the output limit
/// has been exceeded.
///
/// # Parameters
///
/// * `input` - Bytes containing exactly one complete JSON value.
/// * `options` - Indentation and trailing-newline choices.
/// * `max_output_bytes` - Optional maximum formatted output length. `None`
/// disables this output limit; it does not limit input size, nesting depth,
/// or exact heap usage.
///
/// # Returns
///
/// The complete formatted byte vector.
///
/// # Errors
///
/// Returns `Syntax` for invalid or incomplete JSON, `OutputLimit` when the
/// planned bytes exceed the configured maximum, or `Capacity` for checked
/// length overflow or a failed explicit fallible reservation. Other
/// allocations, including the scanner's container stack, follow the global
/// allocation-error behavior; `Capacity` does not guarantee recovery from every
/// allocation failure. No result bytes are returned on these errors.
///
/// # Examples
///
/// ```
/// use qubit_json::format::{format_json_tokens, JsonPrettyOptions};
///
/// let options = JsonPrettyOptions::builder().indent_width(2).build()?;
/// let output = format_json_tokens(b"[1]", &options, Some(16))?;
/// assert_eq!(output, b"[\n 1\n]");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// Validates and plans the full document before writing to the supplied output.
///
/// The complete input is scanned before the first write. Syntax, output-limit,
/// and planning-capacity failures therefore leave the output untouched. Once
/// writing starts, an I/O error may leave a prefix accepted by the output; this
/// function does not make I/O atomic and does not flush the output. Scalar
/// token spellings are preserved, duplicate keys are accepted, numbers are
/// checked for syntax only, and JCS canonicalization is not performed. The
/// scanner consumes the complete input even after detecting that the planned
/// output exceeds its limit, preserving syntax-error precedence.
///
/// The output limit applies only to formatted byte length. It is not a bound on
/// input size, nesting depth, or exact heap usage. `None` disables the output
/// limit.
///
/// # Type Parameters
///
/// * `O` - Output type implementing [`ByteOutput`]; dynamically sized output
/// types are supported through the `?Sized` bound.
///
/// # Parameters
///
/// * `output` - Destination that receives formatted bytes; it is not flushed.
/// * `input` - Bytes containing exactly one complete JSON value.
/// * `options` - Indentation and trailing-newline choices.
/// * `max_output_bytes` - Optional maximum formatted output length.
///
/// # Returns
///
/// `Ok(())` after all planned bytes have been accepted by `output`.
///
/// # Errors
///
/// Returns `Syntax` for invalid or incomplete JSON, `OutputLimit` when the
/// planned bytes exceed the configured maximum, or `Capacity` for checked
/// length overflow or a failed explicit fallible reservation. Other
/// allocations, including the scanner's container stack, follow the global
/// allocation-error behavior; `Capacity` does not guarantee recovery from every
/// allocation failure. These errors occur before writing begins. Returns `Io`
/// if a write fails; in that case the destination may already contain an
/// accepted prefix. No flush is attempted.
///
/// # Examples
///
/// ```
/// use qubit_json::format::{write_json_tokens, JsonPrettyOptions};
///
/// let options = JsonPrettyOptions::builder().indent_width(2).build()?;
/// let mut output = Vec::new();
/// write_json_tokens(&mut output, b"[1]", &options, Some(16))?;
/// assert_eq!(output, b"[\n 1\n]");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Sized>
/// Scans a complete document and returns its output plan or a pre-write error.
///
/// Syntax and consumer errors stop planning immediately. If the output length
/// merely exceeds its limit, actions stop being retained but scanning
/// continues; after a successful scan this function reports `OutputLimit`.
/// Checked arithmetic, invalid planner state, or failed reservations can
/// instead report `Capacity`.
///
/// # Parameters
///
/// * `input` - Complete JSON document whose source spans will be retained.
/// * `options` - Formatting choices used to plan indentation and the final
/// newline.
/// * `maximum` - Optional limit applied to planned output bytes.
///
/// # Returns
///
/// The validated action plan and its full output length.
///
/// # Errors
///
/// Returns the mapped syntax or capacity error, or `OutputLimit` after a
/// successful scan when the full output exceeds `maximum`.
/// Executes an already validated plan into a vector with reserved capacity.
///
/// The caller reserves the full planned length first; actions then copy source
/// spans or emit literal bytes and spaces without further fallible allocation.
///
/// # Parameters
///
/// * `input` - Original bytes referenced by span actions.
/// * `actions` - Validated actions describing the formatted output.
/// * `output` - Destination vector with capacity reserved for the full plan.
/// Maps scanner failures to the formatter's public error categories.
///
/// # Parameters
///
/// * `error` - Scanner failure retaining its lexical or consumer context.
///
/// # Returns
///
/// The corresponding formatter error category, preserving available syntax
/// offsets and causes.
/// Creates a `Capacity` error, retaining an allocation error as its source when
/// present.
///
/// # Parameters
///
/// * `source` - Optional allocation or reservation failure explaining the
/// capacity error.
///
/// # Returns
///
/// A formatter error with kind `Capacity` and the optional cause.
pub
/// Wraps an output write failure as an `Io` error.
///
/// # Parameters
///
/// * `error` - The underlying output failure to retain as the error source.
///
/// # Returns
///
/// A formatter error with kind `Io` and the original write failure as its
/// source.