yprox/
utils.rs

1/// Prints a hex dump of the given data with an optional direction string.
2///
3/// # Arguments
4///
5/// * `data` - A slice of bytes to be printed as a hex dump.
6/// * `direction` - An optional string indicating the direction of the data flow.
7///
8/// # Example
9///
10/// ```
11/// let data = [0x48, 0x65, 0x6C, 0x6C, 0x6F];
12/// hex_dump(&data, "OUTGOING");
13/// ```
14pub fn hex_dump(data: &[u8], direction: &str) {
15    const WIDTH: usize = 16;
16
17    for chunk in data.chunks(WIDTH) {
18        let hex: Vec<String> = chunk.iter().map(|b| format!("{:02X}", b)).collect();
19        let ascii: String = chunk
20            .iter()
21            .map(|&b| {
22                if (0x20..=0x7e).contains(&b) {
23                    b as char
24                } else {
25                    '.'
26                }
27            })
28            .collect();
29
30        println!("{}: {:47}  |{}|", direction, hex.join(" "), ascii);
31    }
32}