From 188aca8fe68f518561020770eee5fd07c06a2c87 Mon Sep 17 00:00:00 2001 From: Yash Thakur <45539777+ysthakur@users.noreply.github.com> Date: Sun, 21 Jan 2024 15:17:28 -0500 Subject: [PATCH] Upgrade byte-unit from 4.0 to 5.1 (#11584) # Description This PR is for using version 5.1 of [byte_unit](https://docs.rs/byte-unit/latest/byte_unit/index.html) instead of 4.0. dependabot opened https://github.com/nushell/nushell/pull/11499 to do this but it's a major version increment so some minor changes were necessary. # User-Facing Changes If something is on the boundary of a unit (e.g. 1024 bytes = 1 kibibytes), that will now be formatted as `1.0 KiB` where it used to be formatted as `1,024 B`. # Tests + Formatting # After Submitting --- Cargo.lock | 258 ++++++++++++++++-- .../nu-command/src/conversions/into/string.rs | 2 +- crates/nu-protocol/Cargo.toml | 2 +- crates/nu-protocol/src/value/mod.rs | 92 ++++--- 4 files changed, 298 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5755e1b7d0..dbc7f0f9d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,17 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" +[[package]] +name = "ahash" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "ahash" version = "0.8.7" @@ -403,6 +414,18 @@ dependencies = [ "serde", ] +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "block" version = "0.1.6" @@ -418,6 +441,30 @@ dependencies = [ "generic-array", ] +[[package]] +name = "borsh" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58b559fd6448c6e2fd0adb5720cd98a2506594cafa4737ff98c396f3e82f667" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aadb5b6ccbd078890f6d7003694e33816e6b784358f18e15e7e6d9f065a57cd" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.48", + "syn_derive", +] + [[package]] name = "bracoxide" version = "0.1.3" @@ -464,14 +511,37 @@ checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" [[package]] name = "byte-unit" -version = "4.0.19" +version = "5.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c" +checksum = "cbda27216be70d08546aa506cecabce0c5eb0d494aaaedbd7ec82c8ae1a60b46" dependencies = [ + "rust_decimal", "serde", "utf8-width", ] +[[package]] +name = "bytecheck" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6372023ac861f6e6dc89c8344a8f398fb42aaba2b5dbc649ca0c0e9dbcb627" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7ec4c6f261935ad534c0c22dbef2201b45918860eb1c574b972bd213a76af61" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "bytecount" version = "0.6.7" @@ -568,6 +638,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chardetng" version = "0.1.17" @@ -1452,6 +1528,12 @@ dependencies = [ "libc", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futf" version = "0.1.5" @@ -1695,13 +1777,22 @@ dependencies = [ "byteorder", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash 0.7.7", +] + [[package]] name = "hashbrown" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" dependencies = [ - "ahash", + "ahash 0.8.7", ] [[package]] @@ -1710,7 +1801,7 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ - "ahash", + "ahash 0.8.7", "allocator-api2", "rayon", ] @@ -2812,7 +2903,7 @@ dependencies = [ name = "nu-cmd-extra" version = "0.89.1" dependencies = [ - "ahash", + "ahash 0.8.7", "fancy-regex", "heck", "nu-ansi-term", @@ -3862,7 +3953,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd503430a6d9779b07915d858865fe998317ef3cfef8973881f578ac5d4baae7" dependencies = [ - "ahash", + "ahash 0.8.7", "arrow-format", "atoi_simd", "avro-schema", @@ -3897,7 +3988,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae73d5b8e55decde670caba1cc82b61f14bfb9a72503198f0997d657a98dcfd6" dependencies = [ - "ahash", + "ahash 0.8.7", "bitflags 2.4.2", "bytemuck", "chrono", @@ -3942,7 +4033,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96e10a0745acd6009db64bef0ceb9e23a70b1c27b26a0a6517c91f3e6363bc06" dependencies = [ - "ahash", + "ahash 0.8.7", "async-trait", "atoi_simd", "bytes", @@ -3983,7 +4074,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93b9cb83c19daf334c398e56a9361bd79c8ad0718296db2afab08d476bd84559" dependencies = [ - "ahash", + "ahash 0.8.7", "chrono", "fallible-streaming-iterator", "hashbrown 0.14.3", @@ -4004,7 +4095,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3555f759705be6dd0d3762d16a0b8787b2dc4da73b57465f3b2bf1a070ba8f20" dependencies = [ - "ahash", + "ahash 0.8.7", "bitflags 2.4.2", "glob", "once_cell", @@ -4028,7 +4119,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a7eb218296aaa7f79945f08288ca32ca3cf25fa505649eeee689ec21eebf636" dependencies = [ - "ahash", + "ahash 0.8.7", "argminmax", "bytemuck", "either", @@ -4055,7 +4146,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "146010e4b7dd4d2d0e58ddc762f6361f77d7a0385c54471199370c17164f67dd" dependencies = [ - "ahash", + "ahash 0.8.7", "async-stream", "base64 0.21.7", "brotli", @@ -4104,7 +4195,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10e32a0958ef854b132bad7f8369cb3237254635d5e864c99505bc0bc1035fbc" dependencies = [ - "ahash", + "ahash 0.8.7", "bytemuck", "once_cell", "percent-encoding", @@ -4177,7 +4268,7 @@ version = "0.35.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da6ce68169fe61d46958c8eab7447360f30f2f23f6e24a0ce703a14b0a3cfbfc" dependencies = [ - "ahash", + "ahash 0.8.7", "bytemuck", "hashbrown 0.14.3", "indexmap", @@ -4269,6 +4360,38 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit 0.21.0", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.78" @@ -4313,6 +4436,26 @@ dependencies = [ "cc", ] +[[package]] +name = "ptr_meta" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +dependencies = [ + "ptr_meta_derive", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "pure-rust-locales" version = "0.7.0" @@ -4397,6 +4540,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79ec282e887b434b68c18fe5c121d38e72a5cf35119b59e54ec5b992ea9c8eb0" +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -4571,6 +4720,15 @@ version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e898588f33fdd5b9420719948f9f2a32c922a246964576f71ba7f24f80610fbc" +[[package]] +name = "rend" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2571463863a6bd50c32f94402933f03457a3fbaf697a707c5be741e459f08fd" +dependencies = [ + "bytecheck", +] + [[package]] name = "rfc2047-decoder" version = "1.0.2" @@ -4585,6 +4743,35 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rkyv" +version = "0.7.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527a97cdfef66f65998b5f3b637c26f5a5ec09cc52a3f9932313ac645f4190f5" +dependencies = [ + "bitvec", + "bytecheck", + "bytes", + "hashbrown 0.12.3", + "ptr_meta", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5c462a1328c8e67e4d6dbad1eb0355dd43e8ab432c6e227a43657f16ade5033" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "rle-decode-fast" version = "1.0.3" @@ -4727,7 +4914,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06676aec5ccb8fc1da723cc8c0f9a46549f21ebb8753d3915c6c41db1e7f1dc4" dependencies = [ "arrayvec 0.7.4", + "borsh", + "bytes", "num-traits", + "rand", + "rkyv", + "serde", + "serde_json", ] [[package]] @@ -4812,7 +5005,7 @@ version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "585480e3719b311b78a573db1c9d9c4c1f8010c2dee4cc59c2efe58ea4dbc3e1" dependencies = [ - "ahash", + "ahash 0.8.7", "cssparser", "ego-tree", "html5ever", @@ -4821,6 +5014,12 @@ dependencies = [ "tendril", ] +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + [[package]] name = "security-framework" version = "2.9.2" @@ -5055,7 +5254,7 @@ version = "0.13.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2faf8f101b9bc484337a6a6b0409cf76c139f2fb70a9e3aee6b6774be7bfbf76" dependencies = [ - "ahash", + "ahash 0.8.7", "getrandom", "halfbrown", "lexical-core", @@ -5355,6 +5554,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "sys-locale" version = "0.3.1" @@ -5405,6 +5616,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "target-features" version = "0.1.5" @@ -6604,6 +6821,15 @@ dependencies = [ "wayland-protocols-wlr", ] +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "x11rb" version = "0.12.0" diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index f4e671840a..dba93c0d6a 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -130,7 +130,7 @@ impl Command for SubCommand { Example { description: "convert filesize to string", example: "1KiB | into string", - result: Some(Value::test_string("1,024 B")), + result: Some(Value::test_string("1.0 KiB")), }, Example { description: "convert duration to string", diff --git a/crates/nu-protocol/Cargo.toml b/crates/nu-protocol/Cargo.toml index 25d1490c90..e30360c12e 100644 --- a/crates/nu-protocol/Cargo.toml +++ b/crates/nu-protocol/Cargo.toml @@ -17,7 +17,7 @@ nu-utils = { path = "../nu-utils", version = "0.89.1" } nu-path = { path = "../nu-path", version = "0.89.1" } nu-system = { path = "../nu-system", version = "0.89.1" } -byte-unit = "4.0" +byte-unit = "5.1" chrono = { version = "0.4", features = [ "serde", "std", "unstable-locales" ], default-features = false } chrono-humanize = "0.2" fancy-regex = "0.12" diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index af77f8e6d5..6bb74cd2bc 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -14,7 +14,7 @@ use crate::engine::{Closure, EngineState}; use crate::ShellError; use crate::{did_you_mean, BlockId, Config, Span, Spanned, Type}; -use byte_unit::ByteUnit; +use byte_unit::UnitType; use chrono::{DateTime, Datelike, Duration, FixedOffset, Locale, TimeZone}; use chrono_humanize::HumanTime; pub use custom_value::CustomValue; @@ -3716,19 +3716,22 @@ pub fn format_filesize( // When format_value is "auto" or an invalid value, the returned ByteUnit doesn't matter // and is always B. - let filesize_format_var = get_filesize_format(format_value, filesize_metric); - - let byte = byte_unit::Byte::from_bytes(num_bytes.unsigned_abs() as u128); - let adj_byte = if filesize_format_var.1 == "auto" { + let filesize_unit = get_filesize_format(format_value, filesize_metric); + let byte = byte_unit::Byte::from_u64(num_bytes.unsigned_abs()); + let adj_byte = if let Some(unit) = filesize_unit { + byte.get_adjusted_unit(unit) + } else { // When filesize_metric is None, format_value should never be "auto", so this // unwrap_or() should always work. - byte.get_appropriate_unit(!filesize_metric.unwrap_or(false)) - } else { - byte.get_adjusted_unit(filesize_format_var.0) + byte.get_appropriate_unit(if filesize_metric.unwrap_or(false) { + UnitType::Decimal + } else { + UnitType::Binary + }) }; match adj_byte.get_unit() { - byte_unit::ByteUnit::B => { + byte_unit::Unit::B => { let locale = get_system_locale(); let locale_byte = adj_byte.get_value() as u64; let locale_byte_string = locale_byte.to_formatted_string(&locale); @@ -3738,7 +3741,7 @@ pub fn format_filesize( locale_byte_string }; - if filesize_format_var.1 == "auto" { + if filesize_unit.is_none() { format!("{locale_signed_byte_string} B") } else { locale_signed_byte_string @@ -3746,44 +3749,39 @@ pub fn format_filesize( } _ => { if num_bytes.is_negative() { - format!("-{}", adj_byte.format(1)) + format!("-{:.1}", adj_byte) } else { - adj_byte.format(1) + format!("{:.1}", adj_byte) } } } } -fn get_filesize_format(format_value: &str, filesize_metric: Option) -> (ByteUnit, &str) { +/// Get the filesize unit, or None if format is "auto" +fn get_filesize_format( + format_value: &str, + filesize_metric: Option, +) -> Option { + // filesize_metric always overrides the unit of filesize_format. + let metric = filesize_metric.unwrap_or(!format_value.ends_with("ib")); macro_rules! either { - ($in:ident, $metric:ident, $binary:ident) => { - ( - // filesize_metric always overrides the unit of - // filesize_format. - match filesize_metric { - Some(true) => byte_unit::ByteUnit::$metric, - Some(false) => byte_unit::ByteUnit::$binary, - None => { - if $in.ends_with("ib") { - byte_unit::ByteUnit::$binary - } else { - byte_unit::ByteUnit::$metric - } - } - }, - "", - ) + ($metric:ident, $binary:ident) => { + Some(if metric { + byte_unit::Unit::$metric + } else { + byte_unit::Unit::$binary + }) }; } match format_value { - "b" => (byte_unit::ByteUnit::B, ""), - "kb" | "kib" => either!(format_value, KB, KiB), - "mb" | "mib" => either!(format_value, MB, MiB), - "gb" | "gib" => either!(format_value, GB, GiB), - "tb" | "tib" => either!(format_value, TB, TiB), - "pb" | "pib" => either!(format_value, TB, TiB), - "eb" | "eib" => either!(format_value, EB, EiB), - _ => (byte_unit::ByteUnit::B, "auto"), + "b" => Some(byte_unit::Unit::B), + "kb" | "kib" => either!(KB, KiB), + "mb" | "mib" => either!(MB, MiB), + "gb" | "gib" => either!(GB, GiB), + "tb" | "tib" => either!(TB, TiB), + "pb" | "pib" => either!(TB, TiB), + "eb" | "eib" => either!(EB, EiB), + _ => None, } } @@ -3866,8 +3864,10 @@ mod tests { mod into_string { use chrono::{DateTime, FixedOffset, NaiveDateTime}; + use rstest::rstest; use super::*; + use crate::format_filesize; #[test] fn test_datetime() { @@ -3896,5 +3896,21 @@ mod tests { let formatted = string.split(' ').next().unwrap(); assert_eq!("-0316-02-11T06:13:20+00:00", formatted); } + + #[rstest] + #[case(1000, Some(true), "auto", "1.0 KB")] + #[case(1000, Some(false), "auto", "1,000 B")] + #[case(1000, Some(false), "kb", "1.0 KiB")] + #[case(3000, Some(false), "auto", "2.9 KiB")] + #[case(3_000_000, None, "auto", "2.9 MiB")] + #[case(3_000_000, None, "kib", "2929.7 KiB")] + fn test_filesize( + #[case] val: i64, + #[case] filesize_metric: Option, + #[case] filesize_format: String, + #[case] exp: &str, + ) { + assert_eq!(exp, format_filesize(val, &filesize_format, filesize_metric)); + } } }