use crate::types::AuthorId;
pub const SHORT_HEX_LEN: usize = 16;
pub fn short_hex(bytes: &[u8]) -> String {
let take = bytes.len().min(SHORT_HEX_LEN / 2);
let mut s = hex::encode(bytes.get(..take).unwrap_or(&[]));
while s.len() < SHORT_HEX_LEN {
s.push('0');
}
s
}
pub fn author_short(author: AuthorId) -> String {
format!("{:016x}", author.as_u64())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_hex_truncates_long_input() {
let bytes = [0xAB; 32];
let s = short_hex(&bytes);
assert_eq!(s.len(), SHORT_HEX_LEN);
assert!(s.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
fn short_hex_pads_short_input() {
let s = short_hex(&[0x12, 0x34]);
assert_eq!(s.len(), SHORT_HEX_LEN);
assert!(s.starts_with("1234"));
}
#[test]
fn short_hex_handles_empty() {
let s = short_hex(&[]);
assert_eq!(s.len(), SHORT_HEX_LEN);
assert_eq!(s, "0".repeat(SHORT_HEX_LEN));
}
#[test]
fn author_short_is_16_chars() {
let s = author_short(AuthorId::new(0x12_34_56_78_9A_BC_DE_F0));
assert_eq!(s, "123456789abcdef0");
}
}