let enc = @moonproto.Encoder::new()
let _ = enc.encode_string(1U, "Alice")
.encode_int32(2U, 30)
.encode_bool(3U, true)
let bytes = enc.to_bytes()
Decoding
let dec = @moonproto.Decoder::new(bytes)
while dec.read_tag() is Some((field_num, wire_type)) {
match field_num {
1U => if dec.read_string() is Some(v) { name = v }
2U => if dec.read_int32() is Some(v) { age = v }
_ => dec.skip_field(wire_type) |> ignore
}
}
Typed Message Pattern
// A typed message with explicit field numbers and skip-unknown-fields logic.
let person = @moonproto.Person::new("Alice", 30, true, ["dev", "lead"])
let bytes = person.to_bytes()
let decoded = @moonproto.Person::from_bytes(bytes)
// decoded.name == "Alice", decoded.age == 30
Person implements encode/decode/to_bytes/from_bytes; AddressBook shows nested message composition.
.proto File Parser
let proto = "syntax = \"proto3\";
message Person { string name = 1; int32 age = 2; }
enum Color { RED = 0; GREEN = 1; }"
match @moonproto.parse_proto(proto) {
Ok(file) => {
// file.messages[0].name == "Person"
// file.enums[0].name == "Color"
}
Err(err) => println(@moonproto.format_proto_error(err))
}
Supported Syntax
The parser covers a proto3 subset:
message with fields, nested messages, nested enums
enum with values (including negative values)
oneof groups
map<K, V> fields
service with rpc methods (including stream)
repeated / optional / required labels
All scalar field types plus custom message/enum type references
// line comments and /* */ block comments
Returns Result[ProtoFile, ProtoError] with line-positioned errors
Not supported (explicitly out of scope): options, reserved, extensions, group, default values, proto2 semantics.
MoonProto
A pure-runtime Protocol Buffers wire format encoder/decoder for MoonBit, with a built-in
.protofile parser.Features
Messagetrait.protofile parser (proto3 subset)Installation
Usage
Encoding
Decoding
Typed Message Pattern
Personimplementsencode/decode/to_bytes/from_bytes;AddressBookshows nested message composition..proto File Parser
Supported Syntax
The parser covers a proto3 subset:
messagewith fields, nested messages, nested enumsenumwith values (including negative values)oneofgroupsmap<K, V>fieldsservicewithrpcmethods (includingstream)repeated/optional/requiredlabels//line comments and/* */block commentsResult[ProtoFile, ProtoError]with line-positioned errorsNot supported (explicitly out of scope):
options,reserved,extensions,group,defaultvalues, proto2 semantics.Wire Format Reference
Run Commands
License
MIT