| 1 | /// An error that occurs when parsing a human readable size description. |
| 2 | /// |
| 3 | /// This error provides an end user friendly message describing why the |
| 4 | /// description couldn't be parsed and what the expected format is. |
| 5 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 6 | pub struct ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 7 | originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 8 | kindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#kind.: ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#, |
| 9 | } |
| 10 | |
| 11 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 12 | enum ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind# { |
| 13 | InvalidFormatrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidFormat#, |
| 14 | InvalidIntrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidInt#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::ParseIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/ParseIntError#), |
| 15 | Overflowrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#Overflow#, |
| 16 | } |
| 17 | |
| 18 | impl ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 19 | fn formatrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]format().(original: &str) -> ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 20 | ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 21 | originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original.: original.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 22 | kindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#kind.: ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#::InvalidFormatrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidFormat#, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn intrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]int().(original: &str, err: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::ParseIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/ParseIntError#) -> ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 27 | ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 28 | originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original.: original.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 29 | kindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#kind.: ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#::InvalidIntrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidInt#(err), |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn overflowrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]overflow().(original: &str) -> ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 34 | ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 35 | originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original.: original.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 36 | kindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#kind.: ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#::Overflowrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#Overflow#, |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# {} |
| 42 | |
| 43 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError# { |
| 44 | fn fmtrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 45 | use selfrust-analyzer cargo grep-cli 0.1.12 human/::ParseSizeErrorKindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#::*; |
| 46 | |
| 47 | match self.kindrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#kind. { |
| 48 | InvalidFormatrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidFormat# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 49 | f, |
| 50 | "invalid format for size '{}', which should be a non-empty \ |
| 51 | sequence of digits followed by an optional 'K', 'M' or 'G' \ |
| 52 | suffix", |
| 53 | self.originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original. |
| 54 | ), |
| 55 | InvalidIntrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#InvalidInt#(ref err) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!( |
| 56 | f, |
| 57 | "invalid integer found in size '{}': {}", |
| 58 | self.originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original., err |
| 59 | ), |
| 60 | Overflowrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeErrorKind#Overflow# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "size too big in '{}'", self.originalrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#original.), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#> for stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# { |
| 66 | fn fromrust-analyzer cargo grep-cli 0.1.12 human/impl#[`std::io::Error`][`From<ParseSizeError>`]from().(size_err: ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error# { |
| 67 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, size_err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Parse a human readable size like `2M` into a corresponding number of bytes. |
| 72 | /// |
| 73 | /// Supported size suffixes are `K` (for kilobyte), `M` (for megabyte) and `G` |
| 74 | /// (for gigabyte). If a size suffix is missing, then the size is interpreted |
| 75 | /// as bytes. If the size is too big to fit into a `u64`, then this returns an |
| 76 | /// error. |
| 77 | /// |
| 78 | /// Additional suffixes may be added over time. |
| 79 | pub fn parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().(size: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<u64, ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#> { |
| 80 | let digits_end = |
| 81 | size.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().take_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take_while().(|&b| b.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_digit().()).countrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#count().(); |
| 82 | let digits = &size[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#digits_end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 83 | if digits.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 84 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#::formatrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]format().(size)); |
| 85 | } |
| 86 | let value = |
| 87 | digits.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<u64>().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#::intrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]int().(size, e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 88 | |
| 89 | let suffix = &size[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().digits_end..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 90 | if suffix.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 91 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value); |
| 92 | } |
| 93 | let bytes = match suffix { |
| 94 | "K" => value.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]checked_mul().(1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().10), |
| 95 | "M" => value.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]checked_mul().(1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().20), |
| 96 | "G" => value.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]checked_mul().(1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().30), |
| 97 | _ => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#::formatrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]format().(size)), |
| 98 | }; |
| 99 | bytes.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ParseSizeErrorrust-analyzer cargo grep-cli 0.1.12 human/ParseSizeError#::overflowrust-analyzer cargo grep-cli 0.1.12 human/impl#[ParseSizeError]overflow().(size)) |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod testsrust-analyzer cargo grep-cli 0.1.12 human/tests/ { |
| 104 | use superrust-analyzer cargo grep-cli 0.1.12 human/::*; |
| 105 | |
| 106 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 107 | fn suffix_nonerust-analyzer cargo grep-cli 0.1.12 human/tests/suffix_none().() { |
| 108 | let x = parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("123").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 109 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(123, x); |
| 110 | } |
| 111 | |
| 112 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 113 | fn suffix_krust-analyzer cargo grep-cli 0.1.12 human/tests/suffix_k().() { |
| 114 | let x = parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("123K").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(123 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). (1 << 10), x); |
| 116 | } |
| 117 | |
| 118 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 119 | fn suffix_mrust-analyzer cargo grep-cli 0.1.12 human/tests/suffix_m().() { |
| 120 | let x = parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("123M").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 121 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(123 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). (1 << 20), x); |
| 122 | } |
| 123 | |
| 124 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 125 | fn suffix_grust-analyzer cargo grep-cli 0.1.12 human/tests/suffix_g().() { |
| 126 | let x = parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("123G").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 127 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(123 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). (1 << 30), x); |
| 128 | } |
| 129 | |
| 130 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 131 | fn invalid_emptyrust-analyzer cargo grep-cli 0.1.12 human/tests/invalid_empty().() { |
| 132 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 133 | } |
| 134 | |
| 135 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 136 | fn invalid_non_digitrust-analyzer cargo grep-cli 0.1.12 human/tests/invalid_non_digit().() { |
| 137 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("a").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 138 | } |
| 139 | |
| 140 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 141 | fn invalid_overflowrust-analyzer cargo grep-cli 0.1.12 human/tests/invalid_overflow().() { |
| 142 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("9999999999999999G").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 143 | } |
| 144 | |
| 145 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 146 | fn invalid_suffixrust-analyzer cargo grep-cli 0.1.12 human/tests/invalid_suffix().() { |
| 147 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parse_human_readable_sizerust-analyzer cargo grep-cli 0.1.12 human/parse_human_readable_size().("123T").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 148 | } |
| 149 | } |