enum Value {
Nil
Bool(Bool)
Int(Int)
Int64(Int64)
UInt64(UInt64)
Float(Double)
String(String)
Binary(Bytes)
Array(Array[Value])
Map(Map[String, Value])
}
enum MsgPackError {
IOError(String)
TypeMismatch(String)
InvalidData(String)
}// Encode a MessagePack value to bytes
fn encode(value : Value) -> Bytes
// Decode bytes to a MessagePack value
fn decode(data : Bytes) -> Result[Value, MsgPackError]fn create_nil() -> Value
fn create_bool(b : Bool) -> Value
fn create_int(i : Int) -> Value
fn create_int64(i : Int64) -> Value
fn create_uint64(u : UInt64) -> Value
fn create_float(f : Double) -> Value
fn create_string(s : String) -> Value
fn create_binary(b : Bytes) -> Value
fn create_array(arr : Array[Value]) -> Value
fn create_map(map : Map[String, Value]) -> Valuefn Value::to_string(self : Value) -> Result[String, MsgPackError]
fn Value::to_int(self : Value) -> Result[Int, MsgPackError]
fn Value::to_bool(self : Value) -> Result[Bool, MsgPackError]// Create values
let nil_val = create_nil()
let bool_val = create_bool(true)
let int_val = create_int(42)
let str_val = create_string("hello")
// Encode to MessagePack
let nil_encoded = encode(nil_val) // 1 byte: [0xc0]
let bool_encoded = encode(bool_val) // 1 byte: [0xc3]
let int_encoded = encode(int_val) // 1 byte: [0x2a]
let str_encoded = encode(str_val) // 6 bytes: [0xa5, 'h', 'e', 'l', 'l', 'o']
// Decode from MessagePack
match decode(nil_encoded) {
Ok(Nil) => println("Successfully decoded nil")
Ok(_) => println("Wrong type")
Err(e) => println("Error: " + e.to_string())
}// Create an array
let array_val = create_array([
create_int(1),
create_int(2),
create_int(3)
])
// Create a map
let map = Map::new()
map["name"] = create_string("MoonBit")
map["version"] = create_int(1)
let map_val = create_map(map)
// Encode collections
let array_encoded = encode(array_val) // [0x93, 0x01, 0x02, 0x03]
let map_encoded = encode(map_val) // [0x82, ...] // Original data
let original = create_array([
create_nil(),
create_bool(true),
create_int(42),
create_string("hello")
])
// Encode and decode
let encoded = encode(original)
match decode(encoded) {
Ok(decoded) => {
// `decoded` should be equivalent to `original`
println("Round-trip successful!")
}
Err(e) => println("Round-trip failed: " + e.to_string())
}moon run .moon testpub enum MsgPackError {
TypeMismatch(String)
InvalidData(String)
}Install
Download zip