Use thiserror for error handling with less boilerplate

This commit is contained in:
Steve Cook
2023-02-20 09:41:28 -05:00
parent 01e4f4a126
commit f8b29f37db
135 changed files with 868 additions and 905 deletions

View File

@@ -121,17 +121,17 @@ impl AddressBookParsedRXingResult {
geo: Vec<String>,
) -> Result<Self> {
if phone_numbers.len() != phone_types.len() && !phone_types.is_empty() {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"Phone numbers and types lengths differ",
));
}
if emails.len() != email_types.len() && !email_types.is_empty() {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"Emails and types lengths differ",
));
}
if addresses.len() != address_types.len() && !address_types.is_empty() {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"Addresses and types lengths differ",
));
}

View File

@@ -167,7 +167,7 @@ impl CalendarParsedRXingResult {
*/
fn parseDate(when: String) -> Result<i64> {
if !DATE_TIME.is_match(&when) {
return Err(Exceptions::parseWith(when));
return Err(Exceptions::parse_with(when));
}
if when.len() == 8 {
// Show only year/month/day
@@ -178,14 +178,14 @@ impl CalendarParsedRXingResult {
// http://code.google.com/p/android/issues/detail?id=8330
return match Utc.datetime_from_str(&format!("{}T000000Z", &when,), date_format_string) {
Ok(dtm) => Ok(dtm.timestamp()),
Err(e) => Err(Exceptions::parseWith(e.to_string())),
Err(e) => Err(Exceptions::parse_with(e.to_string())),
};
}
// The when string can be local time, or UTC if it ends with a Z
if when.len() == 16 && when.chars().nth(15).ok_or(Exceptions::indexOutOfBounds)? == 'Z' {
if when.len() == 16 && when.chars().nth(15).ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)? == 'Z' {
return match Utc.datetime_from_str(&when, "%Y%m%dT%H%M%SZ") {
Ok(dtm) => Ok(dtm.with_timezone(&Utc).timestamp()),
Err(e) => Err(Exceptions::parseWith(format!("couldn't parse string: {e}"))),
Err(e) => Err(Exceptions::parse_with(format!("couldn't parse string: {e}"))),
};
}
// Try once more, with weird tz formatting
@@ -195,14 +195,14 @@ impl CalendarParsedRXingResult {
let tz_parsed: Tz = match tz_part.parse() {
Ok(time_zone) => time_zone,
Err(e) => {
return Err(Exceptions::parseWith(format!(
return Err(Exceptions::parse_with(format!(
"couldn't parse timezone '{tz_part}': {e}"
)))
}
};
return match Utc.datetime_from_str(time_part, "%Y%m%dT%H%M%S") {
Ok(dtm) => Ok(dtm.with_timezone(&tz_parsed).timestamp()),
Err(e) => Err(Exceptions::parseWith(format!("couldn't parse string: {e}"))),
Err(e) => Err(Exceptions::parse_with(format!("couldn't parse string: {e}"))),
};
}
@@ -210,7 +210,7 @@ impl CalendarParsedRXingResult {
if when.len() == 15 {
return match Utc.datetime_from_str(&when, "%Y%m%dT%H%M%S") {
Ok(dtm) => Ok(dtm.timestamp()),
Err(e) => Err(Exceptions::parseWith(format!(
Err(e) => Err(Exceptions::parse_with(format!(
"couldn't parse local time: {e}"
))),
};
@@ -249,7 +249,7 @@ impl CalendarParsedRXingResult {
let z = parseable
.as_str()
.parse::<i64>()
.map_err(|e| Exceptions::parseWith(e.to_string()))?;
.map_err(|e| Exceptions::parse_with(e.to_string()))?;
durationMS += unit * z;
}
}
@@ -274,7 +274,7 @@ impl CalendarParsedRXingResult {
if let Ok(dtm) = DateTime::parse_from_str(dateTimeString, "%Y%m%dT%H%M%S") {
Ok(dtm.timestamp())
} else {
Err(Exceptions::parseWith(format!(
Err(Exceptions::parse_with(format!(
"Couldn't parse {dateTimeString}"
)))
}

View File

@@ -300,7 +300,7 @@ pub fn urlDecode(encoded: &str) -> Result<String> {
if let Ok(decoded) = decode(encoded) {
Ok(decoded.to_string())
} else {
Err(Exceptions::illegalStateWith("UnsupportedEncodingException"))
Err(Exceptions::illegal_state_with("UnsupportedEncodingException"))
}
}

View File

@@ -72,9 +72,9 @@ fn check_checksum(vin: &str) -> Result<bool> {
let mut sum = 0;
for i in 0..vin.len() {
sum += vin_position_weight(i + 1)? as u32
* vin_char_value(vin.chars().nth(i).ok_or(Exceptions::illegalArgument)?)?;
* vin_char_value(vin.chars().nth(i).ok_or(Exceptions::ILLEGAL_ARGUMENT)?)?;
}
let check_to_char = vin.chars().nth(8).ok_or(Exceptions::illegalArgument)?;
let check_to_char = vin.chars().nth(8).ok_or(Exceptions::ILLEGAL_ARGUMENT)?;
let expected_check_char = check_char((sum % 11) as u8)?;
Ok(check_to_char == expected_check_char)
}
@@ -85,7 +85,7 @@ fn vin_char_value(c: char) -> Result<u32> {
'J'..='R' => Ok((c as u8 as u32 - b'J' as u32) + 1),
'S'..='Z' => Ok((c as u8 as u32 - b'S' as u32) + 2),
'0'..='9' => Ok(c as u8 as u32 - b'0' as u32),
_ => Err(Exceptions::illegalArgumentWith("vin char out of range")),
_ => Err(Exceptions::illegal_argument_with("vin char out of range")),
}
}
@@ -95,7 +95,7 @@ fn vin_position_weight(position: usize) -> Result<usize> {
8 => Ok(10),
9 => Ok(0),
10..=17 => Ok(19 - position),
_ => Err(Exceptions::illegalArgumentWith(
_ => Err(Exceptions::illegal_argument_with(
"vin position weight out of bounds",
)),
}
@@ -105,7 +105,7 @@ fn check_char(remainder: u8) -> Result<char> {
match remainder {
0..=9 => Ok((b'0' + remainder) as char),
10 => Ok('X'),
_ => Err(Exceptions::illegalArgumentWith("remainder too high")),
_ => Err(Exceptions::illegal_argument_with("remainder too high")),
}
}
@@ -118,7 +118,7 @@ fn model_year(c: char) -> Result<u32> {
'V'..='Y' => Ok((c as u8 as u32 - b'V' as u32) + 1997),
'1'..='9' => Ok((c as u8 as u32 - b'1' as u32) + 2001),
'A'..='D' => Ok((c as u8 as u32 - b'A' as u32) + 2010),
_ => Err(Exceptions::illegalArgumentWith(
_ => Err(Exceptions::illegal_argument_with(
"model year argument out of range",
)),
}