Add dependency on byte-unit (4.0.19) to be able to print bytes in human readable formats

Contributes to #5487
This commit is contained in:
Fábio Beirão 2023-06-02 17:48:43 +02:00
parent 200449511c
commit ab320e161b
No known key found for this signature in database
GPG key ID: 13FD3A2130278AAE
3 changed files with 15 additions and 2 deletions

11
Cargo.lock generated
View file

@ -358,6 +358,16 @@ version = "3.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c6ed94e98ecff0c12dd1b04c15ec0d7d9458ca8fe806cea6f12954efe74c63b"
[[package]]
name = "byte-unit"
version = "4.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c"
dependencies = [
"serde",
"utf8-width",
]
[[package]]
name = "bytemuck"
version = "1.13.1"
@ -3850,6 +3860,7 @@ name = "roc_reporting"
version = "0.0.1"
dependencies = [
"bumpalo",
"byte-unit",
"distance",
"indoc",
"insta",

View file

@ -22,6 +22,7 @@ roc_solve_problem = { path = "../compiler/solve_problem" }
roc_std = { path = "../roc_std" }
roc_types = { path = "../compiler/types" }
ven_pretty = { path = "../vendor/pretty" }
byte-unit = "4.0.19"
itertools = "0.10.5"
bumpalo.workspace = true

View file

@ -6,6 +6,7 @@ use roc_region::all::LineColumnRegion;
use std::path::{Path, PathBuf};
use std::{fmt, io};
use ven_pretty::{text, BoxAllocator, DocAllocator, DocBuilder, Render, RenderAnnotated};
use byte_unit::Byte;
#[cfg(not(target_family = "wasm"))]
use roc_packaging::https::Problem;
@ -1510,6 +1511,7 @@ pub fn to_https_problem_report<'b>(
}
}
Problem::DownloadTooBig(content_len) => {
let nice_bytes = Byte::from_bytes(content_len.into()).get_appropriate_unit(false).format(3);
let doc = alloc.stack([
alloc.reflow(r"I was trying to download this URL:"),
alloc
@ -1518,8 +1520,7 @@ pub fn to_https_problem_report<'b>(
.indent(4),
alloc.concat([
alloc.reflow(r"But the server stated this file is "),
// TODO: evaluate taking a dependency on https://docs.rs/byte-unit/ to provide a better formatting
alloc.string(format!("{} bytes", content_len)).annotate(Annotation::Keyword),
alloc.string(nice_bytes).annotate(Annotation::Keyword),
alloc.reflow(r" in size. This is larger that the maximum size I can handle (around 32 GB)."),
]),
alloc.concat([