Cleanup of many components of the code.

Many unwraps, expects, and panics removed or handled.

More rustification of the codebase.

Squashed commit of the following:

commit 80c6483cf8c819496a7b781f9e3a149d1f18d5fe
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 20:36:57 2023 -0600

    add other parsed result option

commit 412d92ae585e7bf194697f5282dbc045e448bace
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 19:06:34 2023 -0600

    fix some panics

commit 9108d3e931aadec00fc0bf346ebc52d24e5d8146
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:56:30 2023 -0600

    clippy --fix

commit 8cd89d314323f54f342f15fc73297b62d418ac79
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:55:45 2023 -0600

    some more cleanup in aztec

commit fb7f9242e273b85a4bb212f22a185a0dcbfafc36
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 18:19:54 2023 -0600

    cleanup root

commit 4fb7c0639830869149332b29be789e891eade3e7
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:50:15 2023 -0600

    format

commit 3ac271af657b25fbe5ee39cbdf8859e5aa38ea41
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:43:14 2023 -0600

    clippy --fix

commit 0c5b4c1477159db93a9da9645039f51cd1a7c574
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 16:04:31 2023 -0600

    qrcode cleanup complete

commit 70c1d107cc672e45d46567c650c46fece7e7da9c
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 13:28:25 2023 -0600

    clippy --fix

commit e13263d0f1f2a13dbb59b6e60d8b871a32babc92
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 13:27:44 2023 -0600

    qrcode detector cleanup

commit f7f2855099e469fe1091868c6099196705d0ae83
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:48:13 2023 -0600

    qrcode decoder

commit bdbaaba597b3479639c2ed3726465a69e3004471
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:13:18 2023 -0600

    clippy --fix

commit fd6dbcaeb1f45dc91ae98f0e7c58aad48d67df78
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 11:12:28 2023 -0600

    pdf417 cleanup complete

commit 7bbd47604c2639a5eb61bdb5533e71be3bce95a1
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 10:39:28 2023 -0600

    clippy --fix

commit 8f4962b7bb5f133524fe4d3d4948e5672a88c545
Author: Henry Schimke <henry@azule.info>
Date:   Fri Feb 10 10:39:00 2023 -0600

    cleanup on pdf417 hie

commit 254c8243953776d3c8704dd81529aadd05d5a94e
Author: Henry <henry@azule.info>
Date:   Fri Feb 10 02:51:21 2023 +0000

    partway through pdf417 hle

commit d4a5b00fde8c0282b0c6e4175a21c1e111b11533
Author: Henry <henry@azule.info>
Date:   Wed Feb 8 21:35:25 2023 +0000

    clippy --fix

commit 2bd4a8e218aa963a88997b50bc5a198f9a88205e
Author: Henry <henry@azule.info>
Date:   Wed Feb 8 21:31:55 2023 +0000

    pdf417 decode and detect cleanup

commit 05130ac54a72c2303045d64507bf6185aa74e531
Author: Henry Schimke <henry@azule.info>
Date:   Tue Feb 7 08:06:25 2023 -0600

    at decodeTextCompaction in decoded_bit_straem_parser

commit 924c023a7d2339dfffa6ef0c4a805fb192576796
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 18:22:02 2023 -0600

    clippy --fix

commit 5cfb81a885c22b4b5ec937a79be31d0815def211
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 18:20:22 2023 -0600

    oned cleanup

commit 0649b64854baf56a8e3314728d4f6cf4063db8b2
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 11:47:28 2023 -0600

    rss cleanup

commit 2d3127898fc11f7e65d590e983cda799b798b646
Author: Henry Schimke <henry@azule.info>
Date:   Mon Feb 6 10:19:56 2023 -0600

    multi cleanup

commit 7e8b43a159083a2096123e6204d4d1d359c59981
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:41:59 2023 -0600

    cargo fmt

commit df8285e8da07b4bffd48ca6e596de3f010eb8d9e
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:41:29 2023 -0600

    maxicode cleanup

commit a51f923a14016eb416ca202b2194258808dde3e7
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:57 2023 -0600

    cargo fmt

commit 332d7eef4a25318611025a3fcb0b70c604d917be
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:48 2023 -0600

    cleanup datamatrix

