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
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -458,6 +458,12 @@ version = "1.8.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da"
|
checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bytesize"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bzip2-sys"
|
name = "bzip2-sys"
|
||||||
version = "0.1.11+1.0.8"
|
version = "0.1.11+1.0.8"
|
||||||
|
@ -683,6 +689,7 @@ dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
"axum",
|
"axum",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"bytesize",
|
||||||
"cargo_toml",
|
"cargo_toml",
|
||||||
"checked_ops",
|
"checked_ops",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -466,6 +466,9 @@ version = "1.0.36"
|
||||||
[workspace.dependencies.proc-macro2]
|
[workspace.dependencies.proc-macro2]
|
||||||
version = "1.0.89"
|
version = "1.0.89"
|
||||||
|
|
||||||
|
[workspace.dependencies.bytesize]
|
||||||
|
version = "1.3.0"
|
||||||
|
|
||||||
#
|
#
|
||||||
# Patches
|
# Patches
|
||||||
#
|
#
|
||||||
|
|
|
@ -57,6 +57,7 @@ argon2.workspace = true
|
||||||
arrayvec.workspace = true
|
arrayvec.workspace = true
|
||||||
axum.workspace = true
|
axum.workspace = true
|
||||||
bytes.workspace = true
|
bytes.workspace = true
|
||||||
|
bytesize.workspace = true
|
||||||
cargo_toml.workspace = true
|
cargo_toml.workspace = true
|
||||||
checked_ops.workspace = true
|
checked_ops.workspace = true
|
||||||
chrono.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]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue