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
//! Handle "hex" encoding

use std::fmt::{Debug, Formatter, LowerHex};

pub struct Hex<'a>(pub &'a [u8]);

impl Hex<'_> {
    pub fn to_lower(&self) -> String {
        format!("{self:x}")
    }
}

impl<'a> Debug for Hex<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:x}")
    }
}

impl<'a> LowerHex for Hex<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for b in self.0 {
            write!(f, "{:02x}", b)?;
        }
        Ok(())
    }
}