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:
@@ -78,10 +78,10 @@ pub fn decode(
|
||||
}
|
||||
Mode::STRUCTURED_APPEND => {
|
||||
if bits.available() < 16 {
|
||||
return Err(Exceptions::FormatException(Some(format!(
|
||||
return Err(Exceptions::format(format!(
|
||||
"Mode::Structured append expected bits.available() < 16, found bits of {}",
|
||||
bits.available()
|
||||
))));
|
||||
)));
|
||||
}
|
||||
// sequence number and parity is added later to the result metadata
|
||||
// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
|
||||
@@ -93,9 +93,7 @@ pub fn decode(
|
||||
let value = parseECIValue(&mut bits)?;
|
||||
currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue(value).ok();
|
||||
if currentCharacterSetECI.is_none() {
|
||||
return Err(Exceptions::FormatException(Some(format!(
|
||||
"Value of {value} not valid"
|
||||
))));
|
||||
return Err(Exceptions::format(format!("Value of {value} not valid")));
|
||||
}
|
||||
}
|
||||
Mode::HANZI => {
|
||||
@@ -132,7 +130,7 @@ pub fn decode(
|
||||
currentCharacterSetECI,
|
||||
hints,
|
||||
)?,
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,7 +179,7 @@ fn decodeHanziSegment(
|
||||
) -> Result<(), Exceptions> {
|
||||
// Don't crash trying to read more bits than we have available.
|
||||
if count * 13 > bits.available() {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
|
||||
// Each character will require 2 bytes. Read the characters as 2-byte pairs
|
||||
@@ -207,13 +205,11 @@ fn decodeHanziSegment(
|
||||
count -= 1;
|
||||
}
|
||||
|
||||
let gb_encoder = encoding::label::encoding_from_whatwg_label("GBK")
|
||||
.ok_or(Exceptions::IllegalStateException(None))?;
|
||||
let gb_encoder =
|
||||
encoding::label::encoding_from_whatwg_label("GBK").ok_or(Exceptions::illegalStateEmpty())?;
|
||||
let encode_string = gb_encoder
|
||||
.decode(&buffer, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!("unable to decode buffer {buffer:?}: {e}")))
|
||||
})?;
|
||||
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {buffer:?}: {e}")))?;
|
||||
result.push_str(&encode_string);
|
||||
Ok(())
|
||||
}
|
||||
@@ -227,7 +223,7 @@ fn decodeKanjiSegment(
|
||||
) -> Result<(), Exceptions> {
|
||||
// Don't crash trying to read more bits than we have available.
|
||||
if count * 13 > bits.available() {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
|
||||
// Each character will require 2 bytes. Read the characters as 2-byte pairs
|
||||
@@ -256,8 +252,7 @@ fn decodeKanjiSegment(
|
||||
let encoder = {
|
||||
let _ = currentCharacterSetECI;
|
||||
let _ = hints;
|
||||
encoding::label::encoding_from_whatwg_label("SJIS")
|
||||
.ok_or(Exceptions::FormatException(None))?
|
||||
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::formatEmpty())?
|
||||
};
|
||||
|
||||
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")]
|
||||
@@ -270,15 +265,12 @@ fn decodeKanjiSegment(
|
||||
encoding::all::ISO_8859_1
|
||||
}
|
||||
} else {
|
||||
encoding::label::encoding_from_whatwg_label("SJIS")
|
||||
.ok_or(Exceptions::FormatException(None))?
|
||||
encoding::label::encoding_from_whatwg_label("SJIS").ok_or(Exceptions::formatEmpty())?
|
||||
};
|
||||
|
||||
let encode_string = encoder
|
||||
.decode(&buffer, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!("unable to decode buffer {buffer:?}: {e}")))
|
||||
})?;
|
||||
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {buffer:?}: {e}")))?;
|
||||
|
||||
result.push_str(&encode_string);
|
||||
|
||||
@@ -295,7 +287,7 @@ fn decodeByteSegment(
|
||||
) -> Result<(), Exceptions> {
|
||||
// Don't crash trying to read more bits than we have available.
|
||||
if 8 * count > bits.available() {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
|
||||
let mut readBytes = vec![0u8; count];
|
||||
@@ -311,8 +303,7 @@ fn decodeByteSegment(
|
||||
// give a hint.
|
||||
{
|
||||
#[cfg(not(feature = "allow_forced_iso_ied_18004_compliance"))]
|
||||
StringUtils::guessCharset(&readBytes, hints)
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::illegalStateEmpty())?
|
||||
}
|
||||
|
||||
#[cfg(feature = "allow_forced_iso_ied_18004_compliance")]
|
||||
@@ -327,14 +318,14 @@ fn decodeByteSegment(
|
||||
CharacterSetECI::getCharset(
|
||||
currentCharacterSetECI
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?,
|
||||
.ok_or(Exceptions::illegalStateEmpty())?,
|
||||
)
|
||||
};
|
||||
|
||||
let encode_string = if currentCharacterSetECI.is_some()
|
||||
&& currentCharacterSetECI
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::IllegalStateException(None))?
|
||||
.ok_or(Exceptions::illegalStateEmpty())?
|
||||
== &CharacterSetECI::Cp437
|
||||
{
|
||||
{
|
||||
@@ -346,11 +337,7 @@ fn decodeByteSegment(
|
||||
} else {
|
||||
encoding
|
||||
.decode(&readBytes, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| {
|
||||
Exceptions::ParseException(Some(format!(
|
||||
"unable to decode buffer {readBytes:?}: {e}"
|
||||
)))
|
||||
})?
|
||||
.map_err(|e| Exceptions::parse(format!("unable to decode buffer {readBytes:?}: {e}")))?
|
||||
};
|
||||
|
||||
result.push_str(&encode_string);
|
||||
@@ -361,13 +348,13 @@ fn decodeByteSegment(
|
||||
|
||||
fn toAlphaNumericChar(value: u32) -> Result<char, Exceptions> {
|
||||
if value as usize >= ALPHANUMERIC_CHARS.len() {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
|
||||
ALPHANUMERIC_CHARS
|
||||
.chars()
|
||||
.nth(value as usize)
|
||||
.ok_or(Exceptions::FormatException(None))
|
||||
.ok_or(Exceptions::formatEmpty())
|
||||
}
|
||||
|
||||
fn decodeAlphanumericSegment(
|
||||
@@ -381,7 +368,7 @@ fn decodeAlphanumericSegment(
|
||||
let mut count = count;
|
||||
while count > 1 {
|
||||
if bits.available() < 11 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
let nextTwoCharsBits = bits.readBits(11)?;
|
||||
result.push(toAlphaNumericChar(nextTwoCharsBits / 45)?);
|
||||
@@ -391,7 +378,7 @@ fn decodeAlphanumericSegment(
|
||||
if count == 1 {
|
||||
// special case: one character left
|
||||
if bits.available() < 6 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
result.push(toAlphaNumericChar(bits.readBits(6)?)?);
|
||||
}
|
||||
@@ -402,14 +389,14 @@ fn decodeAlphanumericSegment(
|
||||
if result
|
||||
.chars()
|
||||
.nth(i)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
== '%'
|
||||
{
|
||||
if i < result.len() - 1
|
||||
&& result
|
||||
.chars()
|
||||
.nth(i + 1)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
|
||||
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
|
||||
== '%'
|
||||
{
|
||||
// %% is rendered as %
|
||||
@@ -435,11 +422,11 @@ fn decodeNumericSegment(
|
||||
while count >= 3 {
|
||||
// Each 10 bits encodes three digits
|
||||
if bits.available() < 10 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
let threeDigitsBits = bits.readBits(10)?;
|
||||
if threeDigitsBits >= 1000 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
result.push(toAlphaNumericChar(threeDigitsBits / 100)?);
|
||||
result.push(toAlphaNumericChar((threeDigitsBits / 10) % 10)?);
|
||||
@@ -449,22 +436,22 @@ fn decodeNumericSegment(
|
||||
if count == 2 {
|
||||
// Two digits left over to read, encoded in 7 bits
|
||||
if bits.available() < 7 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
let twoDigitsBits = bits.readBits(7)?;
|
||||
if twoDigitsBits >= 100 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
result.push(toAlphaNumericChar(twoDigitsBits / 10)?);
|
||||
result.push(toAlphaNumericChar(twoDigitsBits % 10)?);
|
||||
} else if count == 1 {
|
||||
// One digit left over to read
|
||||
if bits.available() < 4 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
let digitBits = bits.readBits(4)?;
|
||||
if digitBits >= 10 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
result.push(toAlphaNumericChar(digitBits)?);
|
||||
}
|
||||
@@ -489,5 +476,5 @@ fn parseECIValue(bits: &mut BitSource) -> Result<u32, Exceptions> {
|
||||
return Ok(((firstByte & 0x1F) << 16) | secondThirdBytes);
|
||||
}
|
||||
|
||||
Err(Exceptions::FormatException(None))
|
||||
Err(Exceptions::formatEmpty())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user