Packages

A Protocol Buffers library for Gleam, providing encoding and decoding of protobuf messages.

Current section

Files

Jump to
protozoa src protozoa@wire.erl
Raw

src/protozoa@wire.erl

-module(protozoa@wire).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/protozoa/wire.gleam").
-export([wire_type_value/1, wire_type_from_int/1, make_tag/2, get_field_number/1, get_wire_type/1]).
-export_type([wire_type/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Protocol Buffer Wire Format Module \n"
"\n"
" This module defines the low-level wire format types and utilities for Protocol Buffer\n"
" binary encoding and decoding. It provides the foundational types and functions needed\n"
" to work with the Protocol Buffer binary wire format specification.\n"
"\n"
" ## Wire Format Fundamentals\n"
"\n"
" Protocol Buffers use a binary wire format for efficient serialization. Each field\n"
" is encoded with a specific wire type that determines how the data is represented:\n"
"\n"
" - **Varint**: Variable-length integers (most integers, bools, enums)\n"
" - **Fixed64**: 8-byte fixed-width values (double, fixed64, sfixed64) \n"
" - **LengthDelimited**: Length-prefixed data (strings, bytes, messages, maps)\n"
" - **Fixed32**: 4-byte fixed-width values (float, fixed32, sfixed32)\n"
" - **StartGroup/EndGroup**: Deprecated group encoding (proto2 only)\n"
"\n"
" ## Capabilities\n"
"\n"
" - **Wire type definitions**: Complete enumeration of Protocol Buffer wire types\n"
" - **Tag manipulation**: Functions for creating and parsing field tags\n"
" - **Format validation**: Utilities for validating wire format data\n"
" - **Type safety**: Compile-time guarantees about wire format correctness\n"
"\n"
" ## Usage in Protozoa\n"
" \n"
" This module is primarily used by the encode and decode modules to:\n"
" - Determine the correct wire type for each field type\n"
" - Create properly formatted field tags\n"
" - Parse field numbers and wire types from binary data\n"
" - Validate wire format compliance\n"
"\n"
" ## Public API\n"
"\n"
" The main public component is the `WireType` type which is used throughout\n"
" the encode/decode pipeline. Internal utility functions are marked with\n"
" `@internal` as they are implementation details.\n"
).
-type wire_type() :: varint |
fixed64 |
length_delimited |
start_group |
end_group |
fixed32.
-file("src/protozoa/wire.gleam", 67).
?DOC(false).
-spec wire_type_value(wire_type()) -> integer().
wire_type_value(Wire_type) ->
case Wire_type of
varint ->
0;
fixed64 ->
1;
length_delimited ->
2;
start_group ->
3;
end_group ->
4;
fixed32 ->
5
end.
-file("src/protozoa/wire.gleam", 89).
?DOC(false).
-spec wire_type_from_int(integer()) -> {ok, wire_type()} | {error, binary()}.
wire_type_from_int(Value) ->
case Value of
0 ->
{ok, varint};
1 ->
{ok, fixed64};
2 ->
{ok, length_delimited};
3 ->
{ok, start_group};
4 ->
{ok, end_group};
5 ->
{ok, fixed32};
_ ->
{error,
<<"Invalid wire type: "/utf8,
(erlang:integer_to_binary(Value))/binary>>}
end.
-file("src/protozoa/wire.gleam", 111).
?DOC(false).
-spec make_tag(integer(), wire_type()) -> integer().
make_tag(Field_number, Wire_type) ->
_pipe = erlang:'bsl'(Field_number, 3),
erlang:'bor'(_pipe, wire_type_value(Wire_type)).
-file("src/protozoa/wire.gleam", 126).
?DOC(false).
-spec get_field_number(integer()) -> integer().
get_field_number(Tag) ->
erlang:'bsr'(Tag, 3).
-file("src/protozoa/wire.gleam", 141).
?DOC(false).
-spec get_wire_type(integer()) -> {ok, wire_type()} | {error, binary()}.
get_wire_type(Tag) ->
_pipe = erlang:'band'(Tag, 7),
wire_type_from_int(_pipe).