2023-09-20 15:37:50 +02:00
|
|
|
//* # Tools that help display binary values, data sizes, etc
|
|
|
|
|
2023-09-20 18:16:21 +02:00
|
|
|
use super::*;
|
|
|
|
pub use num_traits::{PrimInt, ToPrimitive};
|
|
|
|
|
2023-09-20 15:37:50 +02:00
|
|
|
/// ## Get the binary representation for a Byte array [`&[u8]`]
|
|
|
|
///
|
|
|
|
/// ### Arguments
|
|
|
|
/// * `data` - The data you are trying to dump
|
|
|
|
pub fn bytes_to_bin(data: &[u8]) -> String {
|
2024-03-03 17:16:50 +01:00
|
|
|
let mut s = String::new();
|
|
|
|
for (i, dat) in data.iter().enumerate() {
|
|
|
|
if i == 0 {
|
|
|
|
s.push_str(&format!("0b{:08b}", dat));
|
|
|
|
} else {
|
|
|
|
s.push_str(&format!("_{:08b}", dat));
|
2023-09-20 15:37:50 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-16 18:07:02 +01:00
|
|
|
s
|
2023-09-20 15:37:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Quickly format a number of Bytes [`usize`] with the corresponding
|
|
|
|
/// number of bits
|
|
|
|
pub fn byte_bit_display(data: usize) -> String {
|
2023-09-20 18:16:21 +02:00
|
|
|
format!("{} B = {} bit", data.clone(), data * 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ## Format total byte sizes to human readable sizes
|
|
|
|
pub fn humanbytes<T>(total: T) -> String
|
|
|
|
where
|
|
|
|
T: PrimInt,
|
|
|
|
T: ToPrimitive,
|
|
|
|
T: Ord,
|
|
|
|
T: std::fmt::Display,
|
|
|
|
T: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
if total < T::from(KIBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{total} B")
|
2023-09-20 18:16:21 +02:00
|
|
|
} else if T::from(KIBI).unwrap() <= total && total < T::from(MEBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{:.2} K", total.to_f64().unwrap() / KIBI as f64)
|
2023-09-20 18:16:21 +02:00
|
|
|
} else if T::from(MEBI).unwrap() <= total && total < T::from(GIBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{:.2} M", total.to_f64().unwrap() / MEBI as f64)
|
2023-09-20 18:16:21 +02:00
|
|
|
} else if T::from(GIBI).unwrap() <= total && total < T::from(TEBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{:.2} G", total.to_f64().unwrap() / GIBI as f64)
|
2023-09-20 18:16:21 +02:00
|
|
|
} else if T::from(TEBI).unwrap() <= total && total < T::from(PEBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{:.2} T", total.to_f64().unwrap() / TEBI as f64)
|
2023-09-20 18:16:21 +02:00
|
|
|
} else if T::from(PEBI).unwrap() <= total && total < T::from(EXBI).unwrap() {
|
2024-02-28 16:54:34 +01:00
|
|
|
format!("{:.2} P", total.to_f64().unwrap() / PEBI as f64)
|
2023-09-20 18:16:21 +02:00
|
|
|
}
|
|
|
|
// now we are starting to reach the sizes that are pretty unrealistic
|
|
|
|
// (as of 2023 that is, hello future)
|
|
|
|
//
|
|
|
|
// the later ones overflow `usize` on 64 Bit computers, so we have
|
|
|
|
// to work with a fixed, larger sized datatype
|
|
|
|
else {
|
|
|
|
let total: u128 = total.to_u128().unwrap();
|
2024-02-28 16:54:34 +01:00
|
|
|
if (EXBI..ZEBI).contains(&total) {
|
2023-09-20 18:16:21 +02:00
|
|
|
return format!("{:.2} E", total.to_f64().unwrap() / EXBI as f64);
|
2024-02-28 16:54:34 +01:00
|
|
|
} else if (ZEBI..YOBI).contains(&total) {
|
2023-09-20 18:16:21 +02:00
|
|
|
return format!("{:.2} Z", total.to_f64().unwrap() / ZEBI as f64);
|
|
|
|
} else if YOBI <= total {
|
|
|
|
return format!("{:.2} Y", total.to_f64().unwrap() / YOBI as f64);
|
2024-01-10 21:52:13 +01:00
|
|
|
} else {
|
2023-09-20 18:16:21 +02:00
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 15:37:50 +02:00
|
|
|
}
|