A lightweight binary serialization and deserialization library for MoonBit.
BinValue <-> Bytes///|
test "encode and decode a value" {
let value = BinValue::Object([
("name", BinValue::String("Hou")),
("age", BinValue::Int(18)),
])
let bytes = encode(value)
assert_true(decode(bytes) == Ok(value))
}///|
test "create primitive bin values" {
inspect(BinValue::Null.kind(), content="null")
inspect(BinValue::Bool(true).kind(), content="bool")
inspect(BinValue::Int(42).kind(), content="int")
inspect(BinValue::Double(3.14).kind(), content="double")
inspect(BinValue::String("moonbin").kind(), content="string")
}///|
struct User {
name : String
age : Int
}
///|
fn User::to_bin(self : User) -> BinValue {
BinValue::Object([
("name", BinValue::String(self.name)),
("age", BinValue::Int(self.age)),
])
}
///|
fn User::from_bin(value : BinValue) -> Result[User, DecodeError] {
match value {
BinValue::Object(
[("name", BinValue::String(name)), ("age", BinValue::Int(age))]
) => Ok({ name, age })
other => Err(DecodeError::InvalidType("User object", other.kind()))
}
}///|
test "decode error categories" {
inspect(DecodeError::UnexpectedEOF.kind(), content="unexpected_eof")
inspect(DecodeError::InvalidTag(255).kind(), content="invalid_tag")
}pub struct ByteReader {
data : Bytes
offset : Int
}fn error_model_name() -> Stringfn value_model_name() -> StringA lightweight binary serialization and deserialization library for MoonBit.