|
|
|
@ -1,8 +1,20 @@
|
|
|
|
|
use super::*;
|
|
|
|
|
use smallmap::Map;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
const UNIT: &'static str = "bytes";
|
|
|
|
@ -11,6 +23,17 @@ impl Conversion for Bytes
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|