|
| 1 | +// Custom codec module for u128 using CBOR bignum encoding |
| 2 | +pub mod u128_cbor_codec { |
| 3 | + use minicbor::{Decoder, Encoder}; |
| 4 | + |
| 5 | + /// Encode u128 as CBOR Tag 2 (positive bignum) |
| 6 | + /// For use with `#[cbor(with = "u128_cbor_codec")]` |
| 7 | + pub fn encode<C, W: minicbor::encode::Write>( |
| 8 | + v: &u128, |
| 9 | + e: &mut Encoder<W>, |
| 10 | + _ctx: &mut C, |
| 11 | + ) -> Result<(), minicbor::encode::Error<W::Error>> { |
| 12 | + // Tag 2 = positive bignum |
| 13 | + e.tag(minicbor::data::Tag::new(2))?; |
| 14 | + |
| 15 | + // Optimize: only encode non-zero leading bytes |
| 16 | + let bytes = v.to_be_bytes(); |
| 17 | + let first_nonzero = bytes.iter().position(|&b| b != 0).unwrap_or(15); |
| 18 | + e.bytes(&bytes[first_nonzero..])?; |
| 19 | + Ok(()) |
| 20 | + } |
| 21 | + |
| 22 | + /// Decode u128 from CBOR Tag 2 (positive bignum) |
| 23 | + pub fn decode<'b, C>( |
| 24 | + d: &mut Decoder<'b>, |
| 25 | + _ctx: &mut C, |
| 26 | + ) -> Result<u128, minicbor::decode::Error> { |
| 27 | + // Expect Tag 2 |
| 28 | + let tag = d.tag()?; |
| 29 | + if tag != minicbor::data::Tag::new(2) { |
| 30 | + return Err(minicbor::decode::Error::message( |
| 31 | + "Expected CBOR Tag 2 (positive bignum) for u128", |
| 32 | + )); |
| 33 | + } |
| 34 | + |
| 35 | + let bytes = d.bytes()?; |
| 36 | + if bytes.len() > 16 { |
| 37 | + return Err(minicbor::decode::Error::message( |
| 38 | + "Bignum too large for u128 (max 16 bytes)", |
| 39 | + )); |
| 40 | + } |
| 41 | + |
| 42 | + // Pad with leading zeros to make 16 bytes (big-endian) |
| 43 | + let mut arr = [0u8; 16]; |
| 44 | + arr[16 - bytes.len()..].copy_from_slice(bytes); |
| 45 | + Ok(u128::from_be_bytes(arr)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::u128_cbor_codec; |
| 52 | + use minicbor::{Decode, Encode}; |
| 53 | + |
| 54 | + #[derive(Debug, PartialEq, Encode, Decode)] |
| 55 | + struct TestStruct { |
| 56 | + #[cbor(n(0), with = "u128_cbor_codec")] |
| 57 | + value: u128, |
| 58 | + } |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_u128_zero() { |
| 62 | + let original = TestStruct { value: 0 }; |
| 63 | + let encoded = minicbor::to_vec(&original).unwrap(); |
| 64 | + let decoded: TestStruct = minicbor::decode(&encoded).unwrap(); |
| 65 | + assert_eq!(original, decoded); |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn test_u128_max() { |
| 70 | + let original = TestStruct { value: u128::MAX }; |
| 71 | + let encoded = minicbor::to_vec(&original).unwrap(); |
| 72 | + let decoded: TestStruct = minicbor::decode(&encoded).unwrap(); |
| 73 | + assert_eq!(original, decoded); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_u128_boundary_values() { |
| 78 | + let test_values = [ |
| 79 | + 0u128, |
| 80 | + 1, |
| 81 | + 127, // Max 1-byte value |
| 82 | + u64::MAX as u128, // 18446744073709551615 |
| 83 | + (u64::MAX as u128) + 1, // First value needing >64 bits |
| 84 | + u128::MAX - 1, // Near max |
| 85 | + u128::MAX, // Maximum u128 value |
| 86 | + ]; |
| 87 | + |
| 88 | + for &val in &test_values { |
| 89 | + let original = TestStruct { value: val }; |
| 90 | + let encoded = minicbor::to_vec(&original).unwrap(); |
| 91 | + let decoded: TestStruct = minicbor::decode(&encoded).unwrap(); |
| 92 | + assert_eq!(original, decoded, "Failed for value {}", val); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments