#![allow(unused_imports)]
use md5::Digest as _;
pub(crate) fn md5_hash(input: impl AsRef<[u8]>) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let digest = md5::Md5::digest(input);
let mut output = String::with_capacity(digest.len() * 2);
for byte in digest.iter().copied() {
output.push(HEX[(byte >> 4) as usize] as char);
output.push(HEX[(byte & 0x0f) as usize] as char);
}
output
}
pub(crate) fn unescape_xml_text(e: &quick_xml::events::BytesText<'_>) -> String {
let decoded = e.decode().unwrap();
quick_xml::escape::unescape(decoded.as_ref())
.unwrap()
.into_owned()
}
#[macro_export]
macro_rules! from_err {
($from:ty, $to:tt, $var:tt) => {
impl From<$from> for $to {
#[inline]
fn from(e: $from) -> $to {
$to::$var(e)
}
}
};
}
macro_rules! assert_sha256 {
($input:expr, $expected_hex:expr) => {{
let hash = Sha256::digest($input).to_vec();
let expected_bytes = hex_literal::hex!($expected_hex);
assert_eq!(
&hash,
&expected_bytes,
"SHA256({}) mismatch! Expected: {:?}, Actual: {:?}",
stringify!($input),
&expected_bytes
.iter()
.map(|b| format!("{:02x}", b))
.collect::<String>(),
&hash
.iter()
.map(|b| format!("{:02x}", b))
.collect::<String>()
);
}};
}
macro_rules! compile_regex {
($re:literal $(,)?) => {{
static RE: std::sync::OnceLock<fancy_regex::Regex> = std::sync::OnceLock::new();
RE.get_or_init(|| fancy_regex::Regex::new($re).unwrap())
}};
}
macro_rules! print_hex {
($var:expr) => {
println!(
"{} = {}",
stringify!($var),
$var.iter()
.map(|b| format!("{:02x}", b))
.collect::<String>()
);
};
}
macro_rules! print_sha256_hex {
($var:expr) => {
let hash = Sha256::digest($var);
println!(
"SHA256({}) = {}",
stringify!($var),
hash.iter()
.map(|b| format!("{:02x}", b))
.collect::<String>()
);
};
}
pub(crate) use assert_sha256;
pub(crate) use compile_regex;
pub(crate) use print_hex;
pub(crate) use print_sha256_hex;