mirror of
https://github.com/YGGverse/aquatic-crawler.git
synced 2026-04-01 09:35:37 +00:00
show bytes as description, implement format bytes trait
This commit is contained in:
parent
d05b15c7a3
commit
f49ed0e11b
5 changed files with 66 additions and 13 deletions
24
src/format.rs
Normal file
24
src/format.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
pub trait Format {
|
||||
/// Format bytes to KB/MB/GB presentation
|
||||
fn bytes(self) -> String;
|
||||
}
|
||||
|
||||
impl Format for u64 {
|
||||
fn bytes(self) -> String {
|
||||
const KB: f32 = 1024.0;
|
||||
const MB: f32 = KB * KB;
|
||||
const GB: f32 = MB * KB;
|
||||
|
||||
let f = self as f32;
|
||||
|
||||
if f < KB {
|
||||
format!("{self} B")
|
||||
} else if f < MB {
|
||||
format!("{:.2} KB", f / KB)
|
||||
} else if f < GB {
|
||||
format!("{:.2} MB", f / MB)
|
||||
} else {
|
||||
format!("{:.2} GB", f / GB)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue