jstrict 0.14.0

Strict RFC 8259 / ECMA-404 JSON parser with source code mapping
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! JSON printing.
//!
//! [`Print`] is the entry point. `pretty_print`, `inline_print` and
//! `compact_print` cover the common cases; `print_with` takes an [`Options`]
//! for everything else — indentation, padding, and the width or item count at
//! which an array or object is expanded onto several lines.
//!
//! Printing is a two-pass process: sizes are precomputed
//! ([`PrecomputeSize`]), then written out ([`PrintWithSize`]), so the layout
//! decision for a nested value is known before it is emitted.
//!
//! Strings are escaped per
//! [RFC 8785](https://www.rfc-editor.org/rfc/rfc8785#name-serialization-of-strings).
use std::fmt;

#[cfg(feature = "contextual")]
mod contextual;

#[cfg(feature = "contextual")]
pub use self::contextual::*;

/// Indentation unit.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Indent {
	/// The given number of spaces.
	Spaces(u8),

	/// The given number of tabs.
	Tabs(u8),
}

impl Indent {
	/// Repeats this indentation unit `n` times.
	pub const fn by(self, n: usize) -> IndentBy {
		IndentBy(self, n)
	}
}

impl fmt::Display for Indent {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Spaces(n) => {
				for _ in 0..*n {
					f.write_str(" ")?
				}
			}
			Self::Tabs(n) => {
				for _ in 0..*n {
					f.write_str("\t")?
				}
			}
		}

		Ok(())
	}
}

/// An [`Indent`] repeated a given number of times, ready to be displayed.
pub struct IndentBy(Indent, usize);

impl fmt::Display for IndentBy {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		for _ in 0..self.1 {
			self.0.fmt(f)?
		}

		Ok(())
	}
}

/// The given number of spaces, ready to be displayed.
pub struct Spaces(pub usize);

impl fmt::Display for Spaces {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		for _ in 0..self.0 {
			f.write_str(" ")?
		}

		Ok(())
	}
}

/// Padding inserted between syntactic elements.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Padding {
	/// The given number of spaces.
	Spaces(u8),

	/// A line break.
	NewLine,
}

impl fmt::Display for Padding {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Spaces(n) => {
				for _ in 0..*n {
					f.write_str(" ")?
				}
			}
			Self::NewLine => f.write_str("\n")?,
		}

		Ok(())
	}
}

/// Threshold past which an array or object is expanded onto several lines.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Limit {
	/// Always expanded, even if empty.
	Always,

	/// Expanded if the array/object has more than the given number of items.
	Item(usize),

	/// Expanded if the representation of the array/object is more than the
	/// given number of characters long.
	Width(usize),

	/// Expanded if the array/object has more than the given number of items
	/// (first argument), or if its the representation is more than the
	/// given number of characters long (second argument).
	ItemOrWidth(usize, usize),
}

/// Print options.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub struct Options {
	/// Indentation string.
	pub indent: Indent,

	/// String added after `[`.
	pub array_begin: usize,

	/// String added before `]`.
	pub array_end: usize,

	/// Number of spaces inside an inlined empty array.
	pub array_empty: usize,

	/// Number of spaces before a comma in an array.
	pub array_before_comma: usize,

	/// Number of spaces after a comma in an array.
	pub array_after_comma: usize,

	/// Limit after which an array is expanded.
	pub array_limit: Option<Limit>,

	/// String added after `{`.
	pub object_begin: usize,

	/// String added before `}`.
	pub object_end: usize,

	/// Number of spaces inside an inlined empty object.
	pub object_empty: usize,

	/// Number of spaces before a comma in an object.
	pub object_before_comma: usize,

	/// Number of spaces after a comma in an object.
	pub object_after_comma: usize,

	/// Number of spaces before a colon in an object.
	pub object_before_colon: usize,

	/// Number of spaces after a colon in an object.
	pub object_after_colon: usize,

	/// Limit after which an array is expanded.
	pub object_limit: Option<Limit>,
}