commit eb2311c476a9c9993f271ab6cf9fd359aedca26f
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 21:27:26 2023 -0600

    cargo clippy --fix

commit 2abaa4c51d287f619d4c7a438f85ba2a30f159cf
Author: Henry <henry@azule.info>
Date:   Sun Feb 5 19:13:02 2023 -0600

    finish cleanup in parsers

commit 716aa2fbf728716cff36657a5fd80e7bca5276b7
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 17:44:56 2023 -0600

    cleanup continues

commit 7d843788a49fa9cc82c4616d226c0f882e73612b
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 15:15:09 2023 -0600

    common cleanup

commit fd251866c68f22d450dd428c93d7a3620bf1be27
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 10:24:54 2023 -0600

    cleanup reedsolomon

commit 132e27ed1468f5c1ce998516ff5a00687217c3e6
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 09:52:04 2023 -0600

    make MathUtils more generic

commit d8352b7fff4cb4f9ecc8e8a969710d26ef410e8b
Author: Henry Schimke <henry@azule.info>
Date:   Sun Feb 5 09:29:33 2023 -0600

    cleanup some string handling in ResultParser

    There may still be some edge cases where multi-byte characters break the parser
This commit is contained in:
Henry
2023-02-10 20:41:25 -06:00
parent 5222bb25b7
commit 28723be2eb
180 changed files with 3248 additions and 4117 deletions

View File

