Skip to content
Snippets Groups Projects
Commit feefa43e authored by Jason Volk's avatar Jason Volk
Browse files

add pretty/si-unit byte size parsing/printing utils


Signed-off-by: default avatarJason Volk <jason@zemos.net>
parent c59f474a
No related branches found
No related tags found
4 merge requests!610docs: add note about the nixos service defaulting to sqlite,!614fix incorrect user id for non-admin invites checking,!609Fix thread pagination,!608Final async database (overall heavy performance improvement), misc bug fixes, `/search` filters, remove legacy mentions (MSC4210) and dependency updates
......@@ -458,6 +458,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da"
[[package]]
name = "bytesize"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc"
[[package]]
name = "bzip2-sys"
version = "0.1.11+1.0.8"
......@@ -683,6 +689,7 @@ dependencies = [
"arrayvec",
"axum",
"bytes",
"bytesize",
"cargo_toml",
"checked_ops",
"chrono",
......
......@@ -466,6 +466,9 @@ version = "1.0.36"
[workspace.dependencies.proc-macro2]
version = "1.0.89"
[workspace.dependencies.bytesize]
version = "1.3.0"
#
# Patches
#
......
......@@ -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
......
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]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment