moonproto

A pure-runtime Protocol Buffers wire format encoder/decoder and proto3 subset parser in MoonBit

protobuf
serialization
encoding
wire-format
proto-parser
moon add Duan-lang-dev/moonproto@0.2.0
Download zip
Version
0.2.0
License
MIT
Last updated
14 days ago
Downloads
14
README

#Duan525/moonproto

#
Message

pub trait Message {
fn encode(Self, Encoder) -> Unit
}

Types implementing Message can encode themselves as protobuf messages.

#
AddressBook

pub(all) struct AddressBook {
people : Array[Person]
} derive(Eq,
Debug
)

A collection of people, encoded as a repeated length-delimited field.

#
AddressBook::decode

fn AddressBook::decode(dec : Decoder) -> AddressBook

Decode an AddressBook from a Decoder.

#
AddressBook::encode

fn AddressBook::encode(self : AddressBook, enc : Encoder) -> Unit

Encode an AddressBook (field 1 is a repeated Person message).

#
AddressBook::from_bytes

fn AddressBook::from_bytes(bytes : Bytes) -> AddressBook

Deserialize an AddressBook from bytes.

#
AddressBook::new

fn AddressBook::new(people : Array[Person]) -> AddressBook

Create an AddressBook from a list of people.

#
AddressBook::to_bytes

fn AddressBook::to_bytes(self : AddressBook) -> Bytes

Serialize an AddressBook to bytes.

#
Decoder

type Decoder

#
Decoder::new

fn Decoder::new(bytes : Bytes) -> Decoder

Create a new Decoder from bytes.

#
Decoder::read_bool

fn Decoder::read_bool(self : Decoder) -> Bool?

Read a bool value (wire type 0).

#
Decoder::read_bytes

fn Decoder::read_bytes(self : Decoder) -> Bytes?

Read a bytes value (wire type 2).

#
Decoder::read_double

fn Decoder::read_double(self : Decoder) -> Double?

Read a double value (wire type 1).

#
Decoder::read_fixed32

fn Decoder::read_fixed32(self : Decoder) -> UInt?

Read a fixed32 value (wire type 5).

#
Decoder::read_fixed64

fn Decoder::read_fixed64(self : Decoder) -> UInt64?

Read a fixed64 value (wire type 1).

#
Decoder::read_float

fn Decoder::read_float(self : Decoder) -> Float?

Read a float value (wire type 5).

#
Decoder::read_int32

fn Decoder::read_int32(self : Decoder) -> Int?

Read an int32 value (wire type 0).

#
Decoder::read_int64

fn Decoder::read_int64(self : Decoder) -> Int64?

Read an int64 value (wire type 0).

#
Decoder::read_message

fn Decoder::read_message(self : Decoder) -> Decoder?

Read a nested message (wire type 2). Returns a new Decoder for the nested bytes.

#
Decoder::read_sfixed32

fn Decoder::read_sfixed32(self : Decoder) -> Int?

Read an sfixed32 value (wire type 5).

#
Decoder::read_sfixed64

fn Decoder::read_sfixed64(self : Decoder) -> Int64?

Read an sfixed64 value (wire type 1).

#
Decoder::read_sint32

fn Decoder::read_sint32(self : Decoder) -> Int?

Read a sint32 value (wire type 0, zigzag).

#
Decoder::read_sint64

fn Decoder::read_sint64(self : Decoder) -> Int64?

Read a sint64 value (wire type 0, zigzag).

#
Decoder::read_string

fn Decoder::read_string(self : Decoder) -> String?

Read a string value (wire type 2).

#
Decoder::read_tag

fn Decoder::read_tag(self : Decoder) -> (UInt, UInt)?

Read the next tag. Returns (field_number, wire_type) or None at end.

#
Decoder::read_uint32

fn Decoder::read_uint32(self : Decoder) -> UInt?

Read a uint32 value (wire type 0).

#
Decoder::read_uint64

fn Decoder::read_uint64(self : Decoder) -> UInt64?

Read a uint64 value (wire type 0).

#
Decoder::skip_field

fn Decoder::skip_field(self : Decoder, wire_type : UInt) -> Int?

Skip a field of the given wire type. Returns bytes consumed or None on error.

#
Encoder

type Encoder

#
Encoder::encode_bool

fn Encoder::encode_bool(self : Encoder, field_number : UInt, value : Bool) -> Encoder

Encode a bool field (wire type 0, varint).

#
Encoder::encode_bytes

fn Encoder::encode_bytes(self : Encoder, field_number : UInt, value : Bytes) -> Encoder

Encode a bytes field (wire type 2, length-delimited).

#
Encoder::encode_double

fn Encoder::encode_double(self : Encoder, field_number : UInt, value : Double) -> Encoder

Encode a double field (wire type 1).

#
Encoder::encode_fixed32

fn Encoder::encode_fixed32(self : Encoder, field_number : UInt, value : UInt) -> Encoder

Encode a fixed32 field (wire type 5).

#
Encoder::encode_fixed64

fn Encoder::encode_fixed64(self : Encoder, field_number : UInt, value : UInt64) -> Encoder

Encode a fixed64 field (wire type 1).

#
Encoder::encode_float

fn Encoder::encode_float(self : Encoder, field_number : UInt, value : Float) -> Encoder

Encode a float field (wire type 5).

#
Encoder::encode_int32

fn Encoder::encode_int32(self : Encoder, field_number : UInt, value : Int) -> Encoder

Encode an int32 field (wire type 0, varint).

#
Encoder::encode_int64

fn Encoder::encode_int64(self : Encoder, field_number : UInt, value : Int64) -> Encoder

Encode an int64 field (wire type 0, varint).

#
Encoder::encode_message

fn Encoder::encode_message(self : Encoder, field_number : UInt, inner : Encoder) -> Encoder

Encode a nested message field (wire type 2, length-delimited).

#
Encoder::encode_sfixed32

fn Encoder::encode_sfixed32(self : Encoder, field_number : UInt, value : Int) -> Encoder

Encode a sfixed32 field (wire type 5).

#
Encoder::encode_sfixed64

fn Encoder::encode_sfixed64(self : Encoder, field_number : UInt, value : Int64) -> Encoder

Encode a sfixed64 field (wire type 1).

#
Encoder::encode_sint32

fn Encoder::encode_sint32(self : Encoder, field_number : UInt, value : Int) -> Encoder

Encode a sint32 field (wire type 0, zigzag + varint).

#
Encoder::encode_sint64

fn Encoder::encode_sint64(self : Encoder, field_number : UInt, value : Int64) -> Encoder

Encode a sint64 field (wire type 0, zigzag + varint).

#
Encoder::encode_string

fn Encoder::encode_string(self : Encoder, field_number : UInt, value : String) -> Encoder

Encode a string field (wire type 2, length-delimited). Strings are encoded as UTF-8 bytes.

#
Encoder::encode_uint32

fn Encoder::encode_uint32(self : Encoder, field_number : UInt, value : UInt) -> Encoder

Encode a uint32 field (wire type 0, varint).

#
Encoder::encode_uint64

fn Encoder::encode_uint64(self : Encoder, field_number : UInt, value : UInt64) -> Encoder

Encode a uint64 field (wire type 0, varint).

#
Encoder::new

fn Encoder::new() -> Encoder

Create a new Encoder.

#
Encoder::to_bytes

fn Encoder::to_bytes(self : Encoder) -> Bytes

Get the encoded bytes from the encoder.

#
FieldLabel

pub enum FieldLabel {
Optional
Required
Repeated
} derive(Eq,
Debug
)

Represents a field label.

#
FieldType

pub enum FieldType {
Int32
Int64
Uint32
Uint64
Sint32
Sint64
Fixed32
Fixed64
Sfixed32
Sfixed64
Float
Double
Bool
String
Bytes
Map(FieldType, FieldType)
Message(String)
Enum(String)
} derive(Eq,
Debug
)

Represents a protobuf field type.

#
Person

