From 63bf2f2ef2902ef1db211323e041b471e32454bc Mon Sep 17 00:00:00 2001 From: Ross Younger Date: Sat, 21 Dec 2024 12:54:42 +1300 Subject: [PATCH] misc: make HumanU64 parse errors more useful --- src/util/humanu64.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/util/humanu64.rs b/src/util/humanu64.rs index 8578fa3..b9e97df 100644 --- a/src/util/humanu64.rs +++ b/src/util/humanu64.rs @@ -3,9 +3,11 @@ use std::{marker::PhantomData, ops::Deref, str::FromStr}; -use anyhow::Context as _; use humanize_rs::bytes::Bytes; -use serde::Serialize; +use serde::{ + de::{self, Error as _}, + Serialize, +}; use super::cli::IntOrString; @@ -41,12 +43,18 @@ impl From for u64 { } impl FromStr for HumanU64 { - type Err = anyhow::Error; + type Err = figment::Error; fn from_str(s: &str) -> Result { + use figment::error::Error as FigmentError; Ok(Self::new( Bytes::from_str(s) - .with_context(|| "parsing bytes string")? + .map_err(|_| { + FigmentError::invalid_value( + de::Unexpected::Str(s), + &"an integer with optional units (examples: `100`, `10M`, `42k`)", + ) + })? .size(), )) }