jsonx-lib 0.5.2

Strict RFC 8259 JSON: a borrowing parser, a pull reader, and a deterministic writer, with budgets checked before allocation and no recursion. No dependencies, no_std.
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
//! The push writer: a document written one event at a time.

use alloc::{string::String, vec::Vec};
use core::fmt;

use crate::{
    Format, ToJson,
    encode::{Flush, Keep, Out, ValueWriter, Written},
    reader::Event,
    write,
};

/// Why a [`Writer`] call was refused.
///
/// The sequence errors (every variant but `Io` and `Fmt`) mean the calling
/// code asked for something that is not JSON; the call wrote nothing and the
/// writer is unchanged, so the caller may go on with a correct call. An
/// output error finishes the writer: every later call fails too.
///
/// The enum is `#[non_exhaustive]`.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{WriteError, Writer};
///
/// let mut out = String::new();
/// let mut w = Writer::new(&mut out);
/// w.begin_object()?;
/// assert!(matches!(w.value(&1), Err(WriteError::ExpectedKey)));
/// w.key("n")?;
/// w.value(&1)?;
/// w.end_object()?;
/// w.finish()?;
/// assert_eq!(out, r#"{"n":1}"#);
/// # Ok::<(), WriteError>(())
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub enum WriteError {
    /// The [`io::Write`](std::io::Write) failed. Its first error is returned
    /// once; later calls return an error of the same kind.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    Io(std::io::Error),
    /// The [`fmt::Write`](core::fmt::Write) failed.
    Fmt,
    /// A value, or the start of an array or object, where an object member's
    /// key belongs.
    ExpectedKey,
    /// A key outside an object, or a second key before the first one's
    /// value.
    UnexpectedKey,
    /// An `end_array` or `end_object` that does not close the innermost open
    /// container (or with none open, or with a key still waiting for its
    /// value).
    MismatchedEnd,
    /// A second top-level value: a document is one value.
    DocumentComplete,
    /// [`finish`](Writer::finish) before the document's value was complete.
    Incomplete,
}

impl fmt::Display for WriteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            #[cfg(feature = "std")]
            Self::Io(e) => return write!(f, "writing JSON failed: {e}"),
            Self::Fmt => "writing JSON failed: the formatter returned an error",
            Self::ExpectedKey => "expected an object key before this value",
            Self::UnexpectedKey => "a key is only allowed where an object member starts",
            Self::MismatchedEnd => "this end does not close the innermost open container",
            Self::DocumentComplete => "the document already has its value",
            Self::Incomplete => "the document is not complete",
        })
    }
}

impl core::error::Error for WriteError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            #[cfg(feature = "std")]
            Self::Io(e) => Some(e),
            _ => None,
        }
    }
}

/// An open container.
#[derive(Clone, Copy, Debug)]
struct Frame {
    object: bool,
    /// No member or item written yet.
    first: bool,
    /// A key was written; its value comes next.
    key: bool,
}

/// Where the output goes.
enum Target<'o> {
    /// Into the caller's `String`, never handed on.
    Keep(Keep),
    #[cfg(feature = "std")]
    Io {
        writer: &'o mut dyn std::io::Write,
        error: Option<std::io::Error>,
        /// The kind of an error already returned.
        failed: Option<std::io::ErrorKind>,
    },
    Fmt {
        writer: &'o mut dyn fmt::Write,
        failed: bool,
    },
}

impl Flush for Target<'_> {
    fn flush(&mut self, buf: &mut String) {
        match self {
            Self::Keep(keep) => keep.flush(buf),
            #[cfg(feature = "std")]
            Self::Io {
                writer,
                error,
                failed,
            } => {
                if error.is_none() && failed.is_none() {
                    if let Err(e) = writer.write_all(buf.as_bytes()) {
                        *error = Some(e);
                    }
                }
                buf.clear();
            }
            Self::Fmt { writer, failed } => {
                if !*failed && writer.write_str(buf).is_err() {
                    *failed = true;
                }
                buf.clear();
            }
        }
    }
}

