Coverage Report

Created: 2026-03-17 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sval_nested-2.16.0/src/flat.rs
Line
Count
Source
1
use core::{fmt, marker::PhantomData, mem};
2
3
use sval_buffer::{BinaryBuf, TextBuf, ValueBuf};
4
5
use crate::{Error, Result, Stream, StreamEnum, StreamMap, StreamRecord, StreamSeq, StreamTuple};
6
7
use super::{flat_enum::FlatStreamEnum, owned_label_ref};
8
9
pub(super) struct FlatStream<'sval, S: Stream<'sval>> {
10
    buffered: Option<Buffered<'sval>>,
11
    state: State<'sval, S>,
12
}
13
14
impl<'sval, S: Stream<'sval>> fmt::Debug for FlatStream<'sval, S> {
15
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16
0
        f.debug_struct("FlatStream")
17
0
            .field("buffered", &self.buffered)
18
0
            .field("state", &self.state)
19
0
            .finish()
20
0
    }
21
}
22
23
enum State<'sval, S: Stream<'sval>> {
24
    Any(Option<Any<'sval, S>>),
25
    Seq(Option<Seq<S::Seq>>),
26
    Map(Option<Map<S::Map>>),
27
    Tagged(Option<Tagged<S>>),
28
    Tuple(Option<Tuple<S::Tuple>>),
29
    Record(Option<Record<S::Record>>),
30
    Enum(Option<Enum<FlatStreamEnum<S::Enum>>>),
31
    EnumVariant(Option<EnumVariant<'sval, S>>),
32
    Done(Option<Result<S::Ok>>),
33
}
34
35
impl<'sval, S: Stream<'sval>> fmt::Debug for State<'sval, S> {
36
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37
0
        match self {
38
0
            State::Any(state) => fmt::Debug::fmt(state, f),
39
0
            State::Seq(state) => fmt::Debug::fmt(state, f),
40
0
            State::Map(state) => fmt::Debug::fmt(state, f),
41
0
            State::Tuple(state) => fmt::Debug::fmt(state, f),
42
0
            State::Record(state) => fmt::Debug::fmt(state, f),
43
0
            State::Tagged(state) => fmt::Debug::fmt(state, f),
44
0
            State::Enum(state) => fmt::Debug::fmt(state, f),
45
0
            State::EnumVariant(state) => fmt::Debug::fmt(state, f),
46
0
            State::Done(_) => f.debug_struct("Done").finish_non_exhaustive(),
47
        }
48
0
    }
49
}
50
51
#[derive(Debug)]
52
enum Buffered<'sval> {
53
    Text(TextBuf<'sval>),
54
    Binary(BinaryBuf<'sval>),
55
    Value(ValueBuf<'sval>),
56
}
57
58
struct Any<'sval, S: Stream<'sval>> {
59
    stream: S,
60
    _marker: PhantomData<&'sval ()>,
61
}
62
63
impl<'sval, S: Stream<'sval>> fmt::Debug for Any<'sval, S> {
64
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65
0
        f.debug_struct("Any").finish_non_exhaustive()
66
0
    }
67
}
68
69
struct Seq<S> {
70
    stream: S,
71
}
72
73
impl<S> fmt::Debug for Seq<S> {
74
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75
0
        f.debug_struct("Seq").finish_non_exhaustive()
76
0
    }
77
}
78
79
struct Map<S> {
80
    stream: S,
81
    is_key: bool,
82
}
83
84
impl<S> fmt::Debug for Map<S> {
85
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86
0
        f.debug_struct("Map")
87
0
            .field("is_key", &self.is_key)
88
0
            .finish_non_exhaustive()
89
0
    }
90
}
91
92
struct Record<S> {
93
    stream: S,
94
    field: Option<(Option<sval::Tag>, sval::Label<'static>)>,
95
}
96
97
impl<S> fmt::Debug for Record<S> {
98
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99
0
        f.debug_struct("Record").finish_non_exhaustive()
100
0
    }
101
}
102
103
struct Tuple<S> {
104
    stream: S,
105
    field: Option<(Option<sval::Tag>, sval::Index)>,
106
}
107
108
impl<S> fmt::Debug for Tuple<S> {
109
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110
0
        f.debug_struct("Tuple").finish_non_exhaustive()
111
0
    }
112
}
113
114
struct Tagged<S> {
115
    stream: S,
116
    tag: Option<sval::Tag>,
117
    label: Option<sval::Label<'static>>,
118
    index: Option<sval::Index>,
119
}
120
121
impl<S> fmt::Debug for Tagged<S> {
122
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
123
0
        f.debug_struct("Tagged")
124
0
            .field("tag", &self.tag)
125
0
            .field("label", &self.label)
126
0
            .field("index", &self.index)
127
0
            .finish_non_exhaustive()
128
0
    }
129
}
130
131
struct Enum<S> {
132
    stream: S,
133
}
134
135
impl<S> fmt::Debug for Enum<S> {
136
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137
0
        f.debug_struct("Enum").finish_non_exhaustive()
138
0
    }
139
}
140
141
enum EnumVariant<'sval, S: Stream<'sval>> {
142
    Tagged(Tagged<FlatStreamEnum<S::Enum>>),
143
    Tuple(Tuple<<S::Enum as StreamEnum<'sval>>::Tuple>),
144
    Record(Record<<S::Enum as StreamEnum<'sval>>::Record>),
145
}
146
147
impl<'sval, S: Stream<'sval>> fmt::Debug for EnumVariant<'sval, S> {
148
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149
0
        match self {
150
0
            EnumVariant::Tagged(stream) => fmt::Debug::fmt(stream, f),
151
0
            EnumVariant::Tuple(stream) => fmt::Debug::fmt(stream, f),
152
0
            EnumVariant::Record(stream) => fmt::Debug::fmt(stream, f),
153
        }
154
0
    }
155
}
156
157
impl<'sval, S: Stream<'sval>> FlatStream<'sval, S> {
158
0
    pub fn new(stream: S) -> Self {
159
0
        FlatStream {
160
0
            buffered: None,
161
0
            state: State::Any(Some(Any {
162
0
                stream,
163
0
                _marker: PhantomData,
164
0
            })),
165
0
        }
166
0
    }
167
168
0
    pub fn finish(&mut self) -> Result<S::Ok> {
169
0
        if let State::Done(ref mut r) = self.state {
170
0
            r.take()
171
0
                .unwrap_or_else(|| Err(Error::invalid_value("incomplete stream")))
172
        } else {
173
0
            Err(Error::invalid_value("incomplete stream"))
174
        }
175
0
    }
