def encode.go

bend-kit-protobuf@0.2.0.0 · protobuf.bend:410

import bend-kit-protobuf@0.2.0.0/protobuf.bend as Protobuf
import Base
import bend-kit-bytes@0.3.3.0/bytes.bend as B

410# Depth is the first decreasing argument for a group; list tails decrease the second.
411def encode.go(+depth: Nat, fields: List<&1, Field>, out: B.Bytes, +max_size: U32) -> Result<&2, &1, Error, B.Bytes>:
412  match depth:
413    case 0n:
414      match fields:
415        case Nil{}:
416          Done{out}
417        case Con{Field{number, value}, tail}:
418          match value:
419            case Group{inner}:
420              Fail{Limit{}}
421            case Varint{w}:
422              do Result<&2, &1, Error, B.Bytes>:
423                next : B.Bytes <- write_field(number, Varint{w}, out, max_size)
424                encode.go(0n, tail, next, max_size)
425            case Fixed32{w}:
426              do Result<&2, &1, Error, B.Bytes>:
427                next : B.Bytes <- write_field(number, Fixed32{w}, out, max_size)
428                encode.go(0n, tail, next, max_size)
429            case Fixed64{w}:
430              do Result<&2, &1, Error, B.Bytes>:
431                next : B.Bytes <- write_field(number, Fixed64{w}, out, max_size)
432                encode.go(0n, tail, next, max_size)
433            case Blob{b}:
434              do Result<&2, &1, Error, B.Bytes>:
435                next : B.Bytes <- write_field(number, Blob{b}, out, max_size)
436                encode.go(0n, tail, next, max_size)
437    case 1n+p:
438      match fields:
439        case Nil{}:
440          Done{out}
441        case Con{Field{+number, value}, tail}:
442          match value:
443            case Group{inner}:
444              do Result<&2, &1, Error, B.Bytes>:
445                start : B.Bytes <- write_tag(number, 3, out, max_size)
446                body : B.Bytes <- encode.go(p, inner, start, max_size)
447                end : B.Bytes <- write_tag(number, 4, body, max_size)
448                encode.go(1n+p, tail, end, max_size)
449            case Varint{w}:
450              do Result<&2, &1, Error, B.Bytes>:
451                next : B.Bytes <- write_field(number, Varint{w}, out, max_size)
452                encode.go(1n+p, tail, next, max_size)
453            case Fixed32{w}:
454              do Result<&2, &1, Error, B.Bytes>:
455                next : B.Bytes <- write_field(number, Fixed32{w}, out, max_size)
456                encode.go(1n+p, tail, next, max_size)
457            case Fixed64{w}:
458              do Result<&2, &1, Error, B.Bytes>:
459                next : B.Bytes <- write_field(number, Fixed64{w}, out, max_size)
460                encode.go(1n+p, tail, next, max_size)
461            case Blob{b}:
462              do Result<&2, &1, Error, B.Bytes>:
463                next : B.Bytes <- write_field(number, Blob{b}, out, max_size)
464                encode.go(1n+p, tail, next, max_size)

Uses (11)

Used by (1)