rename helper methods

This commit is contained in:
Vukašin Stepanović
2023-02-15 12:52:59 +00:00
parent bfcdb397ad
commit 935519ced5
133 changed files with 858 additions and 1044 deletions

View File

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

View File

@@ -166,7 +166,7 @@ impl CalendarParsedRXingResult {
*/
fn parseDate(when: String) -> Result<i64, Exceptions> {
if !DATE_TIME.is_match(&when) {
return Err(Exceptions::parse(when));
return Err(Exceptions::parseWith(when));
}
if when.len() == 8 {
// Show only year/month/day
@@ -177,20 +177,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::parse(e.to_string())),
Err(e) => Err(Exceptions::parseWith(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::indexOutOfBoundsEmpty())?
== 'Z'
{
if when.len() == 16 && when.chars().nth(15).ok_or(Exceptions::indexOutOfBounds)? == '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::parse(format!("couldn't parse string: {e}"))),
Err(e) => Err(Exceptions::parseWith(format!("couldn't parse string: {e}"))),
};
}
// Try once more, with weird tz formatting
@@ -200,14 +194,14 @@ impl CalendarParsedRXingResult {
let tz_parsed: Tz = match tz_part.parse() {
Ok(time_zone) => time_zone,
Err(e) => {
return Err(Exceptions::parse(format!(
return Err(Exceptions::parseWith(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::parse(format!("couldn't parse string: {e}"))),
Err(e) => Err(Exceptions::parseWith(format!("couldn't parse string: {e}"))),
};
}
@@ -215,7 +209,9 @@ 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::parse(format!("couldn't parse local time: {e}"))),
Err(e) => Err(Exceptions::parseWith(format!(
"couldn't parse local time: {e}"
))),
};
}
Self::parseDateTimeString(&when)
@@ -252,7 +248,7 @@ impl CalendarParsedRXingResult {
let z = parseable
.as_str()
.parse::<i64>()
.map_err(|e| Exceptions::parse(e.to_string()))?;
.map_err(|e| Exceptions::parseWith(e.to_string()))?;
durationMS += unit * z;
}
}
@@ -277,7 +273,7 @@ impl CalendarParsedRXingResult {
if let Ok(dtm) = DateTime::parse_from_str(dateTimeString, "%Y%m%dT%H%M%S") {
Ok(dtm.timestamp())
} else {
Err(Exceptions::parse(format!(
Err(Exceptions::parseWith(format!(
"Couldn't parse {dateTimeString}"
)))
}

View File

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

View File

@@ -71,16 +71,9 @@ fn check_checksum(vin: &str) -> Result<bool, Exceptions> {
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::illegalArgumentEmpty())?,
)?;
* vin_char_value(vin.chars().nth(i).ok_or(Exceptions::illegalArgument)?)?;
}
let check_to_char = vin
.chars()
.nth(8)
.ok_or(Exceptions::illegalArgumentEmpty())?;
let check_to_char = vin.chars().nth(8).ok_or(Exceptions::illegalArgument)?;
let expected_check_char = check_char((sum % 11) as u8)?;
Ok(check_to_char == expected_check_char)
}
@@ -91,7 +84,7 @@ 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::illegalArgument("vin char out of range")),
_ => Err(Exceptions::illegalArgumentWith("vin char out of range")),
}
}
@@ -101,7 +94,7 @@ fn vin_position_weight(position: usize) -> Result<usize, Exceptions> {
8 => Ok(10),
9 => Ok(0),
10..=17 => Ok(19 - position),
_ => Err(Exceptions::illegalArgument(
_ => Err(Exceptions::illegalArgumentWith(
"vin position weight out of bounds",
)),
}
@@ -111,7 +104,7 @@ fn check_char(remainder: u8) -> Result<char, Exceptions> {
match remainder {
0..=9 => Ok((b'0' + remainder) as char),
10 => Ok('X'),
_ => Err(Exceptions::illegalArgument("remainder too high")),
_ => Err(Exceptions::illegalArgumentWith("remainder too high")),
}
}
@@ -124,7 +117,7 @@ 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::illegalArgument(
_ => Err(Exceptions::illegalArgumentWith(
"model year argument out of range",
)),
}