Sign in

    cbor

    CBOR (RFC 8949) implementation for MoonBit - Pure and FFI versions

    cbor
    serialization
    encoding
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    10 months ago
    Downloads
    57K

    No dependencies

    #cbor.mbt

    #Features

    • Pure MoonBit implementation - Fast, portable, works on native and wasm-gc
    • RFC 8949 compliant - Comprehensive test suite with 22+ test cases
    • Zero dependencies - Standalone implementation with no external dependencies
    • Well-tested - Includes RFC 8949 example tests and roundtrip verification

    #Supported Types

    • Integers (signed/unsigned, Int64/UInt64)
    • Floating-point numbers (64-bit Double)
    • Strings (UTF-8 text strings)
    • Byte arrays (byte strings)
    • Booleans (true/false)
    • Null values

    #Usage

    // Encoding
    let encoded_int = @cbor.encode_int(42L)
    let encoded_str = @cbor.encode_string("hello")
    let encoded_double = @cbor.encode_double(3.14)
    let encoded_bool = @cbor.encode_bool(true)
    let encoded_bytes = @cbor.encode_bytes(Bytes::from_array([b'\x01', b'\x02']))
    let encoded_null = @cbor.encode_null()

    // Decoding
    let decoded_int = @cbor.decode_int(encoded_int)!
    let decoded_str = @cbor.decode_string(encoded_str)!
    let decoded_double = @cbor.decode_double(encoded_double)!
    let decoded_bool = @cbor.decode_bool(encoded_bool)!
    let decoded_bytes = @cbor.decode_bytes(encoded_bytes)!

    #Installation

    Add to your moon.mod.json:
    { "deps": { "mizchi/cbor": "0.1.0" } }

    #Testing

    The library includes comprehensive tests covering:

    • RFC 8949 Examples - Validates encoding against official specification examples
    • Roundtrip Tests - Ensures encode/decode operations are reversible
    • Edge Cases - Tests boundary values and special cases

    # Run all tests moon test # Total: 22 tests covering all supported types

    #Test Coverage

    • Integer encoding (positive, negative, various sizes)
    • String encoding (ASCII, Unicode, emoji)
    • Byte array encoding (empty, various sizes)
    • Boolean and null values
    • Double precision floats (including infinity)
    • Boundary value testing (format transitions at 24, 256, 65536)

    #Examples

    See examples/ directory for usage examples:

    moon run examples

    #Project Structure

    cbor.mbt # Main CBOR implementation cbor_test.mbt # Comprehensive test suite (22+ tests) cbor_bench.mbt # Performance benchmarks examples/ # Usage examples └── main.mbt # Example usage cbor_rust/ # Rust reference implementation (for comparison) └── src/lib.rs # ciborium-based implementation

    #License

    MIT License

    #References

    CborError

    pub suberror CborError String

    Pure MoonBit CBOR implementation (Optimized) Based on RFC 8949

    CborValue

    pub enum CborValue {
    VUInt(UInt64)
    VInt(Int64)
    VBytes(Bytes)
    VString(String)
    VArray(Array[CborValue])
    VMap(Array[(CborValue, CborValue)])
    VTag(UInt64, CborValue)
    VBool(Bool)
    VNull
    VUndefined
    VDouble(Double)
    }

    CBOR value representation supporting nested structures
    impl Eq for CborValue
    impl Show for CborValue

    decode

    fn decode(input : Bytes) -> CborValue raise CborError

    Decode CBOR bytes to CborValue (supports nested structures)

    decode_bool

    fn decode_bool(input : Bytes) -> Bool raise CborError

    Decode boolean

    decode_bytes

    fn decode_bytes(input : Bytes) -> Bytes raise CborError

    Decode bytes - optimized with direct copy

    decode_double

    fn decode_double(input : Bytes) -> Double raise CborError

    Decode double

    decode_int

    fn decode_int(input : Bytes) -> Int64 raise CborError

    Decode signed integer

    decode_string

    fn decode_string(input : Bytes) -> String raise CborError

    Decode string - optimized UTF-8 decoding

    encode

    fn encode(value : CborValue) -> Bytes

    Encode CborValue to CBOR bytes (supports nested structures)

    encode_bool

    fn encode_bool(value : Bool) -> Bytes

    Encode boolean - uses pre-allocated constants

    encode_bytes

    fn encode_bytes(value : Bytes) -> Bytes

    Encode bytes (byte string) - optimized

    encode_double

    fn encode_double(value : Double) -> Bytes

    Encode double (64-bit float) using Bytes::makei

    encode_int

    fn encode_int(value : Int64) -> Bytes

    Encode signed integer (Int64)

    encode_null

    fn encode_null() -> Bytes

    Encode null - uses pre-allocated constant

    encode_string

    fn encode_string(value : String) -> Bytes

    Encode string (UTF-8 text string) - optimized with FixedArray

    encode_uint

    fn encode_uint(value : UInt64) -> Bytes

    Encode unsigned integer

    varray

    fn varray(v : Array[CborValue]) -> CborValue

    vbool

    fn vbool(v : Bool) -> CborValue

    vbytes

    fn vbytes(v : Bytes) -> CborValue

    vdouble

    fn vdouble(v : Double) -> CborValue

    vint

    fn vint(v : Int64) -> CborValue

    vmap

    fn vmap(v : Array[(CborValue, CborValue)]) -> CborValue

    vnull

    fn vnull() -> CborValue

    vstring

    fn vstring(v : String) -> CborValue

    vtag

    fn vtag(tag : UInt64, v : CborValue) -> CborValue

    vuint

    fn vuint(v : UInt64) -> CborValue

    vundefined

    fn vundefined() -> CborValue