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

@@ -30,7 +30,10 @@
use once_cell::sync::Lazy;
use regex::Regex;
use crate::{common::BitArray, Exceptions};
use crate::{
common::{BitArray, Result},
Exceptions,
};
static ONE: Lazy<Regex> = Lazy::new(|| Regex::new("1").unwrap());
static ZERO: Lazy<Regex> = Lazy::new(|| Regex::new("0").unwrap());
@@ -39,7 +42,7 @@ static SPACE: Lazy<Regex> = Lazy::new(|| Regex::new(" ").unwrap());
/*
* Constructs a BitArray from a String like the one returned from BitArray.toString()
*/
pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
pub fn buildBitArrayFromString(data: &str) -> Result<BitArray> {
let dotsAndXs = ZERO
.replace_all(&ONE.replace_all(data, "X"), ".")
.to_string();
@@ -65,7 +68,7 @@ pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
Ok(binary)
}
pub fn buildBitArrayFromStringWithoutSpaces(data: &str) -> Result<BitArray, Exceptions> {
pub fn buildBitArrayFromStringWithoutSpaces(data: &str) -> Result<BitArray> {
let mut sb = String::new();
// let dotsAndXs = ZERO.matcher(ONE.matcher(data).replaceAll("X")).replaceAll(".");

View File

@@ -24,7 +24,10 @@
* http://www.piramidepse.com/
*/
use crate::{common::BitArray, Exceptions};
use crate::{
common::{BitArray, Result},
Exceptions,
};
use super::{
AI013103decoder, AI01320xDecoder, AI01392xDecoder, AI01393xDecoder, AI013x0x1xDecoder,
@@ -53,14 +56,14 @@ pub trait AbstractExpandedDecoder {
// return generalDecoder;
// }
fn parseInformation(&mut self) -> Result<String, Exceptions>;
fn parseInformation(&mut self) -> Result<String>;
fn getGeneralDecoder(&self) -> &GeneralAppIdDecoder;
// fn new(information:&BitArray) -> Self where Self:Sized;
}
pub fn createDecoder<'a>(
information: &'a BitArray,
) -> Result<Box<dyn AbstractExpandedDecoder + 'a>, Exceptions> {
) -> Result<Box<dyn AbstractExpandedDecoder + 'a>> {
if information.get(1) {
return Ok(Box::new(AI01AndOtherAIs::new(information)));
}

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI013x0xDecoder, AI01decoder, AI01weightDecoder, AbstractExpandedDecoder};
@@ -43,7 +43,7 @@ impl AI01weightDecoder for AI013103decoder<'_> {
}
}
impl AbstractExpandedDecoder for AI013103decoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
self.0.parseInformation()
}

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI013x0xDecoder, AI01decoder, AI01weightDecoder, AbstractExpandedDecoder};
@@ -43,7 +43,7 @@ impl AI01weightDecoder for AI01320xDecoder<'_> {
}
}
impl AbstractExpandedDecoder for AI01320xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
self.0.parseInformation()
}

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -37,7 +37,7 @@ pub struct AI01392xDecoder<'a> {
}
impl AI01decoder for AI01392xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::notFound);
}

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -37,7 +37,7 @@ pub struct AI01393xDecoder<'a> {
}
impl AI01decoder for AI01393xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01393xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::notFound);
}

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI01decoder, AI01weightDecoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -53,7 +53,7 @@ impl AI01weightDecoder for AI013x0x1xDecoder<'_> {
}
impl AI01decoder for AI013x0x1xDecoder<'_> {}
impl AbstractExpandedDecoder for AI013x0x1xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE + Self::DATE_SIZE
{

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI01decoder, AI01weightDecoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -49,7 +49,7 @@ impl AI01weightDecoder for AI013x0xDecoder<'_> {
}
}
impl AbstractExpandedDecoder for AI013x0xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
if self.information.getSize()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE
{

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -35,7 +35,7 @@ use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
pub struct AI01AndOtherAIs<'a>(&'a BitArray, GeneralAppIdDecoder<'a>);
impl AI01decoder for AI01AndOtherAIs<'_> {}
impl AbstractExpandedDecoder for AI01AndOtherAIs<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
let mut buff = String::new();
buff.push_str("(01)");

View File

@@ -24,7 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::BitArray;
use crate::common::{BitArray, Result};
use super::{AbstractExpandedDecoder, GeneralAppIdDecoder};
@@ -37,7 +37,7 @@ pub struct AnyAIDecoder<'a> {
general_decoder: GeneralAppIdDecoder<'a>,
}
impl AbstractExpandedDecoder for AnyAIDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
fn parseInformation(&mut self) -> Result<String> {
let buf = String::new();
self.general_decoder.decodeAllCodes(buf, Self::HEADER_SIZE)
}

