Safe, composable binary protocol codecs for MoonBit with CLI inspection and a Wasm visualizer
Dependencies
用一份可组合的 Codec[T] 同时定义安全解码与编码,并让每个字节都可解释。
moon add prowk/binschemaimport {
"prowk/binschema" @bin,
}import {
"prowk/binschema/formats" @formats,
}///|
test "README quick start" {
let packet = @binschema.pair(
@binschema.magic(b"BS").named("magic"),
@binschema.pair(
@binschema.u8().named("version"),
@binschema.uleb128().named("stream_id"),
),
)
let value = ((), (1U, 624485UL))
let bytes = match @binschema.encode(packet, value) {
Err(error) => fail(error.render())
Ok(bytes) => bytes
}
match @binschema.decode(packet, bytes) {
Err(error) => fail(error.render())
Ok(decoded) => {
assert_eq(decoded.value, value)
assert_eq(decoded.trace.length(), 3)
assert_true(packet.describe().contains("stream_id"))
assert_eq(packet.lint().length(), 0)
}
}
}///|
test "README incremental decode" {
let codec = @binschema.pair(@binschema.u16_be(), @binschema.u8())
let stream = @binschema.IncrementalDecoder::new(codec)
assert_true(stream.feed(b"\x12") is @binschema.NeedMore)
match stream.feed(b"\x34\x56\xaa") {
@binschema.Done(decoded) => assert_eq(decoded.value, (0x1234U, 0x56U))
_ => fail("expected one complete frame")
}
assert_eq(stream.buffered_bytes(), 1)
}magic "BS" | version u8 | stream_id uleb128 | sequence u16_lemoon update
moon run --target native examples/custom_packetencoded: 42 53 01 e5 8e 26 07 00
stream_id: 624485
trace fields: 4moon run --target native examples/demo_protocolmoon run --target native cmd/main -- inspect image.png
moon run --target native cmd/main -- inspect capture.pcap --json
moon run --target native cmd/main -- verify audio.wav
moon run --target native cmd/main -- sample png sample.png
moon run --target native cmd/main -- sample bmff sample.mp4
moon run --target native cmd/main -- sample dns sample.dns
moon run --target native cmd/main -- sample elf sample.elf
moon run --target native cmd/main -- lint png
moon run --target native cmd/main -- lint pcap --json
moon run --target native cmd/main -- inspect sample.mp4 --format bmff
moon run --target native cmd/main -- inspect sample.dns --format dns
moon run --target native cmd/main -- inspect sample.elf
moon run --target native cmd/main -- formatsmoon build --target wasm-gc web/bridge --release
cp _build/wasm-gc/release/build/web/bridge/bridge.wasm web/binschema.wasm
python -m http.server 4173 --directory webCopy-Item _build/wasm-gc/release/build/web/bridge/bridge.wasm web/binschema.wasm├─ codec.mbt / decoder.mbt / encoder.mbt # 安全组合子核心
├─ schema.mbt / lint.mbt # Schema 元数据与静态检查
├─ primitives.mbt / varint.mbt # 定长与变长基础类型
├─ formats/ # ELF / PNG / WAVE / PCAP / ISO BMFF / DNS
├─ cmd/main/ # 原生 CLI
├─ web/ # Wasm-GC 桥接与浏览器检查器
├─ examples/custom_packet/ # 最小自定义协议示例
├─ examples/demo_protocol/ # 更完整的真实协议示例
└─ docs/ # 架构和安全模型moon update
moon info
moon fmt --check
moon check --target all --deny-warn
moon test --target all --deny-warn
moon test README.mbt.md --target native --deny-warn
moon bench --build-only --target native --deny-warn
moon test --target native --enable-coverage --deny-warn
moon coverage analyze
moon build --target native cmd/main --release
moon build --target native examples/custom_packet --release
moon build --target native examples/demo_protocol --release
moon build --target wasm-gc web/bridge --release
cmp README.md README.mbt.mdpub struct Codec[T] {
decode_impl : (Decoder) -> Result[T, BinError]
encode_impl : (Encoder, T) -> Result[Unit, BinError]
kind : String
render : (T) -> String
schema : SchemaNode
}pub struct Decoder {
input : BytesView
base_offset : Int
offset : Int
bit_offset : Int
field_path : String
depth : Int
limits : Limits
trace_entries : Array[TraceEntry]
}fn[T] IncrementalDecoder::append(self : IncrementalDecoder[T], chunk : Bytes) -> Result[Unit, BinError]pub(all) struct LintIssue {
code : String
severity : LintSeverity
path : String
message : String
} derive(Eq, ToJson, Debug)fn SchemaNode::node(kind : String, children : Array[SchemaNode], constraints? : Array[String]) -> SchemaNodefn bytes_to_hex_preview(bytes : Bytes, max_bytes : Int) -> Stringfn bytes_view_to_hex_preview(bytes : BytesView, max_bytes : Int) -> Stringfn[T] decode_prefix_view_with_options(codec : Codec[T], input : BytesView, options : DecodeOptions) -> Result[Decoded[T], BinError]fn[T] decode_prefix_with_options(codec : Codec[T], input : Bytes, options : DecodeOptions) -> Result[Decoded[T], BinError]fn[T] decode_view_with_options(codec : Codec[T], input : BytesView, options : DecodeOptions) -> Result[Decoded[T], BinError]fn[T] decode_with_options(codec : Codec[T], input : Bytes, options : DecodeOptions) -> Result[Decoded[T], BinError]fn[T] encode_with_options(codec : Codec[T], value : T, options : EncodeOptions) -> Result[Bytes, BinError]fn[T] probe_decode_with_options(codec : Codec[T], input : Bytes, options : DecodeOptions) -> DecodeStep[T]Install
Download zipSafe, composable binary protocol codecs for MoonBit with CLI inspection and a Wasm visualizer
Dependencies