176
}
177
178
impl<'sval, S: Stream<'sval>> sval::Stream<'sval> for FlatStream<'sval, S> {
179
0
    fn value<V: sval::Value + ?Sized>(&mut self, v: &'sval V) -> sval::Result {
180
0
        self.buffer_or_stream_with(
181
0
            |buf| sval::Stream::value(buf, v),
182
0
            |stream| match stream.state {
183
                State::Enum(_) => {
184
0
                    sval::default_stream::value(stream, v)
185
0
                        .map_err(|_| Error::invalid_value("failed to stream value"))?;
186
0
                    Ok(None)
187
                }
188
0
                ref mut state => state.value(&sval_ref::to_ref(v), |stream, v| stream.value(v)),
189
0
            },
190
        )
191
0
    }
192
193
0
    fn value_computed<V: sval::Value + ?Sized>(&mut self, v: &V) -> sval::Result {
194
0
        self.buffer_or_stream_with(
195
0
            |buf| sval::Stream::value_computed(buf, v),
196
0
            |stream| match stream.state {
197
                State::Enum(_) => {
198
0
                    sval::default_stream::value_computed(stream, v)
199
0
                        .map_err(|_| Error::invalid_value("failed to stream value"))?;
200
0
                    Ok(None)
201
                }
202
0
                ref mut state => state.value_computed(v, |stream, v| stream.value_computed(v)),
203
0
            },
204
        )
205
0
    }
206
207
0
    fn tag(
208
0
        &mut self,
209
0
        tag: Option<&sval::Tag>,
210
0
        label: Option<&sval::Label>,
211
0
        index: Option<&sval::Index>,
212
0
    ) -> sval::Result {
213
0
        self.buffer_or_stream_with(
214
0
            |buf| buf.tag(tag, label, index),
215
0
            |stream| match stream.state {
216
0
                State::Enum(ref mut stream) => {
217
0
                    let stream = stream.take().ok_or_else(|| {
218
0
                        Error::invalid_value(
219
                            "failed to stream an enum; the stream is already completed",
220
                        )
221
0
                    })?;
222
223
0
                    Ok(Some(stream.stream.tag(
224
0
                        tag.cloned(),
225
0
                        label.cloned(),
226
0
                        index.cloned(),
227
0
                    )?))
228
                }
229
0
                ref mut state => state.value_computed(&Tag(tag, label, index), |stream, _| {
230
0
                    stream.tag(tag.cloned(), label.cloned(), index.cloned())
231
0
                }),
232
0
            },
233
        )
234
0
    }
235
236
0
    fn seq_begin(&mut self, num_entries: Option<usize>) -> sval::Result {
237
0
        self.buffer_or_begin_with(
238
0
            |buf| buf.seq_begin(num_entries),
239
0
            |stream| {
240
                Ok(State::Seq(Some(Seq {
241
0
                    stream: stream.stream.seq_begin(num_entries)?,
242
                })))
243
0
            },
244
0
            |_| {
245
0
                Err(Error::invalid_value(
246
0
                    "sequences cannot be used as enum variants",
247
0
                ))
248
0
            },
249
        )
250
0
    }
251
252
0
    fn seq_value_begin(&mut self) -> sval::Result {
253
0
        Ok(())
254
0
    }
255
256
0
    fn seq_value_end(&mut self) -> sval::Result {
257
0
        Ok(())
258
0
    }
259
260
0
    fn seq_end(&mut self) -> sval::Result {
261
0
        self.buffer_or_end_with(
262
0
            |buf| buf.seq_end(),
263
0
            |stream| stream.take_seq()?.stream.end(),
264
        )
265
0
    }
266
267
0
    fn map_begin(&mut self, num_entries: Option<usize>) -> sval::Result {
268
0
        self.buffer_or_begin_with(
269
0
            |buf| buf.map_begin(num_entries),
270
0
            |stream| {
271
                Ok(State::Map(Some(Map {
272
0
                    stream: stream.stream.map_begin(num_entries)?,
273
                    is_key: true,
274
                })))
275
0
            },
276
0
            |_| Err(Error::invalid_value("maps cannot be used as enum variants")),
277
        )
278
0
    }
279
280
0
    fn map_key_begin(&mut self) -> sval::Result {
281
0
        self.buffer_or_with(
282
0
            |buf| buf.map_key_begin(),
283
0
            |stream| {
284
0
                stream.with_map(|stream| {
285
0
                    stream.is_key = true;
286
287
0
                    Ok(())
288
0
                })
289
0
            },
290
        )
291
0
    }
292
293
0
    fn map_key_end(&mut self) -> sval::Result {
294
0
        self.buffer_or_with(|buf| buf.map_key_end(), |_| Ok(()))
295
0
    }
296
297
0
    fn map_value_begin(&mut self) -> sval::Result {
298
0
        self.buffer_or_with(
299
0
            |buf| buf.map_value_begin(),
300
0
            |stream| {
301
0
                stream.with_map(|stream| {
302
0
                    stream.is_key = false;
303
304
0
                    Ok(())
305
0
                })
306
0
            },
307
        )
308
0
    }
309
310
0
    fn map_value_end(&mut self) -> sval::Result {
311
0
        self.buffer_or_with(|buf| buf.map_value_end(), |_| Ok(()))
312
0
    }
313
314
0
    fn map_end(&mut self) -> sval::Result {
315
0
        self.buffer_or_end_with(
316
0
            |buf| buf.map_end(),
317
0
            |stream| stream.take_map()?.stream.end(),
318
        )
319
0
    }
320
321
0
    fn enum_begin(
322
0
        &mut self,
323
0
        tag: Option<&sval::Tag>,
324
0
        label: Option<&sval::Label>,
325
0
        index: Option<&sval::Index>,
326
0
    ) -> sval::Result {
327
0
        self.buffer_or_begin_with(
328
0
            |buf| buf.enum_begin(tag, label, index),
329
0
            |stream| {
330
                Ok(State::Enum(Some(Enum {
331
0
                    stream: FlatStreamEnum::new(stream.stream.enum_begin(
332
0
                        tag.cloned(),
333
0
                        label.cloned(),
334
0
                        index.cloned(),
335
0
                    )?),
336
                })))
337
0
            },
338
0
            |mut stream| {
339
0
                stream
340
0
                    .stream
341
0
                    .push(tag.cloned(), label.cloned(), index.cloned())?;
342
343
0
                Ok(State::Enum(Some(stream)))
344
0
            },
345
        )
346
0
    }
347
348
0
    fn enum_end(
349
0
        &mut self,
350
0
        tag: Option<&sval::Tag>,
351
0
        label: Option<&sval::Label>,
352
0
        index: Option<&sval::Index>,
353
0
    ) -> sval::Result {
354
0
        self.buffer_or_end_with(
355
0
            |buf| buf.enum_end(tag, label, index),
356
0
            |stream| {
357
0
                if let Some(stream) = stream.take_enum()? {
358
0
                    stream.stream.end()
359
                } else {
360
0
                    stream.finish()
361
                }
362
0
            },
363
        )
364
0
    }
365
366
0
    fn tagged_begin(
367
0
        &mut self,
368
0
        tag: Option<&sval::Tag>,
369
0
        label: Option<&sval::Label>,
370
0
        index: Option<&sval::Index>,
371
0
    ) -> sval::Result {
372
0
        self.buffer_or_begin_with(
373
0
            |buf| buf.tagged_begin(tag, label, index),
374
0
            |stream| {
375
                Ok(State::Tagged(Some(Tagged {
376
0
                    stream: stream.stream,
377
0
                    tag: tag.cloned(),
378
0
                    label: if let Some(label) = label {
379
0
                        Some(owned_label_ref(label)?)
380
                    } else {
381
0
                        None
382
                    },
383
0
                    index: index.cloned(),
384
                })))
385
0
            },
386
0
            |stream| {
387
                Ok(State::EnumVariant(Some(EnumVariant::Tagged(Tagged {
388
0
                    stream: stream.stream,
389
0
                    tag: tag.cloned(),
390
0
                    label: if let Some(label) = label {
391
0
                        Some(owned_label_ref(label)?)
392
                    } else {
393
0
                        None
394
                    },
395
0
                    index: index.cloned(),
396
                }))))
397
0
            },
398
        )
399
0
    }
400
401
0
    fn tagged_end(
402
0
        &mut self,
403
0
        tag: Option<&sval::Tag>,
404
0
        label: Option<&sval::Label>,
405
0
        index: Option<&sval::Index>,
406
0
    ) -> sval::Result {
407
0
        self.buffer_or_end_with(
408
0
            |buf| buf.tagged_end(tag, label, index),
409
0
            |stream| stream.finish(),
410
        )
411
0
    }
412
413
0
    fn record_begin(
414
0
        &mut self,
415
0
        tag: Option<&sval::Tag>,
416
0
        label: Option<&sval::Label>,
417
0
        index: Option<&sval::Index>,
418
0
        num_entries: Option<usize>,
419
0
    ) -> sval::Result {
420
0
        self.buffer_or_begin_with(
421
0
            |buf| buf.record_begin(tag, label, index, num_entries),
422
0
            |stream| {
423
                Ok(State::Record(Some(Record {
424
0
                    stream: stream.stream.record_begin(
425
0
                        tag.cloned(),
426
0
                        label.cloned(),
427
0
                        index.cloned(),
428
0
                        num_entries,
429
0
                    )?,
430
0
                    field: None,
431
                })))
432
0
            },
433
0
            |stream| {
434
                Ok(State::EnumVariant(Some(EnumVariant::Record(Record {
435
0
                    stream: stream.stream.record_begin(
436
0
                        tag.cloned(),
437
0
                        label.cloned(),
438
0
                        index.cloned(),
439
0
                        num_entries,
440
0
                    )?,
441
0
                    field: None,
442
                }))))
443
0
            },
444
        )
445
0
    }
446
447
0
    fn record_value_begin(&mut self, tag: Option<&sval::Tag>, label: &sval::Label) -> sval::Result {
448
0
        self.buffer_or_with(
449
0
            |buf| buf.record_value_begin(tag, label),
450
0
            |stream| {
451
0
                stream.with_record(
452
0
                    |record| {
453
0
                        record.field = Some((tag.cloned(), owned_label_ref(label)?));
454
455
0
                        Ok(())
456
0
                    },
457
0
                    |record_variant| {
458
0
                        record_variant.field = Some((tag.cloned(), owned_label_ref(label)?));
459
460
0
                        Ok(())
461
0
                    },
462
                )
463
0
            },
464
        )
465
0
    }
466
467
0
    fn record_value_end(&mut self, tag: Option<&sval::Tag>, label: &sval::Label) -> sval::Result {
468
0
        self.buffer_or_with(|buf| buf.record_value_end(tag, label), |_| Ok(()))
469
0
    }
470
471
0
    fn record_end(
472
0
        &mut self,
473
0
        tag: Option<&sval::Tag>,
474
0
        label: Option<&sval::Label>,
475
0
        index: Option<&sval::Index>,
476
0
    ) -> sval::Result {
477
0
        self.buffer_or_end_with(
478
0
            |buf| buf.record_end(tag, label, index),
479
0
            |stream| {
480
0
                stream.take_with_record(
481
0
                    |record| record.stream.end(),
482
0
                    |record_variant| record_variant.stream.end(),
483
                )
484
0
            },
485
        )
486
0
    }
487
488
0
    fn tuple_begin(
489
0
        &mut self,
490
0
        tag: Option<&sval::Tag>,
491
0
        label: Option<&sval::Label>,
492
0
        index: Option<&sval::Index>,
493
0
        num_entries: Option<usize>,
494
0
    ) -> sval::Result {
495
0
        self.buffer_or_begin_with(
496
0
            |buf| buf.tuple_begin(tag, label, index, num_entries),
497
0
            |stream| {
498
                Ok(State::Tuple(Some(Tuple {
499
0
                    stream: stream.stream.tuple_begin(
500
0
                        tag.cloned(),
501
0
                        label.cloned(),
502
0
                        index.cloned(),
503
0
                        num_entries,
504
0
                    )?,
505
0
                    field: None,
506
                })))
507
0
            },
508
0
            |stream| {
509
                Ok(State::EnumVariant(Some(EnumVariant::Tuple(Tuple {
510
0
                    stream: stream.stream.tuple_begin(
511
0
                        tag.cloned(),
512
0
                        label.cloned(),
513
0
                        index.cloned(),
514
0
                        num_entries,
515
0
                    )?,
516
0
                    field: None,
517
                }))))
518
0
            },
519
        )
520
0
    }
521
522
0
    fn tuple_value_begin(&mut self, tag: Option<&sval::Tag>, index: &sval::Index) -> sval::Result {
523
0
        self.buffer_or_with(
524
0
            |buf| buf.tuple_value_begin(tag, index),
525
0
            |stream| {
526
0
                stream.with_tuple(
527
0
                    |tuple| {
528
0
                        tuple.field = Some((tag.cloned(), index.clone()));
529
530
0
                        Ok(())
531
0
                    },
532
0
                    |tuple_variant| {
533
0
                        tuple_variant.field = Some((tag.cloned(), index.clone()));
534
535
0
                        Ok(())
536
0
                    },
537
                )
538
0
            },
539
        )
540
0
    }
541
542
0
    fn tuple_value_end(&mut self, tag: Option<&sval::Tag>, index: &sval::Index) -> sval::Result {
543
0
        self.buffer_or_with(|buf| buf.tuple_value_end(tag, index), |_| Ok(()))
544
0
    }
545
546
0
    fn tuple_end(
547
0
        &mut self,
548
0
        tag: Option<&sval::Tag>,
549
0
        label: Option<&sval::Label>,
550
0
        index: Option<&sval::Index>,
551
0
    ) -> sval::Result {
552
0
        self.buffer_or_end_with(
553
0
            |buf| buf.tuple_end(tag, label, index),
554
0
            |stream| {
555
0
                stream.take_with_tuple(
556
0
                    |tuple| tuple.stream.end(),
557
0
                    |tuple_variant| tuple_variant.stream.end(),
558
                )
559
0
            },
560
        )
561
0
    }
562
563
0
    fn null(&mut self) -> sval::Result {
564
0
        self.buffer_or_stream_with(
565
0
            |buf| buf.null(),
566
0
            |stream| {
567
0
                stream
568
0
                    .state
569
0
                    .value(&sval_ref::to_ref(&sval::Null), |stream, _| stream.null())
570
0
            },
571
        )
572
0
    }
573
574
0
    fn bool(&mut self, value: bool) -> sval::Result {
575
0
        self.buffer_or_stream_with(
576
0
            |buf| buf.bool(value),
577
0
            |stream| {
578
0
                stream
579
0
                    .state
580
0
                    .value_computed(&value, |stream, value| stream.bool(*value))
581
0
            },
582
        )
583
0
    }
584
585
0
    fn u8(&mut self, value: u8) -> sval::Result {
586
0
        self.buffer_or_stream_with(
587
0
            |buf| buf.u8(value),
588
0
            |stream| {
589
0
                stream
590
0
                    .state
591
0
                    .value_computed(&value, |stream, value| stream.u8(*value))
592
0
            },
593
        )
594
0
    }
595
596
0
    fn u16(&mut self, value: u16) -> sval::Result {
597
0
        self.buffer_or_stream_with(
598
0
            |buf| buf.u16(value),
599
0
            |stream| {
600
0
                stream
601
0
                    .state
602
0
                    .value_computed(&value, |stream, value| stream.u16(*value))
603
0
            },
604
        )
605
0
    }
606
607
0
    fn u32(&mut self, value: u32) -> sval::Result {
608
0
        self.buffer_or_stream_with(
609
0
            |buf| buf.u32(value),
610
0
            |stream| {
611
0
                stream
612
0
                    .state
613
0
                    .value_computed(&value, |stream, value| stream.u32(*value))
614
0
            },
615
        )
616
0
    }
617
618
0
    fn u64(&mut self, value: u64) -> sval::Result {
619
0
        self.buffer_or_stream_with(
620
0
            |buf| buf.u64(value),
621
0
            |stream| {
622
0
                stream
623
0
                    .state
624
0
                    .value_computed(&value, |stream, value| stream.u64(*value))
625
0
            },
626
        )
627
0
    }
628
629
0
    fn u128(&mut self, value: u128) -> sval::Result {
630
0
        self.buffer_or_stream_with(
631
0
            |buf| buf.u128(value),
632
0
            |stream| {
633
0
                stream
634
0
                    .state
635
0
                    .value_computed(&value, |stream, value| stream.u128(*value))
636
0
            },
637
        )
638
0
    }
639
640
0
    fn i8(&mut self, value: i8) -> sval::Result {
641
0
        self.buffer_or_stream_with(
642
0
            |buf| buf.i8(value),
643
0
            |stream| {
644
0
                stream
645
0
                    .state
646
0
                    .value_computed(&value, |stream, value| stream.i8(*value))
647
0
            },
648
        )
649
0
    }
650
651
0
    fn i16(&mut self, value: i16) -> sval::Result {
652
0
        self.buffer_or_stream_with(
653
0
            |buf| buf.i16(value),
654
0
            |stream| {
655
0
                stream
656
0
                    .state
657
0
                    .value_computed(&value, |stream, value| stream.i16(*value))
658
0
            },
659
        )
660
0
    }
661
662
0
    fn i32(&mut self, value: i32) -> sval::Result {
663
0
        self.buffer_or_stream_with(
664
0
            |buf| buf.i32(value),
665
0
            |stream| {
666
0
                stream
667
0
                    .state
668
0
                    .value_computed(&value, |stream, value| stream.i32(*value))
669
0
            },
670
        )
671
0
    }
672
673
0
    fn i64(&mut self, value: i64) -> sval::Result {
674
0
        self.buffer_or_stream_with(
675
0
            |buf| buf.i64(value),
676
0
            |stream| {
677
0
                stream
678
0
                    .state
679
0
                    .value_computed(&value, |stream, value| stream.i64(*value))
680
0
            },
681
        )
682
0
    }
683
684
0
    fn i128(&mut self, value: i128) -> sval::Result {
685
0
        self.buffer_or_stream_with(
686
0
            |buf| buf.i128(value),
687
0
            |stream| {
688
0
                stream
689
0
                    .state
690
0
                    .value_computed(&value, |stream, value| stream.i128(*value))
691
0
            },
692
        )
693
0
    }
694
695
0
    fn f32(&mut self, value: f32) -> sval::Result {
696
0
        self.buffer_or_stream_with(
697
0
            |buf| buf.f32(value),
698
0
            |stream| {
699
0
                stream
700
0
                    .state
701
0
                    .value_computed(&value, |stream, value| stream.f32(*value))
702
0
            },
703
        )
704
0
    }
705
706
0
    fn f64(&mut self, value: f64) -> sval::Result {
707
0
        self.buffer_or_stream_with(
708
0
            |buf| buf.f64(value),
709
0
            |stream| {
710
0
                stream
711
0
                    .state
712
0
                    .value_computed(&value, |stream, value| stream.f64(*value))
713
0
            },
714
        )
715
0
    }
716
717
0
    fn text_begin(&mut self, size_hint: Option<usize>) -> sval::Result {
718
0
        self.buffer_or_with(
719
0
            |buf| buf.text_begin(size_hint),
720
0
            |stream| stream.put_buffer(Buffered::Text(TextBuf::new())),
721
        )
722
0
    }
723
724
0
    fn text_fragment(&mut self, fragment: &'sval str) -> sval::Result {
725
0
        self.buffer_or_with(
726
0
            |buf| buf.text_fragment(fragment),
727
0
            |stream| stream.with_text(|text| text.push_fragment(fragment).map_err(Error::buffer)),
728
        )
729
0
    }
730
731
0
    fn text_fragment_computed(&mut self, fragment: &str) -> sval::Result {
732
0
        self.buffer_or_with(
733
0
            |buf| buf.text_fragment_computed(fragment),
734
0
            |stream| {
735
0
                stream
736
0
                    .with_text(|text| text.push_fragment_computed(fragment).map_err(Error::buffer))
737
0
            },
738
        )
739
0
    }
740
741
0
    fn text_end(&mut self) -> sval::Result {
742
0
        self.buffer_or_stream_with(
743
0
            |buf| buf.text_end(),
744
0
            |stream| {
745
0
                let buf = stream.take_text()?;
746
747
0
                if let Some(text) = buf.as_borrowed_str() {
748
0
                    stream.state.value(&sval_ref::to_ref(text), |stream, text| {
749
0
                        stream.text(text.into_inner())
750
0
                    })
751
                } else {
752
0
                    stream
753
0
                        .state
754
0
                        .value_computed(buf.as_str(), |stream, text| stream.text_computed(text))
755
                }
756
0
            },
757
        )
758
0
    }
759
760
0
    fn binary_begin(&mut self, size_hint: Option<usize>) -> sval::Result {
761
0
        self.buffer_or_with(
762
0
            |buf| buf.binary_begin(size_hint),
763
0
            |stream| stream.put_buffer(Buffered::Binary(BinaryBuf::new())),
764
        )
765
0
    }
766
767
0
    fn binary_fragment(&mut self, fragment: &'sval [u8]) -> sval::Result {
768
0
        self.buffer_or_with(
769
0
            |buf| buf.binary_fragment(fragment),
770
0
            |stream| {
771
0
                stream.with_binary(|binary| binary.push_fragment(fragment).map_err(Error::buffer))
772
0
            },
773
        )
774
0
    }
775
776
0
    fn binary_fragment_computed(&mut self, fragment: &[u8]) -> sval::Result {
777
0
        self.buffer_or_with(
778
0
            |buf| buf.binary_fragment_computed(fragment),
779
0
            |stream| {
780
0
                stream.with_binary(|binary| {
781
0
                    binary
782
0
                        .push_fragment_computed(fragment)
783
0
                        .map_err(Error::buffer)
784
0
                })
785
0
            },
786
        )
787
0
    }
788
789
0
    fn binary_end(&mut self) -> sval::Result {
790
0
        self.buffer_or_stream_with(
791
0
            |buf| buf.binary_end(),
792
0
            |stream| {
793
0
                let buf = stream.take_binary()?;
794
795
0
                if let Some(binary) = buf.as_borrowed_slice() {
796
0
                    stream.state.value(
797
0
                        &sval_ref::to_ref(sval::BinarySlice::new(binary)),
798
0
                        |stream, binary| stream.binary(binary.into_inner().as_slice()),
799
                    )
800
                } else {
801
0
                    stream
802
0
                        .state
803
0
                        .value_computed(sval::BinarySlice::new(buf.as_slice()), |stream, binary| {
804
0
                            stream.binary_computed(binary.as_slice())
805
0
                        })
806
                }
807
0
            },
808
        )
809
0
    }
810
}
811
812
0
fn try_catch<'sval, T, S: Stream<'sval>>(
813
0
    stream: &mut FlatStream<'sval, S>,
814
0
    f: impl FnOnce(&mut FlatStream<'sval, S>) -> Result<T>,
815
0
) -> sval::Result<T> {
816
0
    match f(stream) {
817
0
        Ok(v) => Ok(v),
818
0
        Err(e) => {
819
0
            stream.state = State::Done(Some(Err(e)));
820
821
0
            sval::error()
822
        }
823
    }
824
0
}
825
826
impl<'sval, S: Stream<'sval>> State<'sval, S> {
827
0
    fn value<V: sval_ref::ValueRef<'sval> + ?Sized>(
828
0
        &mut self,
829
0
        value: &V,
830
0
        any: impl FnOnce(S, &V) -> Result<S::Ok>,
831
0
    ) -> Result<Option<S::Ok>> {
832
0
        self.value_with(
833
0
            |stream| any(stream, value),
834
0
            |stream, tag, label, index| stream.tagged(tag, label, index, value),
835
0
            |stream| StreamSeq::value(stream, value),
836
0
            |stream| StreamMap::key(stream, value),
837
0
            |stream| StreamMap::value(stream, value),
838
0
            |stream, tag, index| stream.value(tag, index, value),
839
0
            |stream, tag, label| stream.value(tag, label, value),
840
0
            |stream, tag, label, index| stream.tagged(tag, label, index, value),
841
0
            |stream, tag, index| stream.value(tag, index, value),
842
0
            |stream, tag, label| stream.value(tag, label, value),
843
        )
844
0
    }
