protobuf
| Kind | kit |
|---|---|
| Categories | data-format serialization encoding |
| Keywords | protobuf protocol-buffers serialization grpc binary |
Protocol Buffers binary serialization for Kit
Files
| File | Description |
|---|---|
kit.toml | Package manifest with metadata and dependencies |
src/protobuf.kit | Wire format encoding, decoding, and field value helpers |
tests/protobuf.test.kit | Module import verification tests |
examples/basic.kit | Encoding, decoding, and inspecting messages |
examples/nested.kit | Embedded messages and multi-level nesting |
examples/person.kit | Complete Person schema with phones and enums |
examples/repeated.kit | Packed and non-packed repeated fields |
LICENSE | MIT license file |
Dependencies
No Kit package dependencies.
Installation
kit add gitlab.com/kit-lang/packages/kit-protobuf.gitUsage
import Kit.ProtobufLicense
MIT License - see LICENSE for details.
Exported Functions & Types
ProtobufError
Protobuf error type with specific variants for different failure modes.
Variants
ProtobufEncodeError {message}ProtobufDecodeError {message}WireType
Protocol buffer wire types (encoding format identifiers).
Variants
VarintFixed64LengthDelimStartGroupEndGroupFixed32FieldValue
Field value variants for different protobuf types.
Variants
VarintValue {Int}Fixed32Value {Int}Fixed64Value {Int}BytesValue {List, Int}StringValue {String}MessageValue {List, Field}RepeatedValue {List, FieldValue}PackedValue {List, Int}IntDecodeResult
Internal result types for decoding with remaining bytes.
Variants
IntDecodeOk {IntWithRest}IntDecodeErr {String}FieldType
Field type descriptors for schema definitions.
Variants
TInt32TInt64TUint32TUint64TSint32TSint64TBoolTEnumTFixed32TSfixed32TFloatTFixed64TSfixed64TDoubleTStringTBytesTMessageTRepeated {FieldType}TPacked {FieldType}varint
WireType
fixed64
WireType
length-delim
WireType
fixed32
WireType
int32
FieldType
int64
FieldType
uint32
FieldType
uint64
FieldType
sint32
FieldType
sint64
FieldType
bool-type
FieldType
enum-type
FieldType
fixed32-type
FieldType
sfixed32
FieldType
float-type
FieldType
fixed64-type
FieldType
sfixed64
FieldType
double-type
FieldType
string-type
FieldType
bytes-type
FieldType
message-type
FieldType
repeated
FieldType -> FieldType
packed
FieldType -> FieldType
field-def
Int -> String -> FieldType -> FieldDef
message-def
String -> List FieldDef -> MessageDef
encode-field
Encode a single field to bytes.
Parameters:
Returns:
Int -> FieldValue -> List Int
bytes = encode-field 1 (int 42)
# Returns: [8, 42] (tag for field 1 varint, value 42)encode
Encode a complete protobuf message to bytes.
Parameters:
Returns:
List Field -> List Int
fields = [field 1 (int 42), field 2 (string "hello")]
bytes = encode fieldsdecode
Decode a complete protobuf message from bytes.
Parameters:
Returns:
List Int -> Result (List Field) ProtobufError
match decode bytes
| Ok fields -> print "Decoded ${Int.to-string (List.length fields)} fields"
| Err err -> print "Error: ${err}"int
Create a varint integer field value (int32/int64/uint32/uint64).
Parameters:
Returns:
Int -> FieldValue
uint
Create an unsigned varint integer field value (alias for int).
Parameters:
Returns:
Int -> FieldValue
sint
Create a signed varint integer field value with zigzag encoding (sint32/sint64).
Parameters:
Returns:
Int -> FieldValue
bool
Create a boolean field value (encoded as varint 0 or 1).
Parameters:
Returns:
Bool -> FieldValue
enum
Create an enum field value (encoded as varint).
Parameters:
Returns:
Int -> FieldValue
fixed32
Create a fixed32 field value (fixed32/sfixed32/float).
Parameters:
Returns:
Int -> FieldValue
sfixed32
Create a signed fixed32 field value (alias for fixed32).
Parameters:
Returns:
Int -> FieldValue
fixed64
Create a fixed64 field value (fixed64/sfixed64/double).
Parameters:
Returns:
Int -> FieldValue
sfixed64
Create a signed fixed64 field value (alias for fixed64).
Parameters:
Returns:
Int -> FieldValue
string
Create a string field value.
Parameters:
Returns:
String -> FieldValue
bytes
Create a bytes field value.
Parameters:
Returns:
List Int -> FieldValue
message
Create a nested message field value.
Parameters:
Returns:
List Field -> FieldValue
repeated-values
Create a repeated field value (non-packed).
Parameters:
Returns:
List FieldValue -> FieldValue
packed-ints
Create a packed repeated integer field value.
Parameters:
Returns:
List Int -> FieldValue
get-field
Get a field by number from decoded message fields.
Parameters:
Returns:
List Field -> Int -> Option FieldValue
match get-field decoded-fields 1
| Some value -> print "Found field 1"
| None -> print "Field 1 not found"get-repeated
Get all fields with a given number (for repeated fields).
Parameters:
Returns:
List Field -> Int -> List FieldValue
repeated-values = get-repeated decoded-fields 3
print "Found ${Int.to-string (List.length repeated-values)} values"as-int
Extract varint as integer.
Parameters:
Returns:
FieldValue -> Option Int
as-sint
Extract varint as signed integer (with zigzag decoding).
Parameters:
Returns:
FieldValue -> Option Int
as-bool
Extract varint as boolean.
Parameters:
Returns:
FieldValue -> Option Bool
as-fixed32
Extract fixed32 value.
Parameters:
Returns:
FieldValue -> Option Int
as-fixed64
Extract fixed64 value.
Parameters:
Returns:
FieldValue -> Option Int
as-bytes
Extract bytes value.
Parameters:
Returns:
FieldValue -> Option (List Int)
as-string
Extract string value.
Parameters:
Returns:
FieldValue -> Option String
as-message
Extract embedded message as fields.
Parameters:
Returns:
FieldValue -> Result (List Field) ProtobufError
as-packed-ints
Extract packed repeated integers.
Parameters:
Returns:
FieldValue -> Result (List Int) ProtobufError
field
Create a Field record from field number and value.
Parameters:
Returns:
Int -> FieldValue -> Field
f = field 1 (int 42)
# Creates: { number: 1, wire-type: Varint, value: VarintValue 42 }bytes-to-hex
Format bytes as hex string for debugging.
Parameters:
Returns:
List Int -> String
hex = bytes-to-hex [8, 42, 18, 5]
print "Encoded: ${hex}"inspect
Print decoded message structure for debugging.
Parameters:
Returns:
List Field -> Unit
inspect decoded-fields
# Prints:
# Field 1:
# varint: 42
# Field 2:
# string: "hello"