qubit-json 0.12.1

Resource-aware infrastructure for lenient and strict JSON processing
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
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Compound state for single-pass budget-aware JSON encoding.

use std::cell::RefCell;

use qubit_budget::ResourceQuantity;
use qubit_budget::json::JsonContainerKind;
use qubit_budget::json::JsonMeasurement;
use serde::Serialize;
use serde::ser::Error;
use serde::ser::SerializeMap;
use serde::ser::SerializeSeq;
use serde::ser::SerializeStruct;
use serde::ser::SerializeStructVariant;
use serde::ser::SerializeTuple;
use serde::ser::SerializeTupleStruct;
use serde::ser::SerializeTupleVariant;

use super::BudgetedKey;
use super::BudgetedPrivateValue;
use super::BudgetedValue;
use super::JsonEncodeContext;
use crate::encode::JsonSerializationErrorKind;
use crate::encode::JsonSerializerStateError;
use crate::internal::JsonPrivateProtocolKind;
use crate::internal::JsonPrivateProtocolState;
use crate::internal::SERDE_JSON_RAW_VALUE_TOKEN;

/// Wraps a Serde compound serializer and checks container operations before
/// delegating them.
///
/// # Type Parameters
///
/// * `C` - Underlying Serde compound serializer.
/// * `R` - Resource identifier used by the shared encode budget.
/// * `Q` - Quantity representation used by the shared encode budget.
/// * `VALUE_LIMITS` - Whether value-level limits are enforced.
pub(in crate::encode) struct JsonEncodeCompound<'transaction, 'budget, 'context, C, R, Q, const VALUE_LIMITS: bool>
where
    Q: ResourceQuantity,
{
    /// Underlying Serde compound serializer.
    inner: C,

    /// Shared mutable budget state for the serialization traversal.
    context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,

    /// Root-inclusive depth assigned to nested values.
    child_depth: usize,

    /// Number of sequence items or map entries observed so far.
    observed: usize,

    /// Whether the serializer declared this compound empty.
    declared_empty: bool,

    /// Whether a map key has been accepted without its corresponding value.
    map_key_pending: bool,

    /// Private serde_json encoding represented by this compound.
    private_kind: Option<JsonPrivateProtocolKind>,

    /// Validated private protocol lifecycle, when this is a private struct.
    private_state: Option<JsonPrivateProtocolState>,
}

impl<'transaction, 'budget, 'context, C, R, Q, const VALUE_LIMITS: bool>
    JsonEncodeCompound<'transaction, 'budget, 'context, C, R, Q, VALUE_LIMITS>
where
    R: Clone,
    Q: ResourceQuantity,
{
    /// Creates a wrapper for a regular JSON array or object compound.
    ///
    /// # Parameters
    ///
    /// * `inner` - Serde compound serializer that receives admitted values.
    /// * `context` - Shared mutable state for this serialization traversal.
    /// * `child_depth` - Root-inclusive depth assigned to child values.
    /// * `declared_empty` - Whether the serializer declared zero items or
    ///   fields for this compound.
    ///
    /// # Returns
    ///
    /// A compound wrapper with regular JSON protocol state initialized.
    #[must_use]
    #[inline]
    pub(super) const fn new(
        inner: C,
        context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
        child_depth: usize,
        declared_empty: bool,
    ) -> Self {
        Self {
            inner,
            context,
            child_depth,
            observed: 0,
            declared_empty,
            map_key_pending: false,
            private_kind: None,
            private_state: None,
        }
    }

    /// Creates a wrapper for serde_json's private raw-value compound.
    ///
    /// # Parameters
    ///
    /// * `inner` - Serde struct serializer for the private compound.
    /// * `context` - Shared mutable state for this serialization traversal.
    /// * `depth` - Root-inclusive depth assigned to the private payload.
    /// * `kind` - Private serde_json protocol represented by the compound.
    /// * `state` - Validated lifecycle state for the private protocol.
    ///
    /// # Returns
    ///
    /// A compound wrapper initialized for the selected private protocol.
    #[must_use]
    pub(super) const fn private(
        inner: C,
        context: &'context RefCell<JsonEncodeContext<'transaction, 'budget, R, Q>>,
        depth: usize,
        kind: JsonPrivateProtocolKind,
        state: JsonPrivateProtocolState,
    ) -> Self {
        Self {
            inner,
            context,
            child_depth: depth,
            observed: 0,
            declared_empty: false,
            map_key_pending: false,
            private_kind: Some(kind),
            private_state: Some(state),
        }
    }

    /// Checks the next observed sequence element.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Serde error type used to report count failures.
    ///
    /// # Returns
    ///
    /// `Ok(())` when limits are disabled or the next element is admitted.
    ///
    /// # Errors
    ///
    /// Returns `E` when the count overflows or the sequence limit is exceeded.
    #[inline(always)]
    fn next_sequence<E>(&mut self) -> Result<(), E>
    where
        E: Error,
    {
        if !VALUE_LIMITS {
            return Ok(());
        }
        let next = self
            .observed
            .checked_add(1)
            .ok_or_else(|| E::custom("JSON sequence item count overflowed usize"))?;
        self.context
            .borrow_mut()
            .check_container_count(JsonContainerKind::Sequence, next)?;
        self.observed = next;
        Ok(())
    }

    /// Checks the next observed map or struct entry.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Serde error type used to report count failures.
    ///
    /// # Returns
    ///
    /// `Ok(())` when limits are disabled or the next entry is admitted.
    ///
    /// # Errors
    ///
    /// Returns `E` when the count overflows or the map limit is exceeded.
    #[inline(always)]
    fn next_map_entry<E>(&mut self) -> Result<(), E>
    where
        E: Error,
    {
        if !VALUE_LIMITS {
            return Ok(());
        }
        let next = self
            .observed
            .checked_add(1)
            .ok_or_else(|| E::custom("JSON map entry count overflowed usize"))?;
        self.context
            .borrow_mut()
            .check_container_count(JsonContainerKind::Map, next)?;
        self.observed = next;
        Ok(())
    }

    /// Rejects completion when a map key has no matching value.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Serde error type used to report an incomplete map entry.
    ///
    /// # Returns
    ///
    /// `Ok(())` when no key is pending.
    ///
    /// # Errors
    ///
    /// Returns `E` when a key was serialized without its corresponding value.
    fn finish_map<E>(&mut self) -> Result<(), E>
    where
        E: Error,
    {
        if self.map_key_pending {
            return Err(self.serialization_error(JsonSerializerStateError::MapEndedWithPendingKey));
        }
        Ok(())
    }

    /// Records and returns one invalid map serializer state.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Serde error type constructed from the recorded state error.
    ///
    /// # Parameters
    ///
    /// * `reason` - Invalid map state to preserve in the serialization error.
    ///
    /// # Returns
    ///
    /// An `E` containing the recorded invalid-state reason.
    fn serialization_error<E>(&self, reason: JsonSerializerStateError) -> E
    where
        E: Error,
    {
        self.context
            .borrow_mut()
            .serialization_error(JsonSerializationErrorKind::InvalidSerializerState { reason })
    }

    /// Rejects a callback for a compound declared with zero items or fields.
    ///
    /// # Returns
    ///
    /// `Ok(())` when the compound declaration permits callbacks.
    ///
    /// # Errors
    ///
    /// Returns a Serde error containing
    /// [`JsonSerializerStateError::UnexpectedCompound`] when the compound
    /// declared no items or fields.
    #[inline(always)]
    fn reject_empty_callback<E>(&self) -> Result<(), E>
    where
        E: Error,
    {
        if self.declared_empty {
            return Err(self.serialization_error(JsonSerializerStateError::UnexpectedCompound));
        }
        Ok(())
    }

    /// Records the current private protocol's stable failure.
    ///
    /// # Type Parameters
    ///
    /// * `E` - Serde error type constructed from the protocol error.
    ///
    /// # Returns
    ///
    /// An `E` containing the private protocol's stable invalid-state reason.
    ///
    /// # Panics
    ///
    /// Panics if called for a regular compound without a private protocol kind.
    fn private_protocol_error<E>(&self) -> E
    where
        E: Error,
    {
        let kind = self.private_kind.expect("private protocol kind must exist");
        self.context
            .borrow_mut()
            .serialization_error(JsonSerializationErrorKind::InvalidSerializerState {
                reason: kind.invalid_state(),
            })
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeSeq for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeSeq,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Checks the observed count, then serializes one decorated child value.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized as the next sequence element.
    ///
    /// # Parameters
    ///
    /// * `value` - Element whose serialization is budgeted before delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the underlying serializer accepts the element.
    ///
    /// # Errors
    ///
    /// Returns the serializer error for an exceeded count or rejected element.
    #[inline(always)]
    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.next_sequence()?;
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_element(&value)
    }

    /// Completes the underlying sequence.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed sequence result.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying serializer cannot finish the
    /// sequence.
    #[inline(always)]
    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeTuple for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeTuple,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Serializes one decorated tuple element.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized as the next tuple element.
    ///
    /// # Parameters
    ///
    /// * `value` - Element whose serialization is budgeted before delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the underlying serializer accepts the element.
    ///
    /// # Errors
    ///
    /// Returns the serializer error for an exceeded count or rejected element.
    #[inline(always)]
    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.next_sequence()?;
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_element(&value)
    }

    /// Completes the underlying tuple.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed tuple result.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying serializer cannot finish the tuple.
    #[inline(always)]
    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeTupleStruct for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeTupleStruct,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Serializes one decorated tuple-struct field.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized as the next tuple-struct field.
    ///
    /// # Parameters
    ///
    /// * `value` - Field whose serialization is budgeted before delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the underlying serializer accepts the field.
    ///
    /// # Errors
    ///
    /// Returns the serializer error for an exceeded count or rejected field.
    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.next_sequence()?;
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_field(&value)
    }

    /// Completes the underlying tuple struct.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed tuple-struct result.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying serializer cannot finish the tuple
    /// struct.
    #[inline(always)]
    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeTupleVariant for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeTupleVariant,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Serializes one decorated tuple-variant field.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized as the next tuple-variant field.
    ///
    /// # Parameters
    ///
    /// * `value` - Field whose serialization is budgeted before delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the underlying serializer accepts the field.
    ///
    /// # Errors
    ///
    /// Returns the serializer error for an exceeded count or rejected field.
    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.next_sequence()?;
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_field(&value)
    }

    /// Completes the underlying tuple variant.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed tuple-variant result.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying serializer cannot finish the tuple
    /// variant.
    #[inline(always)]
    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeMap for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeMap,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Checks the entry count and key before delegating key serialization.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized as the map key.
    ///
    /// # Parameters
    ///
    /// * `key` - Key whose count and serialization are checked before
    ///   delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` and marks the map as awaiting a value after key acceptance.
    ///
    /// # Errors
    ///
    /// Returns an error when a key is already pending, the count is rejected,
    /// or the underlying serializer rejects the key.
    fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        if self.map_key_pending {
            return Err(self.serialization_error(JsonSerializerStateError::MapKeyAlreadyPending));
        }
        self.next_map_entry()?;
        let key = BudgetedKey::<_, _, _, VALUE_LIMITS>::new(key, self.context);
        self.inner.serialize_key(&key)?;
        self.map_key_pending = true;
        Ok(())
    }

    /// Serializes one map value through a child budget decorator.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized for the pending map key.
    ///
    /// # Parameters
    ///
    /// * `value` - Value whose serialization is budgeted before delegation.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the underlying serializer accepts the value and clears
    /// the pending-key state.
    ///
    /// # Errors
    ///
    /// Returns an error when no key is pending or serialization fails.
    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        if !self.map_key_pending {
            return Err(self.serialization_error(JsonSerializerStateError::MapValueWithoutKey));
        }
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_value(&value)?;
        self.map_key_pending = false;
        Ok(())
    }

    /// Checks and serializes one complete map entry.
    ///
    /// # Type Parameters
    ///
    /// * `K` - Value serialized as the key.
    /// * `V` - Value serialized as the entry value.
    ///
    /// # Parameters
    ///
    /// * `key` - Key to serialize before the entry value.
    /// * `value` - Value serialized after the key is accepted.
    ///
    /// # Returns
    ///
    /// `Ok(())` after both parts are accepted by the underlying serializer.
    ///
    /// # Errors
    ///
    /// Returns an error when the count, key, or value is rejected.
    fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
    where
        K: Serialize + ?Sized,
        V: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.serialize_key(key)?;
        self.serialize_value(value)
    }

    /// Completes the underlying map.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed map result.
    ///
    /// # Errors
    ///
    /// Returns an error if a key has no matching value or the serializer fails.
    #[inline(always)]
    fn end(mut self) -> Result<Self::Ok, Self::Error> {
        self.finish_map()?;
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeStruct for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeStruct,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Checks one field key and serializes its decorated value.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized for this struct field.
    ///
    /// # Parameters
    ///
    /// * `key` - Static field name admitted against the key budget.
    /// * `value` - Field value serialized through its child budget decorator.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the field is accepted by the underlying serializer.
    ///
    /// # Errors
    ///
    /// Returns an error for an invalid private protocol, exceeded field count,
    /// rejected key/value, or underlying serializer failure.
    #[inline(always)]
    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        if let Some(kind) = self.private_kind {
            let state = self.private_state.as_mut().expect("private state must exist");
            if state.begin(key).is_err() {
                return Err(self.private_protocol_error());
            }
            let payload = match kind {
                JsonPrivateProtocolKind::RawValue => {
                    BudgetedPrivateValue::raw_value(value, self.context, self.child_depth)
                }
                JsonPrivateProtocolKind::Number => BudgetedPrivateValue::number(value, self.context, self.child_depth),
            };
            let inner_key = if kind == JsonPrivateProtocolKind::Number {
                SERDE_JSON_RAW_VALUE_TOKEN
            } else {
                key
            };
            if let Err(error) = self.inner.serialize_field(inner_key, &payload) {
                self.private_state.as_mut().expect("private state must exist").fail();
                let _: Self::Error = self.private_protocol_error();
                return Err(error);
            }
            if self
                .private_state
                .as_mut()
                .expect("private state must exist")
                .complete()
                .is_err()
            {
                return Err(self.private_protocol_error());
            }
            return Ok(());
        }
        self.next_map_entry()?;
        if VALUE_LIMITS {
            self.context
                .borrow_mut()
                .admit(JsonMeasurement::Key { bytes: key.len() })?;
        }
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_field(key, &value)
    }

    /// Skips one field exactly as the underlying serializer requests.
    ///
    /// # Parameters
    ///
    /// * `key` - Static field name the underlying serializer elects to skip.
    ///
    /// # Returns
    ///
    /// The underlying serializer's skip result.
    ///
    /// # Errors
    ///
    /// Returns an error if a private protocol field is skipped or the
    /// underlying serializer rejects the operation.
    #[inline(always)]
    fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
        if let Some(state) = self.private_state.as_mut() {
            state.fail();
            return Err(self.private_protocol_error());
        }
        self.inner.skip_field(key)
    }

    /// Completes the underlying struct.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed struct result.
    ///
    /// # Errors
    ///
    /// Returns an error for an incomplete private protocol, pending map key,
    /// or underlying serializer failure.
    #[inline(always)]
    fn end(mut self) -> Result<Self::Ok, Self::Error> {
        if let Some(state) = self.private_state.take() {
            if state.finish().is_err() {
                return Err(self.private_protocol_error());
            }
        } else {
            self.finish_map()?;
        }
        self.inner.end()
    }
}

impl<C, R, Q, const VALUE_LIMITS: bool> SerializeStructVariant for JsonEncodeCompound<'_, '_, '_, C, R, Q, VALUE_LIMITS>
where
    C: SerializeStructVariant,
    R: Clone,
    Q: ResourceQuantity,
{
    type Ok = C::Ok;
    type Error = C::Error;

    /// Checks one field key and serializes its decorated value.
    ///
    /// # Type Parameters
    ///
    /// * `T` - Value serialized for this struct-variant field.
    ///
    /// # Parameters
    ///
    /// * `key` - Static field name admitted against the key budget.
    /// * `value` - Field value serialized through its child budget decorator.
    ///
    /// # Returns
    ///
    /// `Ok(())` after the field is accepted by the underlying serializer.
    ///
    /// # Errors
    ///
    /// Returns an error for an exceeded field count, rejected key/value, or
    /// underlying serializer failure.
    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
    where
        T: Serialize + ?Sized,
    {
        self.reject_empty_callback()?;
        self.next_map_entry()?;
        if VALUE_LIMITS {
            self.context
                .borrow_mut()
                .admit(JsonMeasurement::Key { bytes: key.len() })?;
        }
        let value = BudgetedValue::<_, _, _, VALUE_LIMITS>::new(value, self.context, self.child_depth);
        self.inner.serialize_field(key, &value)
    }

    /// Skips one variant field exactly as the underlying serializer requests.
    ///
    /// # Parameters
    ///
    /// * `key` - Static field name the underlying serializer elects to skip.
    ///
    /// # Returns
    ///
    /// The underlying serializer's skip result.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying serializer rejects the operation.
    #[inline(always)]
    fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
        self.inner.skip_field(key)
    }

    /// Completes the underlying struct variant.
    ///
    /// # Returns
    ///
    /// The underlying serializer's completed struct-variant result.
    ///
    /// # Errors
    ///
    /// Returns an error for a pending map key or underlying serializer failure.
    #[inline(always)]
    fn end(mut self) -> Result<Self::Ok, Self::Error> {
        self.finish_map()?;
        self.inner.end()
    }
}