impl Options {
	/// Pretty print options.
	#[inline(always)]
	pub const fn pretty() -> Self {
		Self {
			indent: Indent::Spaces(2),
			array_begin: 1,
			array_end: 1,
			array_empty: 0,
			array_before_comma: 0,
			array_after_comma: 1,
			array_limit: Some(Limit::ItemOrWidth(1, 16)),
			object_begin: 1,
			object_end: 1,
			object_empty: 0,
			object_before_comma: 0,
			object_after_comma: 1,
			object_before_colon: 0,
			object_after_colon: 1,
			object_limit: Some(Limit::ItemOrWidth(1, 16)),
		}
	}

	/// Compact print options.
	///
	/// Values will be formatted on a single line without spaces.
	#[inline(always)]
	pub const fn compact() -> Self {
		Self {
			indent: Indent::Spaces(0),
			array_begin: 0,
			array_end: 0,
			array_empty: 0,
			array_before_comma: 0,
			array_after_comma: 0,
			array_limit: None,
			object_begin: 0,
			object_end: 0,
			object_empty: 0,
			object_before_comma: 0,
			object_after_comma: 0,
			object_before_colon: 0,
			object_after_colon: 0,
			object_limit: None,
		}
	}

	/// Inline print options.
	///
	/// Values will be formatted on a single line with some spaces.
	#[inline(always)]
	pub const fn inline() -> Self {
		Self {
			indent: Indent::Spaces(0),
			array_begin: 1,
			array_end: 1,
			array_empty: 0,
			array_before_comma: 0,
			array_after_comma: 1,
			array_limit: None,
			object_begin: 1,
			object_end: 1,
			object_empty: 0,
			object_before_comma: 0,
			object_after_comma: 1,
			object_before_colon: 0,
			object_after_colon: 1,
			object_limit: None,
		}
	}
}

/// The size of a value.
#[derive(Clone, Copy)]
pub enum Size {
	/// The value (array or object) is expanded on multiple lines.
	Expanded,

	/// The value is formatted in a single line with the given character width.
	Width(usize),
}

impl Size {
	/// Concatenates `other` to `self`.
	///
	/// Widths add up; anything combined with [`Size::Expanded`] stays
	/// expanded.
	pub const fn add(&mut self, other: Self) {
		*self = match (*self, other) {
			(Self::Width(a), Self::Width(b)) => Self::Width(a + b),
			_ => Self::Expanded,
		}
	}
}

/// Print methods.
pub trait Print {
	/// Print the value with `Options::pretty` options.
	#[inline(always)]
	fn pretty_print(&self) -> Printed<'_, Self> {
		self.print_with(Options::pretty())
	}

	/// Print the value with `Options::compact` options.
	#[inline(always)]
	fn compact_print(&self) -> Printed<'_, Self> {
		self.print_with(Options::compact())
	}

	/// Print the value with `Options::inline` options.
	#[inline(always)]
	fn inline_print(&self) -> Printed<'_, Self> {
		self.print_with(Options::inline())
	}

	/// Print the value with the given options.
	#[inline(always)]
	fn print_with(&self, options: Options) -> Printed<'_, Self> {
		Printed(self, options, 0)
	}

	/// Writes the value to `f` with the given options, starting at the given
	/// indentation level.
	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result;
}

impl<T: Print + ?Sized> Print for &T {
	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
		(**self).fmt_with(f, options, indent)
	}
}

/// Printing with precomputed sizes.
///
/// Implemented by the value types that can be expanded over several lines.
/// `sizes` holds the layout decisions produced by [`PrecomputeSize`], in
/// depth-first order, and `index` walks through them as printing proceeds.
pub trait PrintWithSize {
	/// Writes the value to `f`, taking its layout from `sizes[*index]`.
	fn fmt_with_size(
		&self,
		f: &mut fmt::Formatter,
		options: &Options,
		indent: usize,
		sizes: &[Size],
		index: &mut usize,
	) -> fmt::Result;
}

