Implement clippy suggestions

This commit is contained in:
Henry Schimke
2023-01-04 14:48:16 -06:00
parent c65eadf102
commit 48287631dd
196 changed files with 2465 additions and 2574 deletions

View File

@@ -43,7 +43,7 @@ fn checkVersion(version: Result<&Version, Exceptions>, number: u32, dimension: u
assert_eq!(number, version.getVersionNumber());
// assertNotNull(version.getAlignmentPatternCenters());
if number > 1 {
assert!(version.getAlignmentPatternCenters().len() > 0);
assert!(!version.getAlignmentPatternCenters().is_empty());
}
assert_eq!(dimension, version.getDimensionForVersion());
let _tmp = version.getECBlocksForLevel(ErrorCorrectionLevel::H);

View File

@@ -36,10 +36,10 @@ impl BitMatrixParser {
pub fn new(bit_matrix: BitMatrix) -> Result<Self, Exceptions> {
let dimension = bit_matrix.getHeight();
if dimension < 21 || (dimension & 0x03) != 1 {
Err(Exceptions::FormatException(format!(
Err(Exceptions::FormatException(Some(format!(
"{} < 21 || ({} % 0x03) != 1",
dimension, dimension
)))
))))
} else {
Ok(Self {
bitMatrix: bit_matrix,
@@ -59,7 +59,7 @@ impl BitMatrixParser {
*/
pub fn readFormatInformation(&mut self) -> Result<&FormatInformation, Exceptions> {
if self.parsedFormatInfo.is_some() {
return Ok(&self.parsedFormatInfo.as_ref().unwrap());
return Ok(self.parsedFormatInfo.as_ref().unwrap());
}
// Read top-left format info bits
@@ -96,7 +96,7 @@ impl BitMatrixParser {
if let Some(pfi) = &self.parsedFormatInfo {
return Ok(pfi);
}
Err(Exceptions::FormatException("".to_owned()))
Err(Exceptions::FormatException(None))
}
/**
@@ -108,7 +108,7 @@ impl BitMatrixParser {
*/
pub fn readVersion(&mut self) -> Result<VersionRef, Exceptions> {
if let Some(pv) = self.parsedVersion {
return Ok(&pv);
return Ok(pv);
}
let dimension = self.bitMatrix.getHeight();
@@ -152,7 +152,7 @@ impl BitMatrixParser {
return Ok(theParsedVersion);
}
}
Err(Exceptions::FormatException("".to_owned()))
Err(Exceptions::FormatException(None))
}
fn copyBit(&self, i: u32, j: u32, versionBits: u32) -> u32 {
@@ -236,7 +236,7 @@ impl BitMatrixParser {
}
if resultOffset != version.getTotalCodewords() as usize {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
Ok(result)
}
@@ -250,7 +250,7 @@ impl BitMatrixParser {
let dimension = self.bitMatrix.getHeight();
dataMask.unmaskBitMatrix(&mut self.bitMatrix, dimension);
} else {
return; // We have no format information, and have no data mask
// We have no format information, and have no data mask
}
// if self.parsedFormatInfo.is_none() {
// return; // We have no format information, and have no data mask

View File

@@ -55,7 +55,7 @@ impl DataBlock {
ecLevel: ErrorCorrectionLevel,
) -> Result<Vec<Self>, Exceptions> {
if rawCodewords.len() as u32 != version.getTotalCodewords() {
return Err(Exceptions::IllegalArgumentException("".to_owned()));
return Err(Exceptions::IllegalArgumentException(None));
}
// Figure out the number and size of data blocks used by this version and
@@ -109,26 +109,32 @@ impl DataBlock {
let mut rawCodewordsOffset = 0;
for i in 0..shorterBlocksNumDataCodewords {
// for (int i = 0; i < shorterBlocksNumDataCodewords; i++) {
for j in 0..numRXingResultBlocks {
for result_j in result.iter_mut().take(numRXingResultBlocks) {
// for j in 0..numRXingResultBlocks {
// for (int j = 0; j < numRXingResultBlocks; j++) {
result[j].codewords[i] = rawCodewords[rawCodewordsOffset];
result_j.codewords[i] = rawCodewords[rawCodewordsOffset];
rawCodewordsOffset += 1;
}
}
// Fill out the last data block in the longer ones
for j in longerBlocksStartAt..numRXingResultBlocks {
for res in result
.iter_mut()
.take(numRXingResultBlocks)
.skip(longerBlocksStartAt)
{
// for j in longerBlocksStartAt..numRXingResultBlocks {
// for (int j = longerBlocksStartAt; j < numRXingResultBlocks; j++) {
result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset];
res.codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset];
rawCodewordsOffset += 1;
}
// Now add in error correction blocks
let max = result[0].codewords.len();
for i in shorterBlocksNumDataCodewords..max {
// for (int i = shorterBlocksNumDataCodewords; i < max; i++) {
for j in 0..numRXingResultBlocks {
for (j, res) in result.iter_mut().enumerate().take(numRXingResultBlocks) {
// for (int j = 0; j < numRXingResultBlocks; j++) {
let iOffset = if j < longerBlocksStartAt { i } else { i + 1 };
result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset];
res.codewords[iOffset] = rawCodewords[rawCodewordsOffset];
rawCodewordsOffset += 1;
}
}

View File

@@ -230,10 +230,10 @@ impl TryFrom<u8> for DataMask {
5 => Ok(DataMask::DATA_MASK_101),
6 => Ok(DataMask::DATA_MASK_110),
7 => Ok(DataMask::DATA_MASK_111),
_ => Err(Exceptions::IllegalArgumentException(format!(
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
"{} is not between 0 and 7",
value
))),
)))),
}
}
}

View File

@@ -37,12 +37,12 @@ const ALPHANUMERIC_CHARS: &str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
const GB2312_SUBSET: u32 = 1;
pub fn decode(
bytes: &Vec<u8>,
bytes: &[u8],
version: VersionRef,
ecLevel: ErrorCorrectionLevel,
hints: &DecodingHintDictionary,
) -> Result<DecoderRXingResult, Exceptions> {
let mut bits = BitSource::new(bytes.clone());
let mut bits = BitSource::new(bytes.to_owned());
let mut result = String::with_capacity(50);
let mut byteSegments = vec![vec![0u8; 0]; 0];
let mut symbolSequence = -1i32;
@@ -77,10 +77,10 @@ pub fn decode(
}
Mode::STRUCTURED_APPEND => {
if bits.available() < 16 {
return Err(Exceptions::FormatException(format!(
return Err(Exceptions::FormatException(Some(format!(
"Mode::Structured append expected bits.available() < 16, found bits of {}",
bits.available()
)));
))));
}
// sequence number and parity is added later to the result metadata
// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
@@ -92,10 +92,10 @@ pub fn decode(
let value = parseECIValue(&mut bits)?;
currentCharacterSetECI = Some(CharacterSetECI::getCharacterSetECIByValue(value)?);
if currentCharacterSetECI.is_none() {
return Err(Exceptions::FormatException(format!(
return Err(Exceptions::FormatException(Some(format!(
"Value of {} not valid",
value
)));
))));
}
}
Mode::HANZI => {
@@ -126,7 +126,7 @@ pub fn decode(
hints,
)?,
Mode::KANJI => decodeKanjiSegment(&mut bits, &mut result, count)?,
_ => return Err(Exceptions::FormatException("".to_owned())),
_ => return Err(Exceptions::FormatException(None)),
}
}
}
@@ -144,14 +144,12 @@ pub fn decode(
} else {
symbologyModifier = 2;
}
} else if hasFNC1first {
symbologyModifier = 3;
} else if hasFNC1second {
symbologyModifier = 5;
} else {
if hasFNC1first {
symbologyModifier = 3;
} else if hasFNC1second {
symbologyModifier = 5;
} else {
symbologyModifier = 1;
}
symbologyModifier = 1;
}
// } catch (IllegalArgumentException iae) {
@@ -160,7 +158,7 @@ pub fn decode(
// }
Ok(DecoderRXingResult::with_all(
bytes.clone(),
bytes.to_owned(),
result,
byteSegments.to_vec(),
format!("{}", u8::from(ecLevel)),
@@ -180,7 +178,7 @@ fn decodeHanziSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if count * 13 > bits.available() {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -222,7 +220,7 @@ fn decodeKanjiSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if count * 13 > bits.available() {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -266,25 +264,26 @@ fn decodeByteSegment(
) -> Result<(), Exceptions> {
// Don't crash trying to read more bits than we have available.
if 8 * count > bits.available() {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
let mut readBytes = vec![0u8; count];
for i in 0..count {
for byte in readBytes.iter_mut().take(count) {
// for i in 0..count {
// for (int i = 0; i < count; i++) {
readBytes[i] = bits.readBits(8)? as u8;
*byte = bits.readBits(8)? as u8;
}
let encoding;
if currentCharacterSetECI.is_none() {
let encoding = if currentCharacterSetECI.is_none() {
// The spec isn't clear on this mode; see
// section 6.4.5: t does not say which encoding to assuming
// upon decoding. I have seen ISO-8859-1 used as well as
// Shift_JIS -- without anything like an ECI designator to
// give a hint.
encoding = StringUtils::guessCharset(&readBytes, &hints);
StringUtils::guessCharset(&readBytes, hints)
} else {
encoding = CharacterSetECI::getCharset(currentCharacterSetECI.as_ref().unwrap());
}
CharacterSetECI::getCharset(currentCharacterSetECI.as_ref().unwrap())
};
let encode_string = if currentCharacterSetECI.is_some()
&& currentCharacterSetECI.as_ref().unwrap() == &CharacterSetECI::Cp437
@@ -312,7 +311,7 @@ fn decodeByteSegment(
fn toAlphaNumericChar(value: u32) -> Result<char, Exceptions> {
if value as usize >= ALPHANUMERIC_CHARS.len() {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
Ok(ALPHANUMERIC_CHARS.chars().nth(value as usize).unwrap())
}
@@ -328,7 +327,7 @@ fn decodeAlphanumericSegment(
let mut count = count;
while count > 1 {
if bits.available() < 11 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
let nextTwoCharsBits = bits.readBits(11)?;
result.push(toAlphaNumericChar(nextTwoCharsBits / 45)?);
@@ -338,7 +337,7 @@ fn decodeAlphanumericSegment(
if count == 1 {
// special case: one character left
if bits.available() < 6 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
result.push(toAlphaNumericChar(bits.readBits(6)?)?);
}
@@ -374,11 +373,11 @@ fn decodeNumericSegment(
while count >= 3 {
// Each 10 bits encodes three digits
if bits.available() < 10 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
let threeDigitsBits = bits.readBits(10)?;
if threeDigitsBits >= 1000 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
result.push(toAlphaNumericChar(threeDigitsBits / 100)?);
result.push(toAlphaNumericChar((threeDigitsBits / 10) % 10)?);
@@ -388,22 +387,22 @@ fn decodeNumericSegment(
if count == 2 {
// Two digits left over to read, encoded in 7 bits
if bits.available() < 7 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
let twoDigitsBits = bits.readBits(7)?;
if twoDigitsBits >= 100 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
result.push(toAlphaNumericChar(twoDigitsBits / 10)?);
result.push(toAlphaNumericChar(twoDigitsBits % 10)?);
} else if count == 1 {
// One digit left over to read
if bits.available() < 4 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
let digitBits = bits.readBits(4)?;
if digitBits >= 10 {
return Err(Exceptions::FormatException("".to_owned()));
return Err(Exceptions::FormatException(None));
}
result.push(toAlphaNumericChar(digitBits)?);
}
@@ -428,5 +427,5 @@ fn parseECIValue(bits: &mut BitSource) -> Result<u32, Exceptions> {
return Ok(((firstByte & 0x1F) << 16) | secondThirdBytes);
}
Err(Exceptions::FormatException("".to_owned()))
Err(Exceptions::FormatException(None))
}

View File

@@ -124,8 +124,8 @@ pub fn decode_bitmatrix_with_hints(
Ok(res) => Ok(res),
Err(er) => match er {
Exceptions::FormatException(_) | Exceptions::ChecksumException(_) => {
if fe.is_some() {
Err(fe.unwrap())
if let Some(fe) = fe {
Err(fe)
} else {
Err(ce.unwrap())
}
@@ -170,9 +170,10 @@ fn decode_bitmatrix_parser_with_hints(
let mut codewordBytes = dataBlock.getCodewords().to_vec();
let numDataCodewords = dataBlock.getNumDataCodewords() as usize;
correctErrors(&mut codewordBytes, numDataCodewords)?;
for i in 0..numDataCodewords {
for codeword_byte in codewordBytes.iter().take(numDataCodewords) {
// for i in 0..numDataCodewords {
// for (int i = 0; i < numDataCodewords; i++) {
resultBytes[resultOffset] = codewordBytes[i];
resultBytes[resultOffset] = *codeword_byte; //codewordBytes[i];
resultOffset += 1;
}
}
@@ -193,20 +194,21 @@ fn correctErrors(codewordBytes: &mut [u8], numDataCodewords: usize) -> Result<()
let numCodewords = codewordBytes.len();
// First read into an array of ints
let mut codewordsInts = vec![0u8; numCodewords];
for i in 0..numCodewords {
// for (int i = 0; i < numCodewords; i++) {
codewordsInts[i] = codewordBytes[i]; // & 0xFF;
}
// for i in 0..numCodewords {
// // for (int i = 0; i < numCodewords; i++) {
// codewordsInts[i] = codewordBytes[i]; // & 0xFF;
// }
codewordsInts[..numCodewords].copy_from_slice(&codewordBytes[..numCodewords]);
let mut sending_code_words: Vec<i32> = codewordsInts.iter().map(|x| *x as i32).collect();
if let Err(e) = RS_DECODER.decode(
if let Err(Exceptions::ReedSolomonException(error_str)) = RS_DECODER.decode(
&mut sending_code_words,
(codewordBytes.len() - numDataCodewords) as i32,
) {
if let Exceptions::ReedSolomonException(error_str) = e {
return Err(Exceptions::ChecksumException(error_str));
}
// if let Exceptions::ReedSolomonException(error_str) = e {
return Err(Exceptions::ChecksumException(error_str));
// }
}
// Copy back into array of bytes -- only need to worry about the bytes that were data

View File

@@ -47,10 +47,10 @@ impl ErrorCorrectionLevel {
1 => Ok(Self::L),
2 => Ok(Self::H),
3 => Ok(Self::Q),
_ => Err(Exceptions::IllegalArgumentException(format!(
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
"{} is not a valid bit selection",
bits
))),
)))),
}
}
@@ -101,19 +101,19 @@ impl FromStr for ErrorCorrectionLevel {
};
// If we find something, cool, return it, otherwise keep trying as numbers
if as_str.is_some() {
return Ok(as_str.unwrap());
if let Some(as_str) = as_str {
return Ok(as_str);
}
let number_possible = s.parse::<u8>();
if number_possible.is_ok() {
return number_possible.unwrap().try_into();
if let Ok(number_possible) = number_possible {
return number_possible.try_into();
}
return Err(Exceptions::IllegalArgumentException(format!(
return Err(Exceptions::IllegalArgumentException(Some(format!(
"could not parse {} into an ec level",
s
)));
))));
}
}

View File

@@ -68,10 +68,10 @@ impl Mode {
{
Ok(Self::HANZI)
}
_ => Err(Exceptions::IllegalArgumentException(format!(
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
"{} is not valid",
bits
))),
)))),
}
}

View File

@@ -45,9 +45,7 @@ impl QRCodeDecoderMetaData {
if !self.0 || points.is_empty() || points.len() < 3 {
return;
}
let bottom_left = points[0];
points[0] = points[2];
points[2] = bottom_left;
points.swap(0, 2);
// No need to 'fix' top-left and alignment pattern.
}
}

View File

@@ -105,9 +105,9 @@ impl Version {
dimension: u32,
) -> Result<&'static Version, Exceptions> {
if dimension % 4 != 1 {
return Err(Exceptions::FormatException(
return Err(Exceptions::FormatException(Some(
"dimension incorrect".to_owned(),
));
)));
}
Self::getVersionForNumber((dimension - 17) / 4)
// try {
@@ -118,10 +118,10 @@ impl Version {
}
pub fn getVersionForNumber(versionNumber: u32) -> Result<&'static Version, Exceptions> {
if versionNumber < 1 || versionNumber > 40 {
return Err(Exceptions::IllegalArgumentException(
if !(1..=40).contains(&versionNumber) {
return Err(Exceptions::IllegalArgumentException(Some(
"version out of spec".to_owned(),
));
)));
}
Ok(&VERSIONS[versionNumber as usize - 1])
}
@@ -150,7 +150,7 @@ impl Version {
return Self::getVersionForNumber(bestVersion);
}
// If we didn't find a close enough match, fail
Err(Exceptions::NotFoundException("could not find".to_owned()))
Err(Exceptions::NotFoundException(None))
}
/**