harmos-sdk 0.1.8

Harmos SDK for authoring plugins and operations over the Harmos runtime.
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

// Protobuf From And To The Canonical Postcard Value, Driven By The Described Shape.
//
// Every message crosses process boundaries as its Postcard value; the protobuf
// form packaging publishes in `.proto` files is a projection of the same
// described fields. Fields are written in tag order, required scalars equal to
// their default are omitted, numeric repeated fields are packed, and a message
// whose fields are all variants is a oneof whose Postcard form is the variant
// index followed by that variant's fields. Floats are default only at positive
// zero, so negative zero keeps its sign across the wire.

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports

// Third Party Imports
use prost::encoding::{WireType, encode_varint};
// Local Imports
use super::input::{DecodeField, DecodeInput, DecodeScalar, DecodeValue};
use harmos_domain::__private::{PostcardReader as Reader, PostcardValue, decoding_add, decoding_multiply};
use harmos_domain::MessageError;
use harmos_domain::schema::Cardinality;
use harmos_domain::schema::declaration::{Definition, Field, Kind};

/* === Constants === */

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

struct Output<'a, 'payload> {
    bytes: Option<&'a mut Vec<u8>>,
    length: usize,
    retained: usize,
    admit: Option<&'a mut dyn FnMut(usize) -> Result<(), MessageError>>,
    spans: Vec<FieldSpan<'payload>>,
    parameter: Option<BytesParameter<'a>>,
}

#[derive(Clone, Copy)]
struct BytesParameter<'a> {
    path: &'a [&'a str],
    cursor: Option<usize>,
    length: usize,
    matched: usize,
}

struct FieldSpan<'payload> {
    index: usize,
    input: Reader<'payload>,
}

/* === Enums === */

enum Number {
    Varint(u64),
    Bits32(u32),
    Bits64(u64),
}

/* === Traits === */

/* === Implementations === */

impl<'a, 'payload> Output<'a, 'payload> {
    fn counting() -> Self {
        Self {
            bytes: None,
            length: 0,
            retained: 0,
            admit: None,
            spans: Vec::new(),
            parameter: None,
        }
    }

    fn child_counter(&self) -> Self {
        let parameter = self.parameter.map(|mut parameter| {
            parameter.matched = 0;
            parameter
        });
        Self { parameter, ..Self::counting() }
    }

    fn enter(&mut self, field: &Field) -> Result<Option<usize>, MessageError> {
        let Some(parameter) = &mut self.parameter else {
            return Ok(None);
        };
        let previous = parameter.cursor;
        let selected = previous.is_some_and(|index| parameter.path.get(index).copied() == Some(field.name.as_ref()));
        parameter.cursor = previous.filter(|_| selected).map(|index| index + 1);
        if selected && field.cardinality == Cardinality::Repeated {
            return Err(MessageError::borrowed("a bytes parameter cannot select a repeated field"));
        }
        if parameter.cursor == Some(parameter.path.len()) && !matches!(field.kind, Kind::Bytes) {
            return Err(MessageError::borrowed("a bytes parameter must select a bytes field"));
        }
        Ok(previous)
    }

    fn leave(&mut self, previous: Option<usize>) {
        if let Some(parameter) = &mut self.parameter {
            parameter.cursor = previous;
        }
    }

    fn bytes_length(&mut self, actual: usize) -> Result<usize, MessageError> {
        let Some(parameter) = &mut self.parameter else {
            return Ok(actual);
        };
        if parameter.cursor != Some(parameter.path.len()) {
            return Ok(actual);
        }
        parameter.matched = decoding_add(parameter.matched, 1)?;
        Ok(parameter.length)
    }

