Use thiserror for error handling with less boilerplate

This commit is contained in:
Steve Cook
2023-02-20 09:41:28 -05:00
parent 01e4f4a126
commit f8b29f37db
135 changed files with 868 additions and 905 deletions

View File

@@ -80,7 +80,7 @@ impl Reader for QRCodeReader {
// if (decoderRXingResult.getOther() instanceof QRCodeDecoderMetaData) {
other
.downcast_ref::<QRCodeDecoderMetaData>()
.ok_or(Exceptions::illegalState)?
.ok_or(Exceptions::ILLEGAL_STATE)?
.applyMirroredCorrection(&mut points);
}
}
@@ -150,11 +150,11 @@ impl QRCodeReader {
let leftTopBlack = image.getTopLeftOnBit();
let rightBottomBlack = image.getBottomRightOnBit();
if leftTopBlack.is_none() || rightBottomBlack.is_none() {
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
let leftTopBlack = leftTopBlack.ok_or(Exceptions::indexOutOfBounds)?;
let rightBottomBlack = rightBottomBlack.ok_or(Exceptions::indexOutOfBounds)?;
let leftTopBlack = leftTopBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let rightBottomBlack = rightBottomBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
@@ -165,7 +165,7 @@ impl QRCodeReader {
// Sanity check!
if left >= right || top >= bottom {
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
if bottom - top != right - left {
@@ -174,17 +174,17 @@ impl QRCodeReader {
right = left + (bottom - top);
if right >= image.getWidth() as i32 {
// Abort if that would not make sense -- off image
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
}
let matrixWidth = ((right as f32 - left as f32 + 1.0) / moduleSize).round() as u32;
let matrixHeight = ((bottom as f32 - top as f32 + 1.0) / moduleSize).round() as u32;
if matrixWidth == 0 || matrixHeight == 0 {
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
if matrixHeight != matrixWidth {
// Only possibly decode square regions
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
// Push in the "border" by half the module width so that we start
@@ -202,7 +202,7 @@ impl QRCodeReader {
if nudgedTooFarRight > 0 {
if nudgedTooFarRight > nudge as i32 {
// Neither way fits; abort
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
left -= nudgedTooFarRight;
}
@@ -211,7 +211,7 @@ impl QRCodeReader {
if nudgedTooFarDown > 0 {
if nudgedTooFarDown > nudge as i32 {
// Neither way fits; abort
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
top -= nudgedTooFarDown;
}
@@ -248,7 +248,7 @@ impl QRCodeReader {
y += 1;
}
if x == width || y == height {
return Err(Exceptions::notFound);
return Err(Exceptions::NOT_FOUND);
}
Ok((x - leftTopBlack[0]) as f32 / 7.0)
}