View File

@@ -24,6 +24,7 @@
* http://www.piramidepse.com/
*/
use crate::common::Result;
use crate::Exceptions;
use super::DecodedObject;
@@ -45,7 +46,7 @@ impl DecodedObject for DecodedNumeric {
impl DecodedNumeric {
pub const FNC1: u32 = 10;
pub fn new(newPosition: usize, firstDigit: u32, secondDigit: u32) -> Result<Self, Exceptions> {
pub fn new(newPosition: usize, firstDigit: u32, secondDigit: u32) -> Result<Self> {
// super(newPosition);
if

View File

@@ -29,6 +29,7 @@
*/
use std::collections::HashMap;
use crate::common::Result;
use crate::Exceptions;
use once_cell::sync::Lazy;
@@ -137,7 +138,7 @@ static FOUR_DIGIT_DATA_LENGTH: Lazy<HashMap<String, DataLength>> = Lazy::new(||
hm
});
pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Exceptions> {
pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String> {
if rawInformation.is_empty() {
return Ok(String::default());
}
@@ -194,11 +195,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
Err(Exceptions::notFound)
}
fn processFixedAI(
aiSize: usize,
fieldSize: usize,
rawInformation: &str,
) -> Result<String, Exceptions> {
fn processFixedAI(aiSize: usize, fieldSize: usize, rawInformation: &str) -> Result<String> {
if rawInformation.chars().count() < aiSize {
return Err(Exceptions::notFound);
}
@@ -229,7 +226,7 @@ fn processVariableAI(
aiSize: usize,
variableFieldSize: usize,
rawInformation: &str,
) -> Result<String, Exceptions> {
) -> Result<String> {
let ai: String = rawInformation.chars().take(aiSize).collect();
let maxSize = rawInformation
.chars()

View File

@@ -24,7 +24,10 @@
* http://www.piramidepse.com/
*/
use crate::{common::BitArray, Exceptions};
use crate::{
common::{BitArray, Result},
Exceptions,
};
use super::{
field_parser, BlockParsedRXingResult, CurrentParsingState, DecodedChar, DecodedInformation,
@@ -50,11 +53,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
}
pub fn decodeAllCodes(
&mut self,
buff: String,
initialPosition: usize,
) -> Result<String, Exceptions> {
pub fn decodeAllCodes(&mut self, buff: String, initialPosition: usize) -> Result<String> {
let mut buff = buff;
let mut currentPosition = initialPosition;
let mut remaining = String::default();
@@ -97,7 +96,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.information.get(pos + 3)
}
fn decodeNumeric(&self, pos: usize) -> Result<DecodedNumeric, Exceptions> {
fn decodeNumeric(&self, pos: usize) -> Result<DecodedNumeric> {
if pos + 7 > self.information.getSize() {
let numeric = self.extractNumericValueFromBitArray(pos, 4);
if numeric == 0 {
@@ -144,7 +143,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
&mut self,
pos: usize,
remaining: &str,
) -> Result<DecodedInformation, Exceptions> {
) -> Result<DecodedInformation> {
self.buffer.clear();
if !remaining.is_empty() {
@@ -168,7 +167,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
))
}
fn parseBlocks(&mut self) -> Result<DecodedInformation, Exceptions> {
fn parseBlocks(&mut self) -> Result<DecodedInformation> {
let mut isFinished;
let mut result: BlockParsedRXingResult;
loop {
@@ -203,7 +202,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
}
}
fn parseNumericBlock(&mut self) -> Result<BlockParsedRXingResult, Exceptions> {
fn parseNumericBlock(&mut self) -> Result<BlockParsedRXingResult> {
while self.isStillNumeric(self.current.getPosition()) {
let numeric = self.decodeNumeric(self.current.getPosition())?;
self.current.setPosition(numeric.getNewPosition());
@@ -244,7 +243,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
Ok(BlockParsedRXingResult::new())
}
fn parseIsoIec646Block(&mut self) -> Result<BlockParsedRXingResult, Exceptions> {
fn parseIsoIec646Block(&mut self) -> Result<BlockParsedRXingResult> {
while self.isStillIsoIec646(self.current.getPosition()) {
let iso = self.decodeIsoIec646(self.current.getPosition())?;
self.current.setPosition(iso.getNewPosition());
@@ -275,7 +274,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
Ok(BlockParsedRXingResult::new())
}
fn parseAlphaBlock(&mut self) -> Result<BlockParsedRXingResult, Exceptions> {
fn parseAlphaBlock(&mut self) -> Result<BlockParsedRXingResult> {
while self.isStillAlpha(self.current.getPosition()) {
let alpha = self.decodeAlphanumeric(self.current.getPosition())?;
self.current.setPosition(alpha.getNewPosition());
@@ -336,7 +335,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
(232..253).contains(&eightBitValue)
}
fn decodeIsoIec646(&self, pos: usize) -> Result<DecodedChar, Exceptions> {
fn decodeIsoIec646(&self, pos: usize) -> Result<DecodedChar> {
let fiveBitValue = self.extractNumericValueFromBitArray(pos, 5);
if fiveBitValue == 15 {
return Ok(DecodedChar::new(pos + 5, DecodedChar::FNC1));
@@ -414,7 +413,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
(16..63).contains(&sixBitValue) // 63 not included
}
fn decodeAlphanumeric(&self, pos: usize) -> Result<DecodedChar, Exceptions> {
fn decodeAlphanumeric(&self, pos: usize) -> Result<DecodedChar> {
let fiveBitValue = self.extractNumericValueFromBitArray(pos, 5);
if fiveBitValue == 15 {
return Ok(DecodedChar::new(pos + 5, DecodedChar::FNC1));

View File

@@ -27,7 +27,7 @@
use std::collections::HashMap;
use crate::{
common::BitArray,
common::{BitArray, Result},
oned::{
recordPattern, recordPatternInReverse,
rss::{
@@ -156,7 +156,7 @@ impl OneDReader for RSSExpandedReader {
rowNumber: u32,
row: &crate::common::BitArray,
_hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult, crate::Exceptions> {
) -> Result<crate::RXingResult> {
// Rows can start with even pattern in case in prev rows there where odd number of patters.
// So lets try twice
self.pairs.clear();
@@ -173,10 +173,7 @@ impl OneDReader for RSSExpandedReader {
}
}
impl Reader for RSSExpandedReader {
fn decode(
&mut self,
image: &mut crate::BinaryBitmap,
) -> Result<crate::RXingResult, Exceptions> {
fn decode(&mut self, image: &mut crate::BinaryBitmap) -> Result<crate::RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
@@ -185,7 +182,7 @@ impl Reader for RSSExpandedReader {
&mut self,
image: &mut crate::BinaryBitmap,
hints: &DecodingHintDictionary,
) -> Result<crate::RXingResult, Exceptions> {
) -> Result<crate::RXingResult> {
if let Ok(res) = self.doDecode(image, hints) {
Ok(res)
} else {
@@ -289,7 +286,7 @@ impl RSSExpandedReader {
&mut self,
rowNumber: u32,
row: &BitArray,
) -> Result<Vec<ExpandedPair>, Exceptions> {
) -> Result<Vec<ExpandedPair>> {
let mut done = false;
while !done {
let previousPairs = self.pairs.clone();
@@ -370,7 +367,7 @@ impl RSSExpandedReader {
&mut self,
collectedRows: &mut Vec<ExpandedRow>,
currentRow: usize,
) -> Result<Vec<ExpandedPair>, Exceptions> {
) -> Result<Vec<ExpandedPair>> {
for i in currentRow..self.rows.len() {
// for (int i = currentRow; i < rows.size(); i++) {
let row = self.rows.get(i).ok_or(Exceptions::indexOutOfBounds)?;
@@ -530,9 +527,9 @@ impl RSSExpandedReader {
}
// Not private for unit testing
pub(crate) fn constructRXingResult(pairs: &[ExpandedPair]) -> Result<RXingResult, Exceptions> {
let binary =
bit_array_builder::buildBitArray(&pairs.to_vec()).ok_or(Exceptions::illegalState)?;
pub(crate) fn constructRXingResult(pairs: &[ExpandedPair]) -> Result<RXingResult> {
let binary = bit_array_builder::buildBitArray(&pairs.to_vec())
.ok_or(Exceptions::IllegalStateException(None))?;
let mut decoder = abstract_expanded_decoder::createDecoder(&binary)?;
let resultingString = decoder.parseInformation()?;
@@ -620,7 +617,7 @@ impl RSSExpandedReader {
row: &BitArray,
previousPairs: &[ExpandedPair],
rowNumber: u32,
) -> Result<ExpandedPair, Exceptions> {
) -> Result<ExpandedPair> {
let mut isOddPattern = previousPairs.len() % 2 == 0;
if self.startFromEven {
isOddPattern = !isOddPattern;
@@ -679,7 +676,7 @@ impl RSSExpandedReader {
row: &BitArray,
previousPairs: &[ExpandedPair],
forcedOffset: i32,
) -> Result<(), Exceptions> {
) -> Result<()> {
let counters = &mut self.decodeFinderCounters;
counters.fill(0);
// counters[0] = 0;
@@ -819,7 +816,7 @@ impl RSSExpandedReader {
pattern: &FinderPattern,
isOddPattern: bool,
leftChar: bool,
) -> Result<DataCharacter, Exceptions> {
) -> Result<DataCharacter> {
let counters = &mut self.dataCharacterCounters;
counters.fill(0);
@@ -923,7 +920,7 @@ impl RSSExpandedReader {
!(pattern.getValue() == 0 && isOddPattern && leftChar)
}
fn adjustOddEvenCounts(&mut self, numModules: u32) -> Result<(), Exceptions> {
fn adjustOddEvenCounts(&mut self, numModules: u32) -> Result<()> {
let oddSum = self.oddCounts.iter().sum::<u32>();
let evenSum = self.evenCounts.iter().sum::<u32>();

View File

@@ -24,7 +24,8 @@
* http://www.piramidepse.com/
*/
use crate::{oned::rss::expanded::ExpandedPair, Exceptions, Reader};
use crate::common::Result;
use crate::{oned::rss::expanded::ExpandedPair, Reader};
use super::{test_case_util, RSSExpandedReader};
@@ -43,7 +44,7 @@ fn testDecodingRowByRow() {
// let tester = ;
assert!(|| -> Result<Vec<ExpandedPair>, Exceptions> {
assert!(|| -> Result<Vec<ExpandedPair>> {
rssExpandedReader.decodeRow2pairs(firstRowNumber as u32, &firstRow)
// fail(NotFoundException.class.getName() + " expected");
}()