Merge branch 'main' into pr/exception_helpers

This commit is contained in:
Vukašin Stepanović
2023-02-16 07:15:51 +00:00
161 changed files with 900 additions and 933 deletions

View File

@@ -14,7 +14,10 @@
* limitations under the License.
*/
use crate::{common::BitMatrix, Exceptions};
use crate::{
common::{BitMatrix, Result},
Exceptions,
};
use super::{Version, VersionRef};
@@ -31,7 +34,7 @@ impl BitMatrixParser {
* @param bitMatrix {@link BitMatrix} to parse
* @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2
*/
pub fn new(bitMatrix: &BitMatrix) -> Result<Self, Exceptions> {
pub fn new(bitMatrix: &BitMatrix) -> Result<Self> {
let dimension = bitMatrix.getHeight();
if !(8..=144).contains(&dimension) || (dimension & 0x01) != 0 {
return Err(Exceptions::format);
@@ -64,7 +67,7 @@ impl BitMatrixParser {
* @throws FormatException if the dimensions of the mapping matrix are not valid
* Data Matrix dimensions.
*/
fn readVersion(bitMatrix: &BitMatrix) -> Result<VersionRef, Exceptions> {
fn readVersion(bitMatrix: &BitMatrix) -> Result<VersionRef> {
let numRows = bitMatrix.getHeight();
let numColumns = bitMatrix.getWidth();
Version::getVersionForDimensions(numRows, numColumns)
@@ -78,7 +81,7 @@ impl BitMatrixParser {
* @return bytes encoded within the Data Matrix Code
* @throws FormatException if the exact number of bytes expected is not read
*/
pub fn readCodewords(&mut self) -> Result<Vec<u8>, Exceptions> {
pub fn readCodewords(&mut self) -> Result<Vec<u8>> {
let mut result = vec![0u8; self.version.getTotalCodewords() as usize];
let mut resultOffset = 0;
@@ -447,10 +450,7 @@ impl BitMatrixParser {
* @param bitMatrix Original {@link BitMatrix} with alignment patterns
* @return BitMatrix that has the alignment patterns removed
*/
fn extractDataRegion(
bitMatrix: &BitMatrix,
version: VersionRef,
) -> Result<BitMatrix, Exceptions> {
fn extractDataRegion(bitMatrix: &BitMatrix, version: VersionRef) -> Result<BitMatrix> {
// dbg!(bitMatrix.to_string());
let symbolSizeRows = version.getSymbolSizeRows();
let symbolSizeColumns = version.getSymbolSizeColumns();

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
use crate::common::Result;
use crate::Exceptions;
use super::Version;
@@ -52,7 +53,7 @@ impl DataBlock {
rawCodewords: &[u8],
version: &Version,
fix259: bool,
) -> Result<Vec<DataBlock>, Exceptions> {
) -> Result<Vec<DataBlock>> {
// Figure out the number and size of data blocks used by this version
let ecBlocks = version.getECBlocks();

View File

@@ -14,12 +14,9 @@
* limitations under the License.
*/
use crate::{
common::{
reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
BitMatrix, DecoderRXingResult,
},
Exceptions,
use crate::common::{
reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
BitMatrix, DecoderRXingResult, Result,
};
use super::{decoded_bit_stream_parser, BitMatrixParser, DataBlock};
@@ -49,7 +46,7 @@ impl Decoder {
* @throws FormatException if the Data Matrix Code cannot be decoded
* @throws ChecksumException if error correction fails
*/
pub fn decode(&self, bits: &BitMatrix) -> Result<DecoderRXingResult, Exceptions> {
pub fn decode(&self, bits: &BitMatrix) -> Result<DecoderRXingResult> {
let decoded = self.perform_decode(bits, false, false);
if decoded.is_ok() {
return decoded;
@@ -58,7 +55,7 @@ impl Decoder {
self.perform_decode(&Self::flip_bitmatrix(bits)?, false, true)
}
fn flip_bitmatrix(bits: &BitMatrix) -> Result<BitMatrix, Exceptions> {
fn flip_bitmatrix(bits: &BitMatrix) -> Result<BitMatrix> {
let mut res = BitMatrix::new(bits.getHeight(), bits.getWidth())?;
for y in 0..res.getHeight() {
for x in 0..res.getWidth() {
@@ -84,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, Exceptions> {
pub fn decode_bools(&self, image: &Vec<Vec<bool>>) -> Result<DecoderRXingResult> {
self.perform_decode(&BitMatrix::parse_bools(image), false, false)
}
@@ -102,7 +99,7 @@ impl Decoder {
bits: &BitMatrix,
fix259: bool,
is_flipped: bool,
) -> Result<DecoderRXingResult, Exceptions> {
) -> Result<DecoderRXingResult> {
// Construct a parser and read version, error-correction level
let mut parser = BitMatrixParser::new(bits)?;
@@ -153,11 +150,7 @@ impl Decoder {
* @param numDataCodewords number of codewords that are data bytes
* @throws ChecksumException if error correction fails
*/
fn correctErrors(
&self,
codewordBytes: &mut [u8],
numDataCodewords: u32,
) -> Result<(), Exceptions> {
fn correctErrors(&self, codewordBytes: &mut [u8], numDataCodewords: u32) -> Result<()> {
let _numCodewords = codewordBytes.len();
// First read into an array of ints
// let codewordsInts = vec![0i32;numCodewords];

View File

@@ -17,7 +17,7 @@
use encoding::Encoding;
use crate::{
common::{BitSource, DecoderRXingResult, ECIStringBuilder},
common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result},
Exceptions,
};
@@ -110,7 +110,7 @@ const INSERT_STRING_CONST: &str = "\u{001E}\u{0004}";
const VALUE_236: &str = "[)>\u{001E}05\u{001D}";
const VALUE_237: &str = "[)>\u{001E}06\u{001D}";
pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult, Exceptions> {
pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult> {
let mut bits = BitSource::new(bytes.to_vec());
let mut result = ECIStringBuilder::with_capacity(100);
let mut resultTrailer = String::new();
@@ -217,7 +217,7 @@ fn decodeAsciiSegment(
resultTrailer: &mut String,
fnc1positions: &mut Vec<usize>,
is_gs1: &mut bool,
) -> Result<Mode, Exceptions> {
) -> Result<Mode> {
let mut upperShift = false;
let mut firstFNC1Position = 1;
let mut firstCodeword = true;
@@ -351,7 +351,7 @@ fn decodeC40Segment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
fnc1positions: &mut Vec<usize>,
) -> Result<(), Exceptions> {
) -> Result<()> {
// Three C40 values are encoded in a 16-bit value as
// (1600 * C1) + (40 * C2) + C3 + 1
// TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
@@ -460,7 +460,7 @@ fn decodeTextSegment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
fnc1positions: &mut Vec<usize>,
) -> Result<(), Exceptions> {
) -> Result<()> {
// Three Text values are encoded in a 16-bit value as
// (1600 * C1) + (40 * C2) + C3 + 1
// TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
@@ -573,10 +573,7 @@ fn decodeTextSegment(
/**
* See ISO 16022:2006, 5.2.7
*/
fn decodeAnsiX12Segment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
) -> Result<(), Exceptions> {
fn decodeAnsiX12Segment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<()> {
// Three ANSI X12 values are encoded in a 16-bit value as
// (1600 * C1) + (40 * C2) + C3 + 1
@@ -656,10 +653,7 @@ fn parseTwoBytes(firstByte: u32, secondByte: u32, result: &mut [u32]) {
/**
* See ISO 16022:2006, 5.2.8 and Annex C Table C.3
*/
fn decodeEdifactSegment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
) -> Result<(), Exceptions> {
fn decodeEdifactSegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<()> {
loop {
// If there is only two or less bytes left then it will be encoded as ASCII
if bits.available() <= 16 {
@@ -703,7 +697,7 @@ fn decodeBase256Segment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
byteSegments: &mut Vec<Vec<u8>>,
) -> Result<(), Exceptions> {
) -> Result<()> {
// Figure out how long the Base 256 Segment is.
let mut codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
let d1 = unrandomize255State(bits.readBits(8)?, codewordPosition);
@@ -748,10 +742,7 @@ fn decodeBase256Segment(
/**
* See ISO 16022:2007, 5.4.1
*/
fn decodeECISegment(
bits: &mut BitSource,
result: &mut ECIStringBuilder,
) -> Result<bool, Exceptions> {
fn decodeECISegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<bool> {
let firstByte = bits.readBits(8)?;
if firstByte <= 127 {
result.appendECI(firstByte - 1)?;
@@ -773,10 +764,7 @@ fn decodeECISegment(
/**
* See ISO 16022:2006, 5.6
*/
fn parse_structured_append(
bits: &mut BitSource,
sai: &mut StructuredAppendInfo,
) -> Result<(), Exceptions> {
fn parse_structured_append(bits: &mut BitSource, sai: &mut StructuredAppendInfo) -> Result<()> {
// 5.6.2 Table 8
let symbolSequenceIndicator = bits.readBits(8)?;
sai.index = (symbolSequenceIndicator >> 4) as i32;

View File

@@ -17,6 +17,7 @@
use core::fmt;
use once_cell::sync::Lazy;
use crate::common::Result;
use crate::Exceptions;
static VERSIONS: Lazy<Vec<Version>> = Lazy::new(Version::buildVersions);
@@ -100,10 +101,7 @@ impl Version {
* @return Version for a Data Matrix Code of those dimensions
* @throws FormatException if dimensions do correspond to a valid Data Matrix size
*/
pub fn getVersionForDimensions(
numRows: u32,
numColumns: u32,
) -> Result<&'static Version, Exceptions> {
pub fn getVersionForDimensions(numRows: u32, numColumns: u32) -> Result<&'static Version> {
if (numRows & 0x01) != 0 || (numColumns & 0x01) != 0 {
return Err(Exceptions::format);
}