845
846
0
    fn value_computed<V: sval::Value + ?Sized>(
847
0
        &mut self,
848
0
        value: &V,
849
0
        any: impl FnOnce(S, &V) -> Result<S::Ok>,
850
0
    ) -> Result<Option<S::Ok>> {
851
0
        self.value_with(
852
0
            |stream| any(stream, value),
853
0
            |stream, tag, label, index| stream.tagged_computed(tag, label, index, value),
854
0
            |stream| StreamSeq::value_computed(stream, value),
855
0
            |stream| StreamMap::key_computed(stream, value),
856
0
            |stream| StreamMap::value_computed(stream, value),
857
0
            |stream, tag, index| stream.value_computed(tag, index, value),
858
0
            |stream, tag, label| stream.value_computed(tag, label, value),
859
0
            |stream, tag, label, index| stream.tagged_computed(tag, label, index, value),
860
0
            |stream, tag, index| stream.value_computed(tag, index, value),
861
0
            |stream, tag, label| stream.value_computed(tag, label, value),
862
        )
863
0
    }
864
865
0
    fn value_with(
866
0
        &mut self,
867
0
        any: impl FnOnce(S) -> Result<S::Ok>,
868
0
        tagged: impl FnOnce(
869
0
            S,
870
0
            Option<sval::Tag>,
871
0
            Option<sval::Label>,
872
0
            Option<sval::Index>,
873
0
        ) -> Result<S::Ok>,
874
0
        seq: impl FnOnce(&mut S::Seq) -> Result,
875
0
        map_key: impl FnOnce(&mut S::Map) -> Result,
876
0
        map_value: impl FnOnce(&mut S::Map) -> Result,
877
0
        tuple: impl FnOnce(&mut S::Tuple, Option<sval::Tag>, sval::Index) -> Result,
878
0
        record: impl FnOnce(&mut S::Record, Option<sval::Tag>, sval::Label) -> Result,
879
0
        tagged_variant: impl FnOnce(
880
0
            FlatStreamEnum<S::Enum>,
881
0
            Option<sval::Tag>,
882
0
            Option<sval::Label>,
883
0
            Option<sval::Index>,
884
0
        ) -> Result<S::Ok>,
885
0
        tuple_variant: impl FnOnce(
886
0
            &mut <S::Enum as StreamEnum<'sval>>::Tuple,
887
0
            Option<sval::Tag>,
888
0
            sval::Index,
889
0
        ) -> Result,
890
0
        record_variant: impl FnOnce(
891
0
            &mut <S::Enum as StreamEnum<'sval>>::Record,
892
0
            Option<sval::Tag>,
893
0
            sval::Label,
894
0
        ) -> Result,
895
0
    ) -> Result<Option<S::Ok>> {
896
0
        match self {
897
0
            State::Any(ref mut stream) => {
898
0
                let stream = stream.take().ok_or_else(|| {
899
0
                    Error::invalid_value("cannot stream value; the stream is already completed")
900
0
                })?;
901
902
0
                Ok(Some(any(stream.stream)?))
903
            }
904
0
            State::Tagged(ref mut stream) => {
905
0
                let stream = stream.take().ok_or_else(|| {
906
0
                    Error::invalid_value(
907
                        "cannot stream tagged value; the stream is already completed",
908
                    )
909
0
                })?;
910
911
0
                Ok(Some(tagged(
912
0
                    stream.stream,
913
0
                    stream.tag,
914
0
                    stream.label,
915
0
                    stream.index,
916
0
                )?))
917
            }
918
0
            State::Seq(stream) => {
919
0
                let stream = stream.as_mut().ok_or_else(|| {
920
0
                    Error::invalid_value(
921
                        "cannot stream a sequence; the stream is already completed",
922
                    )
923
0
                })?;
924
925
0
                seq(&mut stream.stream)?;
926
927
0
                Ok(None)
928
            }
929
0
            State::Map(stream) => {
930
0
                let stream = stream.as_mut().ok_or_else(|| {
931
0
                    Error::invalid_value("cannot stream a map; the stream is already completed")
932
0
                })?;
933
934
0
                if stream.is_key {
935
0
                    map_key(&mut stream.stream)?;
936
                } else {
937
0
                    map_value(&mut stream.stream)?;
938
                }
939
940
0
                Ok(None)
941
            }
942
0
            State::Tuple(stream) => {
943
0
                let stream = stream.as_mut().ok_or_else(|| {
944
0
                    Error::invalid_value("cannot stream a tuple; the stream is already completed")
945
0
                })?;
946
947
0
                let (tag, index) = stream.field.take().ok_or_else(|| {
948
0
                    Error::invalid_value("cannot stream a tuple; the field index is missing")
949
0
                })?;
950
951
0
                tuple(&mut stream.stream, tag, index)?;
952
953
0
                Ok(None)
954
            }
955
0
            State::Record(stream) => {
956
0
                let stream = stream.as_mut().ok_or_else(|| {
957
0
                    Error::invalid_value("cannot stream a record; the stream is already completed")
958
0
                })?;
959
960
0
                let (tag, label) = stream.field.take().ok_or_else(|| {
961
0
                    Error::invalid_value("cannot stream a record; the field label is missing")
962
0
                })?;
963
964
0
                record(&mut stream.stream, tag, label)?;
965
966
0
                Ok(None)
967
            }
968
0
            State::Enum(_) => Err(Error::invalid_value(
969
0
                "cannot stream an enum; the stream is in an invalid state",
970
0
            )),
971
0
            State::EnumVariant(stream) => match stream {
972
                Some(EnumVariant::Tuple(Tuple {
973
0
                    ref mut stream,
974
0
                    ref mut field,
975
                })) => {
976
0
                    let (tag, index) = field.take().ok_or_else(|| {
977
0
                        Error::invalid_value(
978
                            "cannot stream a tuple variant; the field index is missing",
979
                        )
980
0
                    })?;
981
982
0
                    tuple_variant(stream, tag, index)?;
983
984
0
                    Ok(None)
985
                }
986
                Some(EnumVariant::Record(Record {
987
0
                    ref mut stream,
988
0
                    ref mut field,
989
                })) => {
990
0
                    let (tag, label) = field.take().ok_or_else(|| {
991
0
                        Error::invalid_value(
992
                            "cannot stream a record variant; the field label is missing",
993
                        )
994
0
                    })?;
995
996
0
                    record_variant(stream, tag, label)?;
997
998
0
                    Ok(None)
999
                }
1000
0
                stream => {
1001
0
                    match stream.take().ok_or_else(|| {
1002
0
                        Error::invalid_value(
1003
                            "cannot stream an enum variant; the stream is already completed",
1004
                        )
1005
0
                    })? {
1006
                        EnumVariant::Tagged(Tagged {
1007
0
                            stream,
1008
0
                            tag,
1009
0
                            label,
1010
0
                            index,
1011
0
                        }) => Ok(Some(tagged_variant(stream, tag, label, index)?)),
1012
0
                        _ => unreachable!(),
1013
                    }
1014
                }
1015
            },
1016
0
            State::Done(_) => Err(Error::invalid_value(
1017
0
                "cannot stream a value; the stream is already completed",
1018
0
            )),
1019
        }
1020
0
    }
1021
}
1022
1023
impl<'sval, S: Stream<'sval>> FlatStream<'sval, S> {
1024
0
    fn buffer_or_stream_with(
1025
0
        &mut self,
1026
0
        buffer: impl FnOnce(&mut ValueBuf<'sval>) -> sval::Result,
1027
0
        stream: impl FnOnce(&mut Self) -> Result<Option<S::Ok>>,
1028
0
    ) -> sval::Result {
1029
0
        let mut r = None;
1030
0
        self.buffer_or_with(buffer, |s| match stream(s) {
1031
0
            Ok(ok) => {
1032
0
                r = ok;
1033
0
                Ok(())
1034
            }
1035
0
            Err(e) => Err(e),
1036
0
        })?;
1037
1038
0
        if let Some(ok) = r {
1039
0
            self.state = State::Done(Some(Ok(ok)));
1040
0
        }
1041
1042
0
        Ok(())
1043
0
    }
1044
1045
0
    fn buffer_or_with(
1046
0
        &mut self,
1047
0
        buffer: impl FnOnce(&mut ValueBuf<'sval>) -> sval::Result,
1048
0
        stream: impl FnOnce(&mut Self) -> Result,
1049
0
    ) -> sval::Result {
1050
0
        let r = try_catch(self, |s: &mut FlatStream<'_, S>| match s {
1051
            FlatStream {
1052
0
                buffered: Some(Buffered::Value(ref mut buf)),
1053
                ..
1054
            } => {
1055
0
                if buffer(buf).is_err() {
1056
0
                    let buf = mem::take(buf);
1057
1058
0
                    Err(buf.into_err().map(Error::buffer).unwrap_or_else(|| {
1059
0
                        Error::invalid_value("the value itself failed to stream")
1060
0
                    }))
1061
                } else {
1062
0
                    Ok(())
1063
                }
1064
            }
1065
0
            s => stream(s),
1066
0
        });
1067
1068
0
        r
1069
0
    }
1070
1071
0
    fn buffer_or_begin_with(
1072
0
        &mut self,
1073
0
        mut buffer: impl FnMut(&mut ValueBuf<'sval>) -> sval::Result,
1074
0
        transition_any: impl FnOnce(Any<'sval, S>) -> Result<State<'sval, S>>,
1075
0
        transition_enum: impl FnOnce(Enum<FlatStreamEnum<S::Enum>>) -> Result<State<'sval, S>>,
1076
0
    ) -> sval::Result {
1077
0
        let new_buf = try_catch(self, |stream| match stream {
1078
            FlatStream {
1079
0
                buffered: Some(Buffered::Value(ref mut buf)),
1080
                state: _,
1081
            } => {
1082
0
                if buffer(buf).is_err() {
1083
0
                    let buf = mem::take(buf);
1084
1085
0
                    return Err(buf.into_err().map(Error::buffer).unwrap_or_else(|| {
1086
0
                        Error::invalid_value("the value itself failed to stream")
1087
0
                    }));
1088
0
                }
1089
1090
0
                Ok(None)
1091
            }
1092
            FlatStream {
1093
                buffered: None,
1094
0
                state: State::Any(state),
1095
            } => {
1096
0
                stream.state = transition_any(state.take().ok_or_else(|| {
1097
0
                    Error::invalid_value("cannot stream value; the stream is already completed")
1098
0
                })?)?;
1099
1100
0
                Ok(None)
1101
            }
1102
            FlatStream {
1103
                buffered: None,
1104
0
                state: State::Enum(state),
1105
            } => {
1106
0
                stream.state = transition_enum(state.take().ok_or_else(|| {
1107
0
                    Error::invalid_value(
1108
                        "cannot stream enum value; the stream is already completed",
1109
                    )
1110
0
                })?)?;
1111
1112
0
                Ok(None)
1113
            }
1114
            FlatStream {
1115
                buffered: None,
1116
                state: _,
1117
            } => {
1118
0
                let mut buf = ValueBuf::new();
1119
0
                if buffer(&mut buf).is_err() {
1120
0
                    return Err(buf.into_err().map(Error::buffer).unwrap_or_else(|| {
1121
0
                        Error::invalid_value("the value itself failed to stream")
1122
0
                    }));
1123
0
                }
1124
1125
0
                Ok(Some(Buffered::Value(buf)))
1126
            }
1127
0
            _ => Err(Error::invalid_value(
1128
0
                "cannot begin buffering; the stream is in an invalid state",
1129
0
            )),
1130
0
        })?;
1131
1132
0
        if let Some(new_buf) = new_buf {
1133
0
            self.buffered = Some(new_buf);
1134
0
        }
1135
1136
0
        Ok(())
1137
0
    }
1138
1139
0
    fn buffer_or_end_with(
1140
0
        &mut self,
1141
0
        buffer: impl FnOnce(&mut ValueBuf<'sval>) -> sval::Result,
1142
0
        transition: impl FnOnce(&mut Self) -> Result<S::Ok>,
1143
0
    ) -> sval::Result {
1144
0
        let r = try_catch(self, |stream| match stream {
1145
0
            FlatStream { buffered: None, .. } => Ok(Some(transition(stream)?)),
1146
0
            FlatStream { buffered, .. } => {
1147
0
                let buf = if let Some(Buffered::Value(ref mut buf)) = buffered {
1148
0
                    buf
1149
                } else {
1150
0
                    return Err(Error::invalid_value(
1151
0
                        "cannot end buffering value; the stream is in an invalid state",
1152
0
                    ));
1153
                };
1154
1155
0
                if buffer(buf).is_err() {
1156
0
                    let buf = mem::take(buf);
1157
1158
0
                    return Err(buf.into_err().map(Error::buffer).unwrap_or_else(|| {
1159
0
                        Error::invalid_value("the value itself failed to stream")
1160
0
                    }));
1161
0
                }
1162
1163
0
                if buf.is_complete() {
1164
0
                    let buf = mem::take(buf);
1165
0
                    *buffered = None;
1166
1167
0
                    return stream
1168
0
                        .state
1169
0
                        .value_computed(&buf, |stream, value| stream.value_computed(value));
1170
0
                }
1171
1172
0
                return Ok(None);
1173
            }
1174
0
        })?;
1175
1176
0
        if let Some(r) = r {
1177
0
            self.state = State::Done(Some(Ok(r)));
1178
0
        }
1179
1180
0
        Ok(())
1181
0
    }
1182
1183
0
    fn put_buffer(&mut self, buf: Buffered<'sval>) -> Result {
1184
0
        match self.buffered {
1185
            None => {
1186
0
                self.buffered = Some(buf);
1187
1188
0
                Ok(())
1189
            }
1190
0
            Some(_) => Err(Error::invalid_value(
1191
0
                "cannot begin buffering; a buffer is already active",
1192
0
            )),
1193
        }
1194
0
    }
1195
1196
0
    fn with_text(&mut self, buffer: impl FnOnce(&mut TextBuf<'sval>) -> Result) -> Result {
1197
0
        match self.buffered {
1198
0
            Some(Buffered::Text(ref mut buf)) => buffer(buf),
1199
0
            _ => Err(Error::invalid_value(
1200
0
                "cannot buffer text; no active text buffer",
1201
0
            )),
1202
        }
1203
0
    }
1204
1205
0
    fn take_text(&mut self) -> Result<TextBuf<'sval>> {
1206
0
        match self.buffered {
1207
0
            Some(Buffered::Text(ref mut buf)) => {
1208
0
                let buf = mem::take(buf);
1209
0
                self.buffered = None;
1210
1211
0
                Ok(buf)
1212
            }
1213
0
            _ => Err(Error::invalid_value(
1214
0
                "cannot end buffering text; no active text buffer",
1215
0
            )),
1216
        }
1217
0
    }
1218
1219
0
    fn with_binary(&mut self, buffer: impl FnOnce(&mut BinaryBuf<'sval>) -> Result) -> Result {
1220
0
        match self.buffered {
1221
0
            Some(Buffered::Binary(ref mut buf)) => buffer(buf),
1222
0
            _ => Err(Error::invalid_value(
1223
0
                "cannot buffer binary; no active binary buffer",
1224
0
            )),
1225
        }
1226
0
    }
1227
1228
0
    fn take_binary(&mut self) -> Result<BinaryBuf<'sval>> {
1229
0
        match self.buffered {
1230
0
            Some(Buffered::Binary(ref mut buf)) => {
1231
0
                let buf = mem::take(buf);
1232
0
                self.buffered = None;
1233
1234
0
                Ok(buf)
1235
            }
1236
0
            _ => Err(Error::invalid_value(
1237
0
                "cannot end buffering binary; no active binary buffer",
1238
0
            )),
1239
        }
1240
0
    }
1241
1242
0
    fn take_seq(&mut self) -> Result<Seq<S::Seq>> {
1243
0
        match self {
1244
            FlatStream {
1245
                buffered: None,
1246
0
                state: State::Seq(seq),
1247
0
            } => seq.take().ok_or_else(|| {
1248
0
                Error::invalid_value("cannot end a sequence; the stream is already completed")
1249
0
            }),
1250
0
            _ => Err(Error::invalid_value(
1251
0
                "cannot end a sequence; invalid stream state",
1252
0
            )),
1253
        }
1254
0
    }
1255
1256
0
    fn with_map(&mut self, f: impl FnOnce(&mut Map<S::Map>) -> Result) -> Result {
1257
0
        match self {
1258
            FlatStream {
1259
                buffered: None,
1260
0
                state: State::Map(Some(map)),
1261
0
            } => f(map),
1262
0
            _ => Err(Error::invalid_value(
1263
0
                "cannot stream a map; invalid stream state",
1264
0
            )),
1265
        }
1266
0
    }
1267
1268
0
    fn take_map(&mut self) -> Result<Map<S::Map>> {
1269
0
        match self {
1270
            FlatStream {
1271
                buffered: None,
1272
0
                state: State::Map(map),
1273
0
            } => map.take().ok_or_else(|| {
1274
0
                Error::invalid_value("cannot end a map; the stream is already completed")
1275
0
            }),
1276
0
            _ => Err(Error::invalid_value(
1277
0
                "cannot end a map; invalid stream state",
1278
0
            )),
1279
        }
1280
0
    }
1281
1282
0
    fn with_tuple(
1283
0
        &mut self,
1284
0
        tuple: impl FnOnce(&mut Tuple<S::Tuple>) -> Result,
1285
0
        tuple_variant: impl FnOnce(&mut Tuple<<S::Enum as StreamEnum<'sval>>::Tuple>) -> Result,
1286
0
    ) -> Result {
1287
0
        match self {
1288
            FlatStream {
1289
                buffered: None,
1290
0
                state: State::Tuple(Some(stream)),
1291
0
            } => tuple(stream),
1292
            FlatStream {
1293
                buffered: None,
1294
0
                state: State::EnumVariant(Some(EnumVariant::Tuple(stream))),
1295
0
            } => tuple_variant(stream),
1296
0
            _ => Err(Error::invalid_value(
1297
0
                "cannot stream a tuple; invalid stream state",
1298
0
            )),
1299
        }
1300
0
    }
1301
1302
0
    fn take_with_tuple<T>(
1303
0
        &mut self,
1304
0
        tuple: impl FnOnce(Tuple<S::Tuple>) -> Result<T>,
1305
0
        tuple_variant: impl FnOnce(Tuple<<S::Enum as StreamEnum<'sval>>::Tuple>) -> Result<T>,
1306
0
    ) -> Result<T> {
1307
0
        match self {
1308
            FlatStream {
1309
                buffered: None,
1310
0
                state: State::Tuple(stream),
1311
0
            } => tuple(stream.take().ok_or_else(|| {
1312
0
                Error::invalid_value("cannot end a tuple; the stream is already completed")
1313
0
            })?),
1314
            FlatStream {
1315
                buffered: None,
1316
0
                state: State::EnumVariant(stream),
1317
            } => {
1318
0
                let stream = stream.take().ok_or_else(|| {
1319
0
                    Error::invalid_value("cannot end a tuple; the stream is already completed")
1320
0
                })?;
1321
1322
0
                match stream {
1323
0
                    EnumVariant::Tuple(stream) => tuple_variant(stream),
1324
0
                    _ => Err(Error::invalid_value(
1325
0
                        "cannot end a tuple; invalid stream state",
1326
0
                    )),
1327
                }
1328
            }
1329
0
            _ => Err(Error::invalid_value(
1330
0
                "cannot end a tuple; invalid stream state",
1331
0
            )),
1332
        }
1333
0
    }
1334
1335
0
    fn with_record(
1336
0
        &mut self,
1337
0
        record: impl FnOnce(&mut Record<S::Record>) -> Result,
1338
0
        record_variant: impl FnOnce(&mut Record<<S::Enum as StreamEnum<'sval>>::Record>) -> Result,
1339
0
    ) -> Result {
1340
0
        match self {
1341
            FlatStream {
1342
                buffered: None,
1343
0
                state: State::Record(Some(stream)),
1344
0
            } => record(stream),
1345
            FlatStream {
1346
                buffered: None,
1347
0
                state: State::EnumVariant(Some(EnumVariant::Record(stream))),
1348
0
            } => record_variant(stream),
1349
0
            _ => Err(Error::invalid_value(
1350
0
                "cannot stream a record; invalid stream state",
1351
0
            )),
1352
        }
1353
0
    }
1354
1355
0
    fn take_with_record<T>(
1356
0
        &mut self,
1357
0
        record: impl FnOnce(Record<S::Record>) -> Result<T>,
1358
0
        record_variant: impl FnOnce(Record<<S::Enum as StreamEnum<'sval>>::Record>) -> Result<T>,
1359
0
    ) -> Result<T> {
1360
0
        match self {
1361
            FlatStream {
1362
                buffered: None,
1363
0
                state: State::Record(stream),
1364
0
            } => record(stream.take().ok_or_else(|| {
1365
0
                Error::invalid_value("cannot end a record; the stream is already completed")
1366
0
            })?),
1367
            FlatStream {
1368
                buffered: None,
1369
0
                state: State::EnumVariant(stream),
1370
            } => {
1371
0
                let stream = stream.take().ok_or_else(|| {
1372
0
                    Error::invalid_value("cannot end a record; the stream is already completed")
1373
0
                })?;
1374
1375
0
                match stream {
1376
0
                    EnumVariant::Record(stream) => record_variant(stream),
1377
0
                    _ => Err(Error::invalid_value(
1378
0
                        "cannot end a record; invalid stream state",
1379
0
                    )),
1380
                }
1381
            }
1382
0
            _ => Err(Error::invalid_value(
1383
0
                "cannot end a record; invalid stream state",
1384
0
            )),
1385
        }
1386
0
    }
1387
1388
0
    fn take_enum(&mut self) -> Result<Option<Enum<FlatStreamEnum<S::Enum>>>> {
1389
0
        match self {
1390
            FlatStream {
1391
                buffered: None,
1392
0
                state: State::Enum(variant),
1393
0
            } => Ok(variant.take()),
1394
            FlatStream {
1395
                buffered: None,
1396
                state: State::Done(_),
1397
0
            } => Ok(None),
1398
0
            _ => Err(Error::invalid_value(
1399
0
                "cannot end an enum; invalid stream state",
1400
0
            )),
1401
        }
1402
0
    }
1403
}
1404
1405
struct Tag<'a>(
1406
    Option<&'a sval::Tag>,
1407
    Option<&'a sval::Label<'a>>,
1408
    Option<&'a sval::Index>,
1409
);
1410
1411
impl<'a> sval::Value for Tag<'a> {
1412
0
    fn stream<'sval, S: sval::Stream<'sval> + ?Sized>(&'sval self, stream: &mut S) -> sval::Result {
1413
0
        stream.tag(self.0, self.1, self.2)
1414
0
    }
1415
}