diff --git a/src/model/types/duration.rs b/src/model/types/duration.rs index 2fea87f..c360c07 100644 --- a/src/model/types/duration.rs +++ b/src/model/types/duration.rs @@ -33,17 +33,17 @@ impl<'de> Visitor<'de> for DurationVisitor { where E: de::Error, { - if let Some((value, unit)) = value.split_once(|c: char| c.is_ascii_alphabetic()) { - let value = value.parse().expect("Invalid duration"); - let unit = DurationUnit::from_str(unit).unwrap_or(DurationUnit::Milliseconds); + let (value, unit) = value.split_at( + value + .find(|c: char| c.is_ascii_alphabetic()) + .unwrap_or(value.len()), + ); + let value = value.parse().map_err(|_| { + de::Error::invalid_value(de::Unexpected::Str(value), &"a valid integer") + })?; + let unit = DurationUnit::from_str(unit).unwrap_or(DurationUnit::Milliseconds); - Ok(Duration { value, unit }) - } else { - Err(de::Error::invalid_value( - serde::de::Unexpected::Str(value), - &self, - )) - } + Ok(Duration { value, unit }) } } diff --git a/src/model/types/memory_size.rs b/src/model/types/memory_size.rs index e809c11..241eb9d 100644 --- a/src/model/types/memory_size.rs +++ b/src/model/types/memory_size.rs @@ -66,18 +66,17 @@ impl<'de> Deserialize<'de> for MemorySize { where E: de::Error, { - if let Some((value, unit)) = value.split_once(|c: char| c.is_ascii_alphabetic()) { - let value = value.parse().expect("Invalid memory size"); - Ok(MemorySize { - value, - unit: MemorySizeUnit::from_str(unit).unwrap_or(MemorySizeUnit::MiB), - }) - } else { - Ok(MemorySize { - value: value.parse::().unwrap_or(128), - unit: MemorySizeUnit::MiB, - }) - } + let (value, unit) = value.split_at( + value + .find(|c: char| c.is_ascii_alphabetic()) + .unwrap_or(value.len()), + ); + let value = value.parse().map_err(|_| { + de::Error::invalid_value(de::Unexpected::Str(value), &"a valid integer") + })?; + let unit = MemorySizeUnit::from_str(unit).unwrap_or(MemorySizeUnit::MiB); + + Ok(MemorySize { value, unit }) } }