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

@@ -171,25 +171,25 @@ pub fn encode_bytes_with_charset(
MAX_NB_BITS
})
{
return Err(Exceptions::IllegalArgumentException(format!(
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Illegal value {} for layers",
user_specified_layers
)));
))));
}
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
word_size = WORD_SIZE[layers as usize];
let usable_bits_in_layers = total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
stuffed_bits = stuffBits(&bits, word_size as usize);
if stuffed_bits.getSize() as u32 + ecc_bits > usable_bits_in_layers {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
));
)));
}
if compact && stuffed_bits.getSize() as u32 > word_size * 64 {
// Compact format only allows 64 data words, though C4 can hold more words than that
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
));
)));
}
} else {
word_size = 0;
@@ -201,14 +201,14 @@ pub fn encode_bytes_with_charset(
loop {
// for (int i = 0; ; i++) {
if i > MAX_NB_BITS {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"Data too large for an Aztec code".to_owned(),
));
)));
}
compact = i <= 3;
layers = if compact { i + 1 } else { i };
total_bits_in_layer_var = total_bits_in_layer(layers, compact);
if total_size_bits > total_bits_in_layer_var as u32 {
if total_size_bits > total_bits_in_layer_var {
i += 1;
continue;
}
@@ -239,7 +239,7 @@ pub fn encode_bytes_with_charset(
// generate mode message
let messageSizeInWords = stuffed_bits.getSize() as u32 / word_size;
let modeMessage = generateModeMessage(compact, layers as u32, messageSizeInWords);
let modeMessage = generateModeMessage(compact, layers, messageSizeInWords);
// allocate symbol
let baseMatrixSize = (if compact { 11 } else { 14 }) + layers * 4; // not including alignment lines
@@ -248,10 +248,8 @@ pub fn encode_bytes_with_charset(
if compact {
// no alignment marks in compact mode, alignmentMap is a no-op
matrixSize = baseMatrixSize;
for i in 0..alignmentMap.len() {
// for (int i = 0; i < alignmentMap.length; i++) {
alignmentMap[i] = i as u32;
}
// for i in 0..alignmentMap.len() {
alignmentMap[..].copy_from_slice(&(0..baseMatrixSize).collect::<Vec<u32>>()[..]);
} else {
matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
let origCenter = (baseMatrixSize / 2) as usize;
@@ -259,11 +257,11 @@ pub fn encode_bytes_with_charset(
for i in 0..origCenter {
// for (int i = 0; i < origCenter; i++) {
let newOffset = (i + i / 15) as u32;
alignmentMap[origCenter - i - 1] = center as u32 - newOffset - 1;
alignmentMap[origCenter + i] = center as u32 + newOffset + 1;
alignmentMap[origCenter - i - 1] = center - newOffset - 1;
alignmentMap[origCenter + i] = center + newOffset + 1;
}
}
let mut matrix = BitMatrix::with_single_dimension(matrixSize as u32);
let mut matrix = BitMatrix::with_single_dimension(matrixSize);
// dbg!(matrix.to_string());
@@ -306,25 +304,25 @@ pub fn encode_bytes_with_charset(
// dbg!(matrix.to_string());
// draw mode message
drawModeMessage(&mut matrix, compact, matrixSize as u32, modeMessage);
drawModeMessage(&mut matrix, compact, matrixSize, modeMessage);
// dbg!(matrix.to_string());
// draw alignment marks
if compact {
drawBullsEye(&mut matrix, matrixSize as u32 / 2, 5);
drawBullsEye(&mut matrix, matrixSize / 2, 5);
} else {
drawBullsEye(&mut matrix, matrixSize as u32 / 2, 7);
drawBullsEye(&mut matrix, matrixSize / 2, 7);
let mut i = 0;
let mut j = 0;
while i < baseMatrixSize / 2 - 1 {
let mut k = (matrixSize / 2) & 1;
while k < matrixSize {
// for (int k = (matrixSize / 2) & 1; k < matrixSize; k += 2) {
matrix.set(matrixSize as u32 / 2 - j, k as u32);
matrix.set(matrixSize as u32 / 2 + j, k as u32);
matrix.set(k as u32, matrixSize as u32 / 2 - j);
matrix.set(k as u32, matrixSize as u32 / 2 + j);
matrix.set(matrixSize / 2 - j, k);
matrix.set(matrixSize / 2 + j, k);
matrix.set(k, matrixSize / 2 - j);
matrix.set(k, matrixSize / 2 + j);
k += 2;
}
@@ -344,13 +342,7 @@ pub fn encode_bytes_with_charset(
// dbg!(matrix.to_string());
let aztec = AztecCode::new(
compact,
matrixSize as u32,
layers,
messageSizeInWords as u32,
matrix,
);
let aztec = AztecCode::new(compact, matrixSize, layers, messageSizeInWords, matrix);
// aztec.setCompact(compact);
// aztec.setSize(matrixSize);
// aztec.setLayers(layers);
@@ -399,7 +391,7 @@ pub fn generateModeMessage(compact: bool, layers: u32, messageSizeInWords: u32)
.expect("should append");
mode_message = generateCheckWords(&mode_message, 40, 4);
}
return mode_message;
mode_message
}
fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
@@ -459,7 +451,7 @@ fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) ->
.expect("must append");
}
// dbg!(message_bits.to_string());
return message_bits;
message_bits
}
fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Vec<i32> {
@@ -472,7 +464,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
for j in 0..wordSize {
// for (int j = 0; j < wordSize; j++) {
value |= if stuffedBits.get(i * wordSize + j) {
1 << wordSize - j - 1
1 << (wordSize - j - 1)
} else {
0
};
@@ -481,7 +473,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
i += 1;
}
return message;
message
}
fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
@@ -491,10 +483,10 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
8 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData8)),
10 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData10)),
12 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData12)),
_ => Err(Exceptions::IllegalArgumentException(format!(
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
"Unsupported word size {}",
wordSize
))),
)))),
}
// switch (wordSize) {
// case 4:
@@ -539,7 +531,7 @@ pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
i += word_size as isize;
}
return out;
out
}
fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {

View File

@@ -14,8 +14,6 @@
* limitations under the License.
*/
use std::cmp::Ordering;
use crate::{
common::{BitArray, CharacterSetECI},
exceptions::Exceptions,
@@ -250,9 +248,9 @@ impl HighLevelEncoder {
initial_state = initial_state.appendFLGn(CharacterSetECI::getValue(&eci))?;
}
} else {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"No ECI code for character set".to_owned(),
));
)));
}
// if self.charset != null {
// CharacterSetECI eci = CharacterSetECI.getCharacterSetECI(charset);
@@ -266,13 +264,13 @@ impl HighLevelEncoder {
while index < self.text.len() {
// for index in 0..self.text.len() {
// for (int index = 0; index < text.length; index++) {
let pair_code;
let next_char = if index + 1 < self.text.len() {
self.text[index + 1]
} else {
0
};
pair_code = match self.text[index] {
let pair_code = match self.text[index] {
b'\r' if next_char == b'\n' => 2,
b'.' if next_char == b' ' => 3,
b',' if next_char == b' ' => 4,
@@ -301,13 +299,19 @@ impl HighLevelEncoder {
.into_iter()
.min_by(|a, b| {
let diff: i64 = a.getBitCount() as i64 - b.getBitCount() as i64;
if diff < 0 {
Ordering::Less
} else if diff == 0 {
Ordering::Equal
} else {
Ordering::Greater
}
diff.cmp(&0)
// match diff {
// ..0 => Ordering::Less,
// 0 => Ordering::Equal,
// 0.. => Ordering::Greater,
// }
// if diff < 0 {
// Ordering::Less
// } else if diff == 0 {
// Ordering::Equal
// } else {
// Ordering::Greater
// }
// a.getBitCount() - b.getBitCount()
})
.unwrap();
@@ -344,7 +348,7 @@ impl HighLevelEncoder {
let mut state_no_binary = None;
for mode in 0..=Self::MODE_PUNCT {
// for (int mode = 0; mode <= MODE_PUNCT; mode++) {
let char_in_mode = Self::CHAR_MAP[mode as usize][ch as usize];
let char_in_mode = Self::CHAR_MAP[mode][ch as usize];
if char_in_mode > 0 {
if state_no_binary.is_none() {
// Only create stateNoBinary the first time it's required.
@@ -446,7 +450,7 @@ impl HighLevelEncoder {
add = false;
break;
}
if newState.isBetterThanOrEqualTo(&oldState) {
if newState.isBetterThanOrEqualTo(oldState) {
result.remove(i);
}
}
@@ -455,6 +459,6 @@ impl HighLevelEncoder {
result.push(newState);
}
}
return result;
result
}
}

View File

@@ -80,9 +80,9 @@ impl State {
token.add(0, 3); // 0: FNC1
} else */
if eci > 999999 {
return Err(Exceptions::IllegalArgumentException(
return Err(Exceptions::IllegalArgumentException(Some(
"ECI code must be between 0 and 999999".to_owned(),
));
)));
// throw new IllegalArgumentException("ECI code must be between 0 and 999999");
} else {
let eci_digits = encoding::all::ISO_8859_1
@@ -155,12 +155,10 @@ impl State {
let deltaBitCount =
if self.binary_shift_byte_count == 0 || self.binary_shift_byte_count == 31 {
18
} else if self.binary_shift_byte_count == 62 {
9
} else {
if self.binary_shift_byte_count == 62 {
9
} else {
8
}
8
};
let mut result = State::new(
token,
@@ -240,7 +238,7 @@ impl State {
if binary_shift_byte_count > 0 {
return 10; // one B/S
}
return 0;
0
}
}

View File

@@ -115,3 +115,9 @@ impl IntoIterator for Token {
// fn appendTo(&self, bitArray: BitArray, text: &[u8]);
// }
impl Default for Token {
fn default() -> Self {
Self::new()
}
}