pub(all) struct Person {
name : String
age : Int
active : Bool
tags : Array[String]
} derive(Eq,
Debug
)

A typed protobuf message with explicit field numbers.

#
Person::decode

fn Person::decode(dec : Decoder) -> Person

Decode a Person from a Decoder. Unknown fields are skipped.

#
Person::encode

fn Person::encode(self : Person, enc : Encoder) -> Unit

Encode a Person into an Encoder.

#
Person::from_bytes

fn Person::from_bytes(bytes : Bytes) -> Person

Deserialize a Person from bytes.

#
Person::new

fn Person::new(name : String, age : Int, active : Bool, tags : Array[String]) -> Person

Create a Person from its fields.

#
Person::to_bytes

fn Person::to_bytes(self : Person) -> Bytes

Serialize a Person to bytes.

#
ProtoEnum

pub struct ProtoEnum {
name : String
values : Array[ProtoEnumValue]
} derive(Eq,
Debug
)

Represents an enum definition.

#
ProtoEnumValue

pub struct ProtoEnumValue {
name : String
number : Int
} derive(Eq,
Debug
)

Represents an enum value.

#
ProtoError

pub struct ProtoError {
line : Int
message : String
} derive(Eq,
Debug
)

A parse error with a line position and message.

#
ProtoField

pub struct ProtoField {
name : String
number : UInt
field_type : FieldType
label : FieldLabel
} derive(Eq,
Debug
)

Represents a single field in a message.

#
ProtoFile

pub struct ProtoFile {
messages : Array[ProtoMessage]
enums : Array[ProtoEnum]
services : Array[ProtoService]
} derive(Eq,
Debug
)

A parsed .proto file containing all definitions.

#
ProtoMessage

pub struct ProtoMessage {
name : String
fields : Array[ProtoField]
oneofs : Array[ProtoOneof]
nested_messages : Array[ProtoMessage]
nested_enums : Array[ProtoEnum]
} derive(Eq,
Debug
)

Represents a message definition.

#
ProtoOneof

pub struct ProtoOneof {
name : String
fields : Array[ProtoField]
} derive(Eq,
Debug
)

Represents a oneof group inside a message.

#
ProtoRpc

pub struct ProtoRpc {
name : String
input_type : String
output_type : String
client_streaming : Bool
server_streaming : Bool
} derive(Eq,
Debug
)

Represents an RPC method inside a service.

#
ProtoService

pub struct ProtoService {
name : String
rpcs : Array[ProtoRpc]
} derive(Eq,
Debug
)

Represents a service definition.

#
WIRE_FIXED32

let WIRE_FIXED32 : UInt

#
WIRE_FIXED64

let WIRE_FIXED64 : UInt

#
WIRE_LENGTH_DELIMITED

let WIRE_LENGTH_DELIMITED : UInt

#
WIRE_VARINT

let WIRE_VARINT : UInt

Wire type constants for protobuf encoding.

#
decode_packed_fixed32

fn decode_packed_fixed32(data : Bytes) -> Array[UInt]

Decode all fixed32 values from packed bytes into an array.

#
decode_packed_fixed64

fn decode_packed_fixed64(data : Bytes) -> Array[UInt64]

Decode all fixed64 values from packed bytes into an array.

#
decode_packed_varint

fn decode_packed_varint(data : Bytes) -> Array[UInt64]

Decode all varints from packed bytes into an array.

#
decode_sint32

fn decode_sint32(bytes : Bytes, pos : Int) -> (Int, Int)?

Decode a zigzag-encoded signed 32-bit varint from bytes at a given position. Returns (value, bytes_consumed) or None if the varint is truncated.

#
decode_sint64

fn decode_sint64(bytes : Bytes, pos : Int) -> (Int64, Int)?

Decode a zigzag-encoded signed 64-bit varint from bytes at a given position. Returns (value, bytes_consumed) or None if the varint is truncated.

#
decode_varint

fn decode_varint(bytes : Bytes, pos : Int) -> (UInt64, Int)?

Decode a protobuf varint from bytes at a given position. Returns (value, bytes_consumed) or None if the varint is truncated.

#
decode_zigzag32

fn decode_zigzag32(value : UInt) -> Int

Zigzag decode an unsigned 32-bit integer to signed.

#
decode_zigzag64

fn decode_zigzag64(value : UInt64) -> Int64

Zigzag decode an unsigned 64-bit integer to signed.

#
encode_packed_double

fn encode_packed_double(buf :
Buffer
, field_number : UInt, values : Array[Double]) -> Unit

Encode a packed repeated double field.

#
encode_packed_fixed32

fn encode_packed_fixed32(buf :
Buffer
, field_number : UInt, values : Array[UInt]) -> Unit

Encode a packed repeated fixed32 field.

#
encode_packed_fixed64

fn encode_packed_fixed64(buf :
Buffer
, field_number : UInt, values : Array[UInt64]) -> Unit

Encode a packed repeated fixed64 field.

#
encode_packed_float

fn encode_packed_float(buf :
Buffer
, field_number : UInt, values : Array[Float]) -> Unit

Encode a packed repeated float field.

#
encode_packed_varint

fn encode_packed_varint(buf :
Buffer
, field_number : UInt, values : Array[UInt64]) -> Unit

Encode a packed repeated varint field (int32, uint32, sint32, bool, enum, int64, etc.)

#
encode_sint32

fn encode_sint32(buf :
Buffer
, value : Int) -> Unit

Encode a signed 32-bit integer as a zigzag-encoded varint.

#
encode_sint64

fn encode_sint64(buf :
Buffer
, value : Int64) -> Unit

Encode a signed 64-bit integer as a zigzag-encoded varint.

#
encode_varint

fn encode_varint(buf :
Buffer
, value : UInt64) -> Unit

Encode an unsigned 64-bit integer as a protobuf varint into the buffer.

#
encode_zigzag32

fn encode_zigzag32(value : Int) -> UInt

Zigzag encode a signed 32-bit integer to unsigned.

#
encode_zigzag64

fn encode_zigzag64(value : Int64) -> UInt64

Zigzag encode a signed 64-bit integer to unsigned.

#
format_proto_error

fn format_proto_error(err : ProtoError) -> String

Format a parse error as a readable string.

#
make_tag

fn make_tag(field_number : UInt, wire_type : UInt) -> UInt

Make a protobuf tag from field number and wire type. tag = (field_number << 3) | wire_type

#
parse_proto

fn parse_proto(content : String) -> Result[ProtoFile, ProtoError]

Parse a .proto file content string into a ProtoFile.

#
read_fixed32

fn read_fixed32(bytes : Bytes, pos : Int) -> (UInt, Int)?

Read a fixed32 value from bytes at the given position. Returns (value, new_position) or None if out of bounds.

#
read_fixed64

fn read_fixed64(bytes : Bytes, pos : Int) -> (UInt64, Int)?

Read a fixed64 value from bytes at the given position. Returns (value, new_position) or None if out of bounds.

#
split_tag

fn split_tag(tag : UInt) -> (UInt, UInt)

Split a protobuf tag into (field_number, wire_type).

#
write_double

fn write_double(buf :
Buffer
, value : Double) -> Unit

Write a double as fixed64.

#
write_fixed32

fn write_fixed32(buf :
Buffer
, value : UInt) -> Unit

Write a fixed32 value in little-endian format.

#
write_fixed64

fn write_fixed64(buf :
Buffer
, value : UInt64) -> Unit

Write a fixed64 value in little-endian format.

#
write_float

fn write_float(buf :
Buffer
, value : Float) -> Unit

Write a float as fixed32.

#
write_length_delimited_prefix

fn write_length_delimited_prefix(buf :
Buffer
, field_number : UInt, byte_length : UInt) -> Unit

Write a Length-delimited field prefix: tag + length as varint.

#
write_tag

fn write_tag(buf :
Buffer
, field_number : UInt, wire_type : UInt) -> Unit

Write a protobuf tag to the buffer.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io