A high-performance, standard-compliant CBOR (RFC 8949) serialization library for MoonBit.
moon add 2515050242/cborimport = [
"2515050242/cbor",
]import "2515050242/cbor"
fn main {
let value = cbor.Map([
(cbor.Text("hello"), cbor.Integer(42L)),
(cbor.Text("world"), cbor.Array([cbor.Integer(1L), cbor.Integer(2L)])),
])
let bytes = cbor.encode(value)
println("Encoded \{bytes.length()} bytes.")
let decoded = cbor.decode(bytes) catch {
err => fail("Failed to decode: \{err}")
}
println("Decoded Diagnostic:")
println(cbor.diagnostic(decoded))
let count : Int64 = cbor.decode_as(b"\x18\x2a") catch {
err => fail("Failed to decode Int64: \{err}")
}
println("Decoded integer: \{count}")
}moon run ./cmd/main
moon run ./cmd/main -- decode-hex a26161016162820203
moon run ./cmd/main -- roundtrip-hex 5f42010243030405ff
moon run ./cmd/main -- benchmark 20000
moon run ./cmd/main -- application-demomoon fmt --check
moon info
moon check --target wasm-gc --deny-warn
moon test --target wasm-gc --deny-warnpowershell -ExecutionPolicy Bypass -File .\scripts\verify_acceptance.ps1python ./scripts/check_repo_compliance.pyimpl CborDecode for Boolimpl CborDecode for Int64impl CborDecode for UInt64impl CborDecode for Doubleimpl CborDecode for Stringimpl CborDecode for Bytesimpl CborEncode for Boolimpl CborEncode for Intimpl CborEncode for Int64impl CborEncode for UInt64impl CborEncode for Doubleimpl CborEncode for Stringpub(all) struct AppIngestResult {
accepted : Bool
kind : AppIngressKind
value : CborValue?
values : Array[CborValue]
stats : CborDocumentStats
report : AppPolicyReport
} derive(Debug)pub(all) struct AppMessage {
version : Int
kind : AppMessageKind
id : String
correlation_id : String?
headers : Array[(String, String)]
payload : CborValue
} derive(Eq, Debug)fn AppMessage::new(version~ : Int, kind~ : AppMessageKind, id~ : String, payload~ : CborValue) -> AppMessagepub(all) struct AppMessageBatch {
producer : String
sequence : UInt64
messages : Array[AppMessage]
} derive(Eq, Debug)pub(all) struct AppMessageStore {
records : Array[AppStoreRecord]
dead_letters : Array[AppMessage]
capacity : Int
max_retries : Int
} derive(Debug)pub(all) struct AppStoreRecord {
message : AppMessage
state : AppStoreState
retries : Int
} derive(Eq, Debug)pub(all) struct CborFrameDecoder {
pending : Bytes
max_frame_size : Int
frames_seen : Int
bytes_seen : Int
} derive(Debug)pub(all) enum CborPatch {
Set(Array[CborPathSegment], CborValue)
Remove(Array[CborPathSegment])
Merge(Array[CborPathSegment], CborValue)
} derive(Eq, Debug)pub(all) enum ValidationRule {
Required(String)
RequiredText(String)
RequiredBool(String)
RequiredInteger(String)
IntegerRange(String, Int64, Int64)
TextLength(String, Int, Int)
ArrayLength(String, Int, Int)
ObjectFields(String, Array[ValidationRule])
ArrayItems(String, Array[ValidationRule])
OneOfText(String, Array[String])
} derive(Eq, Debug)fn app_batch_new(producer~ : String, sequence~ : UInt64, messages~ : Array[AppMessage]) -> AppMessageBatchfn app_ingest_add_business_issue(result : AppIngestResult, path : String, code : String, message : String) -> AppIngestResultfn app_store_append_batch(store : AppMessageStore, messages : Array[AppMessage]) -> (AppMessageStore, Int)fn append_frame_with_stats(buffer : Buffer, value : CborValue, max_frame_size : Int, stats : CborFrameStats) -> CborFrameStats raise CborErrorfn cbor_document_within_budget(value : CborValue, max_nodes : Int, max_encoded_bytes : Int, max_depth : Int) -> Boolfn cbor_frame_decoder_feed(decoder : CborFrameDecoder, chunk : Bytes) -> (CborFrameDecoder, Array[CborValue]) raise CborErrorfn cbor_query_optional(value : CborValue, path : Array[CborPathSegment]) -> CborValue? raise CborErrorfn validate_app_batch_for_transport(batch : AppMessageBatch, max_encoded_bytes : Int) -> Unit raise CborErrorfn validate_message_payload(message : AppMessage, rules : Array[ValidationRule]) -> ValidationReportA high-performance, standard-compliant CBOR (RFC 8949) serialization library for MoonBit.