| @@ -33,7 +33,7 @@ import std/list |
| 33 | 33 | import std/result |
| 34 | 34 | |
| 35 | 35 | /// A MessagePack value. |
| 36 | | −@derive(Eq, Debug) |
| 36 | +@derive Eq Debug |
| 37 | 37 | pub type Value |
| 38 | 38 | { Nil |
| 39 | 39 | , Bool(Bool) |
| @@ -49,7 +49,7 @@ pub type Value |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /// Why [`read`] or [`from_msgpack`](FromMsgPack) failed. |
| 52 | | −@derive(Eq, Debug) |
| 52 | +@derive Eq Debug |
| 53 | 53 | pub type DecodeError |
| 54 | 54 | { /// The bytes are not a MessagePack value; `at` is the byte offset. |
| 55 | 55 | Malformed(at: Int, message: String) |
| @@ -58,7 +58,7 @@ pub type DecodeError |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | impl Show<DecodeError> { |
| 61 | | − fn show(e) { |
| 61 | + fn show :: e { |
| 62 | 62 | match e { |
| 63 | 63 | Malformed(at, message) -> "invalid MessagePack at byte {at}: {message}" |
| 64 | 64 | , Mismatch |
| @@ -69,7 +69,7 @@ impl Show<DecodeError> { |
| 69 | 69 | |
| 70 | 70 | /// The most nesting [`read`] accepts (arrays and maps; 512). Deeper |
| 71 | 71 | /// input is an error, so a hostile body cannot overflow the stack. |
| 72 | | −pub fn max_depth() -> Int { |
| 72 | +pub fn max_depth :: -> Int { |
| 73 | 73 | 512 |
| 74 | 74 | } |
| 75 | 75 | |
| @@ -78,178 +78,178 @@ pub fn max_depth() -> Int { |
| 78 | 78 | /// `value` as MessagePack bytes (the smallest encoding of each integer |
| 79 | 79 | /// and length). Panics on an `Int` outside the uint64/int64 range and on |
| 80 | 80 | /// an `Ext` tag outside -128 to 127. |
| 81 | | −pub fn write(value: Value) -> Bytes { |
| 82 | | − bytes.build(write_value(bytes.builder(), value)) |
| 81 | +pub fn write :: value: Value -> Bytes { |
| 82 | + bytes.build (write_value (bytes.builder ()) value) |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | | −fn write_value(b: bytes.Builder, value: Value) -> bytes.Builder { |
| 85 | +fn write_value :: b: bytes.Builder value: Value -> bytes.Builder { |
| 86 | 86 | match value { |
| 87 | | − Nil -> bytes.append_byte(b, 192) |
| 88 | | − , Bool(False) -> bytes.append_byte(b, 194) |
| 89 | | − , Bool(True) -> bytes.append_byte(b, 195) |
| 90 | | − , Int(n) -> write_int(b, n) |
| 91 | | − , Float(x) -> be64(bytes.append_byte(b, 203), float.to_bits(x)) |
| 92 | | − , Str(s) -> write_str(b, s) |
| 93 | | − , Bin(data) -> write_bin(b, data) |
| 87 | + Nil -> bytes.append_byte b 192 |
| 88 | + , Bool(False) -> bytes.append_byte b 194 |
| 89 | + , Bool(True) -> bytes.append_byte b 195 |
| 90 | + , Int(n) -> write_int b n |
| 91 | + , Float(x) -> be64 (bytes.append_byte b 203) (float.to_bits x) |
| 92 | + , Str(s) -> write_str b s |
| 93 | + , Bin(data) -> write_bin b data |
| 94 | 94 | , Array(items) -> { |
| 95 | | − let b = write_array_header(b, list.length(items)) |
| 96 | | − list.fold(items, b, write_value) |
| 95 | + let b = write_array_header b (list.length items) |
| 96 | + list.fold items b write_value |
| 97 | 97 | } |
| 98 | 98 | , Map(entries) -> { |
| 99 | | − let b = write_map_header(b, list.length(entries)) |
| 100 | | − list.fold(entries, b, write_entry) |
| 99 | + let b = write_map_header b (list.length entries) |
| 100 | + list.fold entries b write_entry |
| 101 | 101 | } |
| 102 | | − , Ext(tag, data) -> write_ext(b, tag, data) |
| 102 | + , Ext(tag, data) -> write_ext b tag data |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | | −fn write_entry(b: bytes.Builder, entry: (Value, Value)) -> bytes.Builder { |
| 107 | | − write_value(write_value(b, entry.0), entry.1) |
| 106 | +fn write_entry :: b: bytes.Builder entry: (Value, Value) -> bytes.Builder { |
| 107 | + write_value (write_value b entry.0) entry.1 |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /// The smallest encoding of `n`: positive fixint, then uint 8/16/32/64; |
| 111 | 111 | /// negative fixint, then int 8/16/32/64. |
| 112 | | −fn write_int(b: bytes.Builder, n: Int) -> bytes.Builder { |
| 112 | +fn write_int :: b: bytes.Builder n: Int -> bytes.Builder { |
| 113 | 113 | if n >= 0 { |
| 114 | 114 | if n < 128 { |
| 115 | | − bytes.append_byte(b, n) |
| 115 | + bytes.append_byte b n |
| 116 | 116 | } else if n < 256 { |
| 117 | | − bytes.append_byte(bytes.append_byte(b, 204), n) |
| 117 | + bytes.append_byte (bytes.append_byte b 204) n |
| 118 | 118 | } else if n < 65536 { |
| 119 | | − be16(bytes.append_byte(b, 205), n) |
| 119 | + be16 (bytes.append_byte b 205) n |
| 120 | 120 | } else if n < 4294967296 { |
| 121 | | − be32(bytes.append_byte(b, 206), n) |
| 122 | | − } else if int.bit_length(n) <= 64 { |
| 123 | | − be64(bytes.append_byte(b, 207), n) |
| 121 | + be32 (bytes.append_byte b 206) n |
| 122 | + } else if int.bit_length n <= 64 { |
| 123 | + be64 (bytes.append_byte b 207) n |
| 124 | 124 | } else { |
| 125 | | − panic("msgpack: an Int above 2^64 - 1 has no MessagePack encoding: {n}") |
| 125 | + panic "msgpack: an Int above 2^64 - 1 has no MessagePack encoding: {n}" |
| 126 | 126 | } |
| 127 | 127 | } else if n >= -32 { |
| 128 | | − bytes.append_byte(b, n + 256) |
| 128 | + bytes.append_byte b (n + 256) |
| 129 | 129 | } else if n >= -128 { |
| 130 | | − bytes.append_byte(bytes.append_byte(b, 208), n + 256) |
| 130 | + bytes.append_byte (bytes.append_byte b 208) (n + 256) |
| 131 | 131 | } else if n >= -32768 { |
| 132 | | − be16(bytes.append_byte(b, 209), n) |
| 132 | + be16 (bytes.append_byte b 209) n |
| 133 | 133 | } else if n >= -2147483648 { |
| 134 | | − be32(bytes.append_byte(b, 210), n) |
| 134 | + be32 (bytes.append_byte b 210) n |
| 135 | 135 | } else { |
| 136 | 136 | // -2^63 itself: its magnitude is 2^63, so a bit length cannot tell |
| 137 | 137 | // it from -2^63 - 1; compare against the computed bound. |
| 138 | | − let i64_min = 0 - int.shift_left(1, 63) |
| 138 | + let i64_min = 0 - int.shift_left 1 63 |
| 139 | 139 | if n >= i64_min { |
| 140 | | − be64(bytes.append_byte(b, 211), n) |
| 140 | + be64 (bytes.append_byte b 211) n |
| 141 | 141 | } else { |
| 142 | | − panic("msgpack: an Int below -2^63 has no MessagePack encoding: {n}") |
| 142 | + panic "msgpack: an Int below -2^63 has no MessagePack encoding: {n}" |
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | | −fn write_str(b: bytes.Builder, s: String) -> bytes.Builder { |
| 148 | | − let data = bytes.from_string(s) |
| 149 | | − let b = write_str_header(b, bytes.length(data)) |
| 150 | | − bytes.append(b, data) |
| 147 | +fn write_str :: b: bytes.Builder s: String -> bytes.Builder { |
| 148 | + let data = bytes.from_string s |
| 149 | + let b = write_str_header b (bytes.length data) |
| 150 | + bytes.append b data |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | | −fn write_str_header(b: bytes.Builder, len: Int) -> bytes.Builder { |
| 153 | +fn write_str_header :: b: bytes.Builder len: Int -> bytes.Builder { |
| 154 | 154 | if len < 32 { |
| 155 | | − bytes.append_byte(b, 160 + len) |
| 155 | + bytes.append_byte b (160 + len) |
| 156 | 156 | } else if len < 256 { |
| 157 | | − bytes.append_byte(bytes.append_byte(b, 217), len) |
| 157 | + bytes.append_byte (bytes.append_byte b 217) len |
| 158 | 158 | } else if len < 65536 { |
| 159 | | − be16(bytes.append_byte(b, 218), len) |
| 159 | + be16 (bytes.append_byte b 218) len |
| 160 | 160 | } else { |
| 161 | | − be32(bytes.append_byte(b, 219), check_len(len)) |
| 161 | + be32 (bytes.append_byte b 219) (check_len len) |
| 162 | 162 | } |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | | −fn write_bin(b: bytes.Builder, data: Bytes) -> bytes.Builder { |
| 166 | | − let len = bytes.length(data) |
| 165 | +fn write_bin :: b: bytes.Builder data: Bytes -> bytes.Builder { |
| 166 | + let len = bytes.length data |
| 167 | 167 | let b = if len < 256 { |
| 168 | | − bytes.append_byte(bytes.append_byte(b, 196), len) |
| 168 | + bytes.append_byte (bytes.append_byte b 196) len |
| 169 | 169 | } else if len < 65536 { |
| 170 | | − be16(bytes.append_byte(b, 197), len) |
| 170 | + be16 (bytes.append_byte b 197) len |
| 171 | 171 | } else { |
| 172 | | − be32(bytes.append_byte(b, 198), check_len(len)) |
| 172 | + be32 (bytes.append_byte b 198) (check_len len) |
| 173 | 173 | } |
| 174 | | − bytes.append(b, data) |
| 174 | + bytes.append b data |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | | −fn write_array_header(b: bytes.Builder, len: Int) -> bytes.Builder { |
| 177 | +fn write_array_header :: b: bytes.Builder len: Int -> bytes.Builder { |
| 178 | 178 | if len < 16 { |
| 179 | | − bytes.append_byte(b, 144 + len) |
| 179 | + bytes.append_byte b (144 + len) |
| 180 | 180 | } else if len < 65536 { |
| 181 | | − be16(bytes.append_byte(b, 220), len) |
| 181 | + be16 (bytes.append_byte b 220) len |
| 182 | 182 | } else { |
| 183 | | − be32(bytes.append_byte(b, 221), check_len(len)) |
| 183 | + be32 (bytes.append_byte b 221) (check_len len) |
| 184 | 184 | } |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | | −fn write_map_header(b: bytes.Builder, len: Int) -> bytes.Builder { |
| 187 | +fn write_map_header :: b: bytes.Builder len: Int -> bytes.Builder { |
| 188 | 188 | if len < 16 { |
| 189 | | − bytes.append_byte(b, 128 + len) |
| 189 | + bytes.append_byte b (128 + len) |
| 190 | 190 | } else if len < 65536 { |
| 191 | | − be16(bytes.append_byte(b, 222), len) |
| 191 | + be16 (bytes.append_byte b 222) len |
| 192 | 192 | } else { |
| 193 | | − be32(bytes.append_byte(b, 223), check_len(len)) |
| 193 | + be32 (bytes.append_byte b 223) (check_len len) |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | | −fn write_ext(b: bytes.Builder, tag: Int, data: Bytes) -> bytes.Builder { |
| 197 | +fn write_ext :: b: bytes.Builder tag: Int data: Bytes -> bytes.Builder { |
| 198 | 198 | if tag < -128 || tag > 127 { |
| 199 | | − panic("msgpack: an Ext tag is a signed 8-bit integer, got {tag}") |
| 199 | + panic "msgpack: an Ext tag is a signed 8-bit integer, got {tag}" |
| 200 | 200 | } else { |
| 201 | | − let len = bytes.length(data) |
| 201 | + let len = bytes.length data |
| 202 | 202 | let b = match len { |
| 203 | | − 1 -> bytes.append_byte(b, 212) |
| 204 | | − , 2 -> bytes.append_byte(b, 213) |
| 205 | | − , 4 -> bytes.append_byte(b, 214) |
| 206 | | − , 8 -> bytes.append_byte(b, 215) |
| 207 | | − , 16 -> bytes.append_byte(b, 216) |
| 208 | | − , _ if len < 256 -> bytes.append_byte(bytes.append_byte(b, 199), len) |
| 209 | | − , _ if len < 65536 -> be16(bytes.append_byte(b, 200), len) |
| 210 | | − , _ -> be32(bytes.append_byte(b, 201), check_len(len)) |
| 203 | + 1 -> bytes.append_byte b 212 |
| 204 | + , 2 -> bytes.append_byte b 213 |
| 205 | + , 4 -> bytes.append_byte b 214 |
| 206 | + , 8 -> bytes.append_byte b 215 |
| 207 | + , 16 -> bytes.append_byte b 216 |
| 208 | + , _ if len < 256 -> bytes.append_byte (bytes.append_byte b 199) len |
| 209 | + , _ if len < 65536 -> be16 (bytes.append_byte b 200) len |
| 210 | + , _ -> be32 (bytes.append_byte b 201) (check_len len) |
| 211 | 211 | } |
| 212 | | − let b = bytes.append_byte(b, int.low_bits(tag, 8)) |
| 213 | | − bytes.append(b, data) |
| 212 | + let b = bytes.append_byte b (int.low_bits tag 8) |
| 213 | + bytes.append b data |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /// Lengths are 32 bits on the wire; more cannot be written. |
| 218 | | −fn check_len(len: Int) -> Int { |
| 218 | +fn check_len :: len: Int -> Int { |
| 219 | 219 | if len >= 4294967296 { |
| 220 | | − panic("msgpack: more than 2^32 - 1 items or bytes cannot be written") |
| 220 | + panic "msgpack: more than 2^32 - 1 items or bytes cannot be written" |
| 221 | 221 | } else { |
| 222 | 222 | len |
| 223 | 223 | } |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /// `n` as 2 big-endian bytes (its low 16 bits, two's complement). |
| 227 | | −fn be16(b: bytes.Builder, n: Int) -> bytes.Builder { |
| 228 | | − let n = int.low_bits(n, 16) |
| 229 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 8), 255)) |
| 230 | | − bytes.append_byte(b, int.and(n, 255)) |
| 227 | +fn be16 :: b: bytes.Builder n: Int -> bytes.Builder { |
| 228 | + let n = int.low_bits n 16 |
| 229 | + let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) |
| 230 | + bytes.append_byte b (int.and n 255) |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /// `n` as 4 big-endian bytes (its low 32 bits). |
| 234 | | −fn be32(b: bytes.Builder, n: Int) -> bytes.Builder { |
| 235 | | − let n = int.low_bits(n, 32) |
| 236 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 24), 255)) |
| 237 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 16), 255)) |
| 238 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 8), 255)) |
| 239 | | − bytes.append_byte(b, int.and(n, 255)) |
| 234 | +fn be32 :: b: bytes.Builder n: Int -> bytes.Builder { |
| 235 | + let n = int.low_bits n 32 |
| 236 | + let b = bytes.append_byte b (int.and (int.shift_right n 24) 255) |
| 237 | + let b = bytes.append_byte b (int.and (int.shift_right n 16) 255) |
| 238 | + let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) |
| 239 | + bytes.append_byte b (int.and n 255) |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | /// `n` as 8 big-endian bytes (its low 64 bits). |
| 243 | | −fn be64(b: bytes.Builder, n: Int) -> bytes.Builder { |
| 244 | | − let n = int.low_bits(n, 64) |
| 245 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 56), 255)) |
| 246 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 48), 255)) |
| 247 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 40), 255)) |
| 248 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 32), 255)) |
| 249 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 24), 255)) |
| 250 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 16), 255)) |
| 251 | | − let b = bytes.append_byte(b, int.and(int.shift_right(n, 8), 255)) |
| 252 | | − bytes.append_byte(b, int.and(n, 255)) |
| 243 | +fn be64 :: b: bytes.Builder n: Int -> bytes.Builder { |
| 244 | + let n = int.low_bits n 64 |
| 245 | + let b = bytes.append_byte b (int.and (int.shift_right n 56) 255) |
| 246 | + let b = bytes.append_byte b (int.and (int.shift_right n 48) 255) |
| 247 | + let b = bytes.append_byte b (int.and (int.shift_right n 40) 255) |
| 248 | + let b = bytes.append_byte b (int.and (int.shift_right n 32) 255) |
| 249 | + let b = bytes.append_byte b (int.and (int.shift_right n 24) 255) |
| 250 | + let b = bytes.append_byte b (int.and (int.shift_right n 16) 255) |
| 251 | + let b = bytes.append_byte b (int.and (int.shift_right n 8) 255) |
| 252 | + bytes.append_byte b (int.and n 255) |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | // ---- decoding ------------------------------------------------------------------ |
| @@ -258,67 +258,70 @@ fn be64(b: bytes.Builder, n: Int) -> bytes.Builder { |
| 258 | 258 | type Cursor { Cursor(bytes: Bytes, at: Int) } |
| 259 | 259 | |
| 260 | 260 | /// The first MessagePack value in `data`. Bytes after it are an error. |
| 261 | | −pub fn read(data: Bytes) -> Result<Value, DecodeError> { |
| 262 | | − use (value, cur) <- result.try(read_value(Cursor(data, 0), 0)) |
| 263 | | − if cur.at == bytes.length(data) { |
| 261 | +pub fn read :: data: Bytes -> Result<Value, DecodeError> { |
| 262 | + use (value, cur) <- result.try (read_value Cursor(data, 0) 0) |
| 263 | + if cur.at == bytes.length data { |
| 264 | 264 | Ok(value) |
| 265 | 265 | } else { |
| 266 | 266 | Error( Malformed( cur.at |
| 267 | | − , "{bytes.length(data) - cur.at} bytes after the value" |
| 267 | + , "{bytes.length data - cur.at} bytes after the value" |
| 268 | 268 | ) |
| 269 | 269 | ) |
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | | −fn read_value(cur: Cursor, depth: Int) -> Result<(Value, Cursor), DecodeError> { |
| 274 | | − if depth > max_depth() { |
| 275 | | − Error(Malformed(cur.at, "nesting deeper than {max_depth()} levels")) |
| 273 | +fn read_value :: |
| 274 | + cur: Cursor |
| 275 | + depth: Int |
| 276 | + -> Result<(Value, Cursor), DecodeError> { |
| 277 | + if depth > max_depth () { |
| 278 | + Error(Malformed(cur.at, "nesting deeper than {max_depth ()} levels")) |
| 276 | 279 | } else { |
| 277 | | − use (byte, cur) <- result.try(read_byte(cur)) |
| 280 | + use (byte, cur) <- result.try (read_byte cur) |
| 278 | 281 | if byte < 128 { |
| 279 | 282 | Ok((Int(byte), cur)) |
| 280 | 283 | } else if byte >= 224 { |
| 281 | 284 | Ok((Int(byte - 256), cur)) |
| 282 | 285 | } else if byte < 144 { |
| 283 | | − read_map(cur, byte - 128, depth) |
| 286 | + read_map cur (byte - 128) depth |
| 284 | 287 | } else if byte < 160 { |
| 285 | | − read_array(cur, byte - 144, depth) |
| 288 | + read_array cur (byte - 144) depth |
| 286 | 289 | } else if byte < 192 { |
| 287 | | − read_str(cur, byte - 160) |
| 290 | + read_str cur (byte - 160) |
| 288 | 291 | } else { |
| 289 | 292 | match byte { |
| 290 | 293 | 192 -> Ok((Nil, cur)) |
| 291 | 294 | , 193 -> Error(Malformed(cur.at - 1, "0xc1 is not a MessagePack byte")) |
| 292 | 295 | , 194 -> Ok((Bool(False), cur)) |
| 293 | 296 | , 195 -> Ok((Bool(True), cur)) |
| 294 | | − , 196 -> read_bin_len(cur, 1) |
| 295 | | − , 197 -> read_bin_len(cur, 2) |
| 296 | | − , 198 -> read_bin_len(cur, 4) |
| 297 | | − , 199 -> read_ext_len(cur, 1) |
| 298 | | − , 200 -> read_ext_len(cur, 2) |
| 299 | | − , 201 -> read_ext_len(cur, 4) |
| 300 | | − , 202 -> read_float(cur, 4) |
| 301 | | − , 203 -> read_float(cur, 8) |
| 302 | | − , 204 -> read_uint_value(cur, 1) |
| 303 | | − , 205 -> read_uint_value(cur, 2) |
| 304 | | − , 206 -> read_uint_value(cur, 4) |
| 305 | | − , 207 -> read_uint_value(cur, 8) |
| 306 | | − , 208 -> read_int_value(cur, 1) |
| 307 | | − , 209 -> read_int_value(cur, 2) |
| 308 | | − , 210 -> read_int_value(cur, 4) |
| 309 | | − , 211 -> read_int_value(cur, 8) |
| 310 | | − , 212 -> read_ext(cur, 1) |
| 311 | | − , 213 -> read_ext(cur, 2) |
| 312 | | − , 214 -> read_ext(cur, 4) |
| 313 | | − , 215 -> read_ext(cur, 8) |
| 314 | | − , 216 -> read_ext(cur, 16) |
| 315 | | − , 217 -> read_str_len(cur, 1) |
| 316 | | − , 218 -> read_str_len(cur, 2) |
| 317 | | − , 219 -> read_str_len(cur, 4) |
| 318 | | − , 220 -> read_array_len(cur, 2, depth) |
| 319 | | − , 221 -> read_array_len(cur, 4, depth) |
| 320 | | − , 222 -> read_map_len(cur, 2, depth) |
| 321 | | − , 223 -> read_map_len(cur, 4, depth) |
| 297 | + , 196 -> read_bin_len cur 1 |
| 298 | + , 197 -> read_bin_len cur 2 |
| 299 | + , 198 -> read_bin_len cur 4 |
| 300 | + , 199 -> read_ext_len cur 1 |
| 301 | + , 200 -> read_ext_len cur 2 |
| 302 | + , 201 -> read_ext_len cur 4 |
| 303 | + , 202 -> read_float cur 4 |
| 304 | + , 203 -> read_float cur 8 |
| 305 | + , 204 -> read_uint_value cur 1 |
| 306 | + , 205 -> read_uint_value cur 2 |
| 307 | + , 206 -> read_uint_value cur 4 |
| 308 | + , 207 -> read_uint_value cur 8 |
| 309 | + , 208 -> read_int_value cur 1 |
| 310 | + , 209 -> read_int_value cur 2 |
| 311 | + , 210 -> read_int_value cur 4 |
| 312 | + , 211 -> read_int_value cur 8 |
| 313 | + , 212 -> read_ext cur 1 |
| 314 | + , 213 -> read_ext cur 2 |
| 315 | + , 214 -> read_ext cur 4 |
| 316 | + , 215 -> read_ext cur 8 |
| 317 | + , 216 -> read_ext cur 16 |
| 318 | + , 217 -> read_str_len cur 1 |
| 319 | + , 218 -> read_str_len cur 2 |
| 320 | + , 219 -> read_str_len cur 4 |
| 321 | + , 220 -> read_array_len cur 2 depth |
| 322 | + , 221 -> read_array_len cur 4 depth |
| 323 | + , 222 -> read_map_len cur 2 depth |
| 324 | + , 223 -> read_map_len cur 4 depth |
| 322 | 325 | // Unreachable: the arms above cover 192 to 223. |
| 323 | 326 | , _ -> |
| 324 | 327 | Error(Malformed(cur.at - 1, "a byte the codec does not know: {byte}")) |
| @@ -327,136 +330,156 @@ fn read_value(cur: Cursor, depth: Int) -> Result<(Value, Cursor), DecodeError> { |
| 327 | 330 | } |
| 328 | 331 | } |
| 329 | 332 | |
| 330 | | −fn read_uint_value |
| 331 | | − (cur: Cursor, width: Int) |
| 333 | +fn read_uint_value :: |
| 334 | + cur: Cursor |
| 335 | + width: Int |
| 332 | 336 | -> Result<(Value, Cursor), DecodeError> { |
| 333 | | − use (n, cur) <- result.try(read_uint(cur, width)) |
| 337 | + use (n, cur) <- result.try (read_uint cur width) |
| 334 | 338 | Ok((Int(n), cur)) |
| 335 | 339 | } |
| 336 | 340 | |
| 337 | | −fn read_int_value |
| 338 | | − (cur: Cursor, width: Int) |
| 341 | +fn read_int_value :: |
| 342 | + cur: Cursor |
| 343 | + width: Int |
| 339 | 344 | -> Result<(Value, Cursor), DecodeError> { |
| 340 | | − use (n, cur) <- result.try(read_uint(cur, width)) |
| 341 | | − Ok((Int(signed(n, width * 8)), cur)) |
| 345 | + use (n, cur) <- result.try (read_uint cur width) |
| 346 | + Ok((Int(signed n (width * 8)), cur)) |
| 342 | 347 | } |
| 343 | 348 | |
| 344 | 349 | /// `n` (unsigned, `bits` wide) as signed: two's complement. |
| 345 | | −fn signed(n: Int, bits: Int) -> Int { |
| 346 | | − let sign = int.shift_left(1, bits - 1) |
| 347 | | − if int.and(n, sign) == 0 { n } else { n - int.shift_left(sign, 1) } |
| 350 | +fn signed :: n: Int bits: Int -> Int { |
| 351 | + let sign = int.shift_left 1 (bits - 1) |
| 352 | + if int.and n sign == 0 { n } else { n - int.shift_left sign 1 } |
| 348 | 353 | } |
| 349 | 354 | |
| 350 | 355 | /// A float 32 (`width` 4) is widened; a float 64 (`width` 8) is exact. |
| 351 | | −fn read_float(cur: Cursor, width: Int) -> Result<(Value, Cursor), DecodeError> { |
| 352 | | − use (bits, cur) <- result.try(read_uint(cur, width)) |
| 353 | | − let x = if width == 4 { |
| 354 | | − float.from_bits32(bits) |
| 355 | | − } else { |
| 356 | | − float.from_bits(bits) |
| 357 | | − } |
| 356 | +fn read_float :: |
| 357 | + cur: Cursor |
| 358 | + width: Int |
| 359 | + -> Result<(Value, Cursor), DecodeError> { |
| 360 | + use (bits, cur) <- result.try (read_uint cur width) |
| 361 | + let x = if width == 4 { float.from_bits32 bits } else { float.from_bits bits } |
| 358 | 362 | Ok((Float(x), cur)) |
| 359 | 363 | } |
| 360 | 364 | |
| 361 | | −fn read_str_len |
| 362 | | − (cur: Cursor, width: Int) |
| 365 | +fn read_str_len :: |
| 366 | + cur: Cursor |
| 367 | + width: Int |
| 363 | 368 | -> Result<(Value, Cursor), DecodeError> { |
| 364 | | − use (len, cur) <- result.try(read_uint(cur, width)) |
| 365 | | − read_str(cur, len) |
| 369 | + use (len, cur) <- result.try (read_uint cur width) |
| 370 | + read_str cur len |
| 366 | 371 | } |
| 367 | 372 | |
| 368 | | −fn read_str(cur: Cursor, len: Int) -> Result<(Value, Cursor), DecodeError> { |
| 369 | | − let left = bytes.length(cur.bytes) - cur.at |
| 373 | +fn read_str :: cur: Cursor len: Int -> Result<(Value, Cursor), DecodeError> { |
| 374 | + let left = bytes.length cur.bytes - cur.at |
| 370 | 375 | if len < 0 || len > left { |
| 371 | 376 | Error(Malformed(cur.at, "a length of {len} with {left} bytes left")) |
| 372 | 377 | } else { |
| 373 | | − match bytes.to_string_range(cur.bytes, cur.at, len) { |
| 378 | + match bytes.to_string_range cur.bytes cur.at len { |
| 374 | 379 | Ok(s) -> Ok((Str(s), Cursor(cur.bytes, cur.at + len))) |
| 375 | 380 | , Error(e) -> Error(Malformed(cur.at, "a str that is not UTF-8: {e}")) |
| 376 | 381 | } |
| 377 | 382 | } |
| 378 | 383 | } |
| 379 | 384 | |
| 380 | | −fn read_bin_len |
| 381 | | − (cur: Cursor, width: Int) |
| 385 | +fn read_bin_len :: |
| 386 | + cur: Cursor |
| 387 | + width: Int |
| 382 | 388 | -> Result<(Value, Cursor), DecodeError> { |
| 383 | | − use (len, cur) <- result.try(read_uint(cur, width)) |
| 384 | | − use (data, cur) <- result.try(take(cur, len)) |
| 389 | + use (len, cur) <- result.try (read_uint cur width) |
| 390 | + use (data, cur) <- result.try (take cur len) |
| 385 | 391 | Ok((Bin(data), cur)) |
| 386 | 392 | } |
| 387 | 393 | |
| 388 | | −fn read_ext_len |
| 389 | | − (cur: Cursor, width: Int) |
| 394 | +fn read_ext_len :: |
| 395 | + cur: Cursor |
| 396 | + width: Int |
| 390 | 397 | -> Result<(Value, Cursor), DecodeError> { |
| 391 | | − use (len, cur) <- result.try(read_uint(cur, width)) |
| 392 | | − read_ext(cur, len) |
| 398 | + use (len, cur) <- result.try (read_uint cur width) |
| 399 | + read_ext cur len |
| 393 | 400 | } |
| 394 | 401 | |
| 395 | | −fn read_ext(cur: Cursor, len: Int) -> Result<(Value, Cursor), DecodeError> { |
| 396 | | − use (byte, cur) <- result.try(read_byte(cur)) |
| 402 | +fn read_ext :: cur: Cursor len: Int -> Result<(Value, Cursor), DecodeError> { |
| 403 | + use (byte, cur) <- result.try (read_byte cur) |
| 397 | 404 | let tag = if byte >= 128 { byte - 256 } else { byte } |
| 398 | | − use (data, cur) <- result.try(take(cur, len)) |
| 405 | + use (data, cur) <- result.try (take cur len) |
| 399 | 406 | Ok((Ext(tag, data), cur)) |
| 400 | 407 | } |
| 401 | 408 | |
| 402 | | −fn read_array_len |
| 403 | | − (cur: Cursor, width: Int, depth: Int) |
| 409 | +fn read_array_len :: |
| 410 | + cur: Cursor |
| 411 | + width: Int |
| 412 | + depth: Int |
| 404 | 413 | -> Result<(Value, Cursor), DecodeError> { |
| 405 | | − use (count, cur) <- result.try(read_uint(cur, width)) |
| 406 | | − read_array(cur, count, depth) |
| 414 | + use (count, cur) <- result.try (read_uint cur width) |
| 415 | + read_array cur count depth |
| 407 | 416 | } |
| 408 | 417 | |
| 409 | | −fn read_array |
| 410 | | − (cur: Cursor, count: Int, depth: Int) |
| 418 | +fn read_array :: |
| 419 | + cur: Cursor |
| 420 | + count: Int |
| 421 | + depth: Int |
| 411 | 422 | -> Result<(Value, Cursor), DecodeError> { |
| 412 | | − use _ <- result.try(check_count(cur, count, "an array")) |
| 413 | | − read_items(cur, count, depth, []) |
| 423 | + use _ <- result.try (check_count cur count "an array") |
| 424 | + read_items cur count depth [] |
| 414 | 425 | } |
| 415 | 426 | |
| 416 | | −fn read_items |
| 417 | | − (cur: Cursor, count: Int, depth: Int, acc: List<Value>) |
| 427 | +fn read_items :: |
| 428 | + cur: Cursor |
| 429 | + count: Int |
| 430 | + depth: Int |
| 431 | + acc: List<Value> |
| 418 | 432 | -> Result<(Value, Cursor), DecodeError> { |
| 419 | 433 | if count == 0 { |
| 420 | | − Ok((Array(list.reverse(acc)), cur)) |
| 434 | + Ok((Array(list.reverse acc), cur)) |
| 421 | 435 | } else { |
| 422 | | − use (item, cur) <- result.try(read_value(cur, depth + 1)) |
| 423 | | − read_items(cur, count - 1, depth, [item, ..acc]) |
| 436 | + use (item, cur) <- result.try (read_value cur (depth + 1)) |
| 437 | + read_items cur (count - 1) depth [item, ..acc] |
| 424 | 438 | } |
| 425 | 439 | } |
| 426 | 440 | |
| 427 | | −fn read_map_len |
| 428 | | − (cur: Cursor, width: Int, depth: Int) |
| 441 | +fn read_map_len :: |
| 442 | + cur: Cursor |
| 443 | + width: Int |
| 444 | + depth: Int |
| 429 | 445 | -> Result<(Value, Cursor), DecodeError> { |
| 430 | | − use (count, cur) <- result.try(read_uint(cur, width)) |
| 431 | | − read_map(cur, count, depth) |
| 446 | + use (count, cur) <- result.try (read_uint cur width) |
| 447 | + read_map cur count depth |
| 432 | 448 | } |
| 433 | 449 | |
| 434 | | −fn read_map |
| 435 | | − (cur: Cursor, count: Int, depth: Int) |
| 450 | +fn read_map :: |
| 451 | + cur: Cursor |
| 452 | + count: Int |
| 453 | + depth: Int |
| 436 | 454 | -> Result<(Value, Cursor), DecodeError> { |
| 437 | | − use _ <- result.try(check_count(cur, count, "a map")) |
| 438 | | − read_entries(cur, count, depth, []) |
| 455 | + use _ <- result.try (check_count cur count "a map") |
| 456 | + read_entries cur count depth [] |
| 439 | 457 | } |
| 440 | 458 | |
| 441 | | −fn read_entries |
| 442 | | − (cur: Cursor, count: Int, depth: Int, acc: List<(Value, Value)>) |
| 459 | +fn read_entries :: |
| 460 | + cur: Cursor |
| 461 | + count: Int |
| 462 | + depth: Int |
| 463 | + acc: List<(Value, Value)> |
| 443 | 464 | -> Result<(Value, Cursor), DecodeError> { |
| 444 | 465 | if count == 0 { |
| 445 | | − Ok((Map(list.reverse(acc)), cur)) |
| 466 | + Ok((Map(list.reverse acc), cur)) |
| 446 | 467 | } else { |
| 447 | | − use (key, cur) <- result.try(read_value(cur, depth + 1)) |
| 448 | | − use (value, cur) <- result.try(read_value(cur, depth + 1)) |
| 449 | | − read_entries(cur, count - 1, depth, [(key, value), ..acc]) |
| 468 | + use (key, cur) <- result.try (read_value cur (depth + 1)) |
| 469 | + use (value, cur) <- result.try (read_value cur (depth + 1)) |
| 470 | + read_entries cur (count - 1) depth [(key, value), ..acc] |
| 450 | 471 | } |
| 451 | 472 | } |
| 452 | 473 | |
| 453 | 474 | /// A length prefix is checked against the bytes left before anything is |
| 454 | 475 | /// read: each item takes at least one byte, so more items than bytes can |
| 455 | 476 | /// never be there. |
| 456 | | −fn check_count |
| 457 | | − (cur: Cursor, count: Int, what: String) |
| 477 | +fn check_count :: |
| 478 | + cur: Cursor |
| 479 | + count: Int |
| 480 | + what: String |
| 458 | 481 | -> Result<(), DecodeError> { |
| 459 | | − let left = bytes.length(cur.bytes) - cur.at |
| 482 | + let left = bytes.length cur.bytes - cur.at |
| 460 | 483 | if count > left { |
| 461 | 484 | Error(Malformed(cur.at, "{what} of {count} items with {left} bytes left")) |
| 462 | 485 | } else { |
| @@ -464,8 +487,8 @@ fn check_count |
| 464 | 487 | } |
| 465 | 488 | } |
| 466 | 489 | |
| 467 | | −fn read_byte(cur: Cursor) -> Result<(Int, Cursor), DecodeError> { |
| 468 | | − let byte = bytes.read_u8(cur.bytes, cur.at) |
| 490 | +fn read_byte :: cur: Cursor -> Result<(Int, Cursor), DecodeError> { |
| 491 | + let byte = bytes.read_u8 cur.bytes cur.at |
| 469 | 492 | if byte < 0 { |
| 470 | 493 | Error(Malformed(cur.at, "the input ends early")) |
| 471 | 494 | } else { |
| @@ -475,16 +498,16 @@ fn read_byte(cur: Cursor) -> Result<(Int, Cursor), DecodeError> { |
| 475 | 498 | |
| 476 | 499 | /// The unsigned big-endian integer of the next `width` bytes (1, 2, 4 or |
| 477 | 500 | /// 8; 8 can be a big integer, above 2^62), read in place. |
| 478 | | −fn read_uint(cur: Cursor, width: Int) -> Result<(Int, Cursor), DecodeError> { |
| 479 | | − let left = bytes.length(cur.bytes) - cur.at |
| 501 | +fn read_uint :: cur: Cursor width: Int -> Result<(Int, Cursor), DecodeError> { |
| 502 | + let left = bytes.length cur.bytes - cur.at |
| 480 | 503 | if width > left { |
| 481 | 504 | Error(Malformed(cur.at, "a length of {width} with {left} bytes left")) |
| 482 | 505 | } else { |
| 483 | 506 | let n = match width { |
| 484 | | − 1 -> bytes.read_u8(cur.bytes, cur.at) |
| 485 | | − , 2 -> bytes.read_u16be(cur.bytes, cur.at) |
| 486 | | − , 4 -> bytes.read_u32be(cur.bytes, cur.at) |
| 487 | | − , _ -> bytes.read_u64be(cur.bytes, cur.at) |
| 507 | + 1 -> bytes.read_u8 cur.bytes cur.at |
| 508 | + , 2 -> bytes.read_u16be cur.bytes cur.at |
| 509 | + , 4 -> bytes.read_u32be cur.bytes cur.at |
| 510 | + , _ -> bytes.read_u64be cur.bytes cur.at |
| 488 | 511 | } |
| 489 | 512 | if n < 0 { |
| 490 | 513 | // Unreachable: the bounds were checked above. |
| @@ -496,12 +519,12 @@ fn read_uint(cur: Cursor, width: Int) -> Result<(Int, Cursor), DecodeError> { |
| 496 | 519 | } |
| 497 | 520 | |
| 498 | 521 | /// The next `n` bytes, if they are all there. |
| 499 | | −fn take(cur: Cursor, n: Int) -> Result<(Bytes, Cursor), DecodeError> { |
| 500 | | − let left = bytes.length(cur.bytes) - cur.at |
| 522 | +fn take :: cur: Cursor n: Int -> Result<(Bytes, Cursor), DecodeError> { |
| 523 | + let left = bytes.length cur.bytes - cur.at |
| 501 | 524 | if n < 0 || n > left { |
| 502 | 525 | Error(Malformed(cur.at, "a length of {n} with {left} bytes left")) |
| 503 | 526 | } else { |
| 504 | | − Ok( ( bytes.slice(cur.bytes, cur.at, cur.at + n) |
| 527 | + Ok( ( bytes.slice cur.bytes cur.at (cur.at + n) |
| 505 | 528 | , Cursor(cur.bytes, cur.at + n) |
| 506 | 529 | ) |
| 507 | 530 | ) |
| @@ -514,40 +537,40 @@ fn take(cur: Cursor, n: Int) -> Result<(Bytes, Cursor), DecodeError> { |
| 514 | 537 | /// record as a map keyed by field label, a positional constructor as a |
| 515 | 538 | /// map from its name to its fields' array, and a constructor without |
| 516 | 539 | /// fields as its name. |
| 517 | | −@derivable(to_msgpack_of_constructor) |
| 540 | +@derivable to_msgpack_of_constructor |
| 518 | 541 | pub trait ToMsgPack<a> { |
| 519 | | − fn to_msgpack(x: a) -> Value |
| 542 | + fn to_msgpack :: x: a -> Value |
| 520 | 543 | } |
| 521 | 544 | |
| 522 | 545 | /// The derive function of [`ToMsgPack`] (ADR 0019). |
| 523 | | −fn to_msgpack_of_constructor(c: Constructor<Value>) -> Value { |
| 546 | +fn to_msgpack_of_constructor :: c: Constructor<Value> -> Value { |
| 524 | 547 | match c.fields { |
| 525 | 548 | [] -> Str(c.name) |
| 526 | 549 | , [f, ..] if f.label != "" -> |
| 527 | | − Map(list.map(c.fields, fn(f) { (Str(f.label), f.value) })) |
| 528 | | − , fields -> Map([(Str(c.name), Array(list.map(fields, fn(f) { f.value })))]) |
| 550 | + Map(list.map c.fields fn f { (Str(f.label), f.value) }) |
| 551 | + , fields -> Map([(Str(c.name), Array(list.map fields fn f { f.value }))]) |
| 529 | 552 | } |
| 530 | 553 | } |
| 531 | 554 | |
| 532 | 555 | /// Decoding from MessagePack (ADR 0051). Hand-written until the ADR |
| 533 | 556 | /// lands: ADR 0019's derives consume a value, they do not produce one. |
| 534 | 557 | pub trait FromMsgPack<a> { |
| 535 | | − fn from_msgpack(v: Value) -> Result<a, DecodeError> |
| 558 | + fn from_msgpack :: v: Value -> Result<a, DecodeError> |
| 536 | 559 | } |
| 537 | 560 | |
| 538 | 561 | /// `x` as MessagePack bytes: [`write`] of [`to_msgpack`](ToMsgPack). |
| 539 | | −pub fn encode(x: a) -> Bytes where ToMsgPack<a> { |
| 540 | | − write(to_msgpack(x)) |
| 562 | +pub fn encode :: x: a -> Bytes where ToMsgPack<a> { |
| 563 | + write (to_msgpack x) |
| 541 | 564 | } |
| 542 | 565 | |
| 543 | 566 | /// The `a` encoded in `data`: [`read`], then [`from_msgpack`](FromMsgPack). |
| 544 | | −pub fn decode(data: Bytes) -> Result<a, DecodeError> where FromMsgPack<a> { |
| 545 | | − use value <- result.try(read(data)) |
| 546 | | − from_msgpack(value) |
| 567 | +pub fn decode :: data: Bytes -> Result<a, DecodeError> where FromMsgPack<a> { |
| 568 | + use value <- result.try (read data) |
| 569 | + from_msgpack value |
| 547 | 570 | } |
| 548 | 571 | |
| 549 | 572 | /// What a `Value` is, for error messages ("an Int", "a Map"). |
| 550 | | −pub fn kind(v: Value) -> String { |
| 573 | +pub fn kind :: v: Value -> String { |
| 551 | 574 | match v { |
| 552 | 575 | Nil -> "nil" |
| 553 | 576 | , Bool(_) -> "a Bool" |
| @@ -562,109 +585,109 @@ pub fn kind(v: Value) -> String { |
| 562 | 585 | } |
| 563 | 586 | |
| 564 | 587 | impl ToMsgPack<Int> { |
| 565 | | − fn to_msgpack(n) { |
| 588 | + fn to_msgpack :: n { |
| 566 | 589 | Int(n) |
| 567 | 590 | } |
| 568 | 591 | } |
| 569 | 592 | |
| 570 | 593 | impl FromMsgPack<Int> { |
| 571 | | − fn from_msgpack(v) { |
| 594 | + fn from_msgpack :: v { |
| 572 | 595 | match v { |
| 573 | 596 | Int(n) -> Ok(n) |
| 574 | | − , _ -> Error(Mismatch("an Int", kind(v))) |
| 597 | + , _ -> Error(Mismatch("an Int", kind v)) |
| 575 | 598 | } |
| 576 | 599 | } |
| 577 | 600 | } |
| 578 | 601 | |
| 579 | 602 | impl ToMsgPack<Float> { |
| 580 | | − fn to_msgpack(x) { |
| 603 | + fn to_msgpack :: x { |
| 581 | 604 | Float(x) |
| 582 | 605 | } |
| 583 | 606 | } |
| 584 | 607 | |
| 585 | 608 | impl FromMsgPack<Float> { |
| 586 | | − fn from_msgpack(v) { |
| 609 | + fn from_msgpack :: v { |
| 587 | 610 | match v { |
| 588 | 611 | Float(x) -> Ok(x) |
| 589 | 612 | // Many encoders write a whole number as an integer. |
| 590 | | − , Int(n) -> Ok(float.from_int(n)) |
| 591 | | − , _ -> Error(Mismatch("a Float", kind(v))) |
| 613 | + , Int(n) -> Ok(float.from_int n) |
| 614 | + , _ -> Error(Mismatch("a Float", kind v)) |
| 592 | 615 | } |
| 593 | 616 | } |
| 594 | 617 | } |
| 595 | 618 | |
| 596 | 619 | impl ToMsgPack<String> { |
| 597 | | − fn to_msgpack(s) { |
| 620 | + fn to_msgpack :: s { |
| 598 | 621 | Str(s) |
| 599 | 622 | } |
| 600 | 623 | } |
| 601 | 624 | |
| 602 | 625 | impl FromMsgPack<String> { |
| 603 | | − fn from_msgpack(v) { |
| 626 | + fn from_msgpack :: v { |
| 604 | 627 | match v { |
| 605 | 628 | Str(s) -> Ok(s) |
| 606 | | − , _ -> Error(Mismatch("a Str", kind(v))) |
| 629 | + , _ -> Error(Mismatch("a Str", kind v)) |
| 607 | 630 | } |
| 608 | 631 | } |
| 609 | 632 | } |
| 610 | 633 | |
| 611 | 634 | impl ToMsgPack<Bytes> { |
| 612 | | − fn to_msgpack(b) { |
| 635 | + fn to_msgpack :: b { |
| 613 | 636 | Bin(b) |
| 614 | 637 | } |
| 615 | 638 | } |
| 616 | 639 | |
| 617 | 640 | impl FromMsgPack<Bytes> { |
| 618 | | − fn from_msgpack(v) { |
| 641 | + fn from_msgpack :: v { |
| 619 | 642 | match v { |
| 620 | 643 | Bin(b) -> Ok(b) |
| 621 | | − , _ -> Error(Mismatch("a Bin", kind(v))) |
| 644 | + , _ -> Error(Mismatch("a Bin", kind v)) |
| 622 | 645 | } |
| 623 | 646 | } |
| 624 | 647 | } |
| 625 | 648 | |
| 626 | 649 | impl ToMsgPack<Bool> { |
| 627 | | − fn to_msgpack(b) { |
| 650 | + fn to_msgpack :: b { |
| 628 | 651 | Bool(b) |
| 629 | 652 | } |
| 630 | 653 | } |
| 631 | 654 | |
| 632 | 655 | impl FromMsgPack<Bool> { |
| 633 | | − fn from_msgpack(v) { |
| 656 | + fn from_msgpack :: v { |
| 634 | 657 | match v { |
| 635 | 658 | Bool(b) -> Ok(b) |
| 636 | | − , _ -> Error(Mismatch("a Bool", kind(v))) |
| 659 | + , _ -> Error(Mismatch("a Bool", kind v)) |
| 637 | 660 | } |
| 638 | 661 | } |
| 639 | 662 | } |
| 640 | 663 | |
| 641 | 664 | impl ToMsgPack<List<a>> where ToMsgPack<a> { |
| 642 | | − fn to_msgpack(xs) { |
| 643 | | − Array(list.map(xs, to_msgpack)) |
| 665 | + fn to_msgpack :: xs { |
| 666 | + Array(list.map xs to_msgpack) |
| 644 | 667 | } |
| 645 | 668 | } |
| 646 | 669 | |
| 647 | 670 | impl FromMsgPack<List<a>> where FromMsgPack<a> { |
| 648 | | − fn from_msgpack(v) { |
| 671 | + fn from_msgpack :: v { |
| 649 | 672 | match v { |
| 650 | | − Array(items) -> result.all(list.map(items, from_msgpack)) |
| 651 | | − , _ -> Error(Mismatch("an Array", kind(v))) |
| 673 | + Array(items) -> result.all (list.map items from_msgpack) |
| 674 | + , _ -> Error(Mismatch("an Array", kind v)) |
| 652 | 675 | } |
| 653 | 676 | } |
| 654 | 677 | } |
| 655 | 678 | |
| 656 | 679 | impl ToMsgPack<Array<a>> where ToMsgPack<a> { |
| 657 | | − fn to_msgpack(xs) { |
| 658 | | − Array(list.map(array.to_list(xs), to_msgpack)) |
| 680 | + fn to_msgpack :: xs { |
| 681 | + Array(list.map (array.to_list xs) to_msgpack) |
| 659 | 682 | } |
| 660 | 683 | } |
| 661 | 684 | |
| 662 | 685 | impl FromMsgPack<Array<a>> where FromMsgPack<a> { |
| 663 | | − fn from_msgpack(v) { |
| 686 | + fn from_msgpack :: v { |
| 664 | 687 | match v { |
| 665 | 688 | Array(items) -> |
| 666 | | − result.map(result.all(list.map(items, from_msgpack)), array.from_list) |
| 667 | | − , _ -> Error(Mismatch("an Array", kind(v))) |
| 689 | + result.map (result.all (list.map items from_msgpack)) array.from_list |
| 690 | + , _ -> Error(Mismatch("an Array", kind v)) |
| 668 | 691 | } |
| 669 | 692 | } |
| 670 | 693 | } |
| @@ -673,51 +696,51 @@ impl FromMsgPack<Array<a>> where FromMsgPack<a> { |
| 673 | 696 | /// rmp): a nested `Option<Option<a>>` cannot tell `Some(None)` from |
| 674 | 697 | /// `None`. |
| 675 | 698 | impl ToMsgPack<Option<a>> where ToMsgPack<a> { |
| 676 | | − fn to_msgpack(o) { |
| 699 | + fn to_msgpack :: o { |
| 677 | 700 | match o { |
| 678 | 701 | None -> Nil |
| 679 | | − , Some(x) -> to_msgpack(x) |
| 702 | + , Some(x) -> to_msgpack x |
| 680 | 703 | } |
| 681 | 704 | } |
| 682 | 705 | } |
| 683 | 706 | |
| 684 | 707 | impl FromMsgPack<Option<a>> where FromMsgPack<a> { |
| 685 | | − fn from_msgpack(v) { |
| 708 | + fn from_msgpack :: v { |
| 686 | 709 | match v { |
| 687 | 710 | Nil -> Ok(None) |
| 688 | | − , _ -> result.map(from_msgpack(v), fn(x) { Some(x) }) |
| 711 | + , _ -> result.map (from_msgpack v) fn x { Some(x) } |
| 689 | 712 | } |
| 690 | 713 | } |
| 691 | 714 | } |
| 692 | 715 | |
| 693 | 716 | /// As the derived format for a sum: `\{"Ok": [x]}` or `\{"Error": [e]}`. |
| 694 | 717 | impl ToMsgPack<Result<a, e>> where ToMsgPack<a>, ToMsgPack<e> { |
| 695 | | − fn to_msgpack(r) { |
| 718 | + fn to_msgpack :: r { |
| 696 | 719 | match r { |
| 697 | | − Ok(x) -> Map([(Str("Ok"), Array([to_msgpack(x)]))]) |
| 698 | | − , Error(x) -> Map([(Str("Error"), Array([to_msgpack(x)]))]) |
| 720 | + Ok(x) -> Map([(Str("Ok"), Array([to_msgpack x]))]) |
| 721 | + , Error(x) -> Map([(Str("Error"), Array([to_msgpack x]))]) |
| 699 | 722 | } |
| 700 | 723 | } |
| 701 | 724 | } |
| 702 | 725 | |
| 703 | 726 | impl FromMsgPack<Result<a, e>> where FromMsgPack<a>, FromMsgPack<e> { |
| 704 | | − fn from_msgpack(v) { |
| 727 | + fn from_msgpack :: v { |
| 705 | 728 | match v { |
| 706 | 729 | Map([(key, Array([x]))]) if key == Str("Ok") -> { |
| 707 | | − match from_msgpack(x) { |
| 730 | + match from_msgpack x { |
| 708 | 731 | Ok(y) -> Ok(Ok(y)) |
| 709 | 732 | , Error(e) -> Error(e) |
| 710 | 733 | } |
| 711 | 734 | } |
| 712 | 735 | , Map([(key, Array([x]))]) if key == Str("Error") -> { |
| 713 | | − match from_msgpack(x) { |
| 736 | + match from_msgpack x { |
| 714 | 737 | Ok(y) -> Ok(Error(y)) |
| 715 | 738 | , Error(e) -> Error(e) |
| 716 | 739 | } |
| 717 | 740 | } |
| 718 | 741 | , _ -> |
| 719 | 742 | Error( Mismatch( "a Result (\{\"Ok\": [..]\} or \{\"Error\": [..]\})" |
| 720 | | − , kind(v) |
| 743 | + , kind v |
| 721 | 744 | ) |
| 722 | 745 | ) |
| 723 | 746 | } |
| @@ -726,29 +749,29 @@ impl FromMsgPack<Result<a, e>> where FromMsgPack<a>, FromMsgPack<e> { |
| 726 | 749 | |
| 727 | 750 | /// A `Dict` is a map; decoding needs `Ord<k>` to rebuild it. |
| 728 | 751 | impl ToMsgPack<Dict<k, v>> where ToMsgPack<k>, ToMsgPack<v> { |
| 729 | | − fn to_msgpack(d) { |
| 730 | | − Map(list.map(dict.to_list(d), fn(p) { (to_msgpack(p.0), to_msgpack(p.1)) })) |
| 752 | + fn to_msgpack :: d { |
| 753 | + Map(list.map (dict.to_list d) fn p { (to_msgpack p.0, to_msgpack p.1) }) |
| 731 | 754 | } |
| 732 | 755 | } |
| 733 | 756 | |
| 734 | 757 | impl FromMsgPack<Dict<k, v>> where FromMsgPack<k>, FromMsgPack<v>, Ord<k> { |
| 735 | | − fn from_msgpack(v) { |
| 758 | + fn from_msgpack :: v { |
| 736 | 759 | match v { |
| 737 | 760 | Map(entries) -> { |
| 738 | | − use pairs <- result.try(result.all(list.map(entries, decode_entry))) |
| 739 | | − Ok(dict.from_list(pairs)) |
| 761 | + use pairs <- result.try (result.all (list.map entries decode_entry)) |
| 762 | + Ok(dict.from_list pairs) |
| 740 | 763 | } |
| 741 | | − , _ -> Error(Mismatch("a Map", kind(v))) |
| 764 | + , _ -> Error(Mismatch("a Map", kind v)) |
| 742 | 765 | } |
| 743 | 766 | } |
| 744 | 767 | } |
| 745 | 768 | |
| 746 | | −fn decode_entry |
| 747 | | − (p: (Value, Value)) |
| 769 | +fn decode_entry :: |
| 770 | + p: (Value, Value) |
| 748 | 771 | -> Result<(k, v), DecodeError> |
| 749 | 772 | where FromMsgPack<k>, FromMsgPack<v> { |
| 750 | | − use k <- result.try(from_msgpack(p.0)) |
| 751 | | − use v <- result.try(from_msgpack(p.1)) |
| 773 | + use k <- result.try (from_msgpack p.0) |
| 774 | + use v <- result.try (from_msgpack p.1) |
| 752 | 775 | Ok((k, v)) |
| 753 | 776 | } |
| 754 | 777 | |
| @@ -758,211 +781,202 @@ import std/string |
| 758 | 781 | import std/test.{assert, assert_equal} |
| 759 | 782 | |
| 760 | 783 | @test |
| 761 | | −fn integers_smallest_encoding() { |
| 762 | | − assert_equal(bytes.to_list(write(Int(0))), [0]) |
| 763 | | − assert_equal(bytes.to_list(write(Int(127))), [127]) |
| 764 | | − assert_equal(bytes.to_list(write(Int(128))), [204, 128]) |
| 765 | | − assert_equal(bytes.to_list(write(Int(255))), [204, 255]) |
| 766 | | − assert_equal(bytes.to_list(write(Int(256))), [205, 1, 0]) |
| 767 | | − assert_equal(bytes.to_list(write(Int(65535))), [205, 255, 255]) |
| 768 | | − assert_equal(bytes.to_list(write(Int(65536))), [206, 0, 1, 0, 0]) |
| 769 | | − assert_equal(bytes.to_list(write(Int(4294967295))), [206, 255, 255, 255, 255]) |
| 770 | | − assert_equal( bytes.to_list(write(Int(4294967296))) |
| 771 | | − , [207, 0, 0, 0, 1, 0, 0, 0, 0] |
| 772 | | − ) |
| 773 | | − assert_equal(bytes.to_list(write(Int(-1))), [255]) |
| 774 | | − assert_equal(bytes.to_list(write(Int(-32))), [224]) |
| 775 | | − assert_equal(bytes.to_list(write(Int(-33))), [208, 223]) |
| 776 | | − assert_equal(bytes.to_list(write(Int(-128))), [208, 128]) |
| 777 | | − assert_equal(bytes.to_list(write(Int(-129))), [209, 255, 127]) |
| 778 | | − assert_equal(bytes.to_list(write(Int(-32768))), [209, 128, 0]) |
| 779 | | − assert_equal(bytes.to_list(write(Int(-32769))), [210, 255, 255, 127, 255]) |
| 780 | | − assert_equal(bytes.to_list(write(Int(-2147483648))), [210, 128, 0, 0, 0]) |
| 781 | | − assert_equal( bytes.to_list(write(Int(-2147483649))) |
| 782 | | − , [211, 255, 255, 255, 255, 127, 255, 255, 255] |
| 783 | | − ) |
| 784 | +fn integers_smallest_encoding :: { |
| 785 | + assert_equal (bytes.to_list (write Int(0))) [0] |
| 786 | + assert_equal (bytes.to_list (write Int(127))) [127] |
| 787 | + assert_equal (bytes.to_list (write Int(128))) [204, 128] |
| 788 | + assert_equal (bytes.to_list (write Int(255))) [204, 255] |
| 789 | + assert_equal (bytes.to_list (write Int(256))) [205, 1, 0] |
| 790 | + assert_equal (bytes.to_list (write Int(65535))) [205, 255, 255] |
| 791 | + assert_equal (bytes.to_list (write Int(65536))) [206, 0, 1, 0, 0] |
| 792 | + assert_equal (bytes.to_list (write Int(4294967295))) [206, 255, 255, 255, 255] |
| 793 | + assert_equal |
| 794 | + (bytes.to_list (write Int(4294967296))) |
| 795 | + [207, 0, 0, 0, 1, 0, 0, 0, 0] |
| 796 | + assert_equal (bytes.to_list (write Int(-1))) [255] |
| 797 | + assert_equal (bytes.to_list (write Int(-32))) [224] |
| 798 | + assert_equal (bytes.to_list (write Int(-33))) [208, 223] |
| 799 | + assert_equal (bytes.to_list (write Int(-128))) [208, 128] |
| 800 | + assert_equal (bytes.to_list (write Int(-129))) [209, 255, 127] |
| 801 | + assert_equal (bytes.to_list (write Int(-32768))) [209, 128, 0] |
| 802 | + assert_equal (bytes.to_list (write Int(-32769))) [210, 255, 255, 127, 255] |
| 803 | + assert_equal (bytes.to_list (write Int(-2147483648))) [210, 128, 0, 0, 0] |
| 804 | + assert_equal |
| 805 | + (bytes.to_list (write Int(-2147483649))) |
| 806 | + [211, 255, 255, 255, 255, 127, 255, 255, 255] |
| 784 | 807 | } |
| 785 | 808 | |
| 786 | 809 | @test |
| 787 | | −fn scalars_round_trip() { |
| 810 | +fn scalars_round_trip :: { |
| 788 | 811 | let values = |
| 789 | 812 | [ Nil |
| 790 | 813 | , Bool(True) |
| 791 | 814 | , Bool(False) |
| 792 | | − , Int(0 - int.shift_left(1, 63)) |
| 793 | | − , Int(int.shift_left(1, 64) - 1) |
| 815 | + , Int(0 - int.shift_left 1 63) |
| 816 | + , Int(int.shift_left 1 64 - 1) |
| 794 | 817 | , Float(-0.0) |
| 795 | 818 | , Float(3.14159) |
| 796 | 819 | , Str("") |
| 797 | 820 | , Str("héllo, 世界") |
| 798 | | − , Bin(bytes.empty()) |
| 799 | | − , Bin(bytes.from_list([0, 159, 146, 150])) |
| 821 | + , Bin(bytes.empty ()) |
| 822 | + , Bin(bytes.from_list [0, 159, 146, 150]) |
| 800 | 823 | , Array([]) |
| 801 | 824 | , Map([]) |
| 802 | | − , Ext(-1, bytes.from_list([1, 2, 3, 4])) |
| 803 | | − , Ext(127, bytes.empty()) |
| 804 | | − , Ext(2, bytes.from_list([1, 2])) |
| 805 | | − , Ext(3, bytes.from_list([1, 2, 3, 4, 5, 6, 7, 8])) |
| 825 | + , Ext(-1, bytes.from_list [1, 2, 3, 4]) |
| 826 | + , Ext(127, bytes.empty ()) |
| 827 | + , Ext(2, bytes.from_list [1, 2]) |
| 828 | + , Ext(3, bytes.from_list [1, 2, 3, 4, 5, 6, 7, 8]) |
| 806 | 829 | , Ext( 4 |
| 807 | | − , bytes.from_list([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) |
| 830 | + , bytes.from_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] |
| 808 | 831 | ) |
| 809 | 832 | ] |
| 810 | | − list.fold( values |
| 811 | | − , () |
| 812 | | − , fn(_, v) { assert(read(write(v)) == Ok(v), "round trip of " <> debug(v)) } |
| 813 | | − ) |
| 833 | + list.fold |
| 834 | + values |
| 835 | + () |
| 836 | + fn _ v { assert (read (write v) == Ok(v)) ("round trip of " <> debug v) } |
| 814 | 837 | } |
| 815 | 838 | |
| 816 | 839 | @test |
| 817 | | −fn lengths_smallest_encoding() { |
| 840 | +fn lengths_smallest_encoding :: { |
| 818 | 841 | // fixstr up to 31 bytes, str 8 from 32; fixarray up to 15 items, |
| 819 | 842 | // array 16 from 16. |
| 820 | | − assert_equal( bytes.to_list( bytes.slice( write(Str(string.repeat("a", 31))) |
| 821 | | − , 0 |
| 822 | | − , 1 |
| 823 | | − ) |
| 843 | + assert_equal |
| 844 | + (bytes.to_list (bytes.slice (write Str(string.repeat "a" 31)) 0 1)) |
| 845 | + [191] |
| 846 | + assert_equal |
| 847 | + (bytes.to_list (bytes.slice (write Str(string.repeat "a" 32)) 0 2)) |
| 848 | + [217, 32] |
| 849 | + assert_equal |
| 850 | + (bytes.to_list (bytes.slice (write Str(string.repeat "a" 256)) 0 3)) |
| 851 | + [218, 1, 0] |
| 852 | + let items = list.map (list.range 1 15) Int_of |
| 853 | + assert_equal (bytes.to_list (bytes.slice (write Array(items)) 0 1)) [159] |
| 854 | + let items = list.map (list.range 1 16) Int_of |
| 855 | + assert_equal |
| 856 | + (bytes.to_list (bytes.slice (write Array(items)) 0 3)) |
| 857 | + [220, 0, 16] |
| 858 | + // fixext 1/2/4/8/16 for those sizes, ext 8 beyond. |
| 859 | + assert_equal |
| 860 | + (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0])) 0 1)) |
| 861 | + [212] |
| 862 | + assert_equal |
| 863 | + (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0, 0])) 0 1)) |
| 864 | + [213] |
| 865 | + assert_equal |
| 866 | + (bytes.to_list (bytes.slice (write Ext(0, bytes.from_list [0, 0, 0])) 0 2)) |
| 867 | + [199, 3] |
| 868 | +} |
| 869 | + |
| 870 | +fn Int_of :: n: Int -> Value { |
| 871 | + Int(n) |
| 872 | +} |
| 873 | + |
| 874 | +@test |
| 875 | +fn malformed_inputs_are_errors :: { |
| 876 | + // Truncated, a length past the bytes left, the reserved byte, and |
| 877 | + // nesting past the cap: an error, never a panic or an allocation. |
| 878 | + assert |
| 879 | + ( read (bytes.from_list [204]) |
| 880 | + == Error(Malformed(1, "a length of 1 with 0 bytes left")) |
| 824 | 881 | ) |
| 825 | | − , [191] |
| 826 | | − ) |
| 827 | | − assert_equal( bytes.to_list( bytes.slice( write(Str(string.repeat("a", 32))) |
| 828 | | − , 0 |
| 829 | | − , 2 |
| 830 | | − ) |
| 882 | + (debug (read (bytes.from_list [204]))) |
| 883 | + assert |
| 884 | + (read (bytes.empty ()) == Error(Malformed(0, "the input ends early"))) |
| 885 | + (debug (read (bytes.empty ()))) |
| 886 | + assert |
| 887 | + ( read (bytes.from_list [217, 5, 97]) |
| 888 | + == Error(Malformed(2, "a length of 5 with 1 bytes left")) |
| 831 | 889 | ) |
| 832 | | − , [217, 32] |
| 833 | | − ) |
| 834 | | − assert_equal( bytes.to_list( bytes.slice( write(Str(string.repeat("a", 256))) |
| 835 | | − , 0 |
| 836 | | − , 3 |
| 837 | | − ) |
| 890 | + (debug (read (bytes.from_list [217, 5, 97]))) |
| 891 | + assert |
| 892 | + ( read (bytes.from_list [221, 255, 255, 255, 255, 1]) |
| 893 | + == Error(Malformed(5, "an array of 4294967295 items with 1 bytes left")) |
| 838 | 894 | ) |
| 839 | | − , [218, 1, 0] |
| 840 | | − ) |
| 841 | | − let items = list.map(list.range(1, 15), Int_of) |
| 842 | | − assert_equal(bytes.to_list(bytes.slice(write(Array(items)), 0, 1)), [159]) |
| 843 | | − let items = list.map(list.range(1, 16), Int_of) |
| 844 | | − assert_equal( bytes.to_list(bytes.slice(write(Array(items)), 0, 3)) |
| 845 | | − , [220, 0, 16] |
| 846 | | − ) |
| 847 | | − // fixext 1/2/4/8/16 for those sizes, ext 8 beyond. |
| 848 | | − assert_equal( bytes.to_list( bytes.slice( write(Ext(0, bytes.from_list([0]))) |
| 849 | | − , 0 |
| 850 | | − , 1 |
| 851 | | − ) |
| 895 | + (debug (read (bytes.from_list [221, 255, 255, 255, 255, 1]))) |
| 896 | + assert |
| 897 | + ( read (bytes.from_list [193]) |
| 898 | + == Error(Malformed(0, "0xc1 is not a MessagePack byte")) |
| 852 | 899 | ) |
| 853 | | − , [212] |
| 854 | | − ) |
| 855 | | − assert_equal( bytes.to_list( bytes.slice( write( Ext( 0 |
| 856 | | − , bytes.from_list([0, 0]) |
| 900 | + (debug (read (bytes.from_list [193]))) |
| 901 | + assert |
| 902 | + ( read (bytes.from_list [161, 255]) |
| 903 | + == Error( Malformed( 1 |
| 904 | + , "a str that is not UTF-8: invalid UTF-8 at byte 0" |
| 857 | 905 | ) |
| 858 | 906 | ) |
| 859 | | − , 0 |
| 860 | | − , 1 |
| 861 | | − ) |
| 862 | 907 | ) |
| 863 | | − , [213] |
| 864 | | − ) |
| 865 | | − assert_equal( bytes.to_list( bytes.slice( write( Ext( 0 |
| 866 | | − , bytes.from_list([0, 0, 0]) |
| 867 | | − ) |
| 868 | | − ) |
| 869 | | − , 0 |
| 870 | | − , 2 |
| 871 | | − ) |
| 908 | + (debug (read (bytes.from_list [161, 255]))) |
| 909 | + assert |
| 910 | + ( read (bytes.from_list [1, 1]) |
| 911 | + == Error(Malformed(1, "1 bytes after the value")) |
| 872 | 912 | ) |
| 873 | | − , [199, 3] |
| 874 | | − ) |
| 875 | | −} |
| 876 | | − |
| 877 | | −fn Int_of(n: Int) -> Value { |
| 878 | | − Int(n) |
| 879 | | −} |
| 880 | | − |
| 881 | | −@test |
| 882 | | −fn malformed_inputs_are_errors() { |
| 883 | | − // Truncated, a length past the bytes left, the reserved byte, and |
| 884 | | − // nesting past the cap: an error, never a panic or an allocation. |
| 885 | | − assert( read(bytes.from_list([204])) |
| 886 | | − == Error(Malformed(1, "a length of 1 with 0 bytes left")) |
| 887 | | − , debug(read(bytes.from_list([204]))) |
| 888 | | − ) |
| 889 | | − assert( read(bytes.empty()) == Error(Malformed(0, "the input ends early")) |
| 890 | | − , debug(read(bytes.empty())) |
| 891 | | − ) |
| 892 | | − assert( read(bytes.from_list([217, 5, 97])) |
| 893 | | − == Error(Malformed(2, "a length of 5 with 1 bytes left")) |
| 894 | | − , debug(read(bytes.from_list([217, 5, 97]))) |
| 895 | | − ) |
| 896 | | − assert( read(bytes.from_list([221, 255, 255, 255, 255, 1])) |
| 897 | | − == Error(Malformed(5, "an array of 4294967295 items with 1 bytes left")) |
| 898 | | − , debug(read(bytes.from_list([221, 255, 255, 255, 255, 1]))) |
| 899 | | − ) |
| 900 | | − assert( read(bytes.from_list([193])) |
| 901 | | − == Error(Malformed(0, "0xc1 is not a MessagePack byte")) |
| 902 | | − , debug(read(bytes.from_list([193]))) |
| 903 | | − ) |
| 904 | | − assert( read(bytes.from_list([161, 255])) |
| 905 | | − == Error(Malformed(1, "a str that is not UTF-8: invalid UTF-8 at byte 0")) |
| 906 | | − , debug(read(bytes.from_list([161, 255]))) |
| 907 | | − ) |
| 908 | | − assert( read(bytes.from_list([1, 1])) |
| 909 | | − == Error(Malformed(1, "1 bytes after the value")) |
| 910 | | − , debug(read(bytes.from_list([1, 1]))) |
| 911 | | − ) |
| 913 | + (debug (read (bytes.from_list [1, 1]))) |
| 912 | 914 | // A map entry's key without its value. |
| 913 | | − assert( read(bytes.from_list([129, 161, 97])) |
| 914 | | − == Error(Malformed(3, "the input ends early")) |
| 915 | | − , debug(read(bytes.from_list([129, 161, 97]))) |
| 916 | | − ) |
| 915 | + assert |
| 916 | + ( read (bytes.from_list [129, 161, 97]) |
| 917 | + == Error(Malformed(3, "the input ends early")) |
| 918 | + ) |
| 919 | + (debug (read (bytes.from_list [129, 161, 97]))) |
| 917 | 920 | // Truncated at every width of integer read, and inside bin and ext |
| 918 | 921 | // payloads. |
| 919 | | − assert( read(bytes.from_list([205, 1])) |
| 920 | | − == Error(Malformed(1, "a length of 2 with 1 bytes left")) |
| 921 | | − , debug(read(bytes.from_list([205, 1]))) |
| 922 | | − ) |
| 923 | | − assert( read(bytes.from_list([206, 1, 2, 3])) |
| 924 | | − == Error(Malformed(1, "a length of 4 with 3 bytes left")) |
| 925 | | − , debug(read(bytes.from_list([206, 1, 2, 3]))) |
| 926 | | − ) |
| 927 | | − assert( read(bytes.from_list([207, 1, 2, 3, 4, 5, 6, 7])) |
| 928 | | − == Error(Malformed(1, "a length of 8 with 7 bytes left")) |
| 929 | | − , debug(read(bytes.from_list([207, 1, 2, 3, 4, 5, 6, 7]))) |
| 930 | | − ) |
| 931 | | − assert( read(bytes.from_list([203, 1, 2, 3])) |
| 932 | | − == Error(Malformed(1, "a length of 8 with 3 bytes left")) |
| 933 | | − , debug(read(bytes.from_list([203, 1, 2, 3]))) |
| 934 | | − ) |
| 935 | | − assert( read(bytes.from_list([196, 5, 97])) |
| 936 | | − == Error(Malformed(2, "a length of 5 with 1 bytes left")) |
| 937 | | − , debug(read(bytes.from_list([196, 5, 97]))) |
| 938 | | − ) |
| 922 | + assert |
| 923 | + ( read (bytes.from_list [205, 1]) |
| 924 | + == Error(Malformed(1, "a length of 2 with 1 bytes left")) |
| 925 | + ) |
| 926 | + (debug (read (bytes.from_list [205, 1]))) |
| 927 | + assert |
| 928 | + ( read (bytes.from_list [206, 1, 2, 3]) |
| 929 | + == Error(Malformed(1, "a length of 4 with 3 bytes left")) |
| 930 | + ) |
| 931 | + (debug (read (bytes.from_list [206, 1, 2, 3]))) |
| 932 | + assert |
| 933 | + ( read (bytes.from_list [207, 1, 2, 3, 4, 5, 6, 7]) |
| 934 | + == Error(Malformed(1, "a length of 8 with 7 bytes left")) |
| 935 | + ) |
| 936 | + (debug (read (bytes.from_list [207, 1, 2, 3, 4, 5, 6, 7]))) |
| 937 | + assert |
| 938 | + ( read (bytes.from_list [203, 1, 2, 3]) |
| 939 | + == Error(Malformed(1, "a length of 8 with 3 bytes left")) |
| 940 | + ) |
| 941 | + (debug (read (bytes.from_list [203, 1, 2, 3]))) |
| 942 | + assert |
| 943 | + ( read (bytes.from_list [196, 5, 97]) |
| 944 | + == Error(Malformed(2, "a length of 5 with 1 bytes left")) |
| 945 | + ) |
| 946 | + (debug (read (bytes.from_list [196, 5, 97]))) |
| 939 | 947 | // fixext 1 without its tag byte, and fixext 2 without its payload. |
| 940 | | − assert( read(bytes.from_list([212])) |
| 941 | | − == Error(Malformed(1, "the input ends early")) |
| 942 | | − , debug(read(bytes.from_list([212]))) |
| 943 | | − ) |
| 944 | | − assert( read(bytes.from_list([213, 5])) |
| 945 | | − == Error(Malformed(2, "a length of 2 with 0 bytes left")) |
| 946 | | − , debug(read(bytes.from_list([213, 5]))) |
| 947 | | − ) |
| 948 | + assert |
| 949 | + ( read (bytes.from_list [212]) |
| 950 | + == Error(Malformed(1, "the input ends early")) |
| 951 | + ) |
| 952 | + (debug (read (bytes.from_list [212]))) |
| 953 | + assert |
| 954 | + ( read (bytes.from_list [213, 5]) |
| 955 | + == Error(Malformed(2, "a length of 2 with 0 bytes left")) |
| 956 | + ) |
| 957 | + (debug (read (bytes.from_list [213, 5]))) |
| 948 | 958 | // A str cut in the middle of a UTF-8 sequence. |
| 949 | | − assert( read(bytes.from_list([162, 104, 255])) |
| 950 | | − == Error(Malformed(1, "a str that is not UTF-8: invalid UTF-8 at byte 1")) |
| 951 | | − , debug(read(bytes.from_list([162, 104, 255]))) |
| 952 | | − ) |
| 959 | + assert |
| 960 | + ( read (bytes.from_list [162, 104, 255]) |
| 961 | + == Error( Malformed( 1 |
| 962 | + , "a str that is not UTF-8: invalid UTF-8 at byte 1" |
| 963 | + ) |
| 964 | + ) |
| 965 | + ) |
| 966 | + (debug (read (bytes.from_list [162, 104, 255]))) |
| 953 | 967 | } |
| 954 | 968 | |
| 955 | 969 | @test |
| 956 | | −fn traits_encode_and_decode() { |
| 957 | | − assert_equal(bytes.to_list(encode([1, 2, 3])), [147, 1, 2, 3]) |
| 958 | | − assert_equal(decode(bytes.from_list([147, 1, 2, 3])), Ok([1, 2, 3])) |
| 959 | | − let d = dict.from_list([("a", 1), ("b", 2)]) |
| 960 | | − match decode(encode(d)) { |
| 961 | | − Ok(back) -> assert_equal(dict.to_list(back), [("a", 1), ("b", 2)]) |
| 962 | | − , Error(e) -> panic(show(e)) |
| 970 | +fn traits_encode_and_decode :: { |
| 971 | + assert_equal (bytes.to_list (encode [1, 2, 3])) [147, 1, 2, 3] |
| 972 | + assert_equal (decode (bytes.from_list [147, 1, 2, 3])) Ok([1, 2, 3]) |
| 973 | + let d = dict.from_list [("a", 1), ("b", 2)] |
| 974 | + match decode (encode d) { |
| 975 | + Ok(back) -> assert_equal (dict.to_list back) [("a", 1), ("b", 2)] |
| 976 | + , Error(e) -> panic (show e) |
| 963 | 977 | } |
| 964 | 978 | let none: Option<Int> = None |
| 965 | | − assert_equal(bytes.to_list(encode(none)), [192]) |
| 979 | + assert_equal (bytes.to_list (encode none)) [192] |
| 966 | 980 | let ok: Result<Int, String> = Ok(1) |
| 967 | | − assert_equal(decode(encode(ok)), Ok(ok)) |
| 981 | + assert_equal (decode (encode ok)) Ok(ok) |
| 968 | 982 | } |