Skip to content

Commit 4c353fd

Browse files
author
Devdutt Shenoi
committed
refactor: custom error type
1 parent 0c1d992 commit 4c353fd

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/utils/human_size.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ use std::str::FromStr;
33
use human_size::{Any, SpecificSize};
44
use serde::{de, Deserialize, Deserializer, Serializer};
55

6+
#[derive(Debug, thiserror::Error)]
7+
enum ParsingError {
8+
#[error("Expected 'X' | 'X Bytes', but error: {0}")]
9+
Int(#[from] std::num::ParseIntError),
10+
#[error("Could not parse given string as human size, erro: {0}")]
11+
HumanSize(#[from] human_size::ParsingError),
12+
}
13+
614
// Function to convert human-readable size to bytes (already provided)
715
// NOTE: consider number values as byte count, e.g. "1234" is 1234 bytes.
8-
pub fn human_size_to_bytes(s: &str) -> Result<u64, String> {
16+
fn human_size_to_bytes(s: &str) -> Result<u64, ParsingError> {
917
let s = s.trim();
1018
if let Some(s) = s.strip_suffix("Bytes") {
11-
let size = s.trim().parse().expect("Suffix bytes implies byte count");
19+
let size: u64 = s.trim().parse()?;
1220
return Ok(size);
1321
} else if let Ok(size) = s.parse() {
1422
return Ok(size);
@@ -17,8 +25,7 @@ pub fn human_size_to_bytes(s: &str) -> Result<u64, String> {
1725
fn parse_and_map<T: human_size::Multiple>(s: &str) -> Result<u64, human_size::ParsingError> {
1826
SpecificSize::<T>::from_str(s).map(|x| x.to_bytes())
1927
}
20-
21-
let size = parse_and_map::<Any>(s).map_err(|_| "Could not parse given size".to_string())?;
28+
let size = parse_and_map::<Any>(s)?;
2229

2330
Ok(size)
2431
}
@@ -66,33 +73,41 @@ where
6673

6774
#[cfg(test)]
6875
mod tests {
69-
use crate::utils::human_size::human_size_to_bytes;
76+
use super::*;
7077

7178
#[test]
7279
fn parse_numeric_input_without_unit() {
73-
assert_eq!(human_size_to_bytes("1234"), Ok(1234));
80+
assert_eq!(human_size_to_bytes("1234").unwrap(), 1234);
7481
}
7582

7683
#[test]
7784
fn parse_bytes_string_to_bytes() {
78-
assert_eq!(human_size_to_bytes("1234 Bytes"), Ok(1234));
85+
assert_eq!(human_size_to_bytes("1234 Bytes").unwrap(), 1234);
7986
}
8087

8188
#[test]
8289
fn handle_empty_string_input() {
83-
assert_eq!(
90+
assert!(matches!(
8491
human_size_to_bytes(""),
85-
Err("Could not parse given size".to_string())
86-
);
92+
Err(ParsingError::HumanSize(_))
93+
));
94+
}
95+
96+
#[test]
97+
fn handle_byte_string_input_without_value() {
98+
assert!(matches!(
99+
human_size_to_bytes("Bytes"),
100+
Err(ParsingError::Int(_))
101+
));
87102
}
88103

89104
#[test]
90105
fn convert_mebibyte_string_to_bytes() {
91-
assert_eq!(human_size_to_bytes("1 MiB"), Ok(1048576));
106+
assert_eq!(human_size_to_bytes("1 MiB").unwrap(), 1048576);
92107
}
93108

94109
#[test]
95110
fn parse_gigabyte_string_input() {
96-
assert_eq!(human_size_to_bytes("1 GB"), Ok(1_000_000_000));
111+
assert_eq!(human_size_to_bytes("1 GB").unwrap(), 1_000_000_000);
97112
}
98113
}

0 commit comments

Comments
 (0)