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

@@ -33,12 +33,12 @@ impl OneDimensionalCodeWriter for ITFWriter {
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
let length = contents.chars().count();
if length % 2 != 0 {
return Err(Exceptions::illegalArgumentWith(
return Err(Exceptions::illegal_argument_with(
"The length of the input should be even",
));
}
if length > 80 {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"Requested contents should be less than 80 digits long, but got {length}"
)));
}
@@ -52,15 +52,15 @@ impl OneDimensionalCodeWriter for ITFWriter {
let one = contents
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBounds)?
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::parse)? as usize;
.ok_or(Exceptions::PARSE)? as usize;
let two = contents
.chars()
.nth(i + 1)
.ok_or(Exceptions::indexOutOfBounds)?
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_digit(10)
.ok_or(Exceptions::parse)? as usize;
.ok_or(Exceptions::PARSE)? as usize;
let mut encoding = [0; 10];
for j in 0..5 {
encoding[2 * j] = PATTERNS[one][j];