From 747c231e8ece86a1c647e3727545552902b03206 Mon Sep 17 00:00:00 2001 From: Gavin Morrow Date: Wed, 1 Oct 2025 14:17:54 -0400 Subject: [PATCH] Add `decode_bool()` --- src/protobin.gleam | 9 +++++++++ test/person.pb | Bin 56 -> 60 bytes test/person.protoscope.txt | 2 ++ test/protobin_test.gleam | 8 +++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/protobin.gleam b/src/protobin.gleam index ec5f506..a2abf1c 100644 --- a/src/protobin.gleam +++ b/src/protobin.gleam @@ -329,6 +329,15 @@ pub fn decode_fixed(size: Int) -> Decoder(Int) { } } +pub fn decode_bool() -> Decoder(Bool) { + use bool <- decode.then(decode_uint()) + case bool { + 0 -> False |> decode.success + 1 -> True |> decode.success + _ -> decode.failure(False, "Bool") + } +} + pub fn decode_string() -> Decoder(String) { use bits <- decode.then(single_or_raw_bit_array()) diff --git a/test/person.pb b/test/person.pb index fe6ae457abafb0647dbc7fb9889d3b084e131058..21814adfe8183c026f5e8cfb383db06c69c5ccff 100644 GIT binary patch delta 23 ZcmcDpnIO(6J3)+3SB!yy!GaM=0RSzA0_Fe! delta 19 XcmcDqm>|w5Jwc31SB!yy0Zae@A%_BP diff --git a/test/person.protoscope.txt b/test/person.protoscope.txt index 7460cea..445b191 100644 --- a/test/person.protoscope.txt +++ b/test/person.protoscope.txt @@ -8,5 +8,7 @@ 1: 150 2: 81050 5: 22i32 + 7: true } 5: 22i32 +7: true diff --git a/test/protobin_test.gleam b/test/protobin_test.gleam index 4137011..fd220f6 100644 --- a/test/protobin_test.gleam +++ b/test/protobin_test.gleam @@ -17,6 +17,7 @@ type Person { score: Int, self: option.Option(Person), day: Int, + likes_chocolate: Bool, ) } @@ -27,6 +28,7 @@ const default_person = Person( score: 0, self: option.None, day: 0, + likes_chocolate: False, ) fn person_decoder() -> Decoder(Person) { @@ -47,8 +49,10 @@ fn person_decoder() -> Decoder(Person) { decode.optional(person_inner_decoder), ) use day <- decode.field(5, protobin.decode_fixed(32)) + use likes_chocolate <- decode.field(7, protobin.decode_bool()) - Person(id:, name:, age:, score:, self: person, day:) |> decode.success + Person(id:, name:, age:, score:, self: person, day:, likes_chocolate:) + |> decode.success } pub fn person_pb_test() { @@ -71,8 +75,10 @@ pub fn person_pb_test() { score: 81_050, self: option.None, day: 22, + likes_chocolate: True, )), day: 22, + likes_chocolate: True, ) } -- 2.51.2