1use std::num::ParseIntError;
2
3pub fn parse_u32(text: &str) -> Result<u32, ParseIntError> {
4 if let Some(hex) = text.strip_prefix("0x") {
5 u32::from_str_radix(hex, 16)
6 } else {
7 u32::from_str_radix(text, 10)
8 }
9}
10
11pub fn parse_u16(text: &str) -> Result<u16, ParseIntError> {
12 if let Some(hex) = text.strip_prefix("0x") {
13 u16::from_str_radix(hex, 16)
14 } else {
15 u16::from_str_radix(text, 10)
16 }
17}
18
19pub fn parse_i32(text: &str) -> Result<i32, ParseIntError> {
20 let (negative, value) = text.strip_prefix('-').map(|abs| (true, abs)).unwrap_or((false, text));
21 let abs_value = if let Some(hex) = value.strip_prefix("0x") {
22 i32::from_str_radix(hex, 16)?
23 } else {
24 i32::from_str_radix(value, 10)?
25 };
26 if negative {
27 Ok(-abs_value)
28 } else {
29 Ok(abs_value)
30 }
31}