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

@@ -44,12 +44,12 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
let firstChar = contents
.chars()
.next()
.ok_or(Exceptions::indexOutOfBounds)?
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_ascii_uppercase();
let lastChar = contents
.chars()
.nth(contents.chars().count() - 1)
.ok_or(Exceptions::indexOutOfBounds)?
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_ascii_uppercase();
let startsNormal = CodaBarReader::arrayContains(&START_END_CHARS, firstChar);
let endsNormal = CodaBarReader::arrayContains(&START_END_CHARS, lastChar);
@@ -57,7 +57,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
let endsAlt = CodaBarReader::arrayContains(&ALT_START_END_CHARS, lastChar);
if startsNormal {
if !endsNormal {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"Invalid start/end guards: {contents}"
)));
}
@@ -65,7 +65,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
contents.to_owned()
} else if startsAlt {
if !endsAlt {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"Invalid start/end guards: {contents}"
)));
}
@@ -74,7 +74,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
} else {
// Doesn't start with a guard
if endsNormal || endsAlt {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"Invalid start/end guards: {contents}"
)));
}
@@ -94,7 +94,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
) {
resultLength += 10;
} else {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"Cannot encode : '{ch}'"
)));
}
@@ -109,7 +109,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
let mut c = contents
.chars()
.nth(index)
.ok_or(Exceptions::indexOutOfBounds)?
.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?
.to_ascii_uppercase();
if index == 0 || index == contents.chars().count() - 1 {
// The start/end chars are not in the CodaBarReader.ALPHABET.