mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
refactor to use exception helper factories
This commit is contained in:
@@ -120,19 +120,19 @@ impl AddressBookParsedRXingResult {
|
||||
geo: Vec<String>,
|
||||
) -> Result<Self, Exceptions> {
|
||||
if phone_numbers.len() != phone_types.len() && !phone_types.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"Phone numbers and types lengths differ".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
if emails.len() != email_types.len() && !email_types.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"Emails and types lengths differ".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
if addresses.len() != address_types.len() && !address_types.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"Addresses and types lengths differ".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
names,
|
||||
|
||||
@@ -166,7 +166,7 @@ impl CalendarParsedRXingResult {
|
||||
*/
|
||||
fn parseDate(when: String) -> Result<i64, Exceptions> {
|
||||
if !DATE_TIME.is_match(&when) {
|
||||
return Err(Exceptions::ParseException(Some(when)));
|
||||
return Err(Exceptions::parse(when));
|
||||
}
|
||||
if when.len() == 8 {
|
||||
// Show only year/month/day
|
||||
@@ -177,7 +177,7 @@ 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::ParseException(Some(e.to_string()))),
|
||||
Err(e) => Err(Exceptions::parse(e.to_string())),
|
||||
};
|
||||
}
|
||||
// The when string can be local time, or UTC if it ends with a Z
|
||||
@@ -185,14 +185,12 @@ impl CalendarParsedRXingResult {
|
||||
&& when
|
||||
.chars()
|
||||
.nth(15)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
== '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::ParseException(Some(format!(
|
||||
"couldn't parse string: {e}"
|
||||
)))),
|
||||
Err(e) => Err(Exceptions::parse(format!("couldn't parse string: {e}"))),
|
||||
};
|
||||
}
|
||||
// Try once more, with weird tz formatting
|
||||
@@ -202,16 +200,14 @@ impl CalendarParsedRXingResult {
|
||||
let tz_parsed: Tz = match tz_part.parse() {
|
||||
Ok(time_zone) => time_zone,
|
||||
Err(e) => {
|
||||
return Err(Exceptions::ParseException(Some(format!(
|
||||
return Err(Exceptions::parse(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::ParseException(Some(format!(
|
||||
"couldn't parse string: {e}"
|
||||
)))),
|
||||
Err(e) => Err(Exceptions::parse(format!("couldn't parse string: {e}"))),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -219,9 +215,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::ParseException(Some(format!(
|
||||
"couldn't parse local time: {e}"
|
||||
)))),
|
||||
Err(e) => Err(Exceptions::parse(format!("couldn't parse local time: {e}"))),
|
||||
};
|
||||
}
|
||||
Self::parseDateTimeString(&when)
|
||||
@@ -258,7 +252,7 @@ impl CalendarParsedRXingResult {
|
||||
let z = parseable
|
||||
.as_str()
|
||||
.parse::<i64>()
|
||||
.map_err(|e| Exceptions::ParseException(Some(e.to_string())))?;
|
||||
.map_err(|e| Exceptions::parse(e.to_string()))?;
|
||||
durationMS += unit * z;
|
||||
}
|
||||
}
|
||||
@@ -283,9 +277,9 @@ impl CalendarParsedRXingResult {
|
||||
if let Ok(dtm) = DateTime::parse_from_str(dateTimeString, "%Y%m%dT%H%M%S") {
|
||||
Ok(dtm.timestamp())
|
||||
} else {
|
||||
Err(Exceptions::ParseException(Some(format!(
|
||||
Err(Exceptions::parse(format!(
|
||||
"Couldn't parse {dateTimeString}"
|
||||
))))
|
||||
)))
|
||||
}
|
||||
// DateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH);
|
||||
// return format.parse(dateTimeString).getTime();
|
||||
|
||||
@@ -300,9 +300,9 @@ pub fn urlDecode(encoded: &str) -> Result<String, Exceptions> {
|
||||
if let Ok(decoded) = decode(encoded) {
|
||||
Ok(decoded.to_string())
|
||||
} else {
|
||||
Err(Exceptions::IllegalStateException(Some(String::from(
|
||||
Err(Exceptions::illegalState(String::from(
|
||||
"UnsupportedEncodingException",
|
||||
))))
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,13 +74,13 @@ fn check_checksum(vin: &str) -> Result<bool, Exceptions> {
|
||||
* vin_char_value(
|
||||
vin.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IllegalArgumentException(None))?,
|
||||
.ok_or(Exceptions::illegalArgumentEmpty())?,
|
||||
)?;
|
||||
}
|
||||
let check_to_char = vin
|
||||
.chars()
|
||||
.nth(8)
|
||||
.ok_or(Exceptions::IllegalArgumentException(None))?;
|
||||
.ok_or(Exceptions::illegalArgumentEmpty())?;
|
||||
let expected_check_char = check_char((sum % 11) as u8)?;
|
||||
Ok(check_to_char == expected_check_char)
|
||||
}
|
||||
@@ -91,9 +91,9 @@ fn vin_char_value(c: char) -> Result<u32, Exceptions> {
|
||||
'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::IllegalArgumentException(Some(
|
||||
_ => Err(Exceptions::illegalArgument(
|
||||
"vin char out of range".to_owned(),
|
||||
))),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +103,9 @@ fn vin_position_weight(position: usize) -> Result<usize, Exceptions> {
|
||||
8 => Ok(10),
|
||||
9 => Ok(0),
|
||||
10..=17 => Ok(19 - position),
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(
|
||||
_ => Err(Exceptions::illegalArgument(
|
||||
"vin position weight out of bounds".to_owned(),
|
||||
))),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,9 +113,7 @@ fn check_char(remainder: u8) -> Result<char, Exceptions> {
|
||||
match remainder {
|
||||
0..=9 => Ok((b'0' + remainder) as char),
|
||||
10 => Ok('X'),
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(
|
||||
"remainder too high".to_owned(),
|
||||
))),
|
||||
_ => Err(Exceptions::illegalArgument("remainder too high".to_owned())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,9 +126,9 @@ fn model_year(c: char) -> Result<u32, Exceptions> {
|
||||
'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::IllegalArgumentException(Some(String::from(
|
||||
_ => Err(Exceptions::illegalArgument(String::from(
|
||||
"model year argument out of range",
|
||||
)))),
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user