refactor to use exception helper factories

This commit is contained in:
Vukašin Stepanović
2023-02-15 10:46:13 +00:00
parent 3e27279dc8
commit 722ce78fd0
132 changed files with 966 additions and 1132 deletions

View File

@@ -33,9 +33,9 @@ impl OneDimensionalCodeWriter for Code39Writer {
let mut contents = contents.to_owned();
let mut length = contents.chars().count();
if length > 80 {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgument(format!(
"Requested contents should be less than 80 digits long, but got {length}"
))));
)));
}
let mut i = 0;
@@ -47,14 +47,14 @@ impl OneDimensionalCodeWriter for Code39Writer {
contents
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
)
.is_none()
{
contents = Self::tryToConvertToExtendedMode(&contents)?;
length = contents.chars().count();
if length > 80 {
return Err(Exceptions::IllegalArgumentException(Some(format!("Requested contents should be less than 80 digits long, but got {length} (extended full ASCII mode)"))));
return Err(Exceptions::illegalArgument(format!("Requested contents should be less than 80 digits long, but got {length} (extended full ASCII mode)")));
}
break;
}
@@ -70,7 +70,7 @@ impl OneDimensionalCodeWriter for Code39Writer {
pos += Self::appendPattern(&mut result, pos as usize, &narrowWhite, false);
//append next character to byte matrix
for i in 0..length {
let Some(indexInString) = Code39Reader::ALPHABET_STRING.find(contents.chars().nth(i).ok_or(Exceptions::IndexOutOfBoundsException(None))?) else {
let Some(indexInString) = Code39Reader::ALPHABET_STRING.find(contents.chars().nth(i).ok_or(Exceptions::indexOutOfBoundsEmpty())?) else {
continue;
};
Self::toIntArray(
@@ -117,58 +117,58 @@ impl Code39Writer {
extendedContent.push('$');
extendedContent.push(
char::from_u32('A' as u32 + (character as u32 - 1))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character < ' ' {
extendedContent.push('%');
extendedContent.push(
char::from_u32('A' as u32 + (character as u32 - 27))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= ',' || character == '/' || character == ':' {
extendedContent.push('/');
extendedContent.push(
char::from_u32('A' as u32 + (character as u32 - 33))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= '9' {
extendedContent.push(
char::from_u32('0' as u32 + (character as u32 - 48))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= '?' {
extendedContent.push('%');
extendedContent.push(
char::from_u32('F' as u32 + (character as u32 - 59))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= 'Z' {
extendedContent.push(
char::from_u32('A' as u32 + (character as u32 - 65))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= '_' {
extendedContent.push('%');
extendedContent.push(
char::from_u32('K' as u32 + (character as u32 - 91))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character <= 'z' {
extendedContent.push('+');
extendedContent.push(
char::from_u32('A' as u32 + (character as u32 - 97))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else if character as u32 <= 127 {
extendedContent.push('%');
extendedContent.push(
char::from_u32('P' as u32 + (character as u32 - 123))
.ok_or(Exceptions::ParseException(None))?,
.ok_or(Exceptions::parseEmpty())?,
);
} else {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgument(format!(
"Requested content contains a non-encodable character: '{character}'"
))));
)));
}
}
}