/// MessagePack encoding and decoding (issue #140; `@derive(MsgPack)` is /// ADR 0051). /// /// msgpack.write(Str("hi")) // the Bytes a2 68 69 /// msgpack.read(data) // Result /// msgpack.encode([1, 2, 3]) // Bytes, through ToMsgPack /// msgpack.decode(data) // Result, through FromMsgPack /// /// `write` uses the smallest encoding of each integer and length, big- /// endian, as the specification asks of encoders. `read` is a parser of /// untrusted input (request bodies): a length prefix is checked against /// the bytes left before anything is taken, nesting is bounded (see /// [`max_depth`]), `str` payloads are checked as UTF-8 (ADR 0012), and a /// `float 32` is widened to a `Float`. /// /// An `Int` is MessagePack's uint64 or int64; writing one outside that /// range panics (Louise's `Int` promotes to a big integer, ADR 0014, but /// the wire has no bigger integer — an `Ext` bignum is ADR 0051's /// decision). Reading a uint64 above 2^62 gives a big integer, as it /// should. An `Ext` tag is a signed 8-bit integer; writing any other tag /// panics. /// /// The codec is pure Louise over `Bytes`, so both backends run it. If a /// benchmark shows it hot, a kernel in `louise_rt_prims` is ADR 0051's /// question, not a new extern's. import std/array.{type Array} import std/bytes import std/dict.{type Dict} import std/float import std/int import std/list import std/result /// A MessagePack value. @derive Eq Debug pub type Value { Nil , Bool(Bool) , Int(Int) , Float(Float) , Str(String) , Bin(Bytes) , Array(List) , Map(List<(Value, Value)>) /// An extension: MessagePack's "extension type" (`tag`, signed 8 bits) /// and its payload. , Ext(tag: Int, data: Bytes) } /// Why [`read`] or [`from_msgpack`](FromMsgPack) failed. @derive Eq Debug pub type DecodeError { /// The bytes are not a MessagePack value; `at` is the byte offset. Malformed(at: Int, message: String) /// The bytes decoded, but the value is not the one the type expected. , Mismatch(expected: String, found: String) } impl Show { fn show :: e { match e { Malformed(at, message) -> "invalid MessagePack at byte {at}: {message}" , Mismatch (expected, found) -> "MessagePack: expected {expected}, found {found}" } } } /// The most nesting [`read`] accepts (arrays and maps; 512). Deeper /// input is an error, so a hostile body cannot overflow the stack. pub fn max_depth :: -> Int { 512 } // ---- encoding ------------------------------------------------------------------ /// `value` as MessagePack bytes (the smallest encoding of each integer /// and length). Panics on an `Int` outside the uint64/int64 range and on /// an `Ext` tag outside -128 to 127. pub fn write :: value: Value -> Bytes { bytes.build (write_value (bytes.builder ()) value) } fn write_value :: b: bytes.Builder value: Value -> bytes.Builder { match value { Nil -> bytes.append_byte b 192 , Bool(False) -> bytes.append_byte b 194 , Bool(True) -> bytes.append_byte b 195 , Int(n) -> write_int b n , Float(x) -> be64 (bytes.append_byte b 203) (float.to_bits x) , Str(s) -> write_str b s , Bin(data) -> write_bin b data , Array(items) -> { let b = write_array_header b (list.length items) list.fold items b write_value } , Map(entries) -> { let b = write_map_header b (list.length entries) list.fold entries b write_entry } , Ext(tag, data) -> write_ext b tag data } } fn write_entry :: b: bytes.Builder entry: (Value, Value) -> bytes.Builder { write_value (write_value b entry.0) entry.1 } /// The smallest encoding of `n`: positive fixint, then uint 8/16/32/64; /// negative fixint, then int 8/16/32/64. fn write_int :: b: bytes.Builder n: Int -> bytes.Builder { if n >= 0 { if n < 128 { bytes.append_byte b n } else if n < 256 { bytes.append_byte (bytes.append_byte b 204) n } else if n < 65536 { be16 (bytes.append_byte b 205) n } else if n < 4294967296 { be32 (bytes.append_byte b 206) n } else if int.bit_length n <= 64 { be64 (bytes.append_byte b 207) n } else { panic "msgpack: an Int above 2^64 - 1 has no MessagePack encoding: {n}" } } else if n >= -32 { bytes.append_byte b (n + 256) } else if n >= -128 { bytes.append_byte (bytes.append_byte b 208) (n + 256) } else if n >= -32768 { be16 (bytes.append_byte b 209) n } else if n >= -2147483648 { be32 (bytes.append_byte b 210) n } else { // -2^63 itself: its magnitude is 2^63, so a bit length cannot tell // it from -2^63 - 1; compare against the computed bound. let i64_min = 0 - int.shift_left 1 63 if n >= i64_min { be64 (bytes.append_byte b 211) n } else { panic "msgpack: an Int below -2^63 has no MessagePack encoding: {n}" } } } fn write_str :: b: bytes.Builder s: String -> bytes.Builder { let data = bytes.from_string s let b = write_str_header b (bytes.length data) bytes.append b data } fn write_str_header :: b: bytes.Builder len: Int -> bytes.Builder { if len < 32 { bytes.append_byte b (160 + len) } else if len < 256 { bytes.append_byte (bytes.append_byte b 217) len } else if len < 65536 { be16 (bytes.append_byte b 218) len } else { be32 (bytes.append_byte b 219) (check_len len) } } fn write_bin :: b: bytes.Builder data: Bytes -> bytes.Builder { let len = bytes.length data let b = if len < 256 { bytes.append_byte (bytes.append_byte b 196) len } else if len < 65536 { be16 (bytes.append_byte b 197) len } else { be32 (bytes.append_byte b 198) (check_len len) } bytes.append b data } fn write_array_header :: b: bytes.Builder len: Int -> bytes.Builder { if len < 16 { bytes.append_byte b (144 + len) } else if len < 65536 { be16 (bytes.append_byte b 220) len } else { be32 (bytes.append_byte b 221) (check_len len) } } fn write_map_header :: b: bytes.Builder len: Int -> bytes.Builder { if len < 16 { bytes.append_byte b (128 + len) } else if len < 65536 { be16 (bytes.append_byte b 222) len } else { be32 (bytes.append_byte b 223) (check_len len) } } fn write_ext :: b: bytes.Builder tag: Int data: Bytes -> bytes.Builder { if tag < -128 || tag > 127 { panic "msgpack: an Ext tag is a signed 8-bit integer, got {tag}" } else { let len = bytes.length data let b = match len { 1 -> bytes.append_byte b 212 , 2 -> bytes.append_byte b 213 , 4 -> bytes.append_byte b 214 , 8 -> bytes.append_byte b 215 , 16 -> bytes.append_byte b 216 , _ if len < 256 -> bytes.append_byte (bytes.append_byte b 199) len , _ if len < 65536 -> be16 (bytes.append_byte b 200) len , _ -> be32 (bytes.append_byte b 201) (check_len len) } let b = bytes.append_byte b (int.low_bits tag 8) bytes.append b data } } /// Lengths are 32 bits on the wire; more cannot be written. fn check_len :: len: Int -> Int { if len >= 4294967296 { panic "msgpack: more than 2^32 - 1 items or bytes cannot be written" } else { len } } /// `n` as 2 big-endian bytes (its low 16 bits, two's complement). fn be16 :: b: bytes.Builder n: Int -> bytes.Builder { let n = int.low_bits n 16 let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) bytes.append_byte b (int.and n 255) } /// `n` as 4 big-endian bytes (its low 32 bits). fn be32 :: b: bytes.Builder n: Int -> bytes.Builder { let n = int.low_bits n 32 let b = bytes.append_byte b (int.and (int.shift_right n 24) 255) let b = bytes.append_byte b (int.and (int.shift_right n 16) 255) let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) bytes.append_byte b (int.and n 255) } /// `n` as 8 big-endian bytes (its low 64 bits). fn be64 :: b: bytes.Builder n: Int -> bytes.Builder { let n = int.low_bits n 64 let b = bytes.append_byte b (int.and (int.shift_right n 56) 255) let b = bytes.append_byte b (int.and (int.shift_right n 48) 255) let b = bytes.append_byte b (int.and (int.shift_right n 40) 255) let b = bytes.append_byte b (int.and (int.shift_right n 32) 255) let b = bytes.append_byte b (int.and (int.shift_right n 24) 255) let b = bytes.append_byte b (int.and (int.shift_right n 16) 255) let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) bytes.append_byte b (int.and n 255) } // ---- decoding ------------------------------------------------------------------ /// A cursor over the input. type Cursor { Cursor(bytes: Bytes, at: Int) } /// The first MessagePack value in `data`. Bytes after it are an error. pub fn read :: data: Bytes -> Result { use (value, cur) <- result.try (read_value Cursor(data, 0) 0) if cur.at == bytes.length data { Ok(value) } else { Error( Malformed( cur.at , "{bytes.length data - cur.at} bytes after the value" ) ) } } fn read_value :: cur: Cursor depth: Int -> Result<(Value, Cursor), DecodeError> { if depth > max_depth () { Error(Malformed(cur.at, "nesting deeper than {max_depth ()} levels")) } else { use (byte, cur) <- result.try (read_byte cur) if byte < 128 { Ok((Int(byte), cur)) } else if byte >= 224 { Ok((Int(byte - 256), cur)) } else if byte < 144 { read_map cur (byte - 128) depth } else if byte < 160 { read_array cur (byte - 144) depth } else if byte < 192 { read_str cur (byte - 160) } else { match byte { 192 -> Ok((Nil, cur)) , 193 -> Error(Malformed(cur.at - 1, "0xc1 is not a MessagePack byte")) , 194 -> Ok((Bool(False), cur)) , 195 -> Ok((Bool(True), cur)) , 196 -> read_bin_len cur 1 , 197 -> read_bin_len cur 2 , 198 -> read_bin_len cur 4 , 199 -> read_ext_len cur 1 , 200 -> read_ext_len cur 2 , 201 -> read_ext_len cur 4 , 202 -> read_float cur 4 , 203 -> read_float cur 8 , 204 -> read_uint_value cur 1 , 205 -> read_uint_value cur 2 , 206 -> read_uint_value cur 4 , 207 -> read_uint_value cur 8 , 208 -> read_int_value cur 1 , 209 -> read_int_value cur 2 , 210 -> read_int_value cur 4 , 211 -> read_int_value cur 8 , 212 -> read_ext cur 1 , 213 -> read_ext cur 2 , 214 -> read_ext cur 4 , 215 -> read_ext cur 8 , 216 -> read_ext cur 16 , 217 -> read_str_len cur 1 , 218 -> read_str_len cur 2 , 219 -> read_str_len cur 4 , 220 -> read_array_len cur 2 depth , 221 -> read_array_len cur 4 depth , 222 -> read_map_len cur 2 depth , 223 -> read_map_len cur 4 depth // Unreachable: the arms above cover 192 to 223. , _ -> Error(Malformed(cur.at - 1, "a byte the codec does not know: {byte}")) } } } } fn read_uint_value :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (n, cur) <- result.try (read_uint cur width) Ok((Int(n), cur)) } fn read_int_value :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (n, cur) <- result.try (read_uint cur width) Ok((Int(signed n (width * 8)), cur)) } /// `n` (unsigned, `bits` wide) as signed: two's complement. fn signed :: n: Int bits: Int -> Int { let sign = int.shift_left 1 (bits - 1) if int.and n sign == 0 { n } else { n - int.shift_left sign 1 } } /// A float 32 (`width` 4) is widened; a float 64 (`width` 8) is exact. fn read_float :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (bits, cur) <- result.try (read_uint cur width) let x = if width == 4 { float.from_bits32 bits } else { float.from_bits bits } Ok((Float(x), cur)) } fn read_str_len :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (len, cur) <- result.try (read_uint cur width) read_str cur len } fn read_str :: cur: Cursor len: Int -> Result<(Value, Cursor), DecodeError> { let left = bytes.length cur.bytes - cur.at if len < 0 || len > left { Error(Malformed(cur.at, "a length of {len} with {left} bytes left")) } else { match bytes.to_string_range cur.bytes cur.at len { Ok(s) -> Ok((Str(s), Cursor(cur.bytes, cur.at + len))) , Error(e) -> Error(Malformed(cur.at, "a str that is not UTF-8: {e}")) } } } fn read_bin_len :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (len, cur) <- result.try (read_uint cur width) use (data, cur) <- result.try (take cur len) Ok((Bin(data), cur)) } fn read_ext_len :: cur: Cursor width: Int -> Result<(Value, Cursor), DecodeError> { use (len, cur) <- result.try (read_uint cur width) read_ext cur len } fn read_ext :: cur: Cursor len: Int -> Result<(Value, Cursor), DecodeError> { use (byte, cur) <- result.try (read_byte cur) let tag = if byte >= 128 { byte - 256 } else { byte } use (data, cur) <- result.try (take cur len) Ok((Ext(tag, data), cur)) } fn read_array_len :: cur: Cursor width: Int depth: Int -> Result<(Value, Cursor), DecodeError> { use (count, cur) <- result.try (read_uint cur width) read_array cur count depth } fn read_array :: cur: Cursor count: Int depth: Int -> Result<(Value, Cursor), DecodeError> { use _ <- result.try (check_count cur count "an array") read_items cur count depth [] } fn read_items :: cur: Cursor count: Int depth: Int acc: List -> Result<(Value, Cursor), DecodeError> { if count == 0 { Ok((Array(list.reverse acc), cur)) } else { use (item, cur) <- result.try (read_value cur (depth + 1)) read_items cur (count - 1) depth [item, ..acc] } } fn read_map_len :: cur: Cursor width: Int depth: Int -> Result<(Value, Cursor), DecodeError> { use (count, cur) <- result.try (read_uint cur width) read_map cur count depth } fn read_map :: cur: Cursor count: Int depth: Int -> Result<(Value, Cursor), DecodeError> { use _ <- result.try (check_count cur count "a map") read_entries cur count depth [] } fn read_entries :: cur: Cursor count: Int depth: Int acc: List<(Value, Value)> -> Result<(Value, Cursor), DecodeError> { if count == 0 { Ok((Map(list.reverse acc), cur)) } else { use (key, cur) <- result.try (read_value cur (depth + 1)) use (value, cur) <- result.try (read_value cur (depth + 1)) read_entries cur (count - 1) depth [(key, value), ..acc] } } /// A length prefix is checked against the bytes left before anything is /// read: each item takes at least one byte, so more items than bytes can /// never be there. fn check_count :: cur: Cursor count: Int what: String -> Result<(), DecodeError> { let left = bytes.length cur.bytes - cur.at if count > left { Error(Malformed(cur.at, "{what} of {count} items with {left} bytes left")) } else { Ok(()) } } fn read_byte :: cur: Cursor -> Result<(Int, Cursor), DecodeError> { let byte = bytes.read_u8 cur.bytes cur.at if byte < 0 { Error(Malformed(cur.at, "the input ends early")) } else { Ok((byte, Cursor(cur.bytes, cur.at + 1))) } } /// The unsigned big-endian integer of the next `width` bytes (1, 2, 4 or /// 8; 8 can be a big integer, above 2^62), read in place. fn read_uint :: cur: Cursor width: Int -> Result<(Int, Cursor), DecodeError> { let left = bytes.length cur.bytes - cur.at if width > left { Error(Malformed(cur.at, "a length of {width} with {left} bytes left")) } else { let n = match width { 1 -> bytes.read_u8 cur.bytes cur.at , 2 -> bytes.read_u16be cur.bytes cur.at , 4 -> bytes.read_u32be cur.bytes cur.at , _ -> bytes.read_u64be cur.bytes cur.at } if n < 0 { // Unreachable: the bounds were checked above. Error(Malformed(cur.at, "a length of {width} with {left} bytes left")) } else { Ok((n, Cursor(cur.bytes, cur.at + width))) } } } /// The next `n` bytes, if they are all there. fn take :: cur: Cursor n: Int -> Result<(Bytes, Cursor), DecodeError> { let left = bytes.length cur.bytes - cur.at if n < 0 || n > left { Error(Malformed(cur.at, "a length of {n} with {left} bytes left")) } else { Ok( ( bytes.slice cur.bytes cur.at (cur.at + n) , Cursor(cur.bytes, cur.at + n) ) ) } } // ---- the traits ----------------------------------------------------------------- /// Encoding to MessagePack (ADR 0051). `@derive(ToMsgPack)` writes a /// record as a map keyed by field label, a positional constructor as a /// map from its name to its fields' array, and a constructor without /// fields as its name. @derivable to_msgpack_of_constructor pub trait ToMsgPack { fn to_msgpack :: x: a -> Value } /// The derive function of [`ToMsgPack`] (ADR 0019). fn to_msgpack_of_constructor :: c: Constructor -> Value { match c.fields { [] -> Str(c.name) , [f, ..] if f.label != "" -> Map(list.map c.fields fn f { (Str(f.label), f.value) }) , fields -> Map([(Str(c.name), Array(list.map fields fn f { f.value }))]) } } /// Decoding from MessagePack (ADR 0051). Hand-written until the ADR /// lands: ADR 0019's derives consume a value, they do not produce one. pub trait FromMsgPack { fn from_msgpack :: v: Value -> Result } /// `x` as MessagePack bytes: [`write`] of [`to_msgpack`](ToMsgPack). pub fn encode :: x: a -> Bytes where ToMsgPack { write (to_msgpack x) } /// The `a` encoded in `data`: [`read`], then [`from_msgpack`](FromMsgPack). pub fn decode :: data: Bytes -> Result where FromMsgPack { use value <- result.try (read data) from_msgpack value } /// What a `Value` is, for error messages ("an Int", "a Map"). pub fn kind :: v: Value -> String { match v { Nil -> "nil" , Bool(_) -> "a Bool" , Int(_) -> "an Int" , Float(_) -> "a Float" , Str(_) -> "a Str" , Bin(_) -> "a Bin" , Array(_) -> "an Array" , Map(_) -> "a Map" , Ext(_, _) -> "an Ext" } } impl ToMsgPack { fn to_msgpack :: n { Int(n) } } impl FromMsgPack { fn from_msgpack :: v { match v { Int(n) -> Ok(n), _ -> Error(Mismatch("an Int", kind v)) } } } impl ToMsgPack { fn to_msgpack :: x { Float(x) } } impl FromMsgPack { fn from_msgpack :: v { match v { Float(x) -> Ok(x) // Many encoders write a whole number as an integer. , Int(n) -> Ok(float.from_int n) , _ -> Error(Mismatch("a Float", kind v)) } } } impl ToMsgPack { fn to_msgpack :: s { Str(s) } } impl FromMsgPack { fn from_msgpack :: v { match v { Str(s) -> Ok(s), _ -> Error(Mismatch("a Str", kind v)) } } } impl ToMsgPack { fn to_msgpack :: b { Bin(b) } } impl FromMsgPack { fn from_msgpack :: v { match v { Bin(b) -> Ok(b), _ -> Error(Mismatch("a Bin", kind v)) } } } impl ToMsgPack { fn to_msgpack :: b { Bool(b) } } impl FromMsgPack { fn from_msgpack :: v { match v { Bool(b) -> Ok(b), _ -> Error(Mismatch("a Bool", kind v)) } } } impl ToMsgPack> where ToMsgPack { fn to_msgpack :: xs { Array(list.map xs to_msgpack) } } impl FromMsgPack> where FromMsgPack { fn from_msgpack :: v { match v { Array(items) -> result.all (list.map items from_msgpack) , _ -> Error(Mismatch("an Array", kind v)) } } } impl ToMsgPack> where ToMsgPack { fn to_msgpack :: xs { Array(list.map (array.to_list xs) to_msgpack) } } impl FromMsgPack> where FromMsgPack { fn from_msgpack :: v { match v { Array(items) -> result.map (result.all (list.map items from_msgpack)) array.from_list , _ -> Error(Mismatch("an Array", kind v)) } } } /// `None` is nil and `Some(x)` is `x` (the ecosystem's convention, as in /// rmp): a nested `Option>` cannot tell `Some(None)` from /// `None`. impl ToMsgPack> where ToMsgPack { fn to_msgpack :: o { match o { None -> Nil, Some(x) -> to_msgpack x } } } impl FromMsgPack> where FromMsgPack { fn from_msgpack :: v { match v { Nil -> Ok(None), _ -> result.map (from_msgpack v) fn x { Some(x) } } } } /// As the derived format for a sum: `\{"Ok": [x]}` or `\{"Error": [e]}`. impl ToMsgPack> where ToMsgPack, ToMsgPack { fn to_msgpack :: r { match r { Ok(x) -> Map([(Str("Ok"), Array([to_msgpack x]))]) , Error(x) -> Map([(Str("Error"), Array([to_msgpack x]))]) } } } impl FromMsgPack> where FromMsgPack, FromMsgPack { fn from_msgpack :: v { match v { Map([(key, Array([x]))]) if key == Str("Ok") -> { match from_msgpack x { Ok(y) -> Ok(Ok(y)), Error(e) -> Error(e) } } , Map([(key, Array([x]))]) if key == Str("Error") -> { match from_msgpack x { Ok(y) -> Ok(Error(y)), Error(e) -> Error(e) } } , _ -> Error( Mismatch( "a Result (\{\"Ok\": [..]\} or \{\"Error\": [..]\})" , kind v ) ) } } } /// A `Dict` is a map; decoding needs `Ord` to rebuild it. impl ToMsgPack> where ToMsgPack, ToMsgPack { fn to_msgpack :: d { Map(list.map (dict.to_list d) fn p { (to_msgpack p.0, to_msgpack p.1) }) } } impl FromMsgPack> where FromMsgPack, FromMsgPack, Ord { fn from_msgpack :: v { match v { Map(entries) -> { use pairs <- result.try (result.all (list.map entries decode_entry)) Ok(dict.from_list pairs) } , _ -> Error(Mismatch("a Map", kind v)) } } } fn decode_entry :: p: (Value, Value) -> Result<(k, v), DecodeError> where FromMsgPack, FromMsgPack { use k <- result.try (from_msgpack p.0) use v <- result.try (from_msgpack p.1) Ok((k, v)) } // ---- tests ------------------------------------------------------------------------ import std/string import std/test.{assert, assert_equal} @test fn integers_smallest_encoding :: { assert_equal (bytes.to_list (write Int(0))) [0] assert_equal (bytes.to_list (write Int(127))) [127] assert_equal (bytes.to_list (write Int(128))) [204, 128] assert_equal (bytes.to_list (write Int(255))) [204, 255] assert_equal (bytes.to_list (write Int(256))) [205, 1, 0] assert_equal (bytes.to_list (write Int(65535))) [205, 255, 255] assert_equal (bytes.to_list (write Int(65536))) [206, 0, 1, 0, 0] assert_equal (bytes.to_list (write Int(4294967295))) [206, 255, 255, 255, 255] assert_equal (bytes.to_list (write Int(4294967296))) [207, 0, 0, 0, 1, 0, 0, 0, 0] assert_equal (bytes.to_list (write Int(-1))) [255] assert_equal (bytes.to_list (write Int(-32))) [224] assert_equal (bytes.to_list (write Int(-33))) [208, 223] assert_equal (bytes.to_list (write Int(-128))) [208, 128] assert_equal (bytes.to_list (write Int(-129))) [209, 255, 127] assert_equal (bytes.to_list (write Int(-32768))) [209, 128, 0] assert_equal (bytes.to_list (write Int(-32769))) [210, 255, 255, 127, 255] assert_equal (bytes.to_list (write Int(-2147483648))) [210, 128, 0, 0, 0] assert_equal (bytes.to_list (write Int(-2147483649))) [211, 255, 255, 255, 255, 127, 255, 255, 255] } @test fn scalars_round_trip :: { let values = [ Nil , Bool(True) , Bool(False) , Int(0 - int.shift_left 1 63) , Int(int.shift_left 1 64 - 1) , Float(-0.0) , Float(3.14159) , Str("") , Str("héllo, 世界") , Bin(bytes.empty ()) , Bin(bytes.from_list [0, 159, 146, 150]) , Array([]) , Map([]) , Ext(-1, bytes.from_list [1, 2, 3, 4]) , Ext(127, bytes.empty ()) , Ext(2, bytes.from_list [1, 2]) , Ext(3, bytes.from_list [1, 2, 3, 4, 5, 6, 7, 8]) , Ext( 4 , bytes.from_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] ) ] list.fold values () fn _ v { assert (read (write v) == Ok(v)) ("round trip of " <> debug v) } } @test fn lengths_smallest_encoding :: { // fixstr up to 31 bytes, str 8 from 32; fixarray up to 15 items, // array 16 from 16. assert_equal (bytes.to_list (bytes.slice (write Str(string.repeat "a" 31)) 0 1)) [191] assert_equal (bytes.to_list (bytes.slice (write Str(string.repeat "a" 32)) 0 2)) [217, 32] assert_equal (bytes.to_list (bytes.slice (write Str(string.repeat "a" 256)) 0 3)) [218, 1, 0] let items = list.map (list.range 1 15) Int_of assert_equal (bytes.to_list (bytes.slice (write Array(items)) 0 1)) [159] let items = list.map (list.range 1 16) Int_of assert_equal (bytes.to_list (bytes.slice (write Array(items)) 0 3)) [220, 0, 16] // fixext 1/2/4/8/16 for those sizes, ext 8 beyond. assert_equal (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0])) 0 1)) [212] assert_equal (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0, 0])) 0 1)) [213] assert_equal (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0, 0, 0])) 0 2)) [199, 3] } fn Int_of :: n: Int -> Value { Int(n) } @test fn malformed_inputs_are_errors :: { // Truncated, a length past the bytes left, the reserved byte, and // nesting past the cap: an error, never a panic or an allocation. assert ( read (bytes.from_list [204]) == Error(Malformed(1, "a length of 1 with 0 bytes left")) ) (debug (read (bytes.from_list [204]))) assert (read (bytes.empty ()) == Error(Malformed(0, "the input ends early"))) (debug (read (bytes.empty ()))) assert ( read (bytes.from_list [217, 5, 97]) == Error(Malformed(2, "a length of 5 with 1 bytes left")) ) (debug (read (bytes.from_list [217, 5, 97]))) assert ( read (bytes.from_list [221, 255, 255, 255, 255, 1]) == Error(Malformed(5, "an array of 4294967295 items with 1 bytes left")) ) (debug (read (bytes.from_list [221, 255, 255, 255, 255, 1]))) assert ( read (bytes.from_list [193]) == Error(Malformed(0, "0xc1 is not a MessagePack byte")) ) (debug (read (bytes.from_list [193]))) assert ( read (bytes.from_list [161, 255]) == Error( Malformed( 1 , "a str that is not UTF-8: invalid UTF-8 at byte 0" ) ) ) (debug (read (bytes.from_list [161, 255]))) assert ( read (bytes.from_list [1, 1]) == Error(Malformed(1, "1 bytes after the value")) ) (debug (read (bytes.from_list [1, 1]))) // A map entry's key without its value. assert ( read (bytes.from_list [129, 161, 97]) == Error(Malformed(3, "the input ends early")) ) (debug (read (bytes.from_list [129, 161, 97]))) // Truncated at every width of integer read, and inside bin and ext // payloads. assert ( read (bytes.from_list [205, 1]) == Error(Malformed(1, "a length of 2 with 1 bytes left")) ) (debug (read (bytes.from_list [205, 1]))) assert ( read (bytes.from_list [206, 1, 2, 3]) == Error(Malformed(1, "a length of 4 with 3 bytes left")) ) (debug (read (bytes.from_list [206, 1, 2, 3]))) assert ( read (bytes.from_list [207, 1, 2, 3, 4, 5, 6, 7]) == Error(Malformed(1, "a length of 8 with 7 bytes left")) ) (debug (read (bytes.from_list [207, 1, 2, 3, 4, 5, 6, 7]))) assert ( read (bytes.from_list [203, 1, 2, 3]) == Error(Malformed(1, "a length of 8 with 3 bytes left")) ) (debug (read (bytes.from_list [203, 1, 2, 3]))) assert ( read (bytes.from_list [196, 5, 97]) == Error(Malformed(2, "a length of 5 with 1 bytes left")) ) (debug (read (bytes.from_list [196, 5, 97]))) // fixext 1 without its tag byte, and fixext 2 without its payload. assert ( read (bytes.from_list [212]) == Error(Malformed(1, "the input ends early")) ) (debug (read (bytes.from_list [212]))) assert ( read (bytes.from_list [213, 5]) == Error(Malformed(2, "a length of 2 with 0 bytes left")) ) (debug (read (bytes.from_list [213, 5]))) // A str cut in the middle of a UTF-8 sequence. assert ( read (bytes.from_list [162, 104, 255]) == Error( Malformed( 1 , "a str that is not UTF-8: invalid UTF-8 at byte 1" ) ) ) (debug (read (bytes.from_list [162, 104, 255]))) } @test fn traits_encode_and_decode :: { assert_equal (bytes.to_list (encode [1, 2, 3])) [147, 1, 2, 3] assert_equal (decode (bytes.from_list [147, 1, 2, 3])) Ok([1, 2, 3]) let d = dict.from_list [("a", 1), ("b", 2)] match decode (encode d) { Ok(back) -> assert_equal (dict.to_list back) [("a", 1), ("b", 2)] , Error(e) -> panic (show e) } let none: Option = None assert_equal (bytes.to_list (encode none)) [192] let ok: Result = Ok(1) assert_equal (decode (encode ok)) Ok(ok) }