impl Target<'_> {
    /// The output error, if the output has failed.
    fn error(&mut self) -> Option<WriteError> {
        match self {
            Self::Keep(_) => None,
            #[cfg(feature = "std")]
            Self::Io { error, failed, .. } => {
                if let Some(e) = error.take() {
                    *failed = Some(e.kind());
                    return Some(WriteError::Io(e));
                }
                failed.map(|kind| WriteError::Io(std::io::Error::from(kind)))
            }
            Self::Fmt { failed, .. } => failed.then_some(WriteError::Fmt),
        }
    }
}

/// The caller's buffer, or the writer's own.
enum Buffer<'o> {
    Borrowed(&'o mut String),
    Owned(String),
}

impl Buffer<'_> {
    #[inline]
    fn get(&mut self) -> &mut String {
        match self {
            Self::Borrowed(s) => s,
            Self::Owned(s) => s,
        }
    }
}

/// A push writer: a document written one event at a time, checked as it
/// goes.
///
/// The counterpart of [`Reader`](crate::Reader): where the reader pulls
/// [`Event`]s out of text, the writer takes them in and writes text. Use it
/// to write a document whose shape is only known at run time, to transcode
/// a [`Reader`](crate::Reader)'s events (any depth: the writer keeps an
/// explicit stack, never recursion), or to stream a large document to an
/// [`io::Write`](std::io::Write) without holding it. For a type known at
/// compile time, [`ToJson`] is simpler and checked by the compiler; the two
/// mix, since [`value`](Self::value) takes any `ToJson`.
///
/// Every call is checked against the grammar: a key outside an object, a
/// value where a key belongs, an end that does not match, or a second
/// top-level value is refused with a [`WriteError`] and writes nothing.
/// [`finish`](Self::finish) checks that the document is complete and passes
/// on the last of the output. Escaping and layout are done for you, as for
/// [`to_string_with`](crate::to_string_with).
///
/// Keys are not checked for repeats (that would mean keeping every key of
/// every open object): writing the same key twice in one object is
/// grammatical JSON that this crate's default parser refuses, so a caller
/// whose keys come from input should make them unique.
///
/// # Examples
///
/// ```
/// use jsonx_lib::{WriteError, Writer};
///
/// let mut out = String::new();
/// let mut w = Writer::new(&mut out);
/// w.begin_object()?;
/// w.key("jsonrpc")?;
/// w.value("2.0")?;
/// w.key("result")?;
/// w.begin_array()?;
/// for n in 1..=3 {
///     w.value(&n)?;
/// }
/// w.end_array()?;
/// w.end_object()?;
/// w.finish()?;
/// assert_eq!(out, r#"{"jsonrpc":"2.0","result":[1,2,3]}"#);
/// # Ok::<(), WriteError>(())
/// ```
///
/// Re-indent a document of any depth, event by event:
///
/// ```
/// use jsonx_lib::{Reader, Writer};
///
/// let text = r#"{"a":[1,{"b":null}]}"#;
/// let mut out = String::new();
/// let mut w = Writer::new(&mut out).pretty();
/// for event in Reader::new(text) {
///     w.event(&event?)?;
/// }
/// w.finish()?;
/// assert_eq!(out, jsonx_lib::to_string_pretty(&jsonx_lib::parse(text)?));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Writer<'o> {
    buf: Buffer<'o>,
    target: Target<'o>,
    format: Format,
    flush_at: usize,
    stack: Vec<Frame>,
    /// The top-level value is complete.
    done: bool,
}

impl fmt::Debug for Writer<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Writer")
            .field("format", &self.format)
            .field("depth", &self.stack.len())
            .field("complete", &self.is_complete())
            .finish_non_exhaustive()
    }
}

impl<'o> Writer<'o> {
    /// A writer that appends compact JSON to `out`.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::from("> ");
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// w.value(&true)?;
    /// w.finish()?;
    /// assert_eq!(out, "> true");
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[must_use]
    pub fn new(out: &'o mut String) -> Self {
        Self::with_target(Buffer::Borrowed(out), Target::Keep(Keep), usize::MAX)
    }

