pub struct ByteConverter {
pub bytes: u64,
}
impl ByteConverter {
pub const KB: f64 = 1024.0;
pub const MB: f64 = 1024.0 * Self::KB;
pub const GB: f64 = 1024.0 * Self::MB;
pub const TB: f64 = 1024.0 * Self::GB;
pub fn new(bytes: u64) -> Self {
ByteConverter { bytes }
}
pub fn to_kb(&self) -> (f64, &'static str) {
(self.bytes as f64 / Self::KB, "KB")
}
pub fn to_mb(&self) -> (f64, &'static str) {
(self.bytes as f64 / Self::MB, "MB")
}
pub fn to_gb(&self) -> (f64, &'static str) {
(self.bytes as f64 / Self::GB, "GB")
}
pub fn to_tb(&self) -> (f64, &'static str) {
(self.bytes as f64 / Self::TB, "TB")
}
#[cfg(test)]
fn test_to_kb_conversion() {
let converter = ByteConverter::new(8589934592);
assert_eq!(converter.to_kb().0, 8388608.0);
}
#[cfg(test)]
fn test_to_mb_conversion() {
let converter = ByteConverter::new(8589934592);
assert_eq!(converter.to_mb().0, 8192.0);
}
#[cfg(test)]
fn test_to_gb_conversion() {
let converter = ByteConverter::new(8589934592);
assert_eq!(converter.to_gb().0, 8.0);
}
#[cfg(test)]
fn test_to_tb_conversion() {
let converter = ByteConverter::new(8589934592);
assert_eq!(converter.to_tb().0, 0.0078125);
}
}