Skip to content

Commit

Permalink
Make an error message less confusing when an input value cannot be de…
Browse files Browse the repository at this point in the history
…serialized.
  • Loading branch information
haimgel committed Oct 29, 2023
1 parent 727c70f commit 8b8f384
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/input_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,28 @@ impl InputSource {
}
}

fn parse_int(s: &str) -> Result<u16, std::num::ParseIntError> {
if s.starts_with("0x") {
// Parse as hexadecimal
u16::from_str_radix(&s[2..], 16)
} else {
// Parse as decimal
s.parse::<u16>()
}
}

impl<'de> Deserialize<'de> for InputSource {
fn deserialize<D>(deserializer: D) -> Result<InputSource, D::Error>
where
D: Deserializer<'de>,
{
let str = String::deserialize(deserializer)?.trim().to_lowercase();
let val = SymbolicInputSource::try_from(str.as_str());
if let Ok(sym) = val {
Ok(Self::Symbolic(sym))
} else {
let result;
if str.starts_with("0x") {
result = u16::from_str_radix(str.trim_start_matches("0x"), 16);
} else {
result = u16::from_str_radix(&str, 10);
}
result
.map(|val| Self::Raw(val).normalize())
.map_err(|err| D::Error::custom(format!("{:?}", err)))
if let Ok(val) = parse_int(&str) {
Ok(Self::Raw(val).normalize())
}else {
SymbolicInputSource::try_from(str.as_str())
.map(Self::Symbolic)
.map_err(|_| D::Error::custom(format!("Invalid input source: {}", str)))
}
}
}
Expand Down

0 comments on commit 8b8f384

Please sign in to comment.