Cap'n Proto serialization runtime and schema code generation for MoonBit.
Dependencies
moon add Ridge-Lab/capnp@0.1.0import {
"Ridge-Lab/capnp",
}git clone https://github.com/Ridge-Lab/capnp-mbt
cd capnp-mbt
moon update
moon run --target js examples/roundtripmoon build --target native --release cmd/capnpc-moonbit
capnp compile -o./_build/native/release/build/cmd/capnpc-moonbit/capnpc-moonbit.exe:examples/telemetry/generated examples/telemetry/schema/telemetry.capnp
moon fmt
moon check --target native --deny-warncapnp compile -o- fixtures/conformance.capnp > request.bin
moon run --target js cmd/capnpc-moonbit -- --request request.bin --out generated
moon fmtlet message = @capnp.Builder::new()
let batch = @telemetry.BatchBuilder::init_root(message)
batch.set_job_id(9007199254740993UL)
batch.set_source("room-temperature")
let samples = batch.init_samples(1)
samples.get(0).set_value(21.5)
samples.get(0).set_quality(@telemetry.Valid)
let bytes = message.finish()
let received = @telemetry.BatchReader::from_message(@capnp.read_message(bytes))
assert_eq(received.get_samples().get(0).get_value(), 21.5)| 能力 | 当前范围 |
|---|---|
| 数据类型 | Void、Bool、所有整数和浮点宽度、Text、Data、enum、struct、嵌套声明 |
| 容器和字段 | primitive / pointer / inline-composite list、嵌套 list、group、union、标量及复合默认值 |
| 帧和指针 | 标准分段帧、近指针、单跳和双跳 far pointer 的读取;writer 生成单段帧 |
| 压缩与流 | 标准 packed 编解码;frame_length 识别部分输入和串联帧边界 |
| 防护 | 指针越界检查、共享遍历预算、嵌套深度限制、段数限制、解包和 writer 分配上限 |
| 演进 | 缺失字段默认值、额外字段跳过、未知 enum 值保留、兼容的 list / struct 扩展 |
| 未实现 | RPC、capability、AnyPointer、泛型、常量声明、canonical encoding、builder 原位编辑收到的消息 |
moon fmt --check
moon check --target all --deny-warn
moon test --target all --deny-warn
moon build --target all --releasepub(all) suberror CapnpError {
InvalidMessage(String)
Bounds(String)
Limit(String)
TypeMismatch(String)
Unsupported(String)
} derive(Debug)pub struct Builder {
// private fields
}fn Builder::init_root(self : Builder, data_words : Int, pointer_count : Int) -> StructBuilder raise CapnpErrorpub struct ListBuilder {
// private fields
}fn ListBuilder::init_list(self : ListBuilder, index : Int, element_size : Int, length : Int) -> ListBuilder raise CapnpErrorfn ListBuilder::init_struct(self : ListBuilder, index : Int, data_words : Int, pointer_count : Int) -> StructBuilder raise CapnpErrorfn ListBuilder::init_struct_list(self : ListBuilder, index : Int, length : Int, data_words : Int, pointer_count : Int) -> ListBuilder raise CapnpErrorfn ListBuilder::set_float32(self : ListBuilder, index : Int, value : Float) -> Unit raise CapnpErrorfn ListBuilder::set_float64(self : ListBuilder, index : Int, value : Double) -> Unit raise CapnpErrorfn ListBuilder::set_uint64(self : ListBuilder, index : Int, value : UInt64) -> Unit raise CapnpErrorpub struct ListReader {
// private fields
}pub struct Message {
// private fields
}pub struct PointerReader {
// private fields
}pub struct StructBuilder {
// private fields
}fn StructBuilder::clear_data(self : StructBuilder, byte_offset : Int, byte_length : Int) -> Unit raise CapnpErrorfn StructBuilder::init_list(self : StructBuilder, slot : Int, element_size : Int, length : Int) -> ListBuilder raise CapnpErrorfn StructBuilder::init_struct(self : StructBuilder, slot : Int, data_words : Int, pointer_count : Int) -> StructBuilder raise CapnpErrorfn StructBuilder::init_struct_list(self : StructBuilder, slot : Int, length : Int, data_words : Int, pointer_count : Int) -> ListBuilder raise CapnpErrorfn StructBuilder::set_bool(self : StructBuilder, bit_offset : Int, value : Bool, default? : Bool) -> Unit raise CapnpErrorfn StructBuilder::set_data(self : StructBuilder, slot : Int, value : Bytes) -> Unit raise CapnpErrorfn StructBuilder::set_float32(self : StructBuilder, byte_offset : Int, value : Float, default? : Float) -> Unit raise CapnpErrorfn StructBuilder::set_float64(self : StructBuilder, byte_offset : Int, value : Double, default? : Double) -> Unit raise CapnpErrorfn StructBuilder::set_int16(self : StructBuilder, byte_offset : Int, value : Int, default? : Int) -> Unit raise CapnpErrorfn StructBuilder::set_int32(self : StructBuilder, byte_offset : Int, value : Int, default? : Int) -> Unit raise CapnpErrorfn StructBuilder::set_int64(self : StructBuilder, byte_offset : Int, value : Int64, default? : Int64) -> Unit raise CapnpErrorfn StructBuilder::set_int8(self : StructBuilder, byte_offset : Int, value : Int, default? : Int) -> Unit raise CapnpErrorfn StructBuilder::set_text(self : StructBuilder, slot : Int, value : String) -> Unit raise CapnpErrorfn StructBuilder::set_uint16(self : StructBuilder, byte_offset : Int, value : UInt, default? : UInt) -> Unit raise CapnpErrorfn StructBuilder::set_uint32(self : StructBuilder, byte_offset : Int, value : UInt, default? : UInt) -> Unit raise CapnpErrorfn StructBuilder::set_uint64(self : StructBuilder, byte_offset : Int, value : UInt64, default? : UInt64) -> Unit raise CapnpErrorfn StructBuilder::set_uint8(self : StructBuilder, byte_offset : Int, value : Byte, default? : Byte) -> Unit raise CapnpErrorpub struct StructReader {
// private fields
}fn StructReader::get_bool(self : StructReader, bit_offset : Int, default? : Bool) -> Bool raise CapnpErrorfn StructReader::get_data(self : StructReader, index : Int, default? : Bytes) -> Bytes raise CapnpErrorfn StructReader::get_float32(self : StructReader, byte_offset : Int, default? : Float) -> Float raise CapnpErrorfn StructReader::get_float64(self : StructReader, byte_offset : Int, default? : Double) -> Double raise CapnpErrorfn StructReader::get_int16(self : StructReader, byte_offset : Int, default? : Int) -> Int raise CapnpErrorfn StructReader::get_int32(self : StructReader, byte_offset : Int, default? : Int) -> Int raise CapnpErrorfn StructReader::get_int64(self : StructReader, byte_offset : Int, default? : Int64) -> Int64 raise CapnpErrorfn StructReader::get_int8(self : StructReader, byte_offset : Int, default? : Int) -> Int raise CapnpErrorfn StructReader::get_text(self : StructReader, index : Int, default? : String) -> String raise CapnpErrorfn StructReader::get_uint16(self : StructReader, byte_offset : Int, default? : UInt) -> UInt raise CapnpErrorfn StructReader::get_uint32(self : StructReader, byte_offset : Int, default? : UInt) -> UInt raise CapnpErrorfn StructReader::get_uint64(self : StructReader, byte_offset : Int, default? : UInt64) -> UInt64 raise CapnpErrorfn StructReader::get_uint8(self : StructReader, byte_offset : Int, default? : Byte) -> Byte raise CapnpErrorfn frame_length(input : Bytes, offset? : Int, max_segments? : Int, max_message_bytes? : Int) -> Int? raise CapnpErrorfn read_message(bytes : Bytes, traversal_limit_words? : Int, nesting_limit? : Int, max_segments? : Int) -> Message raise CapnpErrorInstall
Download zipCap'n Proto serialization runtime and schema code generation for MoonBit.
Dependencies