some style clenaup

This commit is contained in:
Henry Schimke
2022-09-10 11:49:26 -05:00
parent 6a059c5a4a
commit a71138590f
3 changed files with 49 additions and 49 deletions

View File

@@ -30,7 +30,7 @@ pub struct VINParsedRXingResult {
vehicle_attributes: String, vehicle_attributes: String,
model_year: u32, model_year: u32,
plant_code: char, plant_code: char,
sequentialNumber: String, sequential_number: String,
} }
impl ParsedRXingResult for VINParsedRXingResult { impl ParsedRXingResult for VINParsedRXingResult {
@@ -54,7 +54,7 @@ impl ParsedRXingResult for VINParsedRXingResult {
result.push(' '); result.push(' ');
result.push(self.plant_code); result.push(self.plant_code);
result.push(' '); result.push(' ');
result.push_str(&self.sequentialNumber); result.push_str(&self.sequential_number);
result.push('\n'); result.push('\n');
result result
@@ -63,25 +63,25 @@ impl ParsedRXingResult for VINParsedRXingResult {
impl VINParsedRXingResult { impl VINParsedRXingResult {
pub fn new( pub fn new(
vin: String, vin: String,
worldManufacturerID: String, world_manufacturer_id: String,
vehicleDescriptorSection: String, vehicle_descriptor_section: String,
vehicleIdentifierSection: String, vehicle_identifier_section: String,
countryCode: String, country_code: String,
vehicleAttributes: String, vehicle_attributes: String,
modelYear: u32, model_year: u32,
plantCode: char, plant_code: char,
sequentialNumber: String, sequential_number: String,
) -> Self { ) -> Self {
Self { Self {
vin, vin,
world_manufacturer_id: worldManufacturerID, world_manufacturer_id,
vehicle_descriptor_section: vehicleDescriptorSection, vehicle_descriptor_section,
vehicle_identifier_section: vehicleIdentifierSection, vehicle_identifier_section,
country_code: countryCode, country_code,
vehicle_attributes: vehicleAttributes, vehicle_attributes,
model_year: modelYear, model_year,
plant_code: plantCode, plant_code,
sequentialNumber, sequential_number,
} }
} }
@@ -118,6 +118,6 @@ impl VINParsedRXingResult {
} }
pub fn getSequentialNumber(&self) -> &str { pub fn getSequentialNumber(&self) -> &str {
&self.sequentialNumber &self.sequential_number
} }
} }

View File

@@ -53,8 +53,8 @@ fn testNotVIN() {
} }
#[test] #[test]
fn testVIN() { fn test_vin() {
doTest( do_test(
"1M8GDM9AXKP042788", "1M8GDM9AXKP042788",
"1M8", "1M8",
"GDM9AX", "GDM9AX",
@@ -65,7 +65,7 @@ fn testVIN() {
'P', 'P',
"042788", "042788",
); );
doTest( do_test(
"I1M8GDM9AXKP042788", "I1M8GDM9AXKP042788",
"1M8", "1M8",
"GDM9AX", "GDM9AX",
@@ -76,7 +76,7 @@ fn testVIN() {
'P', 'P',
"042788", "042788",
); );
doTest( do_test(
"LJCPCBLCX11000237", "LJCPCBLCX11000237",
"LJC", "LJC",
"PCBLCX", "PCBLCX",
@@ -89,7 +89,7 @@ fn testVIN() {
); );
} }
fn doTest( fn do_test(
contents: &str, contents: &str,
wmi: &str, wmi: &str,
vds: &str, vds: &str,
@@ -100,9 +100,9 @@ fn doTest(
plant: char, plant: char,
sequential: &str, sequential: &str,
) { ) {
let fakeRXingResult = let fake_rxing_result =
RXingResult::new(contents, Vec::new(), Vec::new(), BarcodeFormat::CODE_39); RXingResult::new(contents, Vec::new(), Vec::new(), BarcodeFormat::CODE_39);
let result = ResultParser::parseRXingResult(&fakeRXingResult); let result = ResultParser::parseRXingResult(&fake_rxing_result);
assert_eq!(ParsedRXingResultType::VIN, result.getType()); assert_eq!(ParsedRXingResultType::VIN, result.getType());
if let ParsedClientResult::VINResult(vinRXingResult) = result { if let ParsedClientResult::VINResult(vinRXingResult) = result {
// let vinRXingResult = (VINParsedRXingResult) result; // let vinRXingResult = (VINParsedRXingResult) result;

View File

@@ -40,40 +40,40 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
} }
let ioq_matcher = Regex::new(IOQ).unwrap(); let ioq_matcher = Regex::new(IOQ).unwrap();
let az09_matcher = Regex::new(AZ09).unwrap(); let az09_matcher = Regex::new(AZ09).unwrap();
let rawText_res = result.getText().trim(); let raw_text_res = result.getText().trim();
let rawText = ioq_matcher.replace_all(rawText_res, "").to_string(); let raw_text = ioq_matcher.replace_all(raw_text_res, "").to_string();
// rawText = IOQ.matcher(rawText).replaceAll("").trim(); // rawText = IOQ.matcher(rawText).replaceAll("").trim();
if let None = az09_matcher.find(&rawText) { if let None = az09_matcher.find(&raw_text) {
return None; return None;
} }
// if !AZ09.matcher(rawText).matches() { // if !AZ09.matcher(rawText).matches() {
// return null; // return null;
// } // }
let check_cs = checkChecksum(&rawText).unwrap_or(false); let check_cs = check_checksum(&raw_text).unwrap_or(false);
if !check_cs { if !check_cs {
return None; return None;
} }
let wmi = &rawText[..3]; let wmi = &raw_text[..3];
// try { // try {
// if (!checkChecksum(rawText)) { // if (!checkChecksum(rawText)) {
// return null; // return null;
// } // }
// String wmi = rawText.substring(0, 3); // String wmi = rawText.substring(0, 3);
let country_code = countryCode(wmi).unwrap_or(""); let country_code = country_code(wmi).unwrap_or("");
let model_year = modelYear(rawText.chars().nth(9).unwrap_or('_')); let model_year = model_year(raw_text.chars().nth(9).unwrap_or('_'));
if model_year.is_err() { if model_year.is_err() {
return None; return None;
} }
Some(ParsedClientResult::VINResult(VINParsedRXingResult::new( Some(ParsedClientResult::VINResult(VINParsedRXingResult::new(
rawText.to_owned(), raw_text.to_owned(),
wmi.to_owned(), wmi.to_owned(),
rawText[3..9].to_owned(), raw_text[3..9].to_owned(),
rawText[9..17].to_owned(), raw_text[9..17].to_owned(),
country_code.to_owned(), country_code.to_owned(),
rawText[3..8].to_owned(), raw_text[3..8].to_owned(),
model_year.unwrap(), model_year.unwrap(),
rawText.chars().nth(10).unwrap(), raw_text.chars().nth(10).unwrap(),
rawText[11..].to_owned(), raw_text[11..].to_owned(),
))) )))
// return new VINParsedRXingResult(rawText, // return new VINParsedRXingResult(rawText,
// wmi, // wmi,
@@ -92,18 +92,18 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
const IOQ: &'static str = "[IOQ]"; const IOQ: &'static str = "[IOQ]";
const AZ09: &'static str = "[A-Z0-9]{17}"; const AZ09: &'static str = "[A-Z0-9]{17}";
fn checkChecksum(vin: &str) -> Result<bool, Exceptions> { fn check_checksum(vin: &str) -> Result<bool, Exceptions> {
let mut sum = 0; let mut sum = 0;
for i in 0..vin.len() { for i in 0..vin.len() {
// for (int i = 0; i < vin.length(); i++) { // for (int i = 0; i < vin.length(); i++) {
sum += vinPositionWeight(i + 1)? as u32 * vinCharValue(vin.chars().nth(i).unwrap())?; sum += vin_position_weight(i + 1)? as u32 * vin_char_value(vin.chars().nth(i).unwrap())?;
} }
let checkToChar = vin.chars().nth(8).unwrap(); let check_to_char = vin.chars().nth(8).unwrap();
let expectedCheckChar = checkChar((sum % 11) as u8)?; let expected_check_char = check_char((sum % 11) as u8)?;
Ok(checkToChar == expectedCheckChar) Ok(check_to_char == expected_check_char)
} }
fn vinCharValue(c: char) -> Result<u32, Exceptions> { fn vin_char_value(c: char) -> Result<u32, Exceptions> {
match c { match c {
'A'..='I' => Ok((c as u8 as u32 - 'A' as u8 as u32) + 1), 'A'..='I' => Ok((c as u8 as u32 - 'A' as u8 as u32) + 1),
'J'..='R' => Ok((c as u8 as u32 - 'J' as u8 as u32) + 1), 'J'..='R' => Ok((c as u8 as u32 - 'J' as u8 as u32) + 1),
@@ -130,7 +130,7 @@ fn vinCharValue(c: char) -> Result<u32, Exceptions> {
// )) // ))
} }
fn vinPositionWeight(position: usize) -> Result<usize, Exceptions> { fn vin_position_weight(position: usize) -> Result<usize, Exceptions> {
match position { match position {
1..=7 => Ok(9 - position), 1..=7 => Ok(9 - position),
8 => Ok(10), 8 => Ok(10),
@@ -155,7 +155,7 @@ fn vinPositionWeight(position: usize) -> Result<usize, Exceptions> {
// throw new IllegalArgumentException(); // throw new IllegalArgumentException();
} }
fn checkChar(remainder: u8) -> Result<char, Exceptions> { fn check_char(remainder: u8) -> Result<char, Exceptions> {
match remainder { match remainder {
0..=9 => Ok(('0' as u8 + remainder) as char), 0..=9 => Ok(('0' as u8 + remainder) as char),
10 => Ok('X'), 10 => Ok('X'),
@@ -174,7 +174,7 @@ fn checkChar(remainder: u8) -> Result<char, Exceptions> {
// )) // ))
} }
fn modelYear(c: char) -> Result<u32, Exceptions> { fn model_year(c: char) -> Result<u32, Exceptions> {
match c { match c {
'E'..='H' => Ok((c as u8 as u32 - 'E' as u8 as u32) + 1984), 'E'..='H' => Ok((c as u8 as u32 - 'E' as u8 as u32) + 1984),
'J'..='N' => Ok((c as u8 as u32 - 'J' as u8 as u32) + 1988), 'J'..='N' => Ok((c as u8 as u32 - 'J' as u8 as u32) + 1988),
@@ -211,7 +211,7 @@ fn modelYear(c: char) -> Result<u32, Exceptions> {
// throw new IllegalArgumentException(); // throw new IllegalArgumentException();
} }
fn countryCode(wmi: &str) -> Option<&'static str> { fn country_code(wmi: &str) -> Option<&'static str> {
let c1 = wmi.chars().nth(0).unwrap(); let c1 = wmi.chars().nth(0).unwrap();
let c2 = wmi.chars().nth(1).unwrap(); let c2 = wmi.chars().nth(1).unwrap();
match c1 { match c1 {