    fn reserve_spans(&mut self, additional: usize) -> Result<(), MessageError> {
        let required = decoding_add(self.spans.len(), additional)?;
        if required <= self.spans.capacity() {
            return Ok(());
        }
        let retained = decoding_multiply(self.spans.capacity(), std::mem::size_of::<FieldSpan<'_>>())?;
        let replacement = decoding_multiply(required, std::mem::size_of::<FieldSpan<'_>>())?;
        let peak = decoding_add(self.retained, decoding_add(retained, replacement)?)?;
        if let Some(admit) = &mut self.admit {
            admit(peak)?;
        }
        self.spans.try_reserve_exact(additional).map_err(|_| MessageError::borrowed("protobuf field index allocation failed"))?;
        let retained = decoding_multiply(self.spans.capacity(), std::mem::size_of::<FieldSpan<'_>>())?;
        if let Some(admit) = &mut self.admit {
            admit(decoding_add(self.retained, retained)?)?;
        }
        Ok(())
    }

    fn append(&mut self, value: &[u8]) -> Result<(), MessageError> {
        self.advance(value.len())?;
        if let Some(bytes) = &mut self.bytes {
            bytes.extend_from_slice(value);
        }
        Ok(())
    }

    fn advance(&mut self, length: usize) -> Result<(), MessageError> {
        self.length = self.length.checked_add(length).ok_or_else(|| MessageError::borrowed("protobuf encoding size overflows"))?;
        Ok(())
    }

    fn varint(&mut self, value: u64) -> Result<(), MessageError> {
        let mut bytes = [0; 10];
        let mut remaining = &mut bytes[..];
        encode_varint(value, &mut remaining);
        let length = 10 - remaining.len();
        self.append(&bytes[..length])
    }

    fn key(&mut self, tag: u32, wire: WireType) -> Result<(), MessageError> {
        self.varint((u64::from(tag) << 3) | wire as u64)
    }

    fn header(&mut self, tag: u32, length: usize) -> Result<(), MessageError> {
        self.key(tag, WireType::LengthDelimited)?;
        self.varint(length as u64)
    }
}

impl Number {
    fn is_default(&self) -> bool {
        match self {
            Self::Varint(value) | Self::Bits64(value) => *value == 0,
            Self::Bits32(value) => *value == 0,
        }
    }

    fn write(&self, output: &mut Output<'_, '_>) -> Result<(), MessageError> {
        match self {
            Self::Varint(value) => output.varint(*value),
            Self::Bits32(value) => output.append(&value.to_le_bytes()),
            Self::Bits64(value) => output.append(&value.to_le_bytes()),
        }
    }
}

/* === Public Functions === */

pub fn encode_admitted(
    definition: &Definition, postcard: &[u8], bytes: &mut Vec<u8>, retained: usize,
    admit: &mut dyn FnMut(usize) -> Result<(), MessageError>,
) -> Result<(), MessageError> {
    let mut input = Reader::new(postcard);
    let mut output = Output {
        bytes: Some(bytes),
        length: 0,
        retained,
        admit: Some(admit),
        spans: Vec::new(),
        parameter: None,
    };
    message(definition, &mut input, &mut output)?;
    input.finish()
}

pub fn encoded_len(definition: &Definition, postcard: &[u8]) -> Result<usize, MessageError> {
    let mut input = Reader::new(postcard);
    let mut output = Output::counting();
    message(definition, &mut input, &mut output)?;
    input.finish()?;
    Ok(output.length)
}

pub fn parameterized_len(definition: &Definition, postcard: &[u8], path: &[&str], length: usize) -> Result<usize, MessageError> {
    if path.is_empty() {
        return Err(MessageError::borrowed("a bytes parameter requires a field path"));
    }
    let mut input = Reader::new(postcard);
    let parameter = BytesParameter {
        path,
        cursor: Some(0),
        length,
        matched: 0,
    };
    let mut output = Output {
        parameter: Some(parameter),
        ..Output::counting()
    };
    message(definition, &mut input, &mut output)?;
    input.finish()?;
    if output.parameter.is_none_or(|parameter| parameter.matched != 1) {
        return Err(MessageError::borrowed("bytes parameter does not select one present field"));
    }
    Ok(output.length)
}

pub fn decode(definition: &Definition, input: DecodeInput<'_>) -> Result<Vec<u8>, MessageError> {
    // Postcard Rarely Outgrows Its Protobuf Source, So The Bound Stays Near The Payload.
    let mut bytes = Vec::with_capacity(input.encoded_len()?);
    decode_message(definition, input, &mut bytes)?;
    Ok(bytes)
}

/* === Private Functions === */

fn oneof(fields: &[Field]) -> bool {
    !fields.is_empty() && fields.iter().all(|field| field.cardinality == Cardinality::Variant)
}

fn payload(field: &Field) -> Result<&Definition, MessageError> {
    match &field.kind {
        Kind::Message { definition } => Ok(definition),
        _ => Err(MessageError::borrowed("a oneof variant carries a message payload")),
    }
}

/* --- Postcard To Protobuf --- */

fn message<'payload>(definition: &Definition, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    if oneof(fields) {
        let index: u32 = input.value()?;
        let field = usize::try_from(index)
            .ok()
            .and_then(|index| fields.get(index))
            .ok_or_else(|| MessageError::borrowed("unknown Postcard enum variant"))?;
        let definition = payload(field)?;
        let previous = bytes.enter(field)?;
        let result = delimited(field.tag, input, bytes, |input, bytes| members(definition, input, bytes));
        bytes.leave(previous);
        return result;
    }
    members(definition, input, bytes)
}

// Postcard Holds Fields In Declaration Order; Protobuf Writes Them By Tag.
fn members<'payload>(definition: &Definition, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    if bytes.bytes.is_none() || fields.windows(2).all(|pair| pair[0].tag < pair[1].tag) {
        for field in fields {
            member(field, input, bytes)?;
        }
        return Ok(());
    }
    // One Admitted Index Retains Parent Spans And Reuses Child Workspace.
    bytes.reserve_spans(fields.len())?;
    let start = bytes.spans.len();
    for (index, field) in fields.iter().enumerate() {
        bytes.spans.push(FieldSpan { index, input: input.clone() });
        member(field, input, &mut Output::counting())?;
    }
    let end = bytes.spans.len();
    bytes.spans[start..end].sort_unstable_by_key(|span| (fields[span.index].tag, span.index));
    for position in start..end {
        let span = &bytes.spans[position];
        let field = &fields[span.index];
        let mut cursor = span.input.clone();
        member(field, &mut cursor, bytes)?;
    }
    bytes.spans.truncate(start);
    Ok(())
}

fn member<'payload>(field: &Field, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>) -> Result<(), MessageError> {
    let previous = bytes.enter(field)?;
    let result = member_value(field, input, bytes);
    bytes.leave(previous);
    result
}

fn member_value<'payload>(field: &Field, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>) -> Result<(), MessageError> {
    match (field.cardinality, &field.kind) {
        (Cardinality::Required, Kind::Message { definition }) => nested(field.tag, definition, input, bytes),
        (Cardinality::Required, kind) => scalar(field.tag, kind, input, bytes, true),
        (Cardinality::Optional, kind) => {
            if !input.value::<bool>()? {
                return Ok(());
            }
            match kind {
                Kind::Message { definition } => nested(field.tag, definition, input, bytes),
                kind => scalar(field.tag, kind, input, bytes, false),
            }
        }
        (Cardinality::Repeated, kind) => {
            let count: usize = input.value()?;
            match kind {
                Kind::Message { definition } => {
                    for _ in 0..count {
                        nested(field.tag, definition, input, bytes)?;
                    }
                }
                Kind::String | Kind::Bytes => {
                    for _ in 0..count {
                        scalar(field.tag, kind, input, bytes, false)?;
                    }
                }
                kind if count > 0 => {
                    delimited(field.tag, input, bytes, |input, bytes| {
                        for _ in 0..count {
                            number(kind, input)?.write(bytes)?;
                        }
                        Ok(())
                    })?;
                }
                _ => {}
            }
            Ok(())
        }
        (Cardinality::Variant, _) => Err(MessageError::borrowed("a variant field belongs to a oneof")),
    }
}

fn nested<'payload>(
    tag: u32, definition: &Definition, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>,
) -> Result<(), MessageError> {
    delimited(tag, input, bytes, |input, bytes| message(definition, input, bytes))
}

