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:
@@ -31,12 +31,12 @@ impl Encoder for ASCIIEncoder {
|
||||
.getMessage()
|
||||
.chars()
|
||||
.nth(context.pos as usize)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
|
||||
context
|
||||
.getMessage()
|
||||
.chars()
|
||||
.nth(context.pos as usize + 1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
|
||||
)? as u8);
|
||||
context.pos += 2;
|
||||
} else {
|
||||
@@ -73,9 +73,7 @@ impl Encoder for ASCIIEncoder {
|
||||
}
|
||||
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalStateException(Some(format!(
|
||||
"Illegal mode: {newMode}"
|
||||
))));
|
||||
return Err(Exceptions::illegalState(format!("Illegal mode: {newMode}")));
|
||||
}
|
||||
}
|
||||
} else if high_level_encoder::isExtendedASCII(c) {
|
||||
@@ -104,9 +102,9 @@ impl ASCIIEncoder {
|
||||
let num = (digit1 as u8 - 48) * 10 + (digit2 as u8 - 48);
|
||||
Ok((num + 130) as char)
|
||||
} else {
|
||||
Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
Err(Exceptions::illegalArgument(format!(
|
||||
"not digits: {digit1}{digit2}"
|
||||
))))
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ impl Encoder for Base256Encoder {
|
||||
context.updateSymbolInfoWithLength(currentSize);
|
||||
let mustPad = (context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity()
|
||||
- currentSize as u32)
|
||||
> 0;
|
||||
@@ -62,29 +62,29 @@ impl Encoder for Base256Encoder {
|
||||
buffer.replace_range(
|
||||
0..1,
|
||||
&char::from_u32(dataCount as u32)
|
||||
.ok_or(Exceptions::ParseException(None))?
|
||||
.ok_or(Exceptions::parseEmpty())?
|
||||
.to_string(),
|
||||
);
|
||||
} else if dataCount <= 1555 {
|
||||
buffer.replace_range(
|
||||
0..1,
|
||||
&char::from_u32((dataCount as u32 / 250) + 249)
|
||||
.ok_or(Exceptions::ParseException(None))?
|
||||
.ok_or(Exceptions::parseEmpty())?
|
||||
.to_string(),
|
||||
);
|
||||
let (ci_pos, _) = buffer
|
||||
.char_indices()
|
||||
.nth(1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
|
||||
buffer.insert(
|
||||
ci_pos,
|
||||
char::from_u32(dataCount as u32 % 250)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
|
||||
);
|
||||
} else {
|
||||
return Err(Exceptions::IllegalStateException(Some(format!(
|
||||
return Err(Exceptions::illegalState(format!(
|
||||
"Message length not in valid ranges: {dataCount}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
let c = buffer.chars().count();
|
||||
@@ -95,10 +95,10 @@ impl Encoder for Base256Encoder {
|
||||
buffer
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
|
||||
context.getCodewordCount() as u32 + 1,
|
||||
)
|
||||
.ok_or(Exceptions::ParseException(None))? as u8,
|
||||
.ok_or(Exceptions::parseEmpty())? as u8,
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -65,7 +65,7 @@ impl C40Encoder {
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
|
||||
@@ -140,7 +140,7 @@ impl C40Encoder {
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
let rest = buffer.chars().count() % 3;
|
||||
@@ -182,9 +182,7 @@ impl C40Encoder {
|
||||
context: &mut EncoderContext,
|
||||
buffer: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
context.writeCodewords(
|
||||
&Self::encodeToCodewords(buffer).ok_or(Exceptions::FormatException(None))?,
|
||||
);
|
||||
context.writeCodewords(&Self::encodeToCodewords(buffer).ok_or(Exceptions::formatEmpty())?);
|
||||
buffer.replace_range(0..3, "");
|
||||
// buffer.delete(0, 3);
|
||||
Ok(())
|
||||
@@ -207,7 +205,7 @@ impl C40Encoder {
|
||||
context.updateSymbolInfoWithLength(curCodewordCount);
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity() as usize
|
||||
- curCodewordCount;
|
||||
|
||||
@@ -236,9 +234,9 @@ impl C40Encoder {
|
||||
context.writeCodeword(C40_UNLATCH);
|
||||
}
|
||||
} else {
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
return Err(Exceptions::illegalState(
|
||||
"Unexpected case. Please report!".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
context.signalEncoderChange(ASCII_ENCODATION);
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@ impl DefaultPlacement {
|
||||
.codewords
|
||||
.chars()
|
||||
.nth(pos)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as u32;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())? as u32;
|
||||
v &= 1 << (8 - bit);
|
||||
self.setBit(col as usize, row as usize, v != 0);
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ impl EdifactEncoder {
|
||||
context.updateSymbolInfo();
|
||||
let mut available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
let remaining = context.getRemainingCharacters();
|
||||
@@ -85,7 +85,7 @@ impl EdifactEncoder {
|
||||
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1);
|
||||
available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
}
|
||||
@@ -95,9 +95,9 @@ impl EdifactEncoder {
|
||||
}
|
||||
|
||||
if count > 4 {
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
return Err(Exceptions::illegalState(
|
||||
"Count must not exceed 4".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
let restChars = count - 1;
|
||||
let encoded = Self::encodeToCodewords(buffer)?;
|
||||
@@ -108,7 +108,7 @@ impl EdifactEncoder {
|
||||
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
if available >= 3 {
|
||||
@@ -149,32 +149,32 @@ impl EdifactEncoder {
|
||||
fn encodeToCodewords(sb: &str) -> Result<String, Exceptions> {
|
||||
let len = sb.chars().count();
|
||||
if len == 0 {
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
return Err(Exceptions::illegalState(
|
||||
"StringBuilder must not be empty".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
let c1 = sb
|
||||
.chars()
|
||||
.next()
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
|
||||
let c2 = if len >= 2 {
|
||||
sb.chars()
|
||||
.nth(1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
let c3 = if len >= 3 {
|
||||
sb.chars()
|
||||
.nth(2)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
let c4 = if len >= 4 {
|
||||
sb.chars()
|
||||
.nth(3)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
} else {
|
||||
0 as char
|
||||
};
|
||||
@@ -184,12 +184,12 @@ impl EdifactEncoder {
|
||||
let cw2 = (v >> 8) & 255;
|
||||
let cw3 = v & 255;
|
||||
let mut res = String::with_capacity(3);
|
||||
res.push(char::from_u32(cw1).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
res.push(char::from_u32(cw1).ok_or(Exceptions::indexOutOfBoundsEmpty())?);
|
||||
if len >= 2 {
|
||||
res.push(char::from_u32(cw2).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
res.push(char::from_u32(cw2).ok_or(Exceptions::indexOutOfBoundsEmpty())?);
|
||||
}
|
||||
if len >= 3 {
|
||||
res.push(char::from_u32(cw3).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
|
||||
res.push(char::from_u32(cw3).ok_or(Exceptions::indexOutOfBoundsEmpty())?);
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
|
||||
@@ -63,14 +63,12 @@ impl<'a> EncoderContext<'_> {
|
||||
ISO_8859_1_ENCODER
|
||||
.decode(&encoded_bytes, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!(
|
||||
"round trip decode should always work: {e}"
|
||||
)))
|
||||
Exceptions::parse(format!("round trip decode should always work: {e}"))
|
||||
})?
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"Message contains characters outside ISO-8859-1 encoding.".to_owned(),
|
||||
)));
|
||||
));
|
||||
};
|
||||
Ok(Self {
|
||||
symbol_lookup: Rc::new(SymbolInfoLookup::new()),
|
||||
|
||||
@@ -154,9 +154,9 @@ const ALOG: [u32; 255] = {
|
||||
*/
|
||||
pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String, Exceptions> {
|
||||
if codewords.chars().count() != symbolInfo.getDataCapacity() as usize {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"The number of codewords does not match the selected symbol".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
let mut sb = String::with_capacity(
|
||||
(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords()) as usize,
|
||||
@@ -185,7 +185,7 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
|
||||
codewords
|
||||
.chars()
|
||||
.nth(d)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
|
||||
);
|
||||
|
||||
d += blockCount;
|
||||
@@ -198,12 +198,12 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
|
||||
let (char_index, replace_char) = sb
|
||||
.char_indices()
|
||||
.nth(symbolInfo.getDataCapacity() as usize + e)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
|
||||
sb.replace_range(
|
||||
char_index..(replace_char.len_utf8()),
|
||||
&ecc.chars()
|
||||
.nth(pos)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
.to_string(),
|
||||
);
|
||||
// sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos));
|
||||
@@ -228,9 +228,9 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptio
|
||||
}
|
||||
}
|
||||
if table < 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Illegal number of error correction codewords specified: {numECWords}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
let poly = &FACTORS[table as usize];
|
||||
let mut ecc = vec![0 as char; numECWords];
|
||||
@@ -244,21 +244,21 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptio
|
||||
^ codewords
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as usize;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())? as usize;
|
||||
for k in (1..numECWords).rev() {
|
||||
// for (int k = numECWords - 1; k > 0; k--) {
|
||||
if m != 0 && poly[k] != 0 {
|
||||
ecc[k] = char::from_u32(
|
||||
ecc[k - 1] as u32 ^ ALOG[(LOG[m] + LOG[poly[k] as usize]) as usize % 255],
|
||||
)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
|
||||
} else {
|
||||
ecc[k] = ecc[k - 1];
|
||||
}
|
||||
}
|
||||
if m != 0 && poly[0] != 0 {
|
||||
ecc[0] = char::from_u32(ALOG[(LOG[m] + LOG[poly[0] as usize]) as usize % 255])
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
|
||||
} else {
|
||||
ecc[0] = 0 as char;
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
c40Encoder.encodeMaximalC40(&mut context)?;
|
||||
encodingMode = context
|
||||
.getNewEncoding()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
.ok_or(Exceptions::illegalStateEmpty())?;
|
||||
context.resetEncoderSignal();
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
if context.getNewEncoding().is_some() {
|
||||
encodingMode = context
|
||||
.getNewEncoding()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
.ok_or(Exceptions::illegalStateEmpty())?;
|
||||
context.resetEncoderSignal();
|
||||
}
|
||||
}
|
||||
@@ -240,7 +240,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
context.updateSymbolInfo();
|
||||
let capacity = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity();
|
||||
if len < capacity as usize
|
||||
&& encodingMode != ASCII_ENCODATION
|
||||
@@ -611,7 +611,7 @@ pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 {
|
||||
pub fn illegalCharacter(c: char) -> Result<(), Exceptions> {
|
||||
// let hex = Integer.toHexString(c);
|
||||
// hex = "0000".substring(0, 4 - hex.length()) + hex;
|
||||
Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
Err(Exceptions::illegalArgument(format!(
|
||||
"Illegal character: {c} (0x{c})"
|
||||
))))
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ fn addEdge(edges: &mut [Vec<Option<Rc<Edge>>>], edge: Rc<Edge>) -> Result<(), Ex
|
||||
if edges[vertexIndex][edge.getEndMode()?.ordinal()].is_none()
|
||||
|| edges[vertexIndex][edge.getEndMode()?.ordinal()]
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.cachedTotalSize
|
||||
> edge.cachedTotalSize
|
||||
{
|
||||
@@ -635,9 +635,9 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
|
||||
}
|
||||
|
||||
if minimalJ < 0 {
|
||||
return Err(Exceptions::IllegalStateException(Some(format!(
|
||||
return Err(Exceptions::illegalState(format!(
|
||||
"Internal error: failed to encode \"{input}\""
|
||||
))));
|
||||
)));
|
||||
}
|
||||
RXingResult::new(edges[inputLength][minimalJ as usize].clone())
|
||||
}
|
||||
@@ -669,7 +669,7 @@ impl Edge {
|
||||
previous: Option<Rc<Edge>>,
|
||||
) -> Result<Self, Exceptions> {
|
||||
if fromPosition + characterLength > input.length() as u32 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
|
||||
let mut size = if let Some(previous) = previous.clone() {
|
||||
@@ -1276,7 +1276,7 @@ impl RXingResult {
|
||||
let solution = if let Some(edge) = solution {
|
||||
edge
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
return Err(Exceptions::illegalArgumentEmpty());
|
||||
};
|
||||
let input = solution.input.clone();
|
||||
let mut size = 0;
|
||||
|
||||
@@ -128,9 +128,9 @@ impl SymbolInfo {
|
||||
2 | 4 => Ok(2),
|
||||
16 => Ok(4),
|
||||
36 => Ok(6),
|
||||
_ => Err(Exceptions::IllegalStateException(Some(
|
||||
_ => Err(Exceptions::illegalState(
|
||||
"Cannot handle this number of data regions".to_owned(),
|
||||
))),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,9 +140,9 @@ impl SymbolInfo {
|
||||
4 => Ok(2),
|
||||
16 => Ok(4),
|
||||
36 => Ok(6),
|
||||
_ => Err(Exceptions::IllegalStateException(Some(
|
||||
_ => Err(Exceptions::illegalState(
|
||||
"Cannot handle this number of data regions".to_owned(),
|
||||
))),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,9 +310,9 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
}
|
||||
}
|
||||
if fail {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"Can't find a symbol arrangement that matches the message. Data codewords: {dataCodewords}"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ impl X12Encoder {
|
||||
context.updateSymbolInfo();
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
.getDataCapacity()
|
||||
- context.getCodewordCount() as u32;
|
||||
let count = buffer.chars().count();
|
||||
|
||||
Reference in New Issue
Block a user