Gleam ATProto packages
30

Configure Feed

Select the types of activity you want to include in your feed.

cbor: begin work on encoding

Signed-off-by: Naomi Roberts <mia@naomieow.xyz>

+89 -6
+50
cbor/src/alicia/cbor.gleam
··· 288 288 )) 289 289 } 290 290 } 291 + 292 + /// Integers larger than 64 bits are truncated to 64 bits 293 + /// 294 + /// Gleam floats are 64 bit so they are always encoded as a 64 bit float in CBOR. 295 + pub fn to_bit_array(cbor: Cbor) -> BitArray { 296 + case cbor { 297 + UnsignedInt(num:) -> 298 + case num { 299 + x if x <= 24 -> <<0b000:size(3), x:size(5)>> 300 + x if x <= 255 -> <<0b000:size(3), 24:size(5), x:size(8)>> 301 + x if x <= 65_535 -> <<0b000:size(3), 25:size(5), x:size(16)>> 302 + x if x <= 4_294_967_295 -> <<0b000:size(3), 26:size(5), x:size(32)>> 303 + x -> <<0b000:size(3), 27:size(5), x:size(64)>> 304 + } 305 + 306 + NegativeInt(num:) -> 307 + case -1 - num { 308 + x if x <= 24 -> <<0b001:size(3), x:size(5)>> 309 + x if x <= 255 -> <<0b001:size(3), 24:size(5), x:size(8)>> 310 + x if x <= 65_535 -> <<0b001:size(3), 25:size(5), x:size(16)>> 311 + x if x <= 4_294_967_295 -> <<0b001:size(3), 26:size(5), x:size(32)>> 312 + x -> <<0b001:size(3), 27:size(5), x:size(64)>> 313 + } 314 + 315 + ByteString(bytes:) -> todo as "byte string" 316 + TextString(text:) -> todo as "text string" 317 + Array(items:) -> todo as "array" 318 + Map(items:) -> todo as "map" 319 + Tag(num:) -> todo as "tag" 320 + Float(num:) -> 321 + case num { 322 + Infinity -> << 323 + 0b111:size(3), 324 + 27:size(5), 325 + 0b0_11111111111_0000000000000000000000000000000000000000000000000000:size(64), 326 + >> 327 + NegativeInfinity -> << 328 + 0b111:size(3), 329 + 27:size(5), 330 + 0b1_11111111111_0000000000000000000000000000000000000000000000000000:size(64), 331 + >> 332 + Number(num:) -> << 333 + 0b111:size(3), 334 + 27:size(5), 335 + num:float-size(64), 336 + >> 337 + } 338 + SimpleValue(value:) -> todo as "simple value" 339 + } 340 + }
+39 -6
cbor/test/alicia_cbor_test.gleam
··· 140 140 == Ok(3.140625) 141 141 as "closest value to pi" 142 142 143 - assert cbor.parse( 144 - <<0b111_11001, 0b0_11111_0000000000:size(16)>>, 145 - decode.float, 146 - ) 147 - == Ok(0.0) 148 - as "infinity" 143 + // assert cbor.parse( 144 + // <<0b111_11001, 0b0_11111_0000000000:size(16)>>, 145 + // decode.float, 146 + // ) 147 + // == Ok(0.0) 148 + // as "infinity" 149 149 150 150 assert cbor.parse( 151 151 <<0b111_11001, 0b1_00000_0000000000:size(16)>>, ··· 414 414 // == Ok(cbor.ieee_float(cbor.NegativeInfinity)) 415 415 // as "negative infinity" 416 416 } 417 + 418 + pub fn int_to_bit_array_test() { 419 + assert cbor.to_bit_array(cbor.int(10)) == <<0b000_01010>> 420 + assert cbor.to_bit_array(cbor.int(500)) == <<0b000_11001, 0x01, 0xf4>> 421 + } 422 + 423 + pub fn negative_int_to_bit_array_test() { 424 + assert cbor.to_bit_array(cbor.int(-11)) == <<0b001_01010>> 425 + assert cbor.to_bit_array(cbor.int(-500)) == <<0b001_11001, 0x01, 0xf3>> 426 + } 427 + 428 + pub fn float_to_bit_array_test() { 429 + assert cbor.to_bit_array(cbor.float(-2.0)) 430 + == << 431 + 0b111_11011, 432 + 0b1_10000000000_0000000000000000000000000000000000000000000000000000:size(64), 433 + >> 434 + as "negative two" 435 + 436 + assert cbor.to_bit_array(cbor.float(-0.0)) 437 + == << 438 + 0b111_11011, 439 + 0b1_00000000000_0000000000000000000000000000000000000000000000000000:size(64), 440 + >> 441 + as "negative zero" 442 + 443 + assert cbor.to_bit_array(cbor.float(3.141592653589793116)) 444 + == << 445 + 0b111_11011, 446 + 0b0_10000000000_1001001000011111101101010100010001000010110100011000:size(64), 447 + >> 448 + as "closest value to pi" 449 + }