Simplify RAM management
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -1,3 +1,12 @@
|
||||
use std::ops::Mul;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum FilesSizeUtilsError {
|
||||
#[error("UnitConvertError: {0}")]
|
||||
UnitConvert(String),
|
||||
}
|
||||
|
||||
/// Holds a data size, convertible in any form
|
||||
#[derive(
|
||||
serde::Serialize,
|
||||
serde::Deserialize,
|
||||
@ -25,6 +34,30 @@ impl FileSize {
|
||||
Self(gb * 1000 * 1000 * 1000)
|
||||
}
|
||||
|
||||
/// Convert size unit to MB
|
||||
pub fn from_size_unit(unit: &str, value: usize) -> anyhow::Result<Self> {
|
||||
let fact = match unit {
|
||||
"bytes" | "b" => 1f64,
|
||||
"KB" => 1000f64,
|
||||
"MB" => 1000f64 * 1000f64,
|
||||
"GB" => 1000f64 * 1000f64 * 1000f64,
|
||||
"TB" => 1000f64 * 1000f64 * 1000f64 * 1000f64,
|
||||
|
||||
"k" | "KiB" => 1024f64,
|
||||
"M" | "MiB" => 1024f64 * 1024f64,
|
||||
"G" | "GiB" => 1024f64 * 1024f64 * 1024f64,
|
||||
"T" | "TiB" => 1024f64 * 1024f64 * 1024f64 * 1024f64,
|
||||
|
||||
_ => {
|
||||
return Err(
|
||||
FilesSizeUtilsError::UnitConvert(format!("Unknown size unit: {unit}")).into(),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Self((value as f64).mul(fact) as usize))
|
||||
}
|
||||
|
||||
/// Get file size as bytes
|
||||
pub fn as_bytes(&self) -> usize {
|
||||
self.0
|
||||
@ -35,3 +68,24 @@ impl FileSize {
|
||||
self.0 / (1000 * 1000)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::file_size_utils::FileSize;
|
||||
|
||||
#[test]
|
||||
fn convert_units_mb() {
|
||||
assert_eq!(FileSize::from_size_unit("MB", 1).unwrap().as_mb(), 1);
|
||||
assert_eq!(FileSize::from_size_unit("MB", 1000).unwrap().as_mb(), 1000);
|
||||
assert_eq!(
|
||||
FileSize::from_size_unit("GB", 1000).unwrap().as_mb(),
|
||||
1000 * 1000
|
||||
);
|
||||
assert_eq!(FileSize::from_size_unit("GB", 1).unwrap().as_mb(), 1000);
|
||||
assert_eq!(FileSize::from_size_unit("GiB", 3).unwrap().as_mb(), 3221);
|
||||
assert_eq!(
|
||||
FileSize::from_size_unit("KiB", 488281).unwrap().as_mb(),
|
||||
499
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user