bytes: works

master
Avril 4 years ago
parent a536c8f9f5
commit 03af2f2ed9
Signed by: flanchan
GPG Key ID: 284488987C31F630

34
Cargo.lock generated

@ -6,9 +6,43 @@ 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 = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "smallmap"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d61d73d5986b7f728a76234ca60f7826faa44dd9043d2954ca6583bfc7b875d"
dependencies = [
"rustc_version",
]
[[package]] [[package]]
name = "units" name = "units"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"smallmap",
] ]

@ -8,3 +8,4 @@ edition = "2018"
[dependencies] [dependencies]
lazy_static = "1.4.0" lazy_static = "1.4.0"
smallmap = "1.3.0"

@ -1,8 +1,20 @@
use super::*; use super::*;
use smallmap::Map;
#[derive(Debug)] #[derive(Debug)]
pub struct Bytes; pub struct Bytes;
const KB: u64 = 1024;
lazy_static! {
static ref SUFFIX: Map<char, u64> = smallmap::smallmap! {
{'k' => KB},
{'m' => KB * KB},
{'g' => KB * KB * KB},
{'p' => KB * KB * KB * KB},
};
}
impl Conversion for Bytes impl Conversion for Bytes
{ {
const UNIT: &'static str = "bytes"; const UNIT: &'static str = "bytes";
@ -11,6 +23,17 @@ impl Conversion for Bytes
fn convert(&self, input: &str) -> Result<Self::Output, Self::Error> fn convert(&self, input: &str) -> Result<Self::Output, Self::Error>
{ {
todo!() #[macro_export] macro_rules! parse {
($non:expr) => (($non).parse::<u64>().map_err(|e| ConversionError(format!("Failed to parse u64: {}", e)))?)
}
match input.char_indices().last().map(|(idx, chr)| (chr.to_lowercase().next().unwrap(), &input[..idx])) {
Some((chr, _)) if chr.is_numeric() => Ok(parse!(input)),
Some((ref chr, non)) if SUFFIX.contains_key(chr) => Ok(*SUFFIX.get(chr).unwrap() * parse!(non)),
Some((chr, _)) => Err(ConversionError(format!("Unknown suffix {}", chr))),
None => Ok(0),
}
} }
} }

Loading…
Cancel
Save