fn scalar<'payload>(
    tag: u32, kind: &Kind, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>, omit_default: bool,
) -> Result<(), MessageError> {
    match kind {
        Kind::String => {
            let text: &str = input.value()?;
            if !(omit_default && text.is_empty()) {
                bytes.header(tag, text.len())?;
                bytes.append(text.as_bytes())?;
            }
        }
        Kind::Bytes => {
            let length: usize = input.value()?;
            let value = input.take(length)?;
            let length = bytes.bytes_length(value.len())?;
            if !(omit_default && length == 0) {
                bytes.header(tag, length)?;
                if bytes.bytes.is_none() {
                    bytes.advance(length)?;
                } else {
                    bytes.append(value)?;
                }
            }
        }
        kind => {
            let value = number(kind, input)?;
            if !value.is_default() || !omit_default {
                bytes.key(tag, wire(kind))?;
                value.write(bytes)?;
            }
        }
    }
    Ok(())
}

fn number(kind: &Kind, input: &mut Reader<'_>) -> Result<Number, MessageError> {
    Ok(match kind {
        Kind::Bool => {
            let value: bool = input.value()?;
            Number::Varint(u64::from(value))
        }
        Kind::U32 => {
            let value: u32 = input.value()?;
            Number::Varint(u64::from(value))
        }
        Kind::U64 => {
            let value: u64 = input.value()?;
            Number::Varint(value)
        }
        Kind::I32 => {
            let value: i32 = input.value()?;
            Number::Varint(i64::from(value) as u64)
        }
        Kind::I64 => {
            let value: i64 = input.value()?;
            Number::Varint(value as u64)
        }
        Kind::F32 => {
            let value: f32 = input.value()?;
            Number::Bits32(value.to_bits())
        }
        Kind::F64 => {
            let value: f64 = input.value()?;
            Number::Bits64(value.to_bits())
        }
        Kind::String | Kind::Bytes | Kind::Message { .. } => {
            return Err(MessageError::borrowed("expected a numeric protobuf field"));
        }
    })
}

fn wire(kind: &Kind) -> WireType {
    match kind {
        Kind::F32 => WireType::ThirtyTwoBit,
        Kind::F64 => WireType::SixtyFourBit,
        Kind::String | Kind::Bytes | Kind::Message { .. } => WireType::LengthDelimited,
        _ => WireType::Varint,
    }
}

fn delimited<'payload>(
    tag: u32, input: &mut Reader<'payload>, bytes: &mut Output<'_, 'payload>,
    write: impl Fn(&mut Reader<'payload>, &mut Output<'_, 'payload>) -> Result<(), MessageError>,
) -> Result<(), MessageError> {
    let mut end = input.clone();
    let mut count = bytes.child_counter();
    write(&mut end, &mut count)?;
    if let Some(parameter) = &mut bytes.parameter {
        parameter.matched = decoding_add(parameter.matched, count.parameter.map_or(0, |parameter| parameter.matched))?;
    }
    bytes.header(tag, count.length)?;
    if bytes.bytes.is_none() {
        bytes.advance(count.length)?;
        *input = end;
        return Ok(());
    }
    write(input, bytes)
}

/* --- Protobuf To Postcard --- */

fn decode_message(definition: &Definition, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    if !oneof(fields) {
        return decode_members(definition, input, bytes);
    }
    let tags = fields.iter().map(|field| field.tag).collect::<Vec<_>>();
    let (index, selected) = match input.variant(&tags)? {
        Some((tag, selected)) => (tags.iter().position(|known| *known == tag).expect("selected declared variant"), selected),
        None => (definition.default_variant.ok_or_else(|| MessageError::borrowed("missing oneof variant"))?, input.child(&[])?),
    };
    let field = fields.get(index).ok_or_else(|| MessageError::borrowed("default variant lies outside the oneof"))?;
    u32::try_from(index).map_err(|_| MessageError::borrowed("oneof variant index overflows"))?.write(bytes);
    decode_members(payload(field)?, selected, bytes)
}

fn decode_members(definition: &Definition, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let fields = (definition.fields)();
    // Check Every Occurrence Of A Known Field Before Any Is Selected
    input.fields(&mut |occurrence| {
        if let Some(field) = fields.iter().find(|field| field.tag == occurrence.tag) {
            check(field, occurrence)?;
        }
        Ok(())
    })?;
    for field in fields {
        decode_member(field, input, bytes)?;
    }
    Ok(())
}

fn check(field: &Field, occurrence: DecodeField<'_>) -> Result<(), MessageError> {
    match (&field.kind, field.cardinality) {
        (Kind::String, _) => occurrence.text().map(drop),
        (Kind::Bytes | Kind::Message { .. }, _) => occurrence.bytes().map(drop),
        (kind, Cardinality::Repeated) => occurrence.scalars(decode_scalar(kind), &mut |_| Ok(())),
        (kind, _) => occurrence.scalar(decode_scalar(kind)).map(drop),
    }
}

fn decode_member(field: &Field, input: DecodeInput<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    let tag = field.tag;
    match (field.cardinality, &field.kind) {
        (Cardinality::Required, Kind::Message { definition }) => decode_message(definition, input.field(tag)?, bytes),
        (Cardinality::Optional, Kind::Message { definition }) => {
            let present = last(input, tag)?.is_some();
            present.write(bytes);
            if present {
                decode_message(definition, input.field(tag)?, bytes)?;
            }
            Ok(())
        }
        (Cardinality::Repeated, Kind::Message { definition }) => {
            count(input, tag)?.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    decode_message(definition, input.child(occurrence.bytes()?)?, bytes)?;
                }
                Ok(())
            })
        }
        (Cardinality::Required, kind) => match last(input, tag)? {
            Some(occurrence) => decode_scalar_value(kind, occurrence, bytes),
            None => default(kind, bytes),
        },
        (Cardinality::Optional, kind) => {
            let occurrence = last(input, tag)?;
            occurrence.is_some().write(bytes);
            occurrence.map_or(Ok(()), |occurrence| decode_scalar_value(kind, occurrence, bytes))
        }
        (Cardinality::Repeated, kind @ (Kind::String | Kind::Bytes)) => {
            count(input, tag)?.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    decode_scalar_value(kind, occurrence, bytes)?;
                }
                Ok(())
            })
        }
        (Cardinality::Repeated, kind) => {
            let scalar = decode_scalar(kind);
            let mut values = 0usize;
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    occurrence.scalars(scalar, &mut |_| {
                        values += 1;
                        Ok(())
                    })?;
                }
                Ok(())
            })?;
            values.write(bytes);
            input.fields(&mut |occurrence| {
                if occurrence.tag == tag {
                    occurrence.scalars(scalar, &mut |value| write_number(kind, value, bytes))?;
                }
                Ok(())
            })
        }
        (Cardinality::Variant, _) => Err(MessageError::borrowed("a variant field belongs to a oneof")),
    }
}

fn last<'a>(input: DecodeInput<'a>, tag: u32) -> Result<Option<DecodeField<'a>>, MessageError> {
    let mut selected = None;
    input.fields(&mut |occurrence| {
        if occurrence.tag == tag {
            selected = Some(occurrence);
        }
        Ok(())
    })?;
    Ok(selected)
}

fn count(input: DecodeInput<'_>, tag: u32) -> Result<usize, MessageError> {
    let mut count = 0usize;
    input.fields(&mut |occurrence| {
        if occurrence.tag == tag {
            count += 1;
        }
        Ok(())
    })?;
    Ok(count)
}

fn decode_scalar(kind: &Kind) -> DecodeScalar {
    match kind {
        Kind::F32 => DecodeScalar::Bits32,
        Kind::F64 => DecodeScalar::Bits64,
        _ => DecodeScalar::Varint,
    }
}

fn decode_scalar_value(kind: &Kind, occurrence: DecodeField<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match kind {
        Kind::String => {
            let text = occurrence.text()?;
            text.len().write(bytes);
            bytes.extend_from_slice(text.as_bytes());
            Ok(())
        }
        Kind::Bytes => {
            let value = occurrence.bytes()?;
            value.len().write(bytes);
            bytes.extend_from_slice(value);
            Ok(())
        }
        kind => write_number(kind, occurrence.scalar(decode_scalar(kind))?, bytes),
    }
}

