From a9daa80b81a9ff1a002869ab87c8591922c022a2 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Wed, 7 Aug 2024 16:10:30 +0200 Subject: [PATCH] node-data: fix `to_str` to not panic --- node-data/src/ledger.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/node-data/src/ledger.rs b/node-data/src/ledger.rs index 2ee32fadda..37c9823170 100644 --- a/node-data/src/ledger.rs +++ b/node-data/src/ledger.rs @@ -32,16 +32,18 @@ use fake::{Dummy, Fake, Faker}; /// Encode a byte array into a shortened HEX representation. pub fn to_str(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))]