Merge pull request #412 from dezgeg/uucore_parse_size

lscpu: Use parse_size from uucore
This commit is contained in:
Daniel Hofstetter
2025-10-12 15:26:07 +02:00
committed by GitHub
3 changed files with 44 additions and 20 deletions

41
Cargo.lock generated
View File

@@ -85,6 +85,19 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bigdecimal"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a22f228ab7a1b23027ccc6c350b72868017af7ea8356fbdf19f8d991c690013"
dependencies = [
"autocfg",
"libm",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.71.1" version = "0.71.1"
@@ -599,6 +612,12 @@ dependencies = [
"windows-targets 0.52.6", "windows-targets 0.52.6",
] ]
[[package]]
name = "libm"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
[[package]] [[package]]
name = "libmount-sys" name = "libmount-sys"
version = "0.1.1" version = "0.1.1"
@@ -691,12 +710,31 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "num-conv" name = "num-conv"
version = "0.1.0" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.19" version = "0.2.19"
@@ -1549,15 +1587,18 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7203e48e80ac344450cba5323d8b4a71967ec1e81ae4022775ada90d2b0e08ac" checksum = "7203e48e80ac344450cba5323d8b4a71967ec1e81ae4022775ada90d2b0e08ac"
dependencies = [ dependencies = [
"bigdecimal",
"bstr", "bstr",
"clap", "clap",
"dns-lookup", "dns-lookup",
"fluent", "fluent",
"fluent-bundle", "fluent-bundle",
"fluent-syntax", "fluent-syntax",
"glob",
"jiff", "jiff",
"libc", "libc",
"nix", "nix",
"num-traits",
"number_prefix", "number_prefix",
"os_display", "os_display",
"thiserror", "thiserror",

View File

@@ -13,7 +13,7 @@ path = "src/main.rs"
[dependencies] [dependencies]
regex = { workspace = true } regex = { workspace = true }
sysinfo = { workspace = true } sysinfo = { workspace = true }
uucore = { workspace = true } uucore = { workspace = true, features = ["parser"] }
clap = { workspace = true } clap = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }

View File

@@ -4,6 +4,7 @@
// file that was distributed with this source code. // file that was distributed with this source code.
use std::{collections::HashSet, fs, path::PathBuf}; use std::{collections::HashSet, fs, path::PathBuf};
use uucore::parser::parse_size;
pub struct CpuVulnerability { pub struct CpuVulnerability {
pub name: String, pub name: String,
@@ -93,25 +94,7 @@ impl CacheSize {
} }
fn parse(s: &str) -> Self { fn parse(s: &str) -> Self {
// Yes, this will break if we ever reach a point where caches exceed terabytes in size... Self(parse_size::parse_size_u64(s).expect("Could not parse cache size"))
const EXPONENTS: [(char, u32); 4] = [('K', 1), ('M', 2), ('G', 3), ('T', 4)];
// If we only have numbers, treat it as a raw amount of bytes and parse as-is
if s.chars().all(char::is_numeric) {
return Self(s.parse::<u64>().expect("Could not parse cache size"));
};
for (suffix, exponent) in EXPONENTS {
if s.ends_with(suffix) {
let nums = s.strip_suffix(suffix).unwrap();
let value = nums.parse::<u64>().expect("Could not parse cache size");
let multiplier = 1024_u64.pow(exponent);
return Self(value * multiplier);
}
}
panic!("No known suffix in cache size string");
} }
pub fn size_bytes(&self) -> u64 { pub fn size_bytes(&self) -> u64 {