    /// A writer to an [`io::Write`](std::io::Write), through a buffer of a
    /// few kilobytes: a document of any size is written in pieces as it is
    /// produced. Call [`finish`](Self::finish) to write the last piece.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut file = Vec::new(); // any `io::Write`
    /// let mut w = jsonx_lib::Writer::io(&mut file);
    /// w.begin_array()?;
    /// for i in 0..10_000 {
    ///     w.value(&i)?;
    /// }
    /// w.end_array()?;
    /// w.finish()?;
    /// assert!(file.starts_with(b"[0,1,2,") && file.ends_with(b",9999]"));
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    #[must_use]
    pub fn io(out: &'o mut dyn std::io::Write) -> Self {
        let target = Target::Io {
            writer: out,
            error: None,
            failed: None,
        };
        let buf = Buffer::Owned(String::with_capacity(write::CHUNK + 256));
        Self::with_target(buf, target, write::CHUNK)
    }

    /// A writer to a [`fmt::Write`](core::fmt::Write) (a `Formatter`, or
    /// anything else that takes text), in pieces of a few kilobytes. Works
    /// without `std`.
    ///
    /// # Examples
    ///
    /// ```
    /// use core::fmt;
    ///
    /// struct Point(i32, i32);
    ///
    /// impl fmt::Display for Point {
    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    ///         let mut w = jsonx_lib::Writer::fmt(f);
    ///         w.value(&[self.0, self.1]).map_err(|_| fmt::Error)?;
    ///         w.finish().map_err(|_| fmt::Error)
    ///     }
    /// }
    ///
    /// assert_eq!(Point(3, -4).to_string(), "[3,-4]");
    /// ```
    #[must_use]
    pub fn fmt(out: &'o mut dyn fmt::Write) -> Self {
        let target = Target::Fmt {
            writer: out,
            failed: false,
        };
        let buf = Buffer::Owned(String::with_capacity(write::CHUNK + 256));
        Self::with_target(buf, target, write::CHUNK)
    }

    fn with_target(buf: Buffer<'o>, target: Target<'o>, flush_at: usize) -> Self {
        Self {
            buf,
            target,
            format: Format::compact(),
            flush_at,
            stack: Vec::new(),
            done: false,
        }
    }

    /// Switches to the indented layout of
    /// [`to_string_pretty`](crate::to_string_pretty). Call before writing.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out).pretty();
    /// w.value(&[1, 2])?;
    /// w.finish()?;
    /// assert_eq!(out, "[\n  1,\n  2\n]");
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[must_use]
    pub fn pretty(self) -> Self {
        self.with_format(Format::pretty())
    }

    /// Switches to `format`. Call before writing.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{Format, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out).with_format(Format::compact().html_safe(true));
    /// w.value("<b>")?;
    /// w.finish()?;
    /// assert_eq!(out, r#""\u003cb\u003e""#);
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[must_use]
    pub fn with_format(mut self, format: Format) -> Self {
        self.format = format;
        self
    }

    /// Starts an object (as a value: at the top, as an array item, or after
    /// a key).
    ///
    /// # Errors
    ///
    /// [`WriteError::ExpectedKey`] inside an object before a key,
    /// [`WriteError::DocumentComplete`] after the top-level value, or an
    /// output error.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// w.begin_object()?;
    /// w.end_object()?;
    /// w.finish()?;
    /// assert_eq!(out, "{}");
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    pub fn begin_object(&mut self) -> Result<(), WriteError> {
        self.open(true)
    }

    /// Starts an array; placed as [`begin_object`](Self::begin_object).
    ///
    /// # Errors
    ///
    /// As [`begin_object`](Self::begin_object).
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// w.begin_array()?;
    /// w.begin_array()?;
    /// w.end_array()?;
    /// w.end_array()?;
    /// w.finish()?;
    /// assert_eq!(out, "[[]]");
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    pub fn begin_array(&mut self) -> Result<(), WriteError> {
        self.open(false)
    }

    /// Ends the innermost open object.
    ///
    /// # Errors
    ///
    /// [`WriteError::MismatchedEnd`] if the innermost open container is not
    /// an object, none is open, or a key is waiting for its value; or an
    /// output error.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{WriteError, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out);
    /// w.begin_array()?;
    /// assert!(matches!(w.end_object(), Err(WriteError::MismatchedEnd)));
    /// w.end_array()?;
    /// # Ok::<(), WriteError>(())
    /// ```
    pub fn end_object(&mut self) -> Result<(), WriteError> {
        self.close(true)
    }

    /// Ends the innermost open array.
    ///
    /// # Errors
    ///
    /// As [`end_object`](Self::end_object), for an array.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// w.begin_array()?;
    /// w.value(&"x")?;
    /// w.end_array()?;
    /// assert!(w.is_complete());
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    pub fn end_array(&mut self) -> Result<(), WriteError> {
        self.close(false)
    }

    /// Writes an object member's key; its value comes next. A key already
    /// written in the same object is not refused (see [`Writer`]).
    ///
    /// # Errors
    ///
    /// [`WriteError::UnexpectedKey`] outside an object or right after
    /// another key, or an output error.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{WriteError, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out);
    /// assert!(matches!(w.key("k"), Err(WriteError::UnexpectedKey)));
    /// w.begin_object()?;
    /// w.key("say \"hi\"")?;
    /// w.value(&1)?;
    /// w.end_object()?;
    /// w.finish()?;
    /// assert_eq!(out, r#"{"say \"hi\"":1}"#);
    /// # Ok::<(), WriteError>(())
    /// ```
    pub fn key(&mut self, key: &str) -> Result<(), WriteError> {
        self.check()?;
        let format = self.format;
        let depth = self.stack.len();
        let Some(frame) = self.stack.last_mut() else {
            return Err(WriteError::UnexpectedKey);
        };
        if !frame.object || frame.key {
            return Err(WriteError::UnexpectedKey);
        }
        let buf = self.buf.get();
        if !frame.first {
            buf.push(',');
        }
        frame.first = false;
        frame.key = true;
        if format.pretty {
            write::newline(buf, depth, format);
        }
        write::write_str(buf, key, format);
        if format.pretty {
            buf.push_str(": ");
        } else {
            buf.push(':');
        }
        self.flushed()
    }

    /// Writes a whole value, of any type that implements [`ToJson`]: a
    /// scalar, a [`Value`](crate::Value), or your own type (placed as
    /// [`begin_object`](Self::begin_object)).
    ///
    /// # Errors
    ///
    /// As [`begin_object`](Self::begin_object).
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{json, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out);
    /// w.begin_array()?;
    /// w.value(&json!({ "a": [1] }))?;
    /// w.value(&Some(2.5))?;
    /// w.value("three")?;
    /// w.end_array()?;
    /// w.finish()?;
    /// assert_eq!(out, r#"[{"a":[1]},2.5,"three"]"#);
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    pub fn value<T: ToJson + ?Sized>(&mut self, value: &T) -> Result<(), WriteError> {
        self.before_value()?;
        let out = Out {
            buf: self.buf.get(),
            sink: &mut self.target,
            flush_at: self.flush_at,
            format: self.format,
            depth: self.stack.len(),
        };
        let Written(()) = value.to_json(ValueWriter::new(out));
        self.after_value();
        self.flushed()
    }

    /// Writes one event, as from a [`Reader`](crate::Reader): the start or
    /// end of a container, a key, or a scalar.
    ///
    /// # Errors
    ///
    /// The error of the call the event stands for.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{Event, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out);
    /// for e in [Event::StartObject, Event::Key("k".into()), Event::Null, Event::EndObject] {
    ///     w.event(&e)?;
    /// }
    /// w.finish()?;
    /// assert_eq!(out, r#"{"k":null}"#);
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    pub fn event(&mut self, event: &Event<'_>) -> Result<(), WriteError> {
        match event {
            Event::StartObject => self.begin_object(),
            Event::EndObject => self.end_object(),
            Event::StartArray => self.begin_array(),
            Event::EndArray => self.end_array(),
            Event::Key(key) => self.key(key),
            Event::String(text) => self.value(&**text),
            Event::Number(number) => self.value(number),
            Event::Bool(b) => self.value(b),
            Event::Null => self.value(&()),
        }
    }

    /// The number of arrays and objects open.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// w.begin_array()?;
    /// assert_eq!(w.depth(), 1);
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[must_use]
    pub fn depth(&self) -> usize {
        self.stack.len()
    }

    /// Whether the document's value is complete.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut out = String::new();
    /// let mut w = jsonx_lib::Writer::new(&mut out);
    /// assert!(!w.is_complete());
    /// w.value(&0)?;
    /// assert!(w.is_complete());
    /// # Ok::<(), jsonx_lib::WriteError>(())
    /// ```
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.done
    }

    /// Checks that the document is complete and passes on the rest of the
    /// output (for [`io`](Self::io) and [`fmt`](Self::fmt) writers).
    ///
    /// # Errors
    ///
    /// [`WriteError::Incomplete`] with containers open or no value written,
    /// or an output error.
    ///
    /// # Examples
    ///
    /// ```
    /// use jsonx_lib::{WriteError, Writer};
    ///
    /// let mut out = String::new();
    /// let mut w = Writer::new(&mut out);
    /// w.begin_array()?;
    /// assert!(matches!(w.finish(), Err(WriteError::Incomplete)));
    /// # Ok::<(), WriteError>(())
    /// ```
    pub fn finish(mut self) -> Result<(), WriteError> {
        self.check()?;
        if !self.done {
            return Err(WriteError::Incomplete);
        }
        self.target.flush(self.buf.get());
        self.check()
    }

    // ---- internals -----------------------------------------------------------

    /// Fails if the output has failed.
    fn check(&mut self) -> Result<(), WriteError> {
        self.target.error().map_or(Ok(()), Err)
    }

    /// Passes the buffer on if it is full, then reports any output error.
    fn flushed(&mut self) -> Result<(), WriteError> {
        let buf = self.buf.get();
        if buf.len() >= self.flush_at {
            self.target.flush(buf);
        }
        self.check()
    }

    /// Checks that a value may come here and writes what goes before it.
    fn before_value(&mut self) -> Result<(), WriteError> {
        self.check()?;
        let format = self.format;
        let depth = self.stack.len();
        match self.stack.last_mut() {
            None if self.done => Err(WriteError::DocumentComplete),
            None => Ok(()),
            Some(frame) if frame.object => {
                if frame.key {
                    frame.key = false;
                    Ok(())
                } else {
                    Err(WriteError::ExpectedKey)
                }
            }
            Some(frame) => {
                let buf = self.buf.get();
                if !frame.first {
                    buf.push(',');
                }
                frame.first = false;
                if format.pretty {
                    write::newline(buf, depth, format);
                }
                Ok(())
            }
        }
    }

    /// Marks a value complete.
    #[inline]
    fn after_value(&mut self) {
        if self.stack.is_empty() {
            self.done = true;
        }
    }

    fn open(&mut self, object: bool) -> Result<(), WriteError> {
        self.before_value()?;
        self.buf.get().push(if object { '{' } else { '[' });
        self.stack.push(Frame {
            object,
            first: true,
            key: false,
        });
        self.flushed()
    }

    fn close(&mut self, object: bool) -> Result<(), WriteError> {
        self.check()?;
        match self.stack.last() {
            Some(frame) if frame.object == object && !frame.key => {}
            _ => return Err(WriteError::MismatchedEnd),
        }
        let Some(frame) = self.stack.pop() else {
            return Err(WriteError::MismatchedEnd);
        };
        let format = self.format;
        let depth = self.stack.len();
        let buf = self.buf.get();
        if !frame.first && format.pretty {
            write::newline(buf, depth, format);
        }
        buf.push(if object { '}' } else { ']' });
        self.after_value();
        self.flushed()
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;

    use super::*;

    #[test]
    fn test_sequence_errors_write_nothing_and_leave_the_writer_usable() {
        let mut out = String::new();
        let mut w = Writer::new(&mut out);
        assert!(matches!(w.end_array(), Err(WriteError::MismatchedEnd)));
        assert!(matches!(w.key("k"), Err(WriteError::UnexpectedKey)));
        assert!(w.begin_object().is_ok());
        assert!(matches!(w.begin_array(), Err(WriteError::ExpectedKey)));
        assert!(w.key("a").is_ok());
        assert!(matches!(w.key("b"), Err(WriteError::UnexpectedKey)));
        assert!(matches!(w.end_object(), Err(WriteError::MismatchedEnd)));
        assert!(w.value(&1).is_ok());
        assert!(matches!(w.end_array(), Err(WriteError::MismatchedEnd)));
        assert!(w.end_object().is_ok());
        assert!(matches!(w.value(&2), Err(WriteError::DocumentComplete)));
        assert!(matches!(w.begin_array(), Err(WriteError::DocumentComplete)));
        assert_eq!(w.depth(), 0);
        assert!(w.finish().is_ok());
        assert_eq!(out, r#"{"a":1}"#);

        let mut out = String::new();
        assert!(matches!(
            Writer::new(&mut out).finish(),
            Err(WriteError::Incomplete)
        ));
        for e in [
            WriteError::Fmt,
            WriteError::ExpectedKey,
            WriteError::UnexpectedKey,
            WriteError::MismatchedEnd,
            WriteError::DocumentComplete,
            WriteError::Incomplete,
        ] {
            assert!(!e.to_string().is_empty());
            assert!(core::error::Error::source(&e).is_none());
        }
    }

    #[test]
    fn test_pretty_events_match_pretty_values() {
        let v = crate::json!({ "a": [], "b": {}, "c": [{ "d": [1, 2] }], "e": "x" });
        let mut out = String::new();
        let mut w = Writer::new(&mut out).pretty();
        for event in crate::Reader::new(&v.to_string()) {
            assert!(event.is_ok_and(|e| w.event(&e).is_ok()));
        }
        assert!(w.finish().is_ok());
        assert_eq!(out, crate::to_string_pretty(&v));
    }

    #[test]
    fn test_fmt_target_reports_failure() {
        struct Full;
        impl fmt::Write for Full {
            fn write_str(&mut self, _: &str) -> fmt::Result {
                Err(fmt::Error)
            }
        }
        let mut full = Full;
        let mut w = Writer::fmt(&mut full);
        assert!(w.value(&"x").is_ok()); // still buffered
        assert!(matches!(w.finish(), Err(WriteError::Fmt)));
        let mut full = Full;
        let mut w = Writer::fmt(&mut full);
        assert!(w.begin_array().is_ok());
        let long = "y".repeat(write::CHUNK);
        assert!(matches!(w.value(&long), Err(WriteError::Fmt)));
        assert!(matches!(w.value(&1), Err(WriteError::Fmt)));
        assert!(alloc::format!("{w:?}").contains("depth: 1"));
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_io_target_returns_the_error_then_its_kind() {
        struct Broken;
        impl std::io::Write for Broken {
            fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
                Err(std::io::Error::other("disk full"))
            }
            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }
        let mut broken = Broken;
        let mut w = Writer::io(&mut broken);
        assert!(w.begin_array().is_ok());
        let long = "z".repeat(write::CHUNK);
        let first = w.value(&long).err();
        assert_eq!(
            first.map(|e| e.to_string()),
            Some("writing JSON failed: disk full".to_string())
        );
        let later = w.end_array().err();
        assert!(
            matches!(later, Some(WriteError::Io(ref e)) if e.kind() == std::io::ErrorKind::Other)
        );
        assert!(later.is_some_and(|e| core::error::Error::source(&e).is_some()));
    }
}