Skip to content

Commit

Permalink
chore: clippy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Mar 19, 2024
1 parent b3b50b8 commit 59e70c6
Show file tree
Hide file tree
Showing 23 changed files with 137 additions and 97 deletions.
1 change: 0 additions & 1 deletion src/client/result/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub use GeoParsedResult::*;
// pub use GeoResultParser::*;
pub use AddressBookParsedResult::*;
pub use CalendarParsedResult::*;
pub use CalendarParsedResult::*;
pub use EmailAddressParsedResult::*;
pub use ExpandedProductParsedResult::*;
pub use ProductParsedResult::*;
Expand Down
2 changes: 1 addition & 1 deletion src/common/bit_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl BitMatrix {
* @param image bits of the image, as a row-major 2D array. Elements are arrays representing rows
* @return {@code BitMatrix} representation of image
*/
pub fn parse_bools(image: &Vec<Vec<bool>>) -> Self {
pub fn parse_bools(image: &[Vec<bool>]) -> Self {
let height: u32 = image.len().try_into().unwrap();
let width: u32 = image[0].len().try_into().unwrap();
let mut bits = BitMatrix::new(width, height).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ impl FormatInformation {

for mask in masks {
// for (auto mask : masks)
for bitsIndex in 0..bits.len() {
// for bitsIndex in 0..bits.len() {
for (bitsIndex, bitsItem) in bits.iter().enumerate() {
// for (int bitsIndex = 0; bitsIndex < Size(bits); ++bitsIndex)
for ref_pattern in MODEL2_MASKED_PATTERNS {
// for (uint32_t pattern : MODEL2_MASKED_PATTERNS) {
// 'unmask' the pattern first to get the original 5-data bits + 10-ec bits back
let pattern = ref_pattern ^ FORMAT_INFO_MASK_MODEL2;
// Find the pattern with fewest bits differing
let hammingDist = ((bits[bitsIndex] ^ mask) ^ pattern).count_ones();
let hammingDist = ((bitsItem ^ mask) ^ pattern).count_ones();
// if (int hammingDist = BitHacks::CountBitsSet((bits[bitsIndex] ^ mask) ^ pattern);
if hammingDist < fi.hammingDistance {
fi.mask = *mask; // store the used mask to discriminate between types/models
Expand Down Expand Up @@ -207,14 +208,15 @@ impl FormatInformation {
let mut fi = FormatInformation::default();

let mut best = |bits: &[u32], &patterns: &[u32; 64], mask: u32| {
for bitsIndex in 0..bits.len() {
for (bitsIndex, bitsItem) in bits.iter().enumerate() {
// for bitsIndex in 0..bits.len() {
// for (int bitsIndex = 0; bitsIndex < Size(bits); ++bitsIndex) {
for l_pattern in patterns {
// for (uint32_t pattern : patterns) {
// 'unmask' the pattern first to get the original 6-data bits + 12-ec bits back
let pattern = l_pattern ^ mask;
// Find the pattern with fewest bits differing
let hammingDist = ((bits[bitsIndex] ^ mask) ^ pattern).count_ones();
let hammingDist = ((bitsItem ^ mask) ^ pattern).count_ones();
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
Expand Down
1 change: 0 additions & 1 deletion src/common/cpp_essentials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod structured_append;
pub mod util;
pub mod value;

pub use bitmatrix_cursor::*;
pub use bitmatrix_cursor_trait::*;
pub use concentric_finder::*;
pub use decoder_result::*;
Expand Down
2 changes: 0 additions & 2 deletions src/common/cpp_essentials/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::iter::Sum;

use crate::common::Result;
use crate::qrcode::cpp_port::detector::AppendBit;
use crate::{Exceptions, Point};
Expand Down
5 changes: 3 additions & 2 deletions src/common/global_histogram_binarizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ impl<LS: LuminanceSource> GlobalHistogramBinarizer<LS> {
let row = height * y / 5;
let localLuminances = source.get_row(row);
let right = (width * 4) / 5;
for x in (width / 5)..right {
let pixel = localLuminances[x];
for pixel in &localLuminances[(width / 5)..right] {
// for x in (width / 5)..right {
// let pixel = localLuminances[x];
localBuckets[(pixel >> LUMINANCE_SHIFT) as usize] += 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/reedsolomon/reedsolomon_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ReedSolomonDecoder {
* @param twoS number of error-correction codewords available
* @throws ReedSolomonException if decoding fails for any reason
*/
pub fn decode(&self, received: &mut Vec<i32>, twoS: i32) -> Result<usize> {
pub fn decode(&self, received: &mut [i32], twoS: i32) -> Result<usize> {
let poly = GenericGFPoly::new(self.field, received)?;
let mut syndromeCoefficients = vec![0; twoS as usize];
let mut noError = true;
Expand Down Expand Up @@ -204,7 +204,7 @@ impl ReedSolomonDecoder {
fn findErrorMagnitudes(
&self,
errorEvaluator: &GenericGFPoly,
errorLocations: &Vec<usize>,
errorLocations: &[usize],
) -> Result<Vec<i32>> {
// This is directly applying Forney's Formula
let s = errorLocations.len();
Expand Down
2 changes: 1 addition & 1 deletion src/common/reedsolomon/reedsolomon_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ReedSolomonEncoder {
Some(rv)
}

pub fn encode(&mut self, to_encode: &mut Vec<i32>, ec_bytes: usize) -> Result<()> {
pub fn encode(&mut self, to_encode: &mut [i32], ec_bytes: usize) -> Result<()> {
if ec_bytes == 0 {
return Err(Exceptions::illegal_argument_with(
"No error correction bytes",
Expand Down
2 changes: 1 addition & 1 deletion src/datamatrix/decoder/datamatrix_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Decoder {
* @throws FormatException if the Data Matrix Code cannot be decoded
* @throws ChecksumException if error correction fails
*/
pub fn decode_bools(&self, image: &Vec<Vec<bool>>) -> Result<DecoderRXingResult> {
pub fn decode_bools(&self, image: &[Vec<bool>]) -> Result<DecoderRXingResult> {
self.perform_decode(&BitMatrix::parse_bools(image), false, false)
}

Expand Down
5 changes: 1 addition & 4 deletions src/filtered_image_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ impl<R: Reader> Reader for FilteredImageReader<R> {
}
}

#[derive(Debug, Clone)]
#[derive(Default)]
#[derive(Debug, Clone, Default)]
struct LumImagePyramid {
pub layers: Vec<Luma8LuminanceSource>,
}



impl LumImagePyramid {
pub fn new(image: Luma8LuminanceSource, threshold: usize, factor: usize) -> Option<Self> {
let mut new_self = Self::default();
Expand Down
6 changes: 4 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,15 @@ pub fn save_file(file_name: &str, bit_matrix: &BitMatrix) -> Result<()> {
return save_image(file_name, bit_matrix);
}

match || -> std::io::Result<_> {
let result_tester = || -> std::io::Result<_> {
let file = std::fs::File::create(path)?;
let mut output = std::io::BufWriter::new(file);
output.write_all(bit_matrix.to_string().as_bytes())?;
output.flush()?;
Ok(())
}() {
};

match result_tester() {
Ok(_) => Ok(()),
Err(_) => Err(Exceptions::illegal_argument_with(format!(
"could not write to '{file_name}'"
Expand Down
16 changes: 4 additions & 12 deletions src/multi_format_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::common::Result;
#[cfg(feature = "experimental_features")]
use crate::oned::cpp::ODReader;
use crate::qrcode::cpp_port::QrReader;
use crate::ONE_D_FORMATS;
use crate::{
aztec::AztecReader, datamatrix::DataMatrixReader, maxicode::MaxiCodeReader,
oned::MultiFormatOneDReader, pdf417::PDF417Reader, qrcode::QRCodeReader, BarcodeFormat,
Expand Down Expand Up @@ -157,18 +158,9 @@ impl MultiFormatReader {

fn decode_formats<B: Binarizer>(&mut self, image: &mut BinaryBitmap<B>) -> Result<RXingResult> {
if !self.possible_formats.is_empty() {
let one_d = self.possible_formats.contains(&BarcodeFormat::UPC_A)
|| self.possible_formats.contains(&BarcodeFormat::UPC_E)
|| self.possible_formats.contains(&BarcodeFormat::EAN_13)
|| self.possible_formats.contains(&BarcodeFormat::EAN_8)
|| self.possible_formats.contains(&BarcodeFormat::CODABAR)
|| self.possible_formats.contains(&BarcodeFormat::CODE_39)
|| self.possible_formats.contains(&BarcodeFormat::CODE_93)
|| self.possible_formats.contains(&BarcodeFormat::CODE_128)
|| self.possible_formats.contains(&BarcodeFormat::ITF)
|| self.possible_formats.contains(&BarcodeFormat::RSS_14)
|| self.possible_formats.contains(&BarcodeFormat::RSS_EXPANDED)
|| self.possible_formats.contains(&BarcodeFormat::TELEPEN);
let one_d = ONE_D_FORMATS
.iter()
.any(|e| self.possible_formats.contains(e));
if one_d && !self.try_harder {
if let Ok(res) = self.one_d_reader.decode_with_hints(image, &self.hints) {
return Ok(res);
Expand Down
42 changes: 30 additions & 12 deletions src/multi_use_multi_format_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ use crate::{
RXingResult, Reader,
};

pub(crate) const ONE_D_FORMATS: [BarcodeFormat; 12] = [
BarcodeFormat::UPC_A,
BarcodeFormat::UPC_E,
BarcodeFormat::EAN_13,
BarcodeFormat::EAN_8,
BarcodeFormat::CODABAR,
BarcodeFormat::CODE_39,
BarcodeFormat::CODE_93,
BarcodeFormat::CODE_128,
BarcodeFormat::ITF,
BarcodeFormat::RSS_14,
BarcodeFormat::RSS_EXPANDED,
BarcodeFormat::TELEPEN,
];

/**
* MultiFormatReader is a convenience class and the main entry point into the library for most uses.
* By default it attempts to decode all barcode formats that the library supports. Optionally, you
Expand Down Expand Up @@ -166,18 +181,21 @@ impl MultiUseMultiFormatReader {

fn decode_formats<B: Binarizer>(&mut self, image: &mut BinaryBitmap<B>) -> Result<RXingResult> {
if !self.possible_formats.is_empty() {
let one_d = self.possible_formats.contains(&BarcodeFormat::UPC_A)
|| self.possible_formats.contains(&BarcodeFormat::UPC_E)
|| self.possible_formats.contains(&BarcodeFormat::EAN_13)
|| self.possible_formats.contains(&BarcodeFormat::EAN_8)
|| self.possible_formats.contains(&BarcodeFormat::CODABAR)
|| self.possible_formats.contains(&BarcodeFormat::CODE_39)
|| self.possible_formats.contains(&BarcodeFormat::CODE_93)
|| self.possible_formats.contains(&BarcodeFormat::CODE_128)
|| self.possible_formats.contains(&BarcodeFormat::ITF)
|| self.possible_formats.contains(&BarcodeFormat::RSS_14)
|| self.possible_formats.contains(&BarcodeFormat::RSS_EXPANDED)
|| self.possible_formats.contains(&BarcodeFormat::TELEPEN);
let one_d = ONE_D_FORMATS
.iter()
.any(|e| self.possible_formats.contains(e));
// let one_d = self.possible_formats.contains(&BarcodeFormat::UPC_A)
// || self.possible_formats.contains(&BarcodeFormat::UPC_E)
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains()
// || self.possible_formats.contains();
if one_d && !self.try_harder {
if let Ok(res) = self.one_d_reader.decode_with_hints(image, &self.hints) {
return Ok(res);
Expand Down
2 changes: 1 addition & 1 deletion src/oned/rss/expanded/bit_array_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use super::ExpandedPair;
* @author Eduardo Castillejo, University of Deusto ([email protected])
*/

pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> Option<BitArray> {
pub fn buildBitArray(pairs: &[ExpandedPair]) -> Option<BitArray> {
let mut charNumber = (pairs.len() * 2) - 1;
if let Some(pair) = pairs.last() {
if pair.getRightChar().is_none() {
Expand Down
51 changes: 33 additions & 18 deletions src/oned/rss/expanded/rss_expanded_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,38 @@ impl RSSExpandedReader {
// let sequence = FINDER_PATTERN_SEQUENCES.get(i).unwrap();
// for (int[] sequence : FINDER_PATTERN_SEQUENCES) {
if pairs.len() <= sequence.len() {
let mut stop = true;
for (j, seq) in sequence.iter().enumerate().take(pairs.len()) {
// for (int j = 0; j < pairs.size(); j++) {
if pairs
.get(j)
.unwrap()
.getFinderPattern()
.as_ref()
.unwrap()
.getValue()
!= *seq
{
stop = false;
break;
}
}
let stop = sequence
.iter()
.enumerate()
.take(pairs.len())
.all(|(j, seq)| {
pairs
.get(j)
.unwrap()
.getFinderPattern()
.as_ref()
.unwrap()
.getValue()
== *seq
});

// let mut stop = true;
// for (j, seq) in sequence.iter().enumerate().take(pairs.len()) {
// // for (int j = 0; j < pairs.size(); j++) {
// if pairs
// .get(j)
// .unwrap()
// .getFinderPattern()
// .as_ref()
// .unwrap()
// .getValue()
// != *seq
// {
// stop = false;
// break;
// // return true;
// }
// }
if stop {
return true;
}
Expand Down Expand Up @@ -528,8 +544,7 @@ impl RSSExpandedReader {

// Not private for unit testing
pub(crate) fn constructRXingResult(pairs: &[ExpandedPair]) -> Result<RXingResult> {
let binary =
bit_array_builder::buildBitArray(&pairs.to_vec()).ok_or(Exceptions::ILLEGAL_STATE)?;
let binary = bit_array_builder::buildBitArray(pairs).ok_or(Exceptions::ILLEGAL_STATE)?;

let mut decoder = abstract_expanded_decoder::createDecoder(&binary)?;
let resultingString = decoder.parseInformation()?;
Expand Down
4 changes: 2 additions & 2 deletions src/pdf417/pdf_417_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl PDF417Writer {
* @param margin border around the barcode
* @return BitMatrix of the input
*/
fn bitMatrixFromBitArray(input: &Vec<Vec<u8>>, margin: u32) -> Option<BitMatrix> {
fn bitMatrixFromBitArray(input: &[Vec<u8>], margin: u32) -> Option<BitMatrix> {
// Creates the bit matrix with extra space for whitespace
let mut output = BitMatrix::new(
input[0].len() as u32 + 2 * margin,
Expand All @@ -212,7 +212,7 @@ impl PDF417Writer {
/**
* Takes and rotates the it 90 degrees
*/
fn rotateArray(bitarray: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
fn rotateArray(bitarray: &[Vec<u8>]) -> Vec<Vec<u8>> {
let mut temp = vec![vec![0; bitarray.len()]; bitarray[0].len()];

for ii in 0..bitarray.len() {
Expand Down
2 changes: 1 addition & 1 deletion src/qrcode/cpp_port/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::Exceptions;
pub fn CorrectErrors(codewordBytes: &mut [u8], numDataCodewords: u32) -> Result<bool> {
// First read into an array of ints
// std::vector<int> codewordsInts(codewordBytes.begin(), codewordBytes.end());
let mut codewordsInts = codewordBytes.iter().copied().map(|b| b as i32).collect();
let mut codewordsInts: Vec<i32> = codewordBytes.iter().copied().map(|b| b as i32).collect();

let numECCodewords = ((codewordBytes.len() as u32) - numDataCodewords) as i32;
let rs = ReedSolomonDecoder::new(get_predefined_genericgf(
Expand Down
30 changes: 21 additions & 9 deletions src/qrcode/cpp_port/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,29 @@ pub fn SampleQR(image: &BitMatrix, fp: &FinderPatternSet) -> Result<QRCodeDetect
return Err(Exceptions::NOT_FOUND);
}

let best = if top.err == left.err {
if top.dim > left.dim {
top
} else {
left
let best = match top.err.cmp(&left.err) {
std::cmp::Ordering::Less => top,
std::cmp::Ordering::Equal => {
if top.dim > left.dim {
top
} else {
left
}
}
} else if top.err < left.err {
top
} else {
left
std::cmp::Ordering::Greater => left,
};

// let best = if top.err == left.err {
// if top.dim > left.dim {
// top
// } else {
// left
// }
// } else if top.err < left.err {
// top
// } else {
// left
// };
let mut dimension = best.dim;
let moduleSize = (best.ms + 1.0) as i32;

Expand Down
5 changes: 3 additions & 2 deletions src/qrcode/cpp_port/test/QRFormatInformationTest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const MICRO_MASKED_TEST_FORMAT_INFO: u32 = 0x3BBA;
const RMQR_MASKED_TEST_FORMAT_INFO: u32 = 0x20137;
const RMQR_MASKED_TEST_FORMAT_INFO_SUB: u32 = 0x1F1FE;

const FORMAT_INFO_MASK_QR_MODEL1: u32 = 0x2825;
const FORMAT_INFO_MASK_MICRO: u32 = 0x4445;
// Removed as unused
//const FORMAT_INFO_MASK_QR_MODEL1: u32 = 0x2825;
//const FORMAT_INFO_MASK_MICRO: u32 = 0x4445;
const FORMAT_INFO_MASK_RMQR: u32 = 0x1FAB2; // Finder pattern side
const FORMAT_INFO_MASK_RMQR_SUB: u32 = 0x20A7B; // Finder sub pattern side

Expand Down
Loading

0 comments on commit 59e70c6

Please sign in to comment.