@@ -185,7 +185,7 @@ pub fn encode_bytes_with_charset(
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);
stuffed_bits = stuffBits(&bits, word_size as usize)?;
if stuffed_bits.getSize() as u32 + ecc_bits > usable_bits_in_layers {
return Err(Exceptions::IllegalArgumentException(Some(
"Data to large for user specified layer".to_owned(),
@@ -222,7 +222,7 @@ pub fn encode_bytes_with_charset(
// wordSize has changed
if stuffed_bits.getSize() == 0 || word_size != WORD_SIZE[layers as usize] {
word_size = WORD_SIZE[layers as usize];
stuffed_bits = stuffBits(&bits, word_size as usize);
stuffed_bits = stuffBits(&bits, word_size as usize)?;
}
let usable_bits_in_layers =
total_bits_in_layer_var - (total_bits_in_layer_var % word_size);
@@ -267,7 +267,7 @@ pub fn encode_bytes_with_charset(
alignmentMap[origCenter + i] = center + newOffset + 1;
}
}
let mut matrix = BitMatrix::with_single_dimension(matrixSize);
let mut matrix = BitMatrix::with_single_dimension(matrixSize)?;
// dbg!(matrix.to_string());
@@ -307,13 +307,9 @@ pub fn encode_bytes_with_charset(
rowOffset += rowSize * 8;
}
// dbg!(matrix.to_string());
// draw mode message
drawModeMessage(&mut matrix, compact, matrixSize, modeMessage);
// dbg!(matrix.to_string());
// draw alignment marks
if compact {
drawBullsEye(&mut matrix, matrixSize / 2, 5);
@@ -385,20 +381,12 @@ pub fn generateModeMessage(
) -> Result<BitArray, Exceptions> {
let mut mode_message = BitArray::new();
if compact {
mode_message
.appendBits(layers - 1, 2)
.expect("should append");
mode_message
.appendBits(messageSizeInWords - 1, 6)
.expect("should append");
mode_message.appendBits(layers - 1, 2)?;
mode_message.appendBits(messageSizeInWords - 1, 6)?;
mode_message = generateCheckWords(&mode_message, 28, 4)?;
} else {
mode_message
.appendBits(layers - 1, 5)
.expect("should append");
mode_message
.appendBits(messageSizeInWords - 1, 11)
.expect("should append");
mode_message.appendBits(layers - 1, 5)?;
mode_message.appendBits(messageSizeInWords - 1, 11)?;
mode_message = generateCheckWords(&mode_message, 40, 4)?;
}
Ok(mode_message)
@@ -407,7 +395,7 @@ pub fn generateModeMessage(
fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
let center = matrixSize / 2;
if compact {
for i in 0..7usize {
for i in 0..7_usize {
// for (int i = 0; i < 7; i++) {
let offset = (center as usize - 3 + i) as u32;
if modeMessage.get(i) {
@@ -424,7 +412,7 @@ fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeM
}
}
} else {
for i in 0..10usize {
for i in 0..10_usize {
// for (int i = 0; i < 10; i++) {
let offset = (center as usize - 5 + i + i / 5) as u32;
if modeMessage.get(i) {
@@ -450,13 +438,13 @@ fn generateCheckWords(
) -> Result<BitArray, Exceptions> {
// bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
let message_size_in_words = bitArray.getSize() / wordSize;
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?);
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?)?;
let total_words = totalBits / wordSize;
let mut message_words = bitsToWords(bitArray, wordSize, total_words);
rs.encode(&mut message_words, total_words - message_size_in_words)?;
let start_pad = totalBits % wordSize;
let mut message_bits = BitArray::new();
message_bits.appendBits(0, start_pad).expect("must append");
message_bits.appendBits(0, start_pad)?;
for message_word in message_words {
// for (int messageWord : messageWords) {
message_bits.appendBits(message_word as u32, wordSize)?
@@ -498,23 +486,9 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
"Unsupported word size {wordSize}"
)))),
}
// switch (wordSize) {
// case 4:
// return GenericGF.AZTEC_PARAM;
// case 6:
// return GenericGF.AZTEC_DATA_6;
// case 8:
// return GenericGF.AZTEC_DATA_8;
// case 10:
// return GenericGF.AZTEC_DATA_10;
// case 12:
// return GenericGF.AZTEC_DATA_12;
// default:
// throw new IllegalArgumentException("Unsupported word size " + wordSize);
// }
}
pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray, Exceptions> {
let mut out = BitArray::new();
let n = bits.getSize() as isize;
@@ -530,21 +504,20 @@ pub fn stuffBits(bits: &BitArray, word_size: usize) -> BitArray {
}
}
if (word & mask) == mask {
out.appendBits(word & mask, word_size).unwrap();
out.appendBits(word & mask, word_size)?;
i -= 1;
} else if (word & mask) == 0 {
out.appendBits(word | 1, word_size).unwrap();
out.appendBits(word | 1, word_size)?;
i -= 1;
} else {
out.appendBits(word, word_size).unwrap();
out.appendBits(word, word_size)?;
}
i += word_size as isize;
}
out
Ok(out)
}
fn total_bits_in_layer(layers: u32, compact: bool) -> u32 {
((if compact { 88 } else { 112 }) + 16 * layers) * layers
// return ((compact ? 88 : 112) + 16 * layers) * layers;
}

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::common::BitArray;
use crate::{common::BitArray, Exceptions};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BinaryShiftToken {
@@ -32,28 +32,27 @@ impl BinaryShiftToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
let bsbc = self.binary_shift_byte_count as usize;
for i in 0..bsbc {
// for (int i = 0; i < bsbc; i++) {
if i == 0 || (i == 31 && bsbc <= 62) {
// We need a header before the first character, and before
// character 31 when the total byte code is <= 62
bit_array.appendBits(31, 5).unwrap(); // BINARY_SHIFT
bit_array.appendBits(31, 5)?; // BINARY_SHIFT
if bsbc > 62 {
bit_array.appendBits(bsbc as u32 - 31, 16).unwrap();
bit_array.appendBits(bsbc as u32 - 31, 16)?;
} else if i == 0 {
// 1 <= binaryShiftByteCode <= 62
bit_array.appendBits(bsbc.min(31) as u32, 5).unwrap();
bit_array.appendBits(bsbc.min(31) as u32, 5)?;
} else {
// 32 <= binaryShiftCount <= 62 and i == 31
bit_array.appendBits(bsbc as u32 - 31, 5).unwrap();
bit_array.appendBits(bsbc as u32 - 31, 5)?;
}
}
bit_array
.appendBits(text[self.binary_shift_start as usize + i].into(), 8)
.expect("should never fail to append");
bit_array.appendBits(text[self.binary_shift_start as usize + i].into(), 8)?;
}
Ok(())
}
// @Override

View File

@@ -324,7 +324,7 @@ impl HighLevelEncoder {
// Convert it to a bit array, and return.
// dbg!(min_state.clone().toBitArray(&self.text).to_string());
Ok(min_state.toBitArray(&self.text))
min_state.toBitArray(&self.text)
}
// We update a set of states for a new character by updating each state

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::common::BitArray;
use crate::{common::BitArray, Exceptions};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SimpleToken {
@@ -33,10 +33,8 @@ impl SimpleToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) {
bit_array
.appendBits(self.value as u32, self.bit_count as usize)
.expect("append should never fail");
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) -> Result<(), Exceptions> {
bit_array.appendBits(self.value as u32, self.bit_count as usize)
}
// @Override

View File

@@ -85,9 +85,11 @@ impl State {
)));
// throw new IllegalArgumentException("ECI code must be between 0 and 999999");
} else {
let eci_digits = encoding::all::ISO_8859_1
let Ok(eci_digits) = encoding::all::ISO_8859_1
.encode(&format!("{eci}"), encoding::EncoderTrap::Strict)
.unwrap();
else {
return Err(Exceptions::IllegalArgumentException(None))
};
// let eciDigits = Integer.toString(eci).getBytes(StandardCharsets.ISO_8859_1);
token.add(eci_digits.len() as i32, 3); // 1-6: number of ECI digits
for eci_digit in &eci_digits {
@@ -205,7 +207,7 @@ impl State {
new_mode_bit_count <= other.bit_count
}
pub fn toBitArray(self, text: &[u8]) -> BitArray {
pub fn toBitArray(self, text: &[u8]) -> Result<BitArray, Exceptions> {
let mut symbols = Vec::new();
let tok = self.endBinaryShift(text.len() as u32).token;
for tkn in tok.into_iter() {
@@ -223,22 +225,22 @@ impl State {
for symbol in symbols.into_iter().rev() {
// for i in (0..symbols.len()).rev() {
// for (int i = symbols.size() - 1; i >= 0; i--) {
symbol.appendTo(&mut bit_array, text);
symbol.appendTo(&mut bit_array, text)?;
}
bit_array
Ok(bit_array)
}
#[inline(always)]
fn calculate_binary_shift_cost(binary_shift_byte_count: u32) -> u32 {
if binary_shift_byte_count > 62 {
return 21; // B/S with extended length
21 // B/S with extended length
} else if binary_shift_byte_count > 31 {
20 // two B/S
} else if binary_shift_byte_count > 0 {
10 // one B/S
} else {
0
}
if binary_shift_byte_count > 31 {
return 20; // two B/S
}
if binary_shift_byte_count > 0 {
return 10; // one B/S
}
0
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
use crate::common::BitArray;
use crate::{common::BitArray, Exceptions};
use super::{BinaryShiftToken, SimpleToken};
@@ -26,12 +26,14 @@ pub enum TokenType {
}
impl TokenType {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
// let token = &self.tokens[self.current_pointer];
match self {
TokenType::Simple(a) => a.appendTo(bit_array, text),
TokenType::BinaryShift(a) => a.appendTo(bit_array, text),
TokenType::Empty => panic!("cannot appendTo on Empty final item"),
TokenType::Empty => Err(Exceptions::IllegalStateException(Some(String::from(
"cannot appendTo on Empty final item",
)))),
}
}
}
@@ -95,27 +97,6 @@ impl IntoIterator for Token {
}
}
// pub enum Token{
// Simple(Rc<SimpleToken>),
// BinaryShift(),
// Empty,
// }
// pub trait TokenTrait {
// fn getPrevious(&self)->&Token;
// fn add(&self, value: i32, bitCount: u32) -> Token{
// Token::Simple(Rc::new(SimpleToken::new(self, value, bitCount)))
// }
// fn addBinaryShift(&self, start: i32, byteCount: u32) -> &Token; //{
// // //int bitCount = (byteCount * 8) + (byteCount <= 31 ? 10 : byteCount <= 62 ? 20 : 21);
// // return new BinaryShiftToken(this, start, byteCount);
// // }
// fn appendTo(&self, bitArray: BitArray, text: &[u8]);
// }
impl Default for Token {
fn default() -> Self {
Self::new()