Skip to content

Commit

Permalink
Merge pull request #2088 from dusk-network/fix_node_tostr
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia authored Aug 7, 2024
2 parents a0d6f20 + a9daa80 commit 8187d88
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions node-data/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ use fake::{Dummy, Fake, Faker};

/// Encode a byte array into a shortened HEX representation.
pub fn to_str<const N: usize>(bytes: &[u8; N]) -> String {
let e = hex::encode(bytes);
if e.len() != bytes.len() * 2 {
return String::from("invalid hex");
const OFFSET: usize = 16;
let hex = hex::encode(bytes);
if N <= OFFSET {
return hex;
}

const OFFSET: usize = 16;
let (first, last) = e.split_at(OFFSET);
let (_, second) = last.split_at(e.len() - 2 * OFFSET);
let len = hex.len();

let first = &hex[0..OFFSET];
let last = &hex[len - OFFSET..];

first.to_owned() + "..." + second
format!("{first}...{last}")
}

#[cfg(any(feature = "faker", test))]
Expand Down

0 comments on commit 8187d88

Please sign in to comment.