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

@@ -68,10 +68,10 @@ impl ECIInput for MinimalECIInput {
*/
fn charAt(&self, index: usize) -> Result<char> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBoundsWith(index.to_string()));
return Err(Exceptions::index_out_of_bounds_with(index.to_string()));
}
if self.isECI(index as u32)? {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"value at {index} is not a character but an ECI"
)));
}
@@ -104,13 +104,13 @@ impl ECIInput for MinimalECIInput {
*/
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>> {
if start > end || end > self.length() {
return Err(Exceptions::indexOutOfBounds);
return Err(Exceptions::INDEX_OUT_OF_BOUNDS);
}
let mut result = String::new();
for i in start..end {
// for (int i = start; i < end; i++) {
if self.isECI(i as u32)? {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"value at {i} is not a character but an ECI"
)));
}
@@ -132,7 +132,7 @@ impl ECIInput for MinimalECIInput {
*/
fn isECI(&self, index: u32) -> Result<bool> {
if index >= self.length() as u32 {
return Err(Exceptions::indexOutOfBounds);
return Err(Exceptions::INDEX_OUT_OF_BOUNDS);
}
Ok(self.bytes[index as usize] > 255) // && self.bytes[index as usize] <= u16::MAX)
}
@@ -157,10 +157,10 @@ impl ECIInput for MinimalECIInput {
*/
fn getECIValue(&self, index: usize) -> Result<i32> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBounds);
return Err(Exceptions::INDEX_OUT_OF_BOUNDS);
}
if !self.isECI(index as u32)? {
return Err(Exceptions::illegalArgumentWith(format!(
return Err(Exceptions::illegal_argument_with(format!(
"value at {index} is not an ECI but a character"
)));
}
@@ -249,7 +249,7 @@ impl MinimalECIInput {
*/
pub fn isFNC1(&self, index: usize) -> Result<bool> {
if index >= self.length() {
return Err(Exceptions::indexOutOfBounds);
return Err(Exceptions::INDEX_OUT_OF_BOUNDS);
}
Ok(self.bytes[index] == 1000)
}