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

@@ -122,7 +122,7 @@ pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult, Ex
}
MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
result.append_char(
char::from_u32(codewords[codeIndex]).ok_or(Exceptions::ParseException(None))?,
char::from_u32(codewords[codeIndex]).ok_or(Exceptions::parseEmpty())?,
);
codeIndex += 1;
}
@@ -149,7 +149,7 @@ pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult, Ex
BEGIN_MACRO_PDF417_OPTIONAL_FIELD | MACRO_PDF417_TERMINATOR =>
// Should not see these outside a macro block
{
return Err(Exceptions::FormatException(None))
return Err(Exceptions::formatEmpty())
}
_ => {
// Default to text compaction. During testing numerous barcodes
@@ -164,7 +164,7 @@ pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult, Ex
result = result.build_result();
if result.is_empty() && resultMetadata.getFileId().is_empty() {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
let mut decoderRXingResult = DecoderRXingResult::new(
@@ -186,7 +186,7 @@ pub fn decodeMacroBlock(
let mut codeIndex = codeIndex;
if codeIndex + NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0] as usize {
// we must have at least two bytes left for the segment index
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
let mut segmentIndexArray = [0; NUMBER_OF_SEQUENCE_CODEWORDS];
for seq in segmentIndexArray
@@ -204,7 +204,7 @@ pub fn decodeMacroBlock(
resultMetadata.setSegmentIndex(parsed_int);
} else {
// too large; bad input?
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
// Decoding the fileId codewords as 0-899 numbers, each 0-filled to width 3. This follows the spec
@@ -221,7 +221,7 @@ pub fn decodeMacroBlock(
}
if fileId.chars().count() == 0 {
// at least one fileId codeword is required (Annex H.2)
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
resultMetadata.setFileId(fileId);
@@ -258,7 +258,7 @@ pub fn decodeMacroBlock(
codeIndex = numericCompaction(codewords, codeIndex + 1, &mut segmentCount)?;
segmentCount = segmentCount.build_result();
let Ok(parsed_segment_count) = segmentCount.to_string().parse() else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
};
resultMetadata.setSegmentCount(parsed_segment_count);
}
@@ -267,7 +267,7 @@ pub fn decodeMacroBlock(
codeIndex = numericCompaction(codewords, codeIndex + 1, &mut timestamp)?;
timestamp = timestamp.build_result();
let Ok(parsed_timestamp) = timestamp.to_string().parse() else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
};
resultMetadata.setTimestamp(parsed_timestamp);
}
@@ -276,7 +276,7 @@ pub fn decodeMacroBlock(
codeIndex = numericCompaction(codewords, codeIndex + 1, &mut checksum)?;
checksum = checksum.build_result();
let Ok(parsed_checksum ) = checksum.to_string().parse() else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
};
resultMetadata.setChecksum(parsed_checksum);
}
@@ -285,18 +285,18 @@ pub fn decodeMacroBlock(
codeIndex = numericCompaction(codewords, codeIndex + 1, &mut fileSize)?;
fileSize = fileSize.build_result();
let Ok(parsed_file_size)= fileSize.to_string().parse() else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
};
resultMetadata.setFileSize(parsed_file_size);
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
MACRO_PDF417_TERMINATOR => {
codeIndex += 1;
resultMetadata.setLastSegment(true);
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::formatEmpty()),
}
}
@@ -388,7 +388,7 @@ fn textCompaction(
result,
subMode,
)
.ok_or(Exceptions::IllegalStateException(None))?;
.ok_or(Exceptions::illegalStateEmpty())?;
result.appendECI(codewords[codeIndex])?;
codeIndex += 1;
textCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2];
@@ -770,18 +770,16 @@ fn numericCompaction(
Remove leading 1 => RXingResult is 000213298174000
*/
fn decodeBase900toBase10(codewords: &[u32], count: usize) -> Result<String, Exceptions> {
let mut result = 0
.to_biguint()
.ok_or(Exceptions::ArithmeticException(None))?;
let mut result = 0.to_biguint().ok_or(Exceptions::arithmeticEmpty())?;
for i in 0..count {
result += &EXP900[count - i - 1]
* (codewords[i]
.to_biguint()
.ok_or(Exceptions::ArithmeticException(None))?);
.ok_or(Exceptions::arithmeticEmpty())?);
}
let resultString = result.to_string();
if !resultString.starts_with('1') {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::formatEmpty());
}
Ok(resultString[1..].to_owned())
}