use crate::Formatter;
pub struct FullHex;
impl Formatter for FullHex {
fn debug_fmt(
bytes: &[u8],
_type_name: &str,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
write!(f, "0x{}", &hex::encode(bytes))
}
}
pub struct TruncHex<const N: usize>;
impl<const N: usize> Formatter for TruncHex<N> {
fn debug_fmt(
bytes: &[u8],
_type_name: &str,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let encoded = hex::encode(bytes);
write!(f, "0x{}...({} bytes)", &encoded[..{ N }], bytes.len())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Redacted;
#[test]
fn full_smoke() {
let input = "8befe2bf3903ac2ac9eeec203fdef62355926870ac3d4e1bb7afb70516fcc4ca";
let bytes: Redacted<Vec<u8>, FullHex> = hex::decode(input).unwrap().into();
assert_eq!(format!("0x{}", input), format!("{}", bytes));
assert_eq!(format!("0x{}", input), format!("{:?}", bytes));
}
#[test]
fn trunc_smoke() {
let input = "8befe2bf3903ac2ac9eeec203fdef62355926870ac3d4e1bb7afb70516fcc4ca";
let bytes: Redacted<Vec<u8>, TruncHex<8>> = hex::decode(input).unwrap().into();
let output = "0x8befe2bf...(32 bytes)";
assert_eq!(&format!("{}", bytes), output);
assert_eq!(&format!("{}", bytes), output);
}
}