redacted 0.2.0

Wrappers to control debug formatting of potentially sensitive byte arrays
Documentation
//! Formatters that render their content as hex

use crate::Formatter;

/// Render the entire contents as hex
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))
    }
}

/// Render the first N chars of the content in hex
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;

    // Smoke Test for the full hex implmenetation
    #[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));
    }
    // Smoke Test for the trunc hex implmenetation
    #[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);
    }
}