1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// --- crates.io ---
use anyhow::Result as AnyResult;
use thiserror::Error as ThisError;

#[derive(Debug, ThisError)]
pub enum Error {
	#[error("Fail to convert `{}` to bytes", hex_str)]
	InvalidHexLength { hex_str: String },
}

#[macro_export]
macro_rules! bytes_array_unchecked {
	($bytes:expr, $len:expr) => {{
		unsafe { *($bytes.as_ptr() as *const [u8; $len]) }
		}};
}

#[macro_export]
macro_rules! hex_str_array_unchecked {
	($hex_str:expr, $len:expr) => {{
		$crate::bytes_array_unchecked!($crate::bytes($hex_str)?, $len)
		}};
}

pub fn bytes(hex_str: &str) -> AnyResult<Vec<u8>> {
	if hex_str.len() % 2 != 0 {
		Err(Error::InvalidHexLength {
			hex_str: hex_str.into(),
		})?;
	}

	let hex_str = hex_str.trim_start_matches("0x");

	Ok((0..hex_str.len())
		.step_by(2)
		.map(|i| u8::from_str_radix(&hex_str[i..i + 2], 16).map_err(Into::into))
		.collect::<AnyResult<Vec<_>>>()?)
}

pub fn hex_str(bytes: &[u8]) -> String {
	bytes
		.iter()
		.map(|byte| format!("{:02x}", byte))
		.collect::<Vec<_>>()
		.join("")
}