impl<T: PrintWithSize + ?Sized> PrintWithSize for &T {
	fn fmt_with_size(
		&self,
		f: &mut fmt::Formatter,
		options: &Options,
		indent: usize,
		sizes: &[Size],
		index: &mut usize,
	) -> fmt::Result {
		(**self).fmt_with_size(f, options, indent, sizes, index)
	}
}

/// Printed value.
pub struct Printed<'t, T: ?Sized>(&'t T, Options, usize);

impl<'t, T: Print> fmt::Display for Printed<'t, T> {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt_with(f, &self.1, self.2)
	}
}

impl Print for bool {
	#[inline(always)]
	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
		if *self {
			f.write_str("true")
		} else {
			f.write_str("false")
		}
	}
}

impl Print for crate::NumberBuf {
	#[inline(always)]
	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
		fmt::Display::fmt(self, f)
	}
}

/// Lookup table mapping each ASCII control byte / `"` / `\\` to the
/// emitted byte length when escaped per RFC 8785. Non-escape bytes map
/// to 0 so the scanner can detect "needs escaping" with a single
/// table lookup. Bytes >= 0x20 (excluding `"` and `\\`) are 0.
static ESCAPE_LEN: [u8; 256] = {
	let mut t = [0u8; 256];
	let mut i = 0;
	while i < 0x20 {
		t[i] = 6;
		i += 1;
	}
	t[0x08] = 2; // \b
	t[0x09] = 2; // \t
	t[0x0a] = 2; // \n
	t[0x0c] = 2; // \f
	t[0x0d] = 2; // \r
	t[b'"' as usize] = 2;
	t[b'\\' as usize] = 2;
	t
};

/// Writes the escape sequence for a single byte that requires escaping.
fn write_escape(b: u8, f: &mut fmt::Formatter) -> fmt::Result {
	match b {
		b'\\' => f.write_str("\\\\"),
		b'"' => f.write_str("\\\""),
		0x08 => f.write_str("\\b"),
		0x09 => f.write_str("\\t"),
		0x0a => f.write_str("\\n"),
		0x0c => f.write_str("\\f"),
		0x0d => f.write_str("\\r"),
		c => {
			f.write_str("\\u00")?;
			let hi = (c >> 4) & 0x0f;
			let lo = c & 0x0f;
			f.write_str(&HEX[hi as usize..hi as usize + 1])?;
			f.write_str(&HEX[lo as usize..lo as usize + 1])
		}
	}
}

const HEX: &str = "0123456789abcdef";

/// Threshold (in bytes) below which `string_literal` walks the SWAR path
/// directly. Above it, the SIMD escape encoder is used. Tuned so the SIMD
/// startup cost (thread-local borrow + reserve) does not dominate.
const SIMD_ESCAPE_MIN: usize = 64;

thread_local! {
	static ESCAPE_BUFFER: std::cell::RefCell<Vec<u8>> =
		std::cell::RefCell::new(Vec::with_capacity(256));
}

/// Formats a string literal according to [RFC8785](https://www.rfc-editor.org/rfc/rfc8785#name-serialization-of-strings).
pub fn string_literal(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
	let bytes = s.as_bytes();
	if bytes.len() < SIMD_ESCAPE_MIN {
		f.write_str("\"")?;
		let mut start = 0;
		while let Some(off) = crate::byte_scan::find_escape(&bytes[start..]) {
			let i = start + off;
			if start < i {
				// SAFETY: bytes[start..i] is a prefix-aligned slice of the
				// original `&str`. Escape bytes are all ASCII, so any
				// boundary between them lies on a UTF-8 char boundary.
				let run = unsafe { core::str::from_utf8_unchecked(&bytes[start..i]) };
				f.write_str(run)?;
			}
			write_escape(bytes[i], f)?;
			start = i + 1;
		}
		if start < bytes.len() {
			let run = unsafe { core::str::from_utf8_unchecked(&bytes[start..]) };
			f.write_str(run)?;
		}
		f.write_str("\"")
	} else {
		ESCAPE_BUFFER.with_borrow_mut(|buf| {
			buf.clear();
			// `format_string` panics if the destination is undersized; the
			// worst case is 6 bytes per input byte plus the surrounding
			// quotes and a SIMD overshoot margin.
			buf.reserve(s.len() * 6 + 32 + 3);
			json_escape_simd::escape_into(s, buf);
			// SAFETY: `escape_into` writes only ASCII escape sequences and
			// the original UTF-8 source bytes. The two surrounding `"` and
			// any escape bytes are ASCII, and untouched runs of input are
			// already valid UTF-8.
			f.write_str(unsafe { core::str::from_utf8_unchecked(buf) })
		})
	}
}

/// Returns the byte length of string literal according to [RFC8785](https://www.rfc-editor.org/rfc/rfc8785#name-serialization-of-strings).
pub fn printed_string_size(s: &str) -> usize {
	let mut width = 2usize;
	for &b in s.as_bytes() {
		let n = ESCAPE_LEN[b as usize] as usize;
		// Non-escape bytes count as 1 (covers all UTF-8 continuation and
		// leading bytes >= 0x20 except `"` and `\\`).
		width += if n == 0 { 1 } else { n };
	}
	width
}

impl Print for crate::String {
	#[inline(always)]
	fn fmt_with(&self, f: &mut fmt::Formatter, _options: &Options, _indent: usize) -> fmt::Result {
		string_literal(self, f)
	}
}

/// Writes `items` as a JSON array, using the layout at `sizes[*index]`.
pub fn print_array<I: IntoIterator>(
	items: I,
	f: &mut fmt::Formatter,
	options: &Options,
	indent: usize,
	sizes: &[Size],
	index: &mut usize,
) -> fmt::Result
where
	I::IntoIter: ExactSizeIterator,
	I::Item: PrintWithSize,
{
	use fmt::Display;
	let size = sizes[*index];
	*index += 1;

	f.write_str("[")?;

	let items = items.into_iter();
	if items.len() == 0 {
		match size {
			Size::Expanded => {
				f.write_str("\n")?;
				options.indent.by(indent).fmt(f)?;
			}
			Size::Width(_) => Spaces(options.array_empty).fmt(f)?,
		}
	} else {
		match size {
			Size::Expanded => {
				f.write_str("\n")?;

				for (i, item) in items.enumerate() {
					if i > 0 {
						Spaces(options.array_before_comma).fmt(f)?;
						f.write_str(",\n")?
					}

					options.indent.by(indent + 1).fmt(f)?;
					item.fmt_with_size(f, options, indent + 1, sizes, index)?
				}

				f.write_str("\n")?;
				options.indent.by(indent).fmt(f)?;
			}
			Size::Width(_) => {
				Spaces(options.array_begin).fmt(f)?;
				for (i, item) in items.enumerate() {
					if i > 0 {
						Spaces(options.array_before_comma).fmt(f)?;
						f.write_str(",")?;
						Spaces(options.array_after_comma).fmt(f)?
					}

					item.fmt_with_size(f, options, indent + 1, sizes, index)?
				}
				Spaces(options.array_end).fmt(f)?
			}
		}
	}

	f.write_str("]")
}

impl<T: PrintWithSize> PrintWithSize for Vec<T> {
	#[inline(always)]
	fn fmt_with_size(
		&self,
		f: &mut fmt::Formatter,
		options: &Options,
		indent: usize,
		sizes: &[Size],
		index: &mut usize,
	) -> fmt::Result {
		print_array(self, f, options, indent, sizes, index)
	}
}

