目录

MoonProto

A pure-runtime Protocol Buffers wire format encoder/decoder for MoonBit, with a built-in .proto file parser.

Features

  • Varint (ULEB128) and Zigzag encoding/decoding
  • All protobuf wire types: Varint, Fixed64, Length-delimited, Fixed32
  • Builder-style Encoder API with method chaining
  • Position-tracked Decoder API with field skipping
  • All scalar types: int32/64, uint32/64, sint32/64, fixed32/64, sfixed32/64, float, double, bool, string, bytes
  • Nested message encoding/decoding
  • Packed repeated field support
  • Typed message pattern via the Message trait
  • .proto file parser (proto3 subset)

Installation

moon add Duan-lang-dev/moonproto

Usage

Encoding

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.

Wire Format Reference

Wire Type ID Used For
Varint 0 int32, int64, uint32, uint64, sint32, sint64, bool, enum
Fixed64 1 fixed64, sfixed64, double
Length-delimited 2 string, bytes, embedded messages, packed repeated
Fixed32 5 fixed32, sfixed32, float

Run Commands

moon check          # type check
moon test           # run tests (114 tests)
moon fmt --check    # format check
moon build          # build
moon run cmd/main   # run CLI demo

License

MIT

关于

MoonProtobuf 是一个纯 MoonBit 实现的 Protocol Buffers 运行时编码解码库,完整实现 protobuf wire format 规范。

809.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号