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:
@@ -158,7 +158,7 @@ pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult, Exce
|
||||
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
|
||||
mode = Mode::ASCII_ENCODE;
|
||||
}
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
|
||||
if !(mode != Mode::PAD_ENCODE && bits.available() > 0) {
|
||||
@@ -225,16 +225,14 @@ fn decodeAsciiSegment(
|
||||
loop {
|
||||
let mut oneByte = bits.readBits(8)?;
|
||||
match oneByte {
|
||||
0 => return Err(Exceptions::FormatException(None)),
|
||||
0 => return Err(Exceptions::formatEmpty()),
|
||||
1..=128 => {
|
||||
// ASCII data (ASCII value + 1)
|
||||
if upperShift {
|
||||
oneByte += 128;
|
||||
//upperShift = false;
|
||||
}
|
||||
result.append_char(
|
||||
char::from_u32(oneByte - 1).ok_or(Exceptions::ParseException(None))?,
|
||||
);
|
||||
result.append_char(char::from_u32(oneByte - 1).ok_or(Exceptions::parseEmpty())?);
|
||||
return Ok(Mode::ASCII_ENCODE);
|
||||
}
|
||||
129 => return Ok(Mode::PAD_ENCODE), // Pad
|
||||
@@ -280,9 +278,9 @@ fn decodeAsciiSegment(
|
||||
if !firstCodeword
|
||||
// Must be first ISO 16022:2006 5.6.1
|
||||
{
|
||||
return Err(Exceptions::FormatException(Some(
|
||||
return Err(Exceptions::format(
|
||||
"structured append tag must be first code word".to_owned(),
|
||||
)));
|
||||
));
|
||||
}
|
||||
parse_structured_append(bits, &mut sai)?;
|
||||
firstFNC1Position = 5;
|
||||
@@ -333,7 +331,7 @@ fn decodeAsciiSegment(
|
||||
// Not to be used in ASCII encodation
|
||||
// but work around encoders that end with 254, latch back to ASCII
|
||||
if oneByte != 254 || bits.available() != 0 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,26 +386,24 @@ fn decodeC40Segment(
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(c40char as u32 + 128)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
.ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
result.append_char(c40char);
|
||||
}
|
||||
} else {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 128).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
result.append_char(
|
||||
char::from_u32(cValue).ok_or(Exceptions::ParseException(None))?,
|
||||
);
|
||||
result.append_char(char::from_u32(cValue).ok_or(Exceptions::parseEmpty())?);
|
||||
}
|
||||
shift = 0;
|
||||
}
|
||||
@@ -417,7 +413,7 @@ fn decodeC40Segment(
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(c40char as u32 + 128)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
.ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
@@ -436,7 +432,7 @@ fn decodeC40Segment(
|
||||
upperShift = true
|
||||
}
|
||||
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
}
|
||||
shift = 0;
|
||||
@@ -444,18 +440,18 @@ fn decodeC40Segment(
|
||||
3 => {
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 224).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 224).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 96).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 96).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
}
|
||||
shift = 0;
|
||||
}
|
||||
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
}
|
||||
if bits.available() == 0 {
|
||||
@@ -505,26 +501,24 @@ fn decodeTextSegment(
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(textChar as u32 + 128)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
.ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
result.append_char(textChar);
|
||||
}
|
||||
} else {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 128).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
result.append_char(
|
||||
char::from_u32(cValue).ok_or(Exceptions::ParseException(None))?,
|
||||
);
|
||||
result.append_char(char::from_u32(cValue).ok_or(Exceptions::parseEmpty())?);
|
||||
}
|
||||
shift = 0;
|
||||
}
|
||||
@@ -536,7 +530,7 @@ fn decodeTextSegment(
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(textChar as u32 + 128)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
.ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
@@ -555,7 +549,7 @@ fn decodeTextSegment(
|
||||
upperShift = true
|
||||
}
|
||||
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
}
|
||||
shift = 0;
|
||||
@@ -566,7 +560,7 @@ fn decodeTextSegment(
|
||||
if upperShift {
|
||||
result.append_char(
|
||||
char::from_u32(textChar as u32 + 128)
|
||||
.ok_or(Exceptions::ParseException(None))?,
|
||||
.ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
upperShift = false;
|
||||
} else {
|
||||
@@ -574,11 +568,11 @@ fn decodeTextSegment(
|
||||
}
|
||||
shift = 0;
|
||||
} else {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
_ => return Err(Exceptions::formatEmpty()),
|
||||
}
|
||||
}
|
||||
if bits.available() == 0 {
|
||||
@@ -645,15 +639,15 @@ fn decodeAnsiX12Segment(
|
||||
if cValue < 14 {
|
||||
// 0 - 9
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 44).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 44).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
} else if cValue < 40 {
|
||||
// A - Z
|
||||
result.append_char(
|
||||
char::from_u32(cValue + 51).ok_or(Exceptions::ParseException(None))?,
|
||||
char::from_u32(cValue + 51).ok_or(Exceptions::parseEmpty())?,
|
||||
);
|
||||
} else {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -708,8 +702,7 @@ fn decodeEdifactSegment(
|
||||
// no 1 in the leading (6th) bit
|
||||
edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
|
||||
}
|
||||
result
|
||||
.append_char(char::from_u32(edifactValue).ok_or(Exceptions::ParseException(None))?);
|
||||
result.append_char(char::from_u32(edifactValue).ok_or(Exceptions::parseEmpty())?);
|
||||
}
|
||||
|
||||
if bits.available() == 0 {
|
||||
@@ -746,7 +739,7 @@ fn decodeBase256Segment(
|
||||
// We're seeing NegativeArraySizeException errors from users.
|
||||
// but we shouldn't in rust because it's unsigned
|
||||
// if count < 0 {
|
||||
// return Err(Exceptions::FormatException(None));
|
||||
// return Err(Exceptions::formatEmpty());
|
||||
// }
|
||||
|
||||
let mut bytes = vec![0u8; count as usize];
|
||||
@@ -754,7 +747,7 @@ fn decodeBase256Segment(
|
||||
// Have seen this particular error in the wild, such as at
|
||||
// http://www.bcgen.com/demo/IDAutomationStreamingDataMatrix.aspx?MODE=3&D=Fred&PFMT=3&PT=F&X=0.3&O=0&LM=0.2
|
||||
if bits.available() < 8 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
return Err(Exceptions::formatEmpty());
|
||||
}
|
||||
*byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8;
|
||||
codewordPosition += 1;
|
||||
@@ -762,7 +755,7 @@ fn decodeBase256Segment(
|
||||
result.append_string(
|
||||
&encoding::all::ISO_8859_1
|
||||
.decode(&bytes, encoding::DecoderTrap::Strict)
|
||||
.map_err(|e| Exceptions::ParseException(Some(e.to_string())))?,
|
||||
.map_err(|e| Exceptions::parse(e.to_string()))?,
|
||||
);
|
||||
byteSegments.push(bytes);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user