Decode the protobuf wire format in Gleam!
3

Configure Feed

Select the types of activity you want to include in your feed.

Wire type module

Gavin Morrow (Sep 19, 2025, 9:19 PM EDT) bc8d212e c0fa6050

+31 -28
+13 -28
src/protobuf_decode_gleam.gleam
··· 3 3 import gleam/dynamic/decode.{type Decoder} 4 4 import gleam/int 5 5 import gleam/list 6 - import gleam/option.{type Option, None, Some} 6 + import gleam/option 7 7 import gleam/result 8 + 9 + import internal/wire_type.{type WireType} 8 10 9 11 pub fn parse( 10 12 from bits: BitArray, ··· 25 27 case value { 26 28 Ok(value) -> decode.success(value) 27 29 Error(_) -> decode.failure(default, name) 28 - } 29 - } 30 - 31 - type WireType { 32 - VarInt 33 - I64 34 - Len 35 - I32 36 - } 37 - 38 - fn parse_wire_type(i: Int) -> Option(WireType) { 39 - case i { 40 - 0 -> Some(VarInt) 41 - 1 -> Some(I64) 42 - 2 -> Some(Len) 43 - 5 -> Some(I32) 44 - _ -> None 45 - } 46 - } 47 - 48 - fn wire_type_read_fn(ty: WireType) -> fn(BitArray) -> ValueResult { 49 - case ty { 50 - VarInt -> read_varint 51 - I64 -> read_fixed(64) 52 - Len -> read_len 53 - I32 -> read_fixed(32) 54 30 } 55 31 } 56 32 ··· 96 72 #(field.key, field.value) 97 73 } 98 74 75 + fn wire_type_read_fn(ty: WireType) -> fn(BitArray) -> ValueResult { 76 + case ty { 77 + wire_type.VarInt -> read_varint 78 + wire_type.I64 -> read_fixed(64) 79 + wire_type.Len -> read_len 80 + wire_type.I32 -> read_fixed(32) 81 + } 82 + } 83 + 99 84 fn read_field(bits: BitArray) -> DecodeResult(Parsed(Field)) { 100 85 use Parsed(value: tag, rest: bits) <- result.try(read_varint(bits)) 101 86 let tag = bit_array_to_uint(tag) ··· 104 89 let wire_type = tag |> int.bitwise_and(0b111) 105 90 106 91 use wire_type <- result.try(option.to_result( 107 - parse_wire_type(wire_type), 92 + wire_type.parse(wire_type), 108 93 UnknownWireType(wire_type), 109 94 )) 110 95
+18
src/internal/wire_type.gleam
··· 1 + import gleam/option.{type Option, None, Some} 2 + 3 + pub type WireType { 4 + VarInt 5 + I64 6 + Len 7 + I32 8 + } 9 + 10 + pub fn parse(i: Int) -> Option(WireType) { 11 + case i { 12 + 0 -> Some(VarInt) 13 + 1 -> Some(I64) 14 + 2 -> Some(Len) 15 + 5 -> Some(I32) 16 + _ -> None 17 + } 18 + }