Skip to content

Commit

Permalink
misc: make HumanU64 parse errors more useful
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyscot committed Dec 26, 2024
1 parent 013ea2b commit 63bf2f2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/util/humanu64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,12 +43,18 @@ impl From<HumanU64> for u64 {
}

impl FromStr for HumanU64 {
type Err = anyhow::Error;
type Err = figment::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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(),
))
}
Expand Down

0 comments on commit 63bf2f2

Please sign in to comment.