Skip to main content

nym_common/
debug.rs

1// Copyright 2026 Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use std::fmt::{self, Write};
5
6pub fn format_debug_bytes(bytes: &[u8]) -> Result<String, fmt::Error> {
7    let mut out = String::new();
8    const LINE_LEN: usize = 16;
9    for (i, chunk) in bytes.chunks(LINE_LEN).enumerate() {
10        let line_prefix = format!("[{}:{}]", 1 + i * LINE_LEN, i * LINE_LEN + chunk.len());
11        write!(out, "{line_prefix:12}")?;
12        let mut line = String::new();
13        for b in chunk {
14            line.push_str(format!("{:02x} ", b).as_str());
15        }
16        write!(
17            out,
18            "{line:48} {}",
19            chunk
20                .iter()
21                .map(|&b| b as char)
22                .map(|c| if c.is_alphanumeric() { c } else { '.' })
23                .collect::<String>()
24        )?;
25
26        writeln!(out)?;
27    }
28
29    Ok(out)
30}