Merge pull request #412 from dezgeg/uucore_parse_size
lscpu: Use parse_size from uucore
This commit is contained in:
41
Cargo.lock
generated
41
Cargo.lock
generated
@@ -85,6 +85,19 @@ version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "bindgen"
|
||||
version = "0.71.1"
|
||||
@@ -599,6 +612,12 @@ dependencies = [
|
||||
"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]]
|
||||
name = "libmount-sys"
|
||||
version = "0.1.1"
|
||||
@@ -691,12 +710,31 @@ dependencies = [
|
||||
"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]]
|
||||
name = "num-conv"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
@@ -1549,15 +1587,18 @@ version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7203e48e80ac344450cba5323d8b4a71967ec1e81ae4022775ada90d2b0e08ac"
|
||||
dependencies = [
|
||||
"bigdecimal",
|
||||
"bstr",
|
||||
"clap",
|
||||
"dns-lookup",
|
||||
"fluent",
|
||||
"fluent-bundle",
|
||||
"fluent-syntax",
|
||||
"glob",
|
||||
"jiff",
|
||||
"libc",
|
||||
"nix",
|
||||
"num-traits",
|
||||
"number_prefix",
|
||||
"os_display",
|
||||
"thiserror",
|
||||
|
||||
@@ -13,7 +13,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
regex = { workspace = true }
|
||||
sysinfo = { workspace = true }
|
||||
uucore = { workspace = true }
|
||||
uucore = { workspace = true, features = ["parser"] }
|
||||
clap = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// file that was distributed with this source code.
|
||||
|
||||
use std::{collections::HashSet, fs, path::PathBuf};
|
||||
use uucore::parser::parse_size;
|
||||
|
||||
pub struct CpuVulnerability {
|
||||
pub name: String,
|
||||
@@ -93,25 +94,7 @@ impl CacheSize {
|
||||
}
|
||||
|
||||
fn parse(s: &str) -> Self {
|
||||
// Yes, this will break if we ever reach a point where caches exceed terabytes in 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");
|
||||
Self(parse_size::parse_size_u64(s).expect("Could not parse cache size"))
|
||||
}
|
||||
|
||||
pub fn size_bytes(&self) -> u64 {
|
||||
|
||||
Reference in New Issue
Block a user