From ab12dad791a0baa20c506b95a03db38cb3045568 Mon Sep 17 00:00:00 2001 From: Gavin Morrow Date: Mon, 29 Sep 2025 06:47:04 -0400 Subject: [PATCH] Add example to README --- README.md | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 76ee866..f9a02ee 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,38 @@ gleam add protobuf_decode_gleam@1 --> ```gleam import protobuf_decode_gleam +import option + +type Person { + Person(id: Int, age: option.Option(Int), score: Int) +} + +fn person_decoder() -> Decoder(Person) { + use id <- decode.field(3, decoders.fixed(64)) + use age <- decode.optional_field( + 1, + option.None, + decode.optional(decoders.uint()), + ) + use score <- decode.field(2, decoders.uint()) + + Person(id:, age:, score:) |> decode.success +} pub fn main() -> Nil { - // TODO: An example of the project in use - // See the tests, they have decent examples. + // See the tests as well, they have decent examples. + let path = "./path/to/person.pb" + + let assert Ok(bits) = file.read_bits(from: path) + let assert Ok(Parsed(value: person, rest: <<>>, pos: _)) = + parse(from: bits, using: person_decoder()) + + assert person + == Person( + id: 42, + age: 150, + score: option.Some(81_050), + ) } ``` -- 2.51.2