refactor to use exception helper factories

This commit is contained in:
Vukašin Stepanović
2023-02-15 10:46:13 +00:00
parent 3e27279dc8
commit 722ce78fd0
132 changed files with 966 additions and 1132 deletions

View File

@@ -38,7 +38,7 @@ pub trait AbstractRSSReaderTrait: OneDReader {
return Ok(value as u32);
}
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
/**

View File

@@ -50,23 +50,13 @@ pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
// for (int i = 0; i < dotsAndXs.length(); ++i) {
if i % 9 == 0 {
// spaces
if dotsAndXs
.chars()
.nth(i)
.ok_or(Exceptions::ParseException(None))?
!= ' '
{
return Err(Exceptions::IllegalStateException(Some(
"space expected".to_owned(),
)));
if dotsAndXs.chars().nth(i).ok_or(Exceptions::parseEmpty())? != ' ' {
return Err(Exceptions::illegalState("space expected".to_owned()));
}
continue;
}
let currentChar = dotsAndXs
.chars()
.nth(i)
.ok_or(Exceptions::ParseException(None))?;
let currentChar = dotsAndXs.chars().nth(i).ok_or(Exceptions::parseEmpty())?;
if currentChar == 'X' || currentChar == 'x' {
binary.set(counter);
}
@@ -92,7 +82,7 @@ pub fn buildBitArrayFromStringWithoutSpaces(data: &str) -> Result<BitArray, Exce
dotsAndXs
.chars()
.nth(current)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
current += 1;

View File

@@ -149,7 +149,7 @@ pub fn createDecoder<'a>(
_ => {}
}
Err(Exceptions::IllegalStateException(Some(format!(
Err(Exceptions::illegalState(format!(
"unknown decoder: {information}"
))))
)))
}

View File

@@ -39,7 +39,7 @@ impl AI01decoder for AI01392xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::NotFoundException(None));
return Err(crate::Exceptions::notFoundEmpty());
}
let mut buf = String::new();

View File

@@ -39,7 +39,7 @@ impl AI01decoder for AI01393xDecoder<'_> {}
impl AbstractExpandedDecoder for AI01393xDecoder<'_> {
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
return Err(crate::Exceptions::NotFoundException(None));
return Err(crate::Exceptions::notFoundEmpty());
}
let mut buf = String::new();

View File

@@ -57,7 +57,7 @@ impl AbstractExpandedDecoder for AI013x0x1xDecoder<'_> {
if self.information.getSize()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE + Self::DATE_SIZE
{
return Err(crate::Exceptions::NotFoundException(None));
return Err(crate::Exceptions::notFoundEmpty());
}
let mut buf = String::new();

View File

@@ -53,7 +53,7 @@ impl AbstractExpandedDecoder for AI013x0xDecoder<'_> {
if self.information.getSize()
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE
{
return Err(crate::Exceptions::NotFoundException(None));
return Err(crate::Exceptions::notFoundEmpty());
}
let mut buf = String::new();

View File

@@ -51,7 +51,7 @@ impl DecodedNumeric {
if
/*firstDigit < 0 ||*/
firstDigit > 10 || /*secondDigit < 0 ||*/ secondDigit > 10 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
Ok(Self {

View File

@@ -145,7 +145,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
// Processing 2-digit AIs
if rawInformation.chars().count() < 2 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let lookup: String = rawInformation.chars().take(2).collect();
@@ -158,7 +158,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
}
if rawInformation.chars().count() < 3 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let firstThreeDigits: String = rawInformation.chars().take(3).collect();
@@ -171,7 +171,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
}
if rawInformation.chars().count() < 4 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let threeDigitPlusDigitDataLength = THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH.get(&firstThreeDigits);
@@ -191,7 +191,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
return processFixedAI(4, ffdl.length, rawInformation);
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
fn processFixedAI(
@@ -200,13 +200,13 @@ fn processFixedAI(
rawInformation: &str,
) -> Result<String, Exceptions> {
if rawInformation.chars().count() < aiSize {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let ai: String = rawInformation.chars().take(aiSize).collect();
if rawInformation.chars().count() < aiSize + fieldSize {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let field: String = rawInformation

View File

@@ -199,7 +199,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
if let Some(r) = result.getDecodedInformation() {
Ok(r.clone())
} else {
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
}
@@ -345,8 +345,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
if (5..15).contains(&fiveBitValue) {
return Ok(DecodedChar::new(
pos + 5,
char::from_u32('0' as u32 + fiveBitValue - 5)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32('0' as u32 + fiveBitValue - 5).ok_or(Exceptions::parseEmpty())?,
));
}
@@ -355,14 +354,14 @@ impl<'a> GeneralAppIdDecoder<'_> {
if (64..90).contains(&sevenBitValue) {
return Ok(DecodedChar::new(
pos + 7,
char::from_u32(sevenBitValue + 1).ok_or(Exceptions::ParseException(None))?,
char::from_u32(sevenBitValue + 1).ok_or(Exceptions::parseEmpty())?,
));
}
if (90..116).contains(&sevenBitValue) {
return Ok(DecodedChar::new(
pos + 7,
char::from_u32(sevenBitValue + 7).ok_or(Exceptions::ParseException(None))?,
char::from_u32(sevenBitValue + 7).ok_or(Exceptions::parseEmpty())?,
));
}
@@ -389,7 +388,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
250 => '?',
251 => '_',
252 => ' ',
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
};
Ok(DecodedChar::new(pos + 8, c))
@@ -424,8 +423,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
if (5..15).contains(&fiveBitValue) {
return Ok(DecodedChar::new(
pos + 5,
char::from_u32('0' as u32 + fiveBitValue - 5)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32('0' as u32 + fiveBitValue - 5).ok_or(Exceptions::parseEmpty())?,
));
}
@@ -434,7 +432,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
if (32..58).contains(&sixBitValue) {
return Ok(DecodedChar::new(
pos + 6,
char::from_u32(sixBitValue + 33).ok_or(Exceptions::ParseException(None))?,
char::from_u32(sixBitValue + 33).ok_or(Exceptions::parseEmpty())?,
));
}
@@ -445,9 +443,9 @@ impl<'a> GeneralAppIdDecoder<'_> {
61 => '.',
62 => '/',
_ => {
return Err(Exceptions::IllegalStateException(Some(format!(
return Err(Exceptions::illegalState(format!(
"Decoding invalid alphanumeric value: {sixBitValue}"
))))
)))
}
};

View File

@@ -227,7 +227,7 @@ impl Reader for RSSExpandedReader {
Ok(result)
} else {
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
}
}
@@ -297,9 +297,7 @@ impl RSSExpandedReader {
if let Ok(to_add) = to_add_res {
self.pairs.push(to_add);
} else if self.pairs.is_empty() {
return Err(to_add_res
.err()
.unwrap_or(Exceptions::IllegalStateException(None)));
return Err(to_add_res.err().unwrap_or(Exceptions::illegalStateEmpty()));
} else {
// exit this loop when retrieveNextPair() fails and throws
done = true;
@@ -331,7 +329,7 @@ impl RSSExpandedReader {
// }
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
fn checkRows(&mut self, reverse: bool) -> Option<Vec<ExpandedPair>> {
@@ -378,7 +376,7 @@ impl RSSExpandedReader {
let row = self
.rows
.get(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
self.pairs.clear();
for collectedRow in &collectedRows.clone() {
// for (ExpandedRow collectedRow : collectedRows) {
@@ -406,7 +404,7 @@ impl RSSExpandedReader {
}
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
/// Whether the pairs form a valid find pattern sequence,
@@ -537,24 +535,24 @@ 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::IllegalStateException(None))?;
.ok_or(Exceptions::illegalStateEmpty())?;
let mut decoder = abstract_expanded_decoder::createDecoder(&binary)?;
let resultingString = decoder.parseInformation()?;
let firstPoints = pairs
.get(0)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.getFinderPattern()
.as_ref()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalStateEmpty())?
.getRXingResultPoints();
let lastPoints = pairs
.last()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.getFinderPattern()
.as_ref()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalStateEmpty())?
.getRXingResultPoints();
let mut result = RXingResult::new(
@@ -653,9 +651,7 @@ impl RSSExpandedReader {
let leftChar = self.decodeDataCharacter(
row,
pattern
.as_ref()
.ok_or(Exceptions::NotFoundException(None))?,
pattern.as_ref().ok_or(Exceptions::notFoundEmpty())?,
isOddPattern,
true,
)?;
@@ -663,18 +659,16 @@ impl RSSExpandedReader {
if !previousPairs.is_empty()
&& previousPairs
.last()
.ok_or(Exceptions::NotFoundException(None))?
.ok_or(Exceptions::notFoundEmpty())?
.mustBeLast()
{
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let rightChar = self
.decodeDataCharacter(
row,
pattern
.as_ref()
.ok_or(Exceptions::NotFoundException(None))?,
pattern.as_ref().ok_or(Exceptions::notFoundEmpty())?,
isOddPattern,
false,
)
@@ -706,11 +700,11 @@ impl RSSExpandedReader {
} else {
let lastPair = previousPairs
.last()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
rowOffset = lastPair
.getFinderPattern()
.as_ref()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalStateEmpty())?
.getStartEnd()[1] as i32;
}
let mut searchingEvenPair = previousPairs.len() % 2 != 0;
@@ -762,7 +756,7 @@ impl RSSExpandedReader {
isWhite = !isWhite;
}
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
fn reverseCounters(counters: &mut [u32]) {
@@ -859,7 +853,7 @@ impl RSSExpandedReader {
let expectedElementWidth: f32 =
(pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) as f32 / 15.0;
if (elementWidth - expectedElementWidth).abs() / expectedElementWidth > 0.3 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
for (i, counter) in counters.iter().enumerate() {
@@ -868,12 +862,12 @@ impl RSSExpandedReader {
let mut count = (value + 0.5) as i32; // Round
if count < 1 {
if value < 0.3 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
count = 1;
} else if count > 8 {
if value > 8.7 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
count = 8;
}
@@ -913,7 +907,7 @@ impl RSSExpandedReader {
let checksumPortion = oddChecksumPortion + evenChecksumPortion;
if (oddSum & 0x01) != 0 || !(4..=13).contains(&oddSum) {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let group = ((13 - oddSum) / 2) as usize;
@@ -961,12 +955,12 @@ impl RSSExpandedReader {
1 => {
if oddParityBad {
if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
decrementOdd = true;
} else {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
decrementEven = true;
}
@@ -974,12 +968,12 @@ impl RSSExpandedReader {
-1 => {
if oddParityBad {
if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
incrementOdd = true;
} else {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
incrementEven = true;
}
@@ -987,7 +981,7 @@ impl RSSExpandedReader {
0 => {
if oddParityBad {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
// Both bad
if oddSum < evenSum {
@@ -998,16 +992,16 @@ impl RSSExpandedReader {
incrementEven = true;
}
} else if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
}
_ => return Err(Exceptions::NotFoundException(None)),
_ => return Err(Exceptions::notFoundEmpty()),
}
if incrementOdd {
if decrementOdd {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
Self::increment(&mut self.oddCounts, &self.oddRoundingErrors);
}
@@ -1016,7 +1010,7 @@ impl RSSExpandedReader {
}
if incrementEven {
if decrementEven {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
Self::increment(&mut self.evenCounts, &self.oddRoundingErrors);
}

View File

@@ -64,12 +64,12 @@ impl OneDReader for RSS14Reader {
if right.getCount() > 1 && self.checkChecksum(left, right) {
return self
.constructRXingResult(left, right)
.ok_or(Exceptions::IllegalStateException(None));
.ok_or(Exceptions::illegalStateEmpty());
}
}
}
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
}
impl Reader for RSS14Reader {
@@ -126,7 +126,7 @@ impl Reader for RSS14Reader {
Ok(result)
} else {
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
}
}
@@ -343,7 +343,7 @@ impl RSS14Reader {
if outsideChar {
if (oddSum & 0x01) != 0 || !(4..=12).contains(&oddSum) {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let group = ((12 - oddSum) / 2) as usize;
let oddWidest = Self::OUTSIDE_ODD_WIDEST[group];
@@ -358,7 +358,7 @@ impl RSS14Reader {
))
} else {
if (evenSum & 0x01) != 0 || !(4..=10).contains(&evenSum) {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
let group = ((10 - evenSum) / 2) as usize;
let oddWidest = Self::INSIDE_ODD_WIDEST[group];
@@ -417,7 +417,7 @@ impl RSS14Reader {
isWhite = !isWhite;
}
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFoundEmpty())
}
fn parseFoundFinderPattern(
@@ -518,12 +518,12 @@ impl RSS14Reader {
1 => {
if oddParityBad {
if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
decrementOdd = true;
} else {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
decrementEven = true;
}
@@ -531,12 +531,12 @@ impl RSS14Reader {
-1 => {
if oddParityBad {
if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
incrementOdd = true;
} else {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
incrementEven = true;
}
@@ -544,7 +544,7 @@ impl RSS14Reader {
0 => {
if oddParityBad {
if !evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
// Both bad
if oddSum < evenSum {
@@ -555,15 +555,15 @@ impl RSS14Reader {
incrementEven = true;
}
} else if evenParityBad {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
}
_ => return Err(Exceptions::NotFoundException(None)),
_ => return Err(Exceptions::notFoundEmpty()),
}
if incrementOdd {
if decrementOdd {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
Self::increment(&mut self.oddCounts, &self.oddRoundingErrors);
}
@@ -572,7 +572,7 @@ impl RSS14Reader {
}
if incrementEven {
if decrementEven {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFoundEmpty());
}
Self::increment(&mut self.evenCounts, &self.evenRoundingErrors);
}