/// Writes `entries` as a JSON object, using the layout at `sizes[*index]`.
pub fn print_object<'a, V, I: IntoIterator<Item = (&'a str, V)>>(
	entries: I,
	f: &mut fmt::Formatter,
	options: &Options,
	indent: usize,
	sizes: &[Size],
	index: &mut usize,
) -> fmt::Result
where
	I::IntoIter: ExactSizeIterator,
	V: PrintWithSize,
{
	use fmt::Display;
	let size = sizes[*index];
	*index += 1;

	f.write_str("{")?;

	let entries = entries.into_iter();
	if entries.len() == 0 {
		match size {
			Size::Expanded => {
				f.write_str("\n")?;
				options.indent.by(indent).fmt(f)?;
			}
			Size::Width(_) => Spaces(options.object_empty).fmt(f)?,
		}
	} else {
		match size {
			Size::Expanded => {
				f.write_str("\n")?;

				for (i, (key, value)) in entries.enumerate() {
					if i > 0 {
						Spaces(options.object_before_comma).fmt(f)?;
						f.write_str(",\n")?
					}

					options.indent.by(indent + 1).fmt(f)?;

					string_literal(key, f)?;
					Spaces(options.object_before_colon).fmt(f)?;
					f.write_str(":")?;
					Spaces(options.object_after_colon).fmt(f)?;

					value.fmt_with_size(f, options, indent + 1, sizes, index)?
				}

				f.write_str("\n")?;
				options.indent.by(indent).fmt(f)?;
			}
			Size::Width(_) => {
				Spaces(options.object_begin).fmt(f)?;
				for (i, (key, value)) in entries.enumerate() {
					if i > 0 {
						Spaces(options.object_before_comma).fmt(f)?;
						f.write_str(",")?;
						Spaces(options.object_after_comma).fmt(f)?
					}

					string_literal(key, f)?;
					Spaces(options.object_before_colon).fmt(f)?;
					f.write_str(":")?;
					Spaces(options.object_after_colon).fmt(f)?;

					value.fmt_with_size(f, options, indent + 1, sizes, index)?
				}
				Spaces(options.object_end).fmt(f)?
			}
		}
	}

	f.write_str("}")
}

impl PrintWithSize for crate::Object {
	#[inline(always)]
	fn fmt_with_size(
		&self,
		f: &mut fmt::Formatter,
		options: &Options,
		indent: usize,
		sizes: &[Size],
		index: &mut usize,
	) -> fmt::Result {
		print_object(
			self.iter().map(|e| (e.key.as_str(), &e.value)),
			f,
			options,
			indent,
			sizes,
			index,
		)
	}
}

/// Computation of the printed layout, ahead of printing.
///
/// Implementations push one [`Size`] per array/object encountered, in
/// depth-first order, and return the size of the value itself.
pub trait PrecomputeSize {
	/// Computes the printed size of this value, appending the size of each
	/// nested array and object to `sizes`.
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size;
}

impl PrecomputeSize for bool {
	#[inline(always)]
	fn pre_compute_size(&self, _options: &Options, _sizes: &mut Vec<Size>) -> Size {
		if *self {
			Size::Width(4)
		} else {
			Size::Width(5)
		}
	}
}

impl PrecomputeSize for crate::Value {
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		match self {
			crate::Value::Null => Size::Width(4),
			crate::Value::Boolean(b) => b.pre_compute_size(options, sizes),
			crate::Value::Number(n) => Size::Width(n.as_str().len()),
			crate::Value::String(s) => Size::Width(printed_string_size(s)),
			crate::Value::Array(a) => pre_compute_array_size(a, options, sizes),
			crate::Value::Object(o) => pre_compute_object_size(
				o.iter().map(|e| (e.key.as_str(), &e.value)),
				options,
				sizes,
			),
		}
	}
}

impl<T: PrecomputeSize + ?Sized> PrecomputeSize for &T {
	fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size {
		(**self).pre_compute_size(options, sizes)
	}
}

