add pretty/si-unit byte size parsing/printing utils
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
c59f474aff
commit
feefa43e65
4 changed files with 40 additions and 1 deletions
|
@ -57,6 +57,7 @@ argon2.workspace = true
|
|||
arrayvec.workspace = true
|
||||
axum.workspace = true
|
||||
bytes.workspace = true
|
||||
bytesize.workspace = true
|
||||
cargo_toml.workspace = true
|
||||
checked_ops.workspace = true
|
||||
chrono.workspace = true
|
||||
|
|
|
@ -1,4 +1,32 @@
|
|||
use crate::Result;
|
||||
use bytesize::ByteSize;
|
||||
|
||||
use crate::{err, Result};
|
||||
|
||||
/// Parse a human-writable size string w/ si-unit suffix into integer
|
||||
#[inline]
|
||||
pub fn from_str(str: &str) -> Result<usize> {
|
||||
let bytes: ByteSize = str
|
||||
.parse()
|
||||
.map_err(|e| err!(Arithmetic("Failed to parse byte size: {e}")))?;
|
||||
|
||||
let bytes: usize = bytes
|
||||
.as_u64()
|
||||
.try_into()
|
||||
.map_err(|e| err!(Arithmetic("Failed to convert u64 to usize: {e}")))?;
|
||||
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Output a human-readable size string w/ si-unit suffix
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn pretty(bytes: usize) -> String {
|
||||
const SI_UNITS: bool = true;
|
||||
|
||||
let bytes: u64 = bytes.try_into().expect("failed to convert usize to u64");
|
||||
|
||||
bytesize::to_string(bytes, SI_UNITS)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue