mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
refactor to use exception helper factories
This commit is contained in:
@@ -99,23 +99,23 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
let length = contents.chars().count();
|
||||
// Check length
|
||||
if !(1..=80).contains(&length) {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Contents length should be between 1 and 80 characters, but got {length}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
|
||||
// Check for forced code set hint.
|
||||
let mut forcedCodeSet = -1_i32;
|
||||
if hints.contains_key(&EncodeHintType::FORCE_CODE_SET) {
|
||||
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { return Err(Exceptions::IllegalStateException(None)) };
|
||||
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { return Err(Exceptions::illegalStateEmpty()) };
|
||||
match codeSetHint.as_str() {
|
||||
"A" => forcedCodeSet = CODE_CODE_A as i32,
|
||||
"B" => forcedCodeSet = CODE_CODE_B as i32,
|
||||
"C" => forcedCodeSet = CODE_CODE_C as i32,
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Unsupported code set hint: {codeSetHint}"
|
||||
))))
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,9 +134,9 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
if c > 127 {
|
||||
// no full Latin-1 character set available at the moment
|
||||
// shift and manual code change are not supported
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Bad character in input: ASCII value={c}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,18 +149,18 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
// allows no ascii above 95 (no lower caps, no special symbols)
|
||||
{
|
||||
if c > 95 && c <= 127 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Bad character in input for forced code set A: ASCII value={c}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
CODE_CODE_B_I32 =>
|
||||
// allows no ascii below 32 (terminal symbols)
|
||||
{
|
||||
if c <= 32 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Bad character in input for forced code set B: ASCII value={c}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
CODE_CODE_C_I32 =>
|
||||
@@ -172,9 +172,9 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
|| ch == ESCAPE_FNC_3
|
||||
|| ch == ESCAPE_FNC_4
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Bad character in input for forced code set C: ASCII value={c}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -195,8 +195,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
while position < length {
|
||||
//Select code to use
|
||||
let newCodeSet = if forcedCodeSet == -1 {
|
||||
chooseCode(contents, position, codeSet)
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
chooseCode(contents, position, codeSet).ok_or(Exceptions::illegalStateEmpty())?
|
||||
} else {
|
||||
forcedCodeSet as usize // THIS IS RISKY
|
||||
};
|
||||
@@ -209,7 +208,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
match contents
|
||||
.chars()
|
||||
.nth(position)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
{
|
||||
ESCAPE_FNC_1 => patternIndex = CODE_FNC_1 as isize,
|
||||
ESCAPE_FNC_2 => patternIndex = CODE_FNC_2 as isize,
|
||||
@@ -229,7 +228,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
patternIndex = contents
|
||||
.chars()
|
||||
.nth(position)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
as isize
|
||||
- ' ' as isize;
|
||||
if patternIndex < 0 {
|
||||
@@ -241,7 +240,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
patternIndex = contents
|
||||
.chars()
|
||||
.nth(position)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
as isize
|
||||
- ' ' as isize
|
||||
}
|
||||
@@ -249,9 +248,9 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
// CODE_CODE_C
|
||||
if position + 1 == length {
|
||||
// this is the last character, but the encoding is C, which always encodes two characers
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"Bad number of characters for digit only encoding.".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
let s: String = contents
|
||||
.char_indices()
|
||||
@@ -260,7 +259,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
.map(|(_u, c)| c)
|
||||
.collect();
|
||||
patternIndex = s.parse::<isize>().map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!("issue parsing {s}: {e}")))
|
||||
Exceptions::parse(format!("issue parsing {s}: {e}"))
|
||||
})?;
|
||||
position += 1;
|
||||
} // Also incremented below
|
||||
@@ -536,7 +535,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
if contents
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
== ESCAPE_FNC_1
|
||||
{
|
||||
addPattern(
|
||||
@@ -555,9 +554,8 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
.collect();
|
||||
addPattern(
|
||||
&mut patterns,
|
||||
s.parse::<usize>().map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!("unable to parse {s} {e}")))
|
||||
})?,
|
||||
s.parse::<usize>()
|
||||
.map_err(|e| Exceptions::parse(format!("unable to parse {s} {e}")))?,
|
||||
&mut checkSum,
|
||||
&mut checkWeight,
|
||||
i,
|
||||
@@ -572,7 +570,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
let mut patternIndex = match contents
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
{
|
||||
ESCAPE_FNC_1 => CODE_FNC_1 as isize,
|
||||
ESCAPE_FNC_2 => CODE_FNC_2 as isize,
|
||||
@@ -590,7 +588,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
contents
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
as isize
|
||||
- ' ' as isize
|
||||
}
|
||||
@@ -682,7 +680,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
minPath: &mut Vec<Vec<Latch>>,
|
||||
) -> Result<u32, Exceptions> {
|
||||
if position >= contents.chars().count() {
|
||||
return Err(Exceptions::IllegalStateException(None));
|
||||
return Err(Exceptions::illegalStateEmpty());
|
||||
}
|
||||
let mCost = memoizedCost[charset.ordinal()][position];
|
||||
if mCost > 0 {
|
||||
@@ -763,10 +761,10 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
}
|
||||
if minCost == u32::MAX {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Bad character in input: ASCII value={}",
|
||||
contents.chars().nth(position).unwrap_or('x')
|
||||
))));
|
||||
)));
|
||||
// throw new IllegalArgumentException("Bad character in input: ASCII value=" + (int) contents.charAt(position));
|
||||
}
|
||||
memoizedCost[charset.ordinal()][position] = minCost;
|
||||
|
||||
Reference in New Issue
Block a user