2018-08-29 20:53:52 -04:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
use std::num::ParseIntError;
|
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
|
|
|
|
/// An error that occurs when parsing a human readable size description.
|
|
|
|
///
|
2020-06-04 21:06:09 +08:00
|
|
|
/// This error provides an end user friendly message describing why the
|
2018-08-29 20:53:52 -04:00
|
|
|
/// description coudln't be parsed and what the expected format is.
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct ParseSizeError {
|
|
|
|
original: String,
|
|
|
|
kind: ParseSizeErrorKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
enum ParseSizeErrorKind {
|
|
|
|
InvalidFormat,
|
|
|
|
InvalidInt(ParseIntError),
|
|
|
|
Overflow,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseSizeError {
|
|
|
|
fn format(original: &str) -> ParseSizeError {
|
|
|
|
ParseSizeError {
|
|
|
|
original: original.to_string(),
|
|
|
|
kind: ParseSizeErrorKind::InvalidFormat,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int(original: &str, err: ParseIntError) -> ParseSizeError {
|
|
|
|
ParseSizeError {
|
|
|
|
original: original.to_string(),
|
|
|
|
kind: ParseSizeErrorKind::InvalidInt(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn overflow(original: &str) -> ParseSizeError {
|
|
|
|
ParseSizeError {
|
|
|
|
original: original.to_string(),
|
|
|
|
kind: ParseSizeErrorKind::Overflow,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for ParseSizeError {
|
2020-02-17 18:08:47 -05:00
|
|
|
fn description(&self) -> &str {
|
|
|
|
"invalid size"
|
|
|
|
}
|
2018-08-29 20:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ParseSizeError {
|
2021-06-01 19:47:46 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-29 20:53:52 -04:00
|
|
|
use self::ParseSizeErrorKind::*;
|
|
|
|
|
|
|
|
match self.kind {
|
2020-02-17 18:08:47 -05:00
|
|
|
InvalidFormat => write!(
|
|
|
|
f,
|
|
|
|
"invalid format for size '{}', which should be a sequence \
|
2018-08-29 20:53:52 -04:00
|
|
|
of digits followed by an optional 'K', 'M' or 'G' \
|
|
|
|
suffix",
|
2020-02-17 18:08:47 -05:00
|
|
|
self.original
|
|
|
|
),
|
|
|
|
InvalidInt(ref err) => write!(
|
|
|
|
f,
|
|
|
|
"invalid integer found in size '{}': {}",
|
|
|
|
self.original, err
|
|
|
|
),
|
|
|
|
Overflow => write!(f, "size too big in '{}'", self.original),
|
2018-08-29 20:53:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseSizeError> for io::Error {
|
|
|
|
fn from(size_err: ParseSizeError) -> io::Error {
|
|
|
|
io::Error::new(io::ErrorKind::Other, size_err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a human readable size like `2M` into a corresponding number of bytes.
|
|
|
|
///
|
|
|
|
/// Supported size suffixes are `K` (for kilobyte), `M` (for megabyte) and `G`
|
|
|
|
/// (for gigabyte). If a size suffix is missing, then the size is interpreted
|
|
|
|
/// as bytes. If the size is too big to fit into a `u64`, then this returns an
|
|
|
|
/// error.
|
|
|
|
///
|
|
|
|
/// Additional suffixes may be added over time.
|
|
|
|
pub fn parse_human_readable_size(size: &str) -> Result<u64, ParseSizeError> {
|
2021-06-01 20:45:45 -04:00
|
|
|
lazy_static::lazy_static! {
|
2018-08-29 20:53:52 -04:00
|
|
|
// Normally I'd just parse something this simple by hand to avoid the
|
|
|
|
// regex dep, but we bring regex in any way for glob matching, so might
|
|
|
|
// as well use it.
|
|
|
|
static ref RE: Regex = Regex::new(r"^([0-9]+)([KMG])?$").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let caps = match RE.captures(size) {
|
|
|
|
Some(caps) => caps,
|
|
|
|
None => return Err(ParseSizeError::format(size)),
|
|
|
|
};
|
2020-02-17 18:08:47 -05:00
|
|
|
let value: u64 =
|
|
|
|
caps[1].parse().map_err(|err| ParseSizeError::int(size, err))?;
|
2018-08-29 20:53:52 -04:00
|
|
|
let suffix = match caps.get(2) {
|
|
|
|
None => return Ok(value),
|
|
|
|
Some(cap) => cap.as_str(),
|
|
|
|
};
|
|
|
|
let bytes = match suffix {
|
2020-02-17 18:08:47 -05:00
|
|
|
"K" => value.checked_mul(1 << 10),
|
|
|
|
"M" => value.checked_mul(1 << 20),
|
|
|
|
"G" => value.checked_mul(1 << 30),
|
2018-08-29 20:53:52 -04:00
|
|
|
// Because if the regex matches this group, it must be [KMG].
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
bytes.ok_or_else(|| ParseSizeError::overflow(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suffix_none() {
|
|
|
|
let x = parse_human_readable_size("123").unwrap();
|
|
|
|
assert_eq!(123, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suffix_k() {
|
|
|
|
let x = parse_human_readable_size("123K").unwrap();
|
2020-02-17 18:08:47 -05:00
|
|
|
assert_eq!(123 * (1 << 10), x);
|
2018-08-29 20:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suffix_m() {
|
|
|
|
let x = parse_human_readable_size("123M").unwrap();
|
2020-02-17 18:08:47 -05:00
|
|
|
assert_eq!(123 * (1 << 20), x);
|
2018-08-29 20:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suffix_g() {
|
|
|
|
let x = parse_human_readable_size("123G").unwrap();
|
2020-02-17 18:08:47 -05:00
|
|
|
assert_eq!(123 * (1 << 30), x);
|
2018-08-29 20:53:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_empty() {
|
|
|
|
assert!(parse_human_readable_size("").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_non_digit() {
|
|
|
|
assert!(parse_human_readable_size("a").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_overflow() {
|
|
|
|
assert!(parse_human_readable_size("9999999999999999G").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn invalid_suffix() {
|
|
|
|
assert!(parse_human_readable_size("123T").is_err());
|
|
|
|
}
|
|
|
|
}
|