fn write_number(kind: &Kind, value: DecodeValue<'_>, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match (kind, value) {
        (Kind::Bool, DecodeValue::Varint(value)) => (value != 0).write(bytes),
        (Kind::U32, DecodeValue::Varint(value)) => (value as u32).write(bytes),
        (Kind::U64, DecodeValue::Varint(value)) => value.write(bytes),
        (Kind::I32, DecodeValue::Varint(value)) => (value as i32).write(bytes),
        (Kind::I64, DecodeValue::Varint(value)) => (value as i64).write(bytes),
        (Kind::F32, DecodeValue::Bits32(value)) => f32::from_bits(value).write(bytes),
        (Kind::F64, DecodeValue::Bits64(value)) => f64::from_bits(value).write(bytes),
        _ => return Err(MessageError::borrowed("protobuf scalar wire type mismatch")),
    }
    Ok(())
}

fn default(kind: &Kind, bytes: &mut Vec<u8>) -> Result<(), MessageError> {
    match kind {
        Kind::Bool => false.write(bytes),
        Kind::U32 => 0u32.write(bytes),
        Kind::U64 => 0u64.write(bytes),
        Kind::I32 => 0i32.write(bytes),
        Kind::I64 => 0i64.write(bytes),
        Kind::F32 => 0f32.write(bytes),
        Kind::F64 => 0f64.write(bytes),
        Kind::String | Kind::Bytes => 0usize.write(bytes),
        Kind::Message { .. } => {
            return Err(MessageError::borrowed("expected a scalar protobuf field"));
        }
    }
    Ok(())
}

/* === Tests === */

#[cfg(test)]
mod tests {
    use super::*;
    use std::borrow::Cow;

    #[harmos_domain::message(harmos_path = ::harmos_domain)]
    struct Leaf {
        value: i64,
        negative_zero: f64,
    }

    #[harmos_domain::message(harmos_path = ::harmos_domain)]
    struct Row {
        leaf: Leaf,
        values: Vec<u64>,
        optional: Option<u32>,
    }

    #[harmos_domain::message(harmos_path = ::harmos_domain)]
    enum Choice {
        Empty,
        Row(Row),
    }

    #[harmos_domain::message(harmos_path = ::harmos_domain)]
    struct Wide {
        alpha: u32,
        bravo: Row,
        charlie: u64,
        delta: Choice,
        echo: Vec<Row>,
        foxtrot: Option<Row>,
        golf: String,
        hotel: Vec<u64>,
        india: Vec<u8>,
        juliet: Leaf,
        kilo: bool,
        lima: i64,
    }

    #[test]
    fn counted_nested_packed_and_variant_encoding() {
        use harmos_domain::{Message, schema::Described};
        let value = Choice::Row(Row {
            leaf: Leaf { value: -1, negative_zero: -0.0 },
            values: vec![0, 127, 128, u64::MAX],
            optional: Some(0),
        });
        let postcard = value.encode();
        let expected = encoded_len(&Choice::DEFINITION, &postcard).unwrap();
        let mut bytes = Vec::with_capacity(expected);
        encode_admitted(&Choice::DEFINITION, &postcard, &mut bytes, 0, &mut |_| Ok(())).unwrap();
        assert_eq!(bytes.len(), expected);
        assert_eq!(decode(&Choice::DEFINITION, DecodeInput::new(&bytes)).unwrap(), postcard);
        let mut truncated = postcard.clone();
        truncated.pop();
        assert!(encoded_len(&Choice::DEFINITION, &truncated).is_err());
        let mut trailing = postcard;
        trailing.push(0);
        assert!(encoded_len(&Choice::DEFINITION, &trailing).is_err());
    }

    #[test]
    fn declaration_order_is_projected_in_tag_order() {
        use harmos_domain::schema::Described;
        let definition = Definition { fields: || FIELDS, ..Leaf::DEFINITION };
        const FIELDS: &[Field] = &[
            Field {
                name: Cow::Borrowed("later"),
                tag: 3,
                kind: Kind::U32,
                cardinality: Cardinality::Required,
            },
            Field {
                name: Cow::Borrowed("first"),
                tag: 1,
                kind: Kind::U32,
                cardinality: Cardinality::Optional,
            },
            Field {
                name: Cow::Borrowed("middle"),
                tag: 2,
                kind: Kind::U32,
                cardinality: Cardinality::Repeated,
            },
        ];
        let postcard = postcard::to_allocvec(&(7u32, Some(0u32), vec![127u32, 128])).unwrap();
        let mut bytes = vec![0xaa];
        encode_admitted(&definition, &postcard, &mut bytes, 0, &mut |_| Ok(())).unwrap();
        assert_eq!(bytes, [0xaa, 0x08, 0, 0x12, 3, 127, 128, 1, 0x18, 7]);
        assert_eq!(encoded_len(&definition, &postcard).unwrap(), bytes.len() - 1);
        assert_eq!(decode(&definition, DecodeInput::new(&bytes[1..])).unwrap(), postcard);
    }

    #[test]
    fn hashed_field_index_is_admitted_and_preserves_nested_spans() {
        use harmos_domain::{Message, schema::Described};
        let row = |seed| Row {
            leaf: Leaf {
                value: -(seed as i64),
                negative_zero: -0.0,
            },
            values: vec![seed; 64],
            optional: Some(0),
        };
        let value = Wide {
            alpha: 17,
            bravo: row(1),
            charlie: u64::MAX,
            delta: Choice::Row(row(2)),
            echo: vec![row(3), row(4), row(5)],
            foxtrot: Some(row(6)),
            golf: "field spans".into(),
            hotel: vec![0, 127, 128, u64::MAX],
            india: vec![0xff; 4096],
            juliet: Leaf { value: -1, negative_zero: -0.0 },
            kilo: true,
            lima: i64::MIN,
        };
        assert!(Wide::FIELDS.windows(2).any(|pair| pair[0].tag > pair[1].tag));
        let postcard = value.encode();
        let length = encoded_len(&Wide::DEFINITION, &postcard).unwrap();
        let mut bytes = Vec::with_capacity(length);
        let baseline = postcard.capacity() + bytes.capacity();

        let refused = crate::grpc::encode_admitted(&value, &mut bytes, usize::MAX, &mut |required| {
            if required > baseline { Err(MessageError::borrowed("fixture index refusal")) } else { Ok(()) }
        });
        assert!(refused.is_err());
        assert!(bytes.is_empty());

        let mut claims = Vec::new();
        crate::grpc::encode_admitted(&value, &mut bytes, usize::MAX, &mut |required| {
            claims.push(required);
            Ok(())
        })
        .unwrap();
        assert_eq!(bytes.len(), length);
        assert!(claims.iter().any(|required| *required > baseline));
        assert_eq!(claims.last(), Some(&bytes.capacity()));
        assert_eq!(decode(&Wide::DEFINITION, DecodeInput::new(&bytes)).unwrap(), postcard);

        let mut tags = Vec::new();
        DecodeInput::new(&bytes)
            .fields(&mut |field| {
                tags.push(field.tag);
                Ok(())
            })
            .unwrap();
        let mut expected = Wide::FIELDS
            .iter()
            .flat_map(|field| std::iter::repeat_n(field.tag, if field.name == "echo" { value.echo.len() } else { 1 }))
            .collect::<Vec<_>>();
        expected.sort_unstable();
        assert_eq!(tags, expected);
    }

    #[test]
    fn index_growth_refusal_retains_existing_backing() {
        use std::cell::Cell;
        let ceiling = Cell::new(usize::MAX);
        let retained = Cell::new(0);
        let mut admit = |required| {
            if required > ceiling.get() {
                return Err(MessageError::borrowed("fixture arena growth refusal"));
            }
            retained.set(required);
            Ok(())
        };
        let mut output = Output::counting();
        output.retained = 23;
        output.admit = Some(&mut admit);
        output.reserve_spans(4).unwrap();
        let capacity = output.spans.capacity();
        let pointer = output.spans.as_ptr();
        let claimed = retained.get();

        // The Final Arena Fits; Replacing Its Still-Live Backing Does Not.
        let requested = capacity + 1;
        ceiling.set(23 + requested * std::mem::size_of::<FieldSpan<'_>>());
        assert!(output.reserve_spans(requested).is_err());
        assert_eq!(output.spans.capacity(), capacity);
        assert_eq!(output.spans.as_ptr(), pointer);
        assert_eq!(retained.get(), claimed);

        ceiling.set(usize::MAX);
        output.reserve_spans(requested).unwrap();
        assert!(output.spans.capacity() >= requested);
        assert_eq!(retained.get(), 23 + output.spans.capacity() * std::mem::size_of::<FieldSpan<'_>>());
    }
}