/// Computes the printed size of an array made of `items`.
pub fn pre_compute_array_size<I: IntoIterator>(
	items: I,
	options: &Options,
	sizes: &mut Vec<Size>,
) -> Size
where
	I::Item: PrecomputeSize,
{
	let index = sizes.len();
	sizes.push(Size::Width(0));

	let mut size = Size::Width(2 + options.object_begin + options.object_end);

	let mut len = 0;
	for (i, item) in items.into_iter().enumerate() {
		if i > 0 {
			size.add(Size::Width(
				1 + options.array_before_comma + options.array_after_comma,
			));
		}

		size.add(item.pre_compute_size(options, sizes));
		len += 1
	}

	let size = match size {
		Size::Expanded => Size::Expanded,
		Size::Width(width) => match options.array_limit {
			None => Size::Width(width),
			Some(Limit::Always) => Size::Expanded,
			Some(Limit::Item(i)) => {
				if len > i {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::ItemOrWidth(i, w)) => {
				if len > i || width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::Width(w)) => {
				if width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
		},
	};

	sizes[index] = size;
	size
}

/// Computes the printed size of an object made of `entries`.
pub fn pre_compute_object_size<'a, V, I: IntoIterator<Item = (&'a str, V)>>(
	entries: I,
	options: &Options,
	sizes: &mut Vec<Size>,
) -> Size
where
	V: PrecomputeSize,
{
	let index = sizes.len();
	sizes.push(Size::Width(0));

	let mut size = Size::Width(2 + options.object_begin + options.object_end);

	let mut len = 0;
	for (i, (key, value)) in entries.into_iter().enumerate() {
		if i > 0 {
			size.add(Size::Width(
				1 + options.object_before_comma + options.object_after_comma,
			));
		}

		size.add(Size::Width(
			printed_string_size(key) + 1 + options.object_before_colon + options.object_after_colon,
		));
		size.add(value.pre_compute_size(options, sizes));
		len += 1;
	}

	let size = match size {
		Size::Expanded => Size::Expanded,
		Size::Width(width) => match options.object_limit {
			None => Size::Width(width),
			Some(Limit::Always) => Size::Expanded,
			Some(Limit::Item(i)) => {
				if len > i {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::ItemOrWidth(i, w)) => {
				if len > i || width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
			Some(Limit::Width(w)) => {
				if width > w {
					Size::Expanded
				} else {
					Size::Width(width)
				}
			}
		},
	};

	sizes[index] = size;
	size
}

impl Print for crate::Value {
	fn fmt_with(&self, f: &mut fmt::Formatter, options: &Options, indent: usize) -> fmt::Result {
		match self {
			Self::Null => f.write_str("null"),
			Self::Boolean(b) => b.fmt_with(f, options, indent),
			Self::Number(n) => n.fmt_with(f, options, indent),
			Self::String(s) => s.fmt_with(f, options, indent),
			Self::Array(a) => {
				let mut sizes =
					Vec::with_capacity(self.count(|_, v| v.is_array() || v.is_object()));
				self.pre_compute_size(options, &mut sizes);
				let mut index = 0;
				a.fmt_with_size(f, options, indent, &sizes, &mut index)
			}
			Self::Object(o) => {
				let mut sizes =
					Vec::with_capacity(self.count(|_, v| v.is_array() || v.is_object()));
				self.pre_compute_size(options, &mut sizes);
				let mut index = 0;
				o.fmt_with_size(f, options, indent, &sizes, &mut index)
			}
		}
	}
}

impl PrintWithSize for crate::Value {
	fn fmt_with_size(
		&self,
		f: &mut fmt::Formatter,
		options: &Options,
		indent: usize,
		sizes: &[Size],
		index: &mut usize,
	) -> fmt::Result {
		match self {
			Self::Null => f.write_str("null"),
			Self::Boolean(b) => b.fmt_with(f, options, indent),
			Self::Number(n) => n.fmt_with(f, options, indent),
			Self::String(s) => s.fmt_with(f, options, indent),
			Self::Array(a) => a.fmt_with_size(f, options, indent, sizes, index),
			Self::Object(o) => o.fmt_with_size(f, options, indent, sizes, index),
		}
	}
}