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

@@ -59,18 +59,18 @@ impl Writer for QRCodeWriter {
hints: &crate::EncodingHintDictionary,
) -> Result<crate::common::BitMatrix> {
if contents.is_empty() {
return Err(Exceptions::illegalArgumentWith("found empty contents"));
return Err(Exceptions::illegal_argument_with("found empty contents"));
}
if format != &BarcodeFormat::QR_CODE {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"can only encode QR_CODE, but got {format:?}"
)));
// throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
}
if width < 0 || height < 0 {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"requested dimensions are too small: {width}x{height}"
)));
}
@@ -87,7 +87,7 @@ impl Writer for QRCodeWriter {
if let Some(EncodeHintValue::Margin(margin)) = hints.get(&EncodeHintType::MARGIN) {
margin
.parse::<i32>()
.map_err(|e| Exceptions::parseWith(format!("could not parse {margin}: {e}")))?
.map_err(|e| Exceptions::parse_with(format!("could not parse {margin}: {e}")))?
} else {
QUIET_ZONE_SIZE
};
@@ -109,10 +109,10 @@ impl QRCodeWriter {
) -> Result<BitMatrix> {
let input = code.getMatrix();
if input.is_none() {
return Err(Exceptions::illegalStateWith("matrix is empty"));
return Err(Exceptions::illegal_state_with("matrix is empty"));
}
let input = input.as_ref().ok_or(Exceptions::illegalState)?;
let input = input.as_ref().ok_or(Exceptions::ILLEGAL_STATE)?;
let inputWidth = input.getWidth() as i32;
let inputHeight = input.getHeight() as i32;