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

@@ -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}"
)))
}