From a71138590f52bdf37196998c5ba78a963d4aab2c Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 10 Sep 2022 11:49:26 -0500 Subject: [PATCH] some style clenaup --- src/client/result/VINParsedResult.rs | 38 ++++++++-------- src/client/result/VINParsedResultTestCase.rs | 14 +++--- src/client/result/VINResultParser.rs | 46 ++++++++++---------- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/client/result/VINParsedResult.rs b/src/client/result/VINParsedResult.rs index aee3f70..52273ff 100644 --- a/src/client/result/VINParsedResult.rs +++ b/src/client/result/VINParsedResult.rs @@ -30,7 +30,7 @@ pub struct VINParsedRXingResult { vehicle_attributes: String, model_year: u32, plant_code: char, - sequentialNumber: String, + sequential_number: String, } impl ParsedRXingResult for VINParsedRXingResult { @@ -54,7 +54,7 @@ impl ParsedRXingResult for VINParsedRXingResult { result.push(' '); result.push(self.plant_code); result.push(' '); - result.push_str(&self.sequentialNumber); + result.push_str(&self.sequential_number); result.push('\n'); result @@ -63,25 +63,25 @@ impl ParsedRXingResult for VINParsedRXingResult { impl VINParsedRXingResult { pub fn new( vin: String, - worldManufacturerID: String, - vehicleDescriptorSection: String, - vehicleIdentifierSection: String, - countryCode: String, - vehicleAttributes: String, - modelYear: u32, - plantCode: char, - sequentialNumber: String, + world_manufacturer_id: String, + vehicle_descriptor_section: String, + vehicle_identifier_section: String, + country_code: String, + vehicle_attributes: String, + model_year: u32, + plant_code: char, + sequential_number: String, ) -> Self { Self { vin, - world_manufacturer_id: worldManufacturerID, - vehicle_descriptor_section: vehicleDescriptorSection, - vehicle_identifier_section: vehicleIdentifierSection, - country_code: countryCode, - vehicle_attributes: vehicleAttributes, - model_year: modelYear, - plant_code: plantCode, - sequentialNumber, + world_manufacturer_id, + vehicle_descriptor_section, + vehicle_identifier_section, + country_code, + vehicle_attributes, + model_year, + plant_code, + sequential_number, } } @@ -118,6 +118,6 @@ impl VINParsedRXingResult { } pub fn getSequentialNumber(&self) -> &str { - &self.sequentialNumber + &self.sequential_number } } diff --git a/src/client/result/VINParsedResultTestCase.rs b/src/client/result/VINParsedResultTestCase.rs index 8332b82..d0fe914 100644 --- a/src/client/result/VINParsedResultTestCase.rs +++ b/src/client/result/VINParsedResultTestCase.rs @@ -53,8 +53,8 @@ fn testNotVIN() { } #[test] -fn testVIN() { - doTest( +fn test_vin() { + do_test( "1M8GDM9AXKP042788", "1M8", "GDM9AX", @@ -65,7 +65,7 @@ fn testVIN() { 'P', "042788", ); - doTest( + do_test( "I1M8GDM9AXKP042788", "1M8", "GDM9AX", @@ -76,7 +76,7 @@ fn testVIN() { 'P', "042788", ); - doTest( + do_test( "LJCPCBLCX11000237", "LJC", "PCBLCX", @@ -89,7 +89,7 @@ fn testVIN() { ); } -fn doTest( +fn do_test( contents: &str, wmi: &str, vds: &str, @@ -100,9 +100,9 @@ fn doTest( plant: char, sequential: &str, ) { - let fakeRXingResult = + let fake_rxing_result = 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()); if let ParsedClientResult::VINResult(vinRXingResult) = result { // let vinRXingResult = (VINParsedRXingResult) result; diff --git a/src/client/result/VINResultParser.rs b/src/client/result/VINResultParser.rs index 369a321..9f88a1e 100644 --- a/src/client/result/VINResultParser.rs +++ b/src/client/result/VINResultParser.rs @@ -40,40 +40,40 @@ pub fn parse(result: &RXingResult) -> Option { } let ioq_matcher = Regex::new(IOQ).unwrap(); let az09_matcher = Regex::new(AZ09).unwrap(); - let rawText_res = result.getText().trim(); - let rawText = ioq_matcher.replace_all(rawText_res, "").to_string(); + let raw_text_res = result.getText().trim(); + let raw_text = ioq_matcher.replace_all(raw_text_res, "").to_string(); // rawText = IOQ.matcher(rawText).replaceAll("").trim(); - if let None = az09_matcher.find(&rawText) { + if let None = az09_matcher.find(&raw_text) { return None; } // if !AZ09.matcher(rawText).matches() { // return null; // } - let check_cs = checkChecksum(&rawText).unwrap_or(false); + let check_cs = check_checksum(&raw_text).unwrap_or(false); if !check_cs { return None; } - let wmi = &rawText[..3]; + let wmi = &raw_text[..3]; // try { // if (!checkChecksum(rawText)) { // return null; // } // String wmi = rawText.substring(0, 3); - let country_code = countryCode(wmi).unwrap_or(""); - let model_year = modelYear(rawText.chars().nth(9).unwrap_or('_')); + let country_code = country_code(wmi).unwrap_or(""); + let model_year = model_year(raw_text.chars().nth(9).unwrap_or('_')); if model_year.is_err() { return None; } Some(ParsedClientResult::VINResult(VINParsedRXingResult::new( - rawText.to_owned(), + raw_text.to_owned(), wmi.to_owned(), - rawText[3..9].to_owned(), - rawText[9..17].to_owned(), + raw_text[3..9].to_owned(), + raw_text[9..17].to_owned(), country_code.to_owned(), - rawText[3..8].to_owned(), + raw_text[3..8].to_owned(), model_year.unwrap(), - rawText.chars().nth(10).unwrap(), - rawText[11..].to_owned(), + raw_text.chars().nth(10).unwrap(), + raw_text[11..].to_owned(), ))) // return new VINParsedRXingResult(rawText, // wmi, @@ -92,18 +92,18 @@ pub fn parse(result: &RXingResult) -> Option { const IOQ: &'static str = "[IOQ]"; const AZ09: &'static str = "[A-Z0-9]{17}"; -fn checkChecksum(vin: &str) -> Result { +fn check_checksum(vin: &str) -> Result { let mut sum = 0; for i in 0..vin.len() { // 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 expectedCheckChar = checkChar((sum % 11) as u8)?; - Ok(checkToChar == expectedCheckChar) + let check_to_char = vin.chars().nth(8).unwrap(); + let expected_check_char = check_char((sum % 11) as u8)?; + Ok(check_to_char == expected_check_char) } -fn vinCharValue(c: char) -> Result { +fn vin_char_value(c: char) -> Result { match c { '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), @@ -130,7 +130,7 @@ fn vinCharValue(c: char) -> Result { // )) } -fn vinPositionWeight(position: usize) -> Result { +fn vin_position_weight(position: usize) -> Result { match position { 1..=7 => Ok(9 - position), 8 => Ok(10), @@ -155,7 +155,7 @@ fn vinPositionWeight(position: usize) -> Result { // throw new IllegalArgumentException(); } -fn checkChar(remainder: u8) -> Result { +fn check_char(remainder: u8) -> Result { match remainder { 0..=9 => Ok(('0' as u8 + remainder) as char), 10 => Ok('X'), @@ -174,7 +174,7 @@ fn checkChar(remainder: u8) -> Result { // )) } -fn modelYear(c: char) -> Result { +fn model_year(c: char) -> Result { match c { '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), @@ -211,7 +211,7 @@ fn modelYear(c: char) -> Result { // 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 c2 = wmi.chars().nth(1).unwrap(); match c1 {