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

@@ -34,7 +34,7 @@ impl BitMatrixParser {
pub fn new(bitMatrix: &BitMatrix) -> Result<Self, Exceptions> {
let dimension = bitMatrix.getHeight();
if !(8..=144).contains(&dimension) || (dimension & 0x01) != 0 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
let version = Self::readVersion(bitMatrix)?;
@@ -178,7 +178,7 @@ impl BitMatrixParser {
}
if resultOffset != self.version.getTotalCodewords() as usize {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
Ok(result)
@@ -456,9 +456,9 @@ impl BitMatrixParser {
let symbolSizeColumns = version.getSymbolSizeColumns();
if bitMatrix.getHeight() != symbolSizeRows {
return Err(Exceptions::IllegalArgumentException(Some(
return Err(Exceptions::illegalArgument(
"Dimension of bitMatrix must match the version size".to_owned(),
)));
));
}
let dataRegionSizeRows = version.getDataRegionSizeRows();

View File

@@ -138,7 +138,7 @@ impl DataBlock {
}
if rawCodewordsOffset != rawCodewords.len() {
return Err(Exceptions::IllegalArgumentException(None));
return Err(Exceptions::illegalArgumentEmpty());
}
Ok(result)

View File

@@ -158,7 +158,7 @@ pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult, Exce
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
mode = Mode::ASCII_ENCODE;
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
if !(mode != Mode::PAD_ENCODE && bits.available() > 0) {
@@ -225,16 +225,14 @@ fn decodeAsciiSegment(
loop {
let mut oneByte = bits.readBits(8)?;
match oneByte {
0 => return Err(Exceptions::FormatException(None)),
0 => return Err(Exceptions::formatEmpty()),
1..=128 => {
// ASCII data (ASCII value + 1)
if upperShift {
oneByte += 128;
//upperShift = false;
}
result.append_char(
char::from_u32(oneByte - 1).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(oneByte - 1).ok_or(Exceptions::parseEmpty())?);
return Ok(Mode::ASCII_ENCODE);
}
129 => return Ok(Mode::PAD_ENCODE), // Pad
@@ -280,9 +278,9 @@ fn decodeAsciiSegment(
if !firstCodeword
// Must be first ISO 16022:2006 5.6.1
{
return Err(Exceptions::FormatException(Some(
return Err(Exceptions::format(
"structured append tag must be first code word".to_owned(),
)));
));
}
parse_structured_append(bits, &mut sai)?;
firstFNC1Position = 5;
@@ -333,7 +331,7 @@ fn decodeAsciiSegment(
// Not to be used in ASCII encodation
// but work around encoders that end with 254, latch back to ASCII
if oneByte != 254 || bits.available() != 0 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
}
}
@@ -388,26 +386,24 @@ fn decodeC40Segment(
if upperShift {
result.append_char(
char::from_u32(c40char as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
result.append_char(c40char);
}
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
}
1 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 128).ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
result.append_char(
char::from_u32(cValue).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue).ok_or(Exceptions::parseEmpty())?);
}
shift = 0;
}
@@ -417,7 +413,7 @@ fn decodeC40Segment(
if upperShift {
result.append_char(
char::from_u32(c40char as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
@@ -436,7 +432,7 @@ fn decodeC40Segment(
upperShift = true
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
shift = 0;
@@ -444,18 +440,18 @@ fn decodeC40Segment(
3 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 224).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 224).ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
result.append_char(
char::from_u32(cValue + 96).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 96).ok_or(Exceptions::parseEmpty())?,
);
}
shift = 0;
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
if bits.available() == 0 {
@@ -505,26 +501,24 @@ fn decodeTextSegment(
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
result.append_char(textChar);
}
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
}
1 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 128).ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
result.append_char(
char::from_u32(cValue).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue).ok_or(Exceptions::parseEmpty())?);
}
shift = 0;
}
@@ -536,7 +530,7 @@ fn decodeTextSegment(
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
@@ -555,7 +549,7 @@ fn decodeTextSegment(
upperShift = true
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
shift = 0;
@@ -566,7 +560,7 @@ fn decodeTextSegment(
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
upperShift = false;
} else {
@@ -574,11 +568,11 @@ fn decodeTextSegment(
}
shift = 0;
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
if bits.available() == 0 {
@@ -645,15 +639,15 @@ fn decodeAnsiX12Segment(
if cValue < 14 {
// 0 - 9
result.append_char(
char::from_u32(cValue + 44).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 44).ok_or(Exceptions::parseEmpty())?,
);
} else if cValue < 40 {
// A - Z
result.append_char(
char::from_u32(cValue + 51).ok_or(Exceptions::ParseException(None))?,
char::from_u32(cValue + 51).ok_or(Exceptions::parseEmpty())?,
);
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
}
}
@@ -708,8 +702,7 @@ fn decodeEdifactSegment(
// no 1 in the leading (6th) bit
edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
}
result
.append_char(char::from_u32(edifactValue).ok_or(Exceptions::ParseException(None))?);
result.append_char(char::from_u32(edifactValue).ok_or(Exceptions::parseEmpty())?);
}
if bits.available() == 0 {
@@ -746,7 +739,7 @@ fn decodeBase256Segment(
// We're seeing NegativeArraySizeException errors from users.
// but we shouldn't in rust because it's unsigned
// if count < 0 {
// return Err(Exceptions::FormatException(None));
// return Err(Exceptions::formatEmpty());
// }
let mut bytes = vec![0u8; count as usize];
@@ -754,7 +747,7 @@ fn decodeBase256Segment(
// Have seen this particular error in the wild, such as at
// http://www.bcgen.com/demo/IDAutomationStreamingDataMatrix.aspx?MODE=3&D=Fred&PFMT=3&PT=F&X=0.3&O=0&LM=0.2
if bits.available() < 8 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
*byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8;
codewordPosition += 1;
@@ -762,7 +755,7 @@ fn decodeBase256Segment(
result.append_string(
&encoding::all::ISO_8859_1
.decode(&bytes, encoding::DecoderTrap::Strict)
.map_err(|e| Exceptions::ParseException(Some(e.to_string())))?,
.map_err(|e| Exceptions::parse(e.to_string()))?,
);
byteSegments.push(bytes);

View File

@@ -105,7 +105,7 @@ impl Version {
numColumns: u32,
) -> Result<&'static Version, Exceptions> {
if (numRows & 0x01) != 0 || (numColumns & 0x01) != 0 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
for version in VERSIONS.iter() {
@@ -114,7 +114,7 @@ impl Version {
}
}
Err(Exceptions::FormatException(None))
Err(Exceptions::formatEmpty())
}
/**