bookdata/parsing/
bindata.rs1use std::array::TryFromSliceError;
3
4use hex;
5use thiserror::Error;
6
7#[derive(Error, Debug)]
9pub enum HexDecodeError {
10 #[error("could not decode hex data")]
11 Decode(#[from] hex::FromHexError),
12 #[error("decoded data incorrect size")]
13 Split(#[from] TryFromSliceError),
14}
15
16pub fn decode_hex_i64_pair(data: &str) -> Result<(i64, i64), HexDecodeError> {
18 let data = hex::decode(data)?;
19 let (hi, lo) = data.split_at(8);
20
21 let hi: [u8; 8] = hi.try_into()?;
22 let lo: [u8; 8] = lo.try_into()?;
23
24 let hi = i64::from_be_bytes(hi);
25 let lo = i64::from_be_bytes(lo);
26
27 Ok((hi, lo))
28}
29
30#[test]
31fn test_decode_zero_pair() {
32 let zeros = "00000000000000000000000000000000";
33 let (hi, lo) = decode_hex_i64_pair(zeros).unwrap();
34 assert_eq!(hi, 0);
35 assert_eq!(lo, 0);
36}
37
38#[test]
39fn test_decode_n_pair() {
40 let zeros = "80000000000000000000000000000001";
41 let (hi, lo) = decode_hex_i64_pair(zeros).unwrap();
42 assert_eq!(hi, i64::MIN);
43 assert_eq!(lo, 1);
44}