From 6a06762fd8a2bf526b74852a1aff45578fb8a30a Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 13 Jan 2024 14:02:02 -0600 Subject: [PATCH] chore: clippy cleanup --- .../base_extentions/qr_formatinformation.rs | 6 ++-- .../base_extentions/qrcode_version.rs | 14 ++++---- src/qrcode/cpp_port/bitmatrix_parser.rs | 6 ++-- src/qrcode/cpp_port/detector.rs | 33 +++++++++---------- src/qrcode/cpp_port/qr_cpp_reader.rs | 8 ++--- src/qrcode/cpp_port/test/RMQRDecoderTest.rs | 2 +- src/qrcode/decoder/mode.rs | 18 +++++----- src/qrcode/decoder/version.rs | 4 +-- 8 files changed, 41 insertions(+), 50 deletions(-) diff --git a/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs b/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs index 533a6c3..bee5c63 100644 --- a/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs +++ b/src/common/cpp_essentials/base_extentions/qr_formatinformation.rs @@ -103,7 +103,7 @@ impl FormatInformation { pub fn DecodeRMQR(formatInfoBits1: u32, formatInfoBits2: u32) -> Self { //FormatInformation fi; let mirror18Bits = |bits: u32| bits.reverse_bits() >> 14; - let mut fi = if (formatInfoBits2 != 0) { + let mut fi = if formatInfoBits2 != 0 { Self::FindBestFormatInfoRMQR( &[formatInfoBits1, mirror18Bits(formatInfoBits1)], &[formatInfoBits2, mirror18Bits(formatInfoBits2)], @@ -218,7 +218,7 @@ impl FormatInformation { let pattern = l_pattern ^ mask; // Find the pattern with fewest bits differing let hammingDist = ((bits[bitsIndex] ^ mask) ^ pattern).count_ones(); - if (hammingDist < fi.hammingDistance) { + if hammingDist < fi.hammingDistance { fi.mask = mask; // store the used mask to discriminate between types/models fi.data = pattern >> 12; // drop the 12 BCH error correction bits fi.hammingDistance = hammingDist; @@ -229,7 +229,7 @@ impl FormatInformation { }; best(bits, &MASKED_PATTERNS, FORMAT_INFO_MASK_RMQR); - if (!subbits.is_empty()) + if !subbits.is_empty() // TODO probably remove if `sampleRMQR()` done properly { best(subbits, &MASKED_PATTERNS_SUB, FORMAT_INFO_MASK_RMQR_SUB); diff --git a/src/common/cpp_essentials/base_extentions/qrcode_version.rs b/src/common/cpp_essentials/base_extentions/qrcode_version.rs index 039fbf3..0bdb1a2 100644 --- a/src/common/cpp_essentials/base_extentions/qrcode_version.rs +++ b/src/common/cpp_essentials/base_extentions/qrcode_version.rs @@ -77,10 +77,10 @@ impl Version { pub fn rMQR(version_number: u32) -> Result { let version_number = version_number as usize; - if (version_number < 1 || version_number > (RMQR_VERSIONS.len())) { + if version_number < 1 || version_number > (RMQR_VERSIONS.len()) { Err(Exceptions::ILLEGAL_ARGUMENT) } else { - Ok(&RMQR_VERSIONS[version_number as usize - 1]) + Ok(&RMQR_VERSIONS[version_number - 1]) } } @@ -146,7 +146,7 @@ impl Version { pub fn HasMicroSize(bitMatrix: &BitMatrix) -> bool { let size = bitMatrix.height(); - size == bitMatrix.width() && size >= 11 && size <= 17 && (size % 2) == 1 + size == bitMatrix.width() && (11..=17).contains(&size) && (size % 2) == 1 } pub fn HasRMQRSize(bitMatrix: &BitMatrix) -> bool { @@ -187,10 +187,8 @@ impl Version { if width != height && width.is_odd() && height.is_odd() - && width >= 27 - && width <= 139 - && height >= 7 - && height <= 17 + && (27..=139).contains(&width) + && (7..=17).contains(&height) { for i in 0..dimsVersionRMQR.len() { // for (int i = 0; i < Size(dimsVersionRMQR); i++){ @@ -199,6 +197,6 @@ impl Version { } } } - return -1; + -1 } } diff --git a/src/qrcode/cpp_port/bitmatrix_parser.rs b/src/qrcode/cpp_port/bitmatrix_parser.rs index a0c9f31..9417f4b 100644 --- a/src/qrcode/cpp_port/bitmatrix_parser.rs +++ b/src/qrcode/cpp_port/bitmatrix_parser.rs @@ -52,7 +52,7 @@ pub fn ReadFormatInformation(bitMatrix: &BitMatrix) -> Result return Ok(FormatInformation::DecodeMQR(formatInfoBits as u32)); } - if (Version::HasRMQRSize(bitMatrix)) { + if Version::HasRMQRSize(bitMatrix) { // Read top-left format info bits let mut formatInfoBits1 = 0; for y in (1..=3).rev() { @@ -378,7 +378,7 @@ pub fn ReadRMQRCodewords( // for (int col = 0; col < 2; col++) { let xx = x - col; // Ignore bits covered by the function pattern - if (!functionPattern.get(xx as u32, y)) { + if !functionPattern.get(xx as u32, y) { // Read a bit AppendBit( &mut currentByte, @@ -399,7 +399,7 @@ pub fn ReadRMQRCodewords( x -= 2 } - if ((result.len()) != version.getTotalCodewords() as usize) { + if (result.len()) != version.getTotalCodewords() as usize { return Err(Exceptions::FORMAT); } diff --git a/src/qrcode/cpp_port/detector.rs b/src/qrcode/cpp_port/detector.rs index a43fb52..91c4f26 100644 --- a/src/qrcode/cpp_port/detector.rs +++ b/src/qrcode/cpp_port/detector.rs @@ -5,12 +5,12 @@ use crate::{ }, DefaultGridSampler, GridSampler, Result, SamplerControl, }, - point, point_i, + point_i, qrcode::{ decoder::{FormatInformation, Version, VersionRef}, detector::QRCodeDetectorResult, }, - Exceptions, PointF, + Exceptions, }; use multimap::MultiMap; use num::Integer; @@ -24,7 +24,7 @@ use crate::{ }, BitMatrix, PerspectiveTransform, Quadrilateral, }, - point_f, Point, PointI, + point_f, Point, }; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq)] @@ -914,8 +914,7 @@ pub fn DetectPureMQR(image: &BitMatrix) -> Result { let moduleSize: f32 = (fpWidth as f32) / 7.0; let dimension = (width as f32 / moduleSize).floor() as u32; - if dimension < MIN_MODULES - || dimension > MAX_MODULES + if !(MIN_MODULES..=MAX_MODULES).contains(&dimension) || !image.is_in(point_f( left as f32 + moduleSize / 2.0 + (dimension - 1) as f32 * moduleSize, top as f32 + moduleSize / 2.0 + (dimension - 1) as f32 * moduleSize, @@ -969,7 +968,7 @@ pub fn DetectPureRMQR(image: &BitMatrix) -> Result { let (found, left, top, width, height) = image.findBoundingBox(0, 0, 0, 0, MIN_MODULES); - if (!found) { + if !found { return Err(Exceptions::NOT_FOUND); } let right = left + width - 1; @@ -994,7 +993,7 @@ pub fn DetectPureRMQR(image: &BitMatrix) -> Result { let mut subdiagonal: SubPattern = EdgeTracer::new(image, br, point_i(-1, -1)) .readPatternFromBlack(1, None) .ok_or(Exceptions::ILLEGAL_STATE)?; - if (subdiagonal.len() == 5 && subdiagonal[4] > subdiagonal[3]) { + if subdiagonal.len() == 5 && subdiagonal[4] > subdiagonal[3] { // Sub pattern has no separator so can run off along the diagonal subdiagonal[4] = subdiagonal[3]; // Hack it back to previous } @@ -1024,23 +1023,21 @@ pub fn DetectPureRMQR(image: &BitMatrix) -> Result { let dimW = (width as f32 / moduleSize as f32).floor() as u32; let dimH = (height as f32 / moduleSize as f32).floor() as u32; - if (dimW == dimH + if dimW == dimH || dimW.is_even() || dimH.is_even() - || dimW < MIN_MODULES_W - || dimW > MAX_MODULES_W - || dimH < MIN_MODULES_H - || dimH > MAX_MODULES_H + || !(MIN_MODULES_W..=MAX_MODULES_W).contains(&dimW) + || !(MIN_MODULES_H..=MAX_MODULES_H).contains(&dimH) || !image.is_in(point_f( left as f32 + moduleSize / 2.0 + (dimW as f32 - 1.0) * moduleSize, top as f32 + moduleSize / 2.0 + (dimH as f32 - 1.0) * moduleSize, - ))) + )) { return Err(Exceptions::NOT_FOUND); } // Vertical corner finder patterns - if (dimH > 7) { + if dimH > 7 { // None for R7 let corner: CornerEdgePattern = EdgeTracer::new(image, tr, point_i(0, 1)) .readPatternFromBlack(1, None) @@ -1050,7 +1047,7 @@ pub fn DetectPureRMQR(image: &BitMatrix) -> Result { if !(IsPattern::(&view, &CORNER_EDGE_RMQR, None, 0.0, 0.0) != 0.0) { return Err(Exceptions::NOT_FOUND); } - if (dimH > 9) { + if dimH > 9 { // No bottom left for R9 let corner: CornerEdgePattern = EdgeTracer::new(image, bl, point_i(0, -1)) .readPatternFromBlack(1, None) @@ -1241,7 +1238,7 @@ pub fn SampleRMQR(image: &BitMatrix, fp: ConcentricPattern) -> Result Result= 0x00 && bits <= 0x05) || (bits >= 0x07 && bits <= 0x09) || bits == 0x0d) { - return Mode::try_from(bits as u32); - } + } else if (0x00..=0x05).contains(&bits) || (0x07..=0x09).contains(&bits) || bits == 0x0d { + return Mode::try_from(bits as u32); } Err(Exceptions::format_with("Invalid codec mode")) @@ -196,7 +194,7 @@ impl Mode { } } - if (version.isRMQR()) { + if version.isRMQR() { // See ISO/IEC 23941:2022 7.4.1, Table 3 - Number of bits of character count indicator const numeric: [u32; 32] = [ 4, 5, 6, 7, 7, 5, 6, 7, 7, 8, 4, 6, 7, 7, 8, 8, 5, 6, 7, 7, 8, 8, 7, 7, 8, 8, 9, 7, @@ -214,7 +212,7 @@ impl Mode { 2, 3, 4, 5, 5, 3, 4, 5, 5, 6, 2, 4, 5, 5, 6, 6, 3, 5, 5, 6, 6, 7, 5, 5, 6, 6, 7, 5, 6, 6, 6, 7, ]; - match (self) { + match self { Mode::NUMERIC => return numeric[number - 1], Mode::ALPHANUMERIC => return alphanum[number - 1], Mode::BYTE => return byte[number - 1], diff --git a/src/qrcode/decoder/version.rs b/src/qrcode/decoder/version.rs index 1b0a8ff..f5d00f1 100755 --- a/src/qrcode/decoder/version.rs +++ b/src/qrcode/decoder/version.rs @@ -201,7 +201,7 @@ impl Version { * See ISO 18004:2006 Annex E */ pub fn buildFunctionPattern(&self) -> Result { - if (self.isRMQR()) { + if self.isRMQR() { let dimension = Version::DimensionOfVersionRMQR(self.versionNumber); let mut bitMatrix = BitMatrix::new(dimension.x as u32, dimension.y as u32)?; @@ -240,7 +240,7 @@ impl Version { // Top right corner finder bitMatrix.set((dimension.x - 2) as u32, 1); - if (dimension.y > 9) { + if dimension.y > 9 { // Bottom left corner finder bitMatrix.set(1, (dimension.y - 2) as u32); }