Decode the protobuf wire format in Gleam!
3

Configure Feed

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

Don't repeat module name in `decoders` functions

Gavin Morrow (Sep 21, 2025, 8:53 PM EDT) 304f55d3 3a01f88c

+13 -13
+4 -4
src/decoders.gleam
··· 4 4 import internal/util 5 5 import protobuf_decode_gleam.{parse} 6 6 7 - pub fn decode_protobuf( 7 + pub fn protobuf( 8 8 using decoder: fn() -> Decoder(t), 9 9 named name: String, 10 10 default default: t, ··· 18 18 } 19 19 } 20 20 21 - pub fn decode_uint() -> Decoder(Int) { 21 + pub fn uint() -> Decoder(Int) { 22 22 use bits <- decode.then(decode.bit_array) 23 23 util.bit_array_to_uint(bits) |> decode.success 24 24 } 25 25 26 - pub fn decode_fixed(size: Int) -> Decoder(Int) { 26 + pub fn fixed(size: Int) -> Decoder(Int) { 27 27 use bits <- decode.then(decode.bit_array) 28 28 let assert <<num:unsigned-little-size(size)>> = bits 29 29 num |> decode.success 30 30 } 31 31 32 - pub fn decode_string() -> Decoder(String) { 32 + pub fn string() -> Decoder(String) { 33 33 use bits <- decode.then(decode.bit_array) 34 34 35 35 let str = bit_array.to_string(bits)
+9 -9
test/protobuf_decode_gleam_test.gleam
··· 3 3 import gleeunit 4 4 import simplifile as file 5 5 6 - import decoders.{decode_fixed, decode_protobuf, decode_string, decode_uint} 6 + import decoders 7 7 import protobuf_decode_gleam.{parse} 8 8 9 9 pub fn main() -> Nil { ··· 24 24 25 25 fn person_decoder() -> Decoder(Person) { 26 26 let person_inner_decoder = 27 - decode_protobuf( 27 + decoders.protobuf( 28 28 using: person_decoder, 29 29 named: "Person", 30 30 default: default_person, 31 31 ) 32 32 33 - use id <- decode.field(3, decode_fixed(64)) 34 - use age <- decode.field(1, decode_uint()) 35 - use score <- decode.field(2, decode_uint()) 33 + use id <- decode.field(3, decoders.fixed(64)) 34 + use age <- decode.field(1, decoders.uint()) 35 + use score <- decode.field(2, decoders.uint()) 36 36 use person <- decode.optional_field( 37 37 4, 38 38 option.None, 39 39 decode.optional(person_inner_decoder), 40 40 ) 41 - use day <- decode.field(5, decode_fixed(32)) 41 + use day <- decode.field(5, decoders.fixed(32)) 42 42 43 43 decode.success(Person(id:, age:, score:, self: person, day:)) 44 44 } ··· 68 68 } 69 69 70 70 fn two_ints_decoder() -> Decoder(TwoInts) { 71 - use id <- decode.field(1, decode_uint()) 72 - use age <- decode.field(2, decode_uint()) 71 + use id <- decode.field(1, decoders.uint()) 72 + use age <- decode.field(2, decoders.uint()) 73 73 74 74 decode.success(Test(id:, age:)) 75 75 } ··· 98 98 fn gtfs_decoder() -> Decoder(Gtfs) { 99 99 use header <- decode.field( 100 100 1, 101 - decode_protobuf( 101 + decoders.protobuf( 102 102 using: feed_header_decoder, 103 103 named: "FeedHeader", 104 104 default: feed_header_default,