mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 20:32:34 +00:00
refactor to use exception helper factories
This commit is contained in:
@@ -36,9 +36,9 @@ impl BitMatrixParser {
|
||||
pub fn new(bit_matrix: BitMatrix) -> Result<Self, Exceptions> {
|
||||
let dimension = bit_matrix.getHeight();
|
||||
if dimension < 21 || (dimension & 0x03) != 1 {
|
||||
Err(Exceptions::FormatException(Some(format!(
|
||||
Err(Exceptions::format(format!(
|
||||
"{dimension} < 21 || ({dimension} % 0x03) != 1"
|
||||
))))
|
||||
)))
|
||||
} else {
|
||||
Ok(Self {
|
||||
bitMatrix: bit_matrix,
|
||||
@@ -61,7 +61,7 @@ impl BitMatrixParser {
|
||||
return self
|
||||
.parsedFormatInfo
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::ParseException(None));
|
||||
.ok_or(Exceptions::parseEmpty());
|
||||
}
|
||||
|
||||
// Read top-left format info bits
|
||||
@@ -94,7 +94,7 @@ impl BitMatrixParser {
|
||||
|
||||
self.parsedFormatInfo
|
||||
.as_ref()
|
||||
.ok_or(Exceptions::FormatException(None))
|
||||
.ok_or(Exceptions::formatEmpty())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +146,7 @@ impl BitMatrixParser {
|
||||
return Ok(theParsedVersion);
|
||||
}
|
||||
}
|
||||
Err(Exceptions::FormatException(None))
|
||||
Err(Exceptions::formatEmpty())
|
||||
}
|
||||
|
||||
fn copyBit(&self, i: u32, j: u32, versionBits: u32) -> u32 {
|
||||
@@ -227,7 +227,7 @@ impl BitMatrixParser {
|
||||
}
|
||||
|
||||
if resultOffset != version.getTotalCodewords() as usize {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ impl DataBlock {
|
||||
ecLevel: ErrorCorrectionLevel,
|
||||
) -> Result<Vec<Self>, Exceptions> {
|
||||
if rawCodewords.len() as u32 != version.getTotalCodewords() {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
return Err(Exceptions::illegalArgumentEmpty());
|
||||
}
|
||||
|
||||
// Figure out the number and size of data blocks used by this version and
|
||||
|
||||
@@ -228,9 +228,9 @@ impl TryFrom<u8> for DataMask {
|
||||
5 => Ok(DataMask::DATA_MASK_101),
|
||||
6 => Ok(DataMask::DATA_MASK_110),
|
||||
7 => Ok(DataMask::DATA_MASK_111),
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
_ => Err(Exceptions::illegalArgument(format!(
|
||||
"{value} is not between 0 and 7"
|
||||
)))),
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -47,9 +47,9 @@ impl ErrorCorrectionLevel {
|
||||
1 => Ok(Self::L),
|
||||
2 => Ok(Self::H),
|
||||
3 => Ok(Self::Q),
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
_ => Err(Exceptions::illegalArgument(format!(
|
||||
"{bits} is not a valid bit selection"
|
||||
)))),
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,8 +109,8 @@ impl FromStr for ErrorCorrectionLevel {
|
||||
return number_possible.try_into();
|
||||
}
|
||||
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
return Err(Exceptions::illegalArgument(format!(
|
||||
"could not parse {s} into an ec level"
|
||||
))));
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,9 +68,7 @@ impl Mode {
|
||||
{
|
||||
Ok(Self::HANZI)
|
||||
}
|
||||
_ => Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"{bits} is not valid"
|
||||
)))),
|
||||
_ => Err(Exceptions::illegalArgument(format!("{bits} is not valid"))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ pub fn decode_bitmatrix_with_hints(
|
||||
if let Some(fe) = fe {
|
||||
Err(fe)
|
||||
} else {
|
||||
Err(ce.unwrap_or(Exceptions::ChecksumException(None)))
|
||||
Err(ce.unwrap_or(Exceptions::checksumEmpty()))
|
||||
}
|
||||
}
|
||||
_ => Err(er),
|
||||
|
||||
@@ -101,18 +101,16 @@ impl Version {
|
||||
dimension: u32,
|
||||
) -> Result<&'static Version, Exceptions> {
|
||||
if dimension % 4 != 1 {
|
||||
return Err(Exceptions::FormatException(Some(
|
||||
"dimension incorrect".to_owned(),
|
||||
)));
|
||||
return Err(Exceptions::format("dimension incorrect".to_owned()));
|
||||
}
|
||||
Self::getVersionForNumber((dimension - 17) / 4)
|
||||
}
|
||||
|
||||
pub fn getVersionForNumber(versionNumber: u32) -> Result<&'static Version, Exceptions> {
|
||||
if !(1..=40).contains(&versionNumber) {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
return Err(Exceptions::illegalArgument(
|
||||
"version out of spec".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
Ok(&VERSIONS[versionNumber as usize - 1])
|
||||
}
|
||||
@@ -140,7 +138,7 @@ impl Version {
|
||||
return Self::getVersionForNumber(bestVersion);
|
||||
}
|
||||
// If we didn't find a close enough match, fail
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
Err(Exceptions::notFoundEmpty())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user