Merge branch 'main' into pr/point_refactor

This commit is contained in:
Vukašin Stepanović
2023-02-16 16:43:24 +00:00
132 changed files with 1048 additions and 1329 deletions

View File

@@ -102,7 +102,7 @@ impl Reader for DataMatrixReader {
DECODER.decode(&bits)?
}
} else {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFound);
};
// decoderRXingResult = DECODER.decode(detectorRXingResult.getBits())?;
@@ -178,10 +178,10 @@ impl DataMatrixReader {
*/
fn extractPureBits(&self, image: &BitMatrix) -> Result<BitMatrix> {
let Some(leftTopBlack) = image.getTopLeftOnBit() else {
return Err(Exceptions::NotFoundException(None))
return Err(Exceptions::notFound)
};
let Some(rightBottomBlack) = image.getBottomRightOnBit()else {
return Err(Exceptions::NotFoundException(None))
return Err(Exceptions::notFound)
};
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
@@ -194,7 +194,7 @@ impl DataMatrixReader {
let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
if matrixWidth <= 0 || matrixHeight <= 0 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFound);
// throw NotFoundException.getNotFoundInstance();
}
@@ -231,12 +231,12 @@ impl DataMatrixReader {
x += 1;
}
if x == width {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFound);
}
let moduleSize = x - leftTopBlack[0];
if moduleSize == 0 {
return Err(Exceptions::NotFoundException(None));
return Err(Exceptions::notFound);
}
Ok(moduleSize)

View File

@@ -61,21 +61,19 @@ impl Writer for DataMatrixWriter {
hints: &crate::EncodingHintDictionary,
) -> Result<crate::common::BitMatrix> {
if contents.is_empty() {
return Err(Exceptions::IllegalArgumentException(Some(
"Found empty contents".to_owned(),
)));
return Err(Exceptions::illegalArgumentWith("Found empty contents"));
}
if format != &BarcodeFormat::DATA_MATRIX {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Can only encode DATA_MATRIX, but got {format:?}"
))));
)));
}
if width < 0 || height < 0 {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Requested dimensions can't be negative: {width}x{height}"
))));
)));
}
// Try to get force shape & min / max size
@@ -124,7 +122,7 @@ impl Writer for DataMatrixWriter {
if hasEncodingHint {
let Some(EncodeHintValue::CharacterSet(char_set_name)) =
hints.get(&EncodeHintType::CHARACTER_SET) else {
return Err(Exceptions::IllegalArgumentException(Some("charset does not exist".to_owned())))
return Err(Exceptions::illegalArgumentWith("charset does not exist"))
};
charset = encoding::label::encoding_from_whatwg_label(char_set_name);
// charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
@@ -158,7 +156,7 @@ impl Writer for DataMatrixWriter {
let symbol_lookup = SymbolInfoLookup::new();
let Some(symbolInfo) = symbol_lookup.lookup_with_codewords_shape_size_fail(encoded.chars().count() as u32, *shape, &minSize, &maxSize, true)? else {
return Err(Exceptions::NotFoundException(Some("symbol info is bad".to_owned())))
return Err(Exceptions::notFoundWith("symbol info is bad"))
};
//2. step: ECC generation

View File

@@ -37,7 +37,7 @@ impl BitMatrixParser {
pub fn new(bitMatrix: &BitMatrix) -> Result<Self> {
let dimension = bitMatrix.getHeight();
if !(8..=144).contains(&dimension) || (dimension & 0x01) != 0 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
let version = Self::readVersion(bitMatrix)?;
@@ -181,7 +181,7 @@ impl BitMatrixParser {
}
if resultOffset != self.version.getTotalCodewords() as usize {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
Ok(result)
@@ -456,9 +456,9 @@ impl BitMatrixParser {
let symbolSizeColumns = version.getSymbolSizeColumns();
if bitMatrix.getHeight() != symbolSizeRows {
return Err(Exceptions::IllegalArgumentException(Some(
"Dimension of bitMatrix must match the version size".to_owned(),
)));
return Err(Exceptions::illegalArgumentWith(
"Dimension of bitMatrix must match the version size",
));
}
let dataRegionSizeRows = version.getDataRegionSizeRows();

View File

@@ -139,7 +139,7 @@ impl DataBlock {
}
if rawCodewordsOffset != rawCodewords.len() {
return Err(Exceptions::IllegalArgumentException(None));
return Err(Exceptions::illegalArgument);
}
Ok(result)

View File

@@ -158,7 +158,7 @@ pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult> {
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
mode = Mode::ASCII_ENCODE;
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::format),
}
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::format),
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::parse)?);
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(
"structured append tag must be first code word".to_owned(),
)));
return Err(Exceptions::formatWith(
"structured append tag must be first code word",
));
}
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::format);
}
}
}
@@ -387,27 +385,22 @@ fn decodeC40Segment(
let c40char = C40_BASIC_SET_CHARS[cValue as usize];
if upperShift {
result.append_char(
char::from_u32(c40char as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32(c40char as u32 + 128).ok_or(Exceptions::parse)?,
);
upperShift = false;
} else {
result.append_char(c40char);
}
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
}
1 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 128).ok_or(Exceptions::parse)?);
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::parse)?);
}
shift = 0;
}
@@ -416,8 +409,7 @@ fn decodeC40Segment(
let c40char = C40_SHIFT2_SET_CHARS[cValue as usize];
if upperShift {
result.append_char(
char::from_u32(c40char as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32(c40char as u32 + 128).ok_or(Exceptions::parse)?,
);
upperShift = false;
} else {
@@ -436,26 +428,22 @@ fn decodeC40Segment(
upperShift = true
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::format),
}
}
shift = 0;
}
3 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 224).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 224).ok_or(Exceptions::parse)?);
upperShift = false;
} else {
result.append_char(
char::from_u32(cValue + 96).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 96).ok_or(Exceptions::parse)?);
}
shift = 0;
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::format),
}
}
if bits.available() == 0 {
@@ -504,27 +492,22 @@ fn decodeTextSegment(
let textChar = TEXT_BASIC_SET_CHARS[cValue as usize];
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32(textChar as u32 + 128).ok_or(Exceptions::parse)?,
);
upperShift = false;
} else {
result.append_char(textChar);
}
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
}
1 => {
if upperShift {
result.append_char(
char::from_u32(cValue + 128).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 128).ok_or(Exceptions::parse)?);
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::parse)?);
}
shift = 0;
}
@@ -535,8 +518,7 @@ fn decodeTextSegment(
let textChar = TEXT_SHIFT2_SET_CHARS[cValue as usize];
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32(textChar as u32 + 128).ok_or(Exceptions::parse)?,
);
upperShift = false;
} else {
@@ -555,7 +537,7 @@ fn decodeTextSegment(
upperShift = true
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::format),
}
}
shift = 0;
@@ -565,8 +547,7 @@ fn decodeTextSegment(
let textChar = TEXT_SHIFT3_SET_CHARS[cValue as usize];
if upperShift {
result.append_char(
char::from_u32(textChar as u32 + 128)
.ok_or(Exceptions::ParseException(None))?,
char::from_u32(textChar as u32 + 128).ok_or(Exceptions::parse)?,
);
upperShift = false;
} else {
@@ -574,11 +555,11 @@ fn decodeTextSegment(
}
shift = 0;
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
}
_ => return Err(Exceptions::FormatException(None)),
_ => return Err(Exceptions::format),
}
}
if bits.available() == 0 {
@@ -641,16 +622,12 @@ fn decodeAnsiX12Segment(bits: &mut BitSource, result: &mut ECIStringBuilder) ->
_ => {
if cValue < 14 {
// 0 - 9
result.append_char(
char::from_u32(cValue + 44).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 44).ok_or(Exceptions::parse)?);
} else if cValue < 40 {
// A - Z
result.append_char(
char::from_u32(cValue + 51).ok_or(Exceptions::ParseException(None))?,
);
result.append_char(char::from_u32(cValue + 51).ok_or(Exceptions::parse)?);
} else {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
}
}
@@ -702,8 +679,7 @@ fn decodeEdifactSegment(bits: &mut BitSource, result: &mut ECIStringBuilder) ->
// 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::parse)?);
}
if bits.available() == 0 {
@@ -740,7 +716,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];
@@ -748,7 +724,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::format);
}
*byte = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8;
codewordPosition += 1;
@@ -756,7 +732,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::parseWith(e))?,
);
byteSegments.push(bytes);

View File

@@ -103,7 +103,7 @@ impl Version {
*/
pub fn getVersionForDimensions(numRows: u32, numColumns: u32) -> Result<&'static Version> {
if (numRows & 0x01) != 0 || (numColumns & 0x01) != 0 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
for version in VERSIONS.iter() {
@@ -112,7 +112,7 @@ impl Version {
}
}
Err(Exceptions::FormatException(None))
Err(Exceptions::format)
}
/**

View File

@@ -55,9 +55,7 @@ impl<'a> Detector<'_> {
if let Some(point) = self.correctTopRight(&points) {
points[3] = point;
} else {
return Err(Exceptions::NotFoundException(Some(
"point 4 unfound".to_owned(),
)));
return Err(Exceptions::notFoundWith("point 4 unfound"));
}
// points[3] = self.correctTopRight(&points);
// if points[3] == null {

View File

@@ -247,7 +247,7 @@ fn Scan(
));
}
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFound)
}
pub fn detect(
@@ -351,6 +351,6 @@ pub fn detect(
}
// #ifndef __cpp_impl_coroutine
Err(Exceptions::NotFoundException(None))
Err(Exceptions::notFound)
// #endif
}

View File

@@ -76,7 +76,7 @@ impl RegressionLine for DMRegressionLine {
fn add(&mut self, p: Point) -> Result<()> {
if self.direction_inward == Point::default() {
return Err(Exceptions::IllegalStateException(None));
return Err(Exceptions::illegalState);
}
self.points.push(p);
if self.points.len() == 1 {
@@ -237,7 +237,7 @@ impl DMRegressionLine {
pub fn modules(&mut self, beg: Point, end: Point) -> Result<f64> {
if self.points.len() <= 3 {
return Err(Exceptions::IllegalStateException(None));
return Err(Exceptions::illegalState);
}
// re-evaluate and filter out all points too far away. required for the gapSizes calculation.
@@ -263,12 +263,12 @@ impl DMRegressionLine {
self.points
.last()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
.ok_or(Exceptions::indexOutOfBounds)?
- self
.points
.first()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
)) as f64;
// calculate the width of 2 modules (first black pixel to first black pixel)
@@ -295,7 +295,7 @@ impl DMRegressionLine {
self.points
.last()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
),
) as f64,
);

View File

@@ -203,7 +203,7 @@ impl<'a> EdgeTracer<'_> {
if self.whiteAt(pEdge) {
// if we are not making any progress, we still have another endless loop bug
if self.p == pEdge.centered() {
return Err(Exceptions::IllegalStateException(None));
return Err(Exceptions::illegalState);
}
self.p = pEdge.centered();
@@ -274,7 +274,7 @@ impl<'a> EdgeTracer<'_> {
.points()
.first()
.as_ref()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
) {
return Ok(false);
}
@@ -304,9 +304,9 @@ impl<'a> EdgeTracer<'_> {
.points()
.last()
.as_ref()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?)
.ok_or(Exceptions::indexOutOfBounds)?)
{
return Err(Exceptions::IllegalStateException(None));
return Err(Exceptions::illegalState);
}
if !line.points().is_empty()
&& &&self.p
@@ -314,7 +314,7 @@ impl<'a> EdgeTracer<'_> {
.points()
.last()
.as_ref()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
.ok_or(Exceptions::indexOutOfBounds)?
{
return Ok(false);
}
@@ -358,7 +358,7 @@ impl<'a> EdgeTracer<'_> {
line.points()
.last()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
),
) < 1.0
{
@@ -376,7 +376,7 @@ impl<'a> EdgeTracer<'_> {
.points()
.last()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
)
};
line.add(self.p)?;
@@ -393,7 +393,7 @@ impl<'a> EdgeTracer<'_> {
.points()
.first()
.copied()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
) {
return Ok(false);
}

View File

@@ -24,7 +24,7 @@ pub fn float_max<T: PartialOrd>(a: T, b: T) -> T {
#[inline(always)]
pub fn intersect(l1: &DMRegressionLine, l2: &DMRegressionLine) -> Result<Point> {
if !(l1.isValid() && l2.isValid()) {
return Err(Exceptions::IllegalStateException(None));
return Err(Exceptions::illegalState);
}
let d = l1.a * l2.b - l1.b * l2.a;
let x = (l1.c * l2.b - l1.b * l2.c) / d;

View File

@@ -32,12 +32,12 @@ impl Encoder for ASCIIEncoder {
.getMessage()
.chars()
.nth(context.pos as usize)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
context
.getMessage()
.chars()
.nth(context.pos as usize + 1)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
)? as u8);
context.pos += 2;
} else {
@@ -74,9 +74,9 @@ impl Encoder for ASCIIEncoder {
}
_ => {
return Err(Exceptions::IllegalStateException(Some(format!(
return Err(Exceptions::illegalStateWith(format!(
"Illegal mode: {newMode}"
))));
)));
}
}
} else if high_level_encoder::isExtendedASCII(c) {
@@ -105,9 +105,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::illegalArgumentWith(format!(
"not digits: {digit1}{digit2}"
))))
)))
}
}
}

View File

@@ -54,7 +54,7 @@ impl Encoder for Base256Encoder {
context.updateSymbolInfoWithLength(currentSize);
let mustPad = (context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- currentSize as u32)
> 0;
@@ -63,29 +63,28 @@ impl Encoder for Base256Encoder {
buffer.replace_range(
0..1,
&char::from_u32(dataCount as u32)
.ok_or(Exceptions::ParseException(None))?
.ok_or(Exceptions::parse)?
.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::parse)?
.to_string(),
);
let (ci_pos, _) = buffer
.char_indices()
.nth(1)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
.ok_or(Exceptions::indexOutOfBounds)?;
buffer.insert(
ci_pos,
char::from_u32(dataCount as u32 % 250)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
char::from_u32(dataCount as u32 % 250).ok_or(Exceptions::indexOutOfBounds)?,
);
} else {
return Err(Exceptions::IllegalStateException(Some(format!(
return Err(Exceptions::illegalStateWith(format!(
"Message length not in valid ranges: {dataCount}"
))));
)));
}
}
let c = buffer.chars().count();
@@ -93,13 +92,10 @@ impl Encoder for Base256Encoder {
// for (int i = 0, c = buffer.length(); i < c; i++) {
context.writeCodeword(
Self::randomize255State(
buffer
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
buffer.chars().nth(i).ok_or(Exceptions::indexOutOfBounds)?,
context.getCodewordCount() as u32 + 1,
)
.ok_or(Exceptions::ParseException(None))? as u8,
.ok_or(Exceptions::parse)? as u8,
);
}
Ok(())

View File

@@ -66,7 +66,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
@@ -141,7 +141,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
let rest = buffer.chars().count() % 3;
@@ -205,7 +205,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
@@ -234,9 +234,9 @@ impl C40Encoder {
context.writeCodeword(C40_UNLATCH);
}
} else {
return Err(Exceptions::IllegalStateException(Some(
"Unexpected case. Please report!".to_owned(),
)));
return Err(Exceptions::illegalStateWith(
"Unexpected case. Please report!",
));
}
context.signalEncoderChange(ASCII_ENCODATION);

View File

@@ -165,7 +165,7 @@ impl DefaultPlacement {
.codewords
.chars()
.nth(pos)
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as u32;
.ok_or(Exceptions::indexOutOfBounds)? as u32;
v &= 1 << (8 - bit);
self.setBit(col as usize, row as usize, v != 0);

View File

@@ -77,7 +77,7 @@ impl EdifactEncoder {
context.updateSymbolInfo();
let mut available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
let remaining = context.getRemainingCharacters();
@@ -86,7 +86,7 @@ impl EdifactEncoder {
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1);
available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
}
@@ -96,9 +96,7 @@ impl EdifactEncoder {
}
if count > 4 {
return Err(Exceptions::IllegalStateException(Some(
"Count must not exceed 4".to_owned(),
)));
return Err(Exceptions::illegalStateWith("Count must not exceed 4"));
}
let restChars = count - 1;
let encoded = Self::encodeToCodewords(buffer)?;
@@ -109,7 +107,7 @@ impl EdifactEncoder {
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
if available >= 3 {
@@ -150,32 +148,23 @@ impl EdifactEncoder {
fn encodeToCodewords(sb: &str) -> Result<String> {
let len = sb.chars().count();
if len == 0 {
return Err(Exceptions::IllegalStateException(Some(
"StringBuilder must not be empty".to_owned(),
)));
return Err(Exceptions::illegalStateWith(
"StringBuilder must not be empty",
));
}
let c1 = sb
.chars()
.next()
.ok_or(Exceptions::IndexOutOfBoundsException(None))?;
let c1 = sb.chars().next().ok_or(Exceptions::indexOutOfBounds)?;
let c2 = if len >= 2 {
sb.chars()
.nth(1)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
sb.chars().nth(1).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
let c3 = if len >= 3 {
sb.chars()
.nth(2)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
sb.chars().nth(2).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
let c4 = if len >= 4 {
sb.chars()
.nth(3)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
sb.chars().nth(3).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
@@ -185,12 +174,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::indexOutOfBounds)?);
if len >= 2 {
res.push(char::from_u32(cw2).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
res.push(char::from_u32(cw2).ok_or(Exceptions::indexOutOfBounds)?);
}
if len >= 3 {
res.push(char::from_u32(cw3).ok_or(Exceptions::IndexOutOfBoundsException(None))?);
res.push(char::from_u32(cw3).ok_or(Exceptions::indexOutOfBounds)?);
}
Ok(res)

View File

@@ -64,14 +64,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::parseWith(format!("round trip decode should always work: {e}"))
})?
} else {
return Err(Exceptions::IllegalArgumentException(Some(
"Message contains characters outside ISO-8859-1 encoding.".to_owned(),
)));
return Err(Exceptions::illegalArgumentWith(
"Message contains characters outside ISO-8859-1 encoding.",
));
};
Ok(Self {
symbol_lookup: Rc::new(SymbolInfoLookup::new()),

View File

@@ -155,9 +155,9 @@ const ALOG: [u32; 255] = {
*/
pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String> {
if codewords.chars().count() != symbolInfo.getDataCapacity() as usize {
return Err(Exceptions::IllegalArgumentException(Some(
"The number of codewords does not match the selected symbol".to_owned(),
)));
return Err(Exceptions::illegalArgumentWith(
"The number of codewords does not match the selected symbol",
));
}
let mut sb = String::with_capacity(
(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords()) as usize,
@@ -186,7 +186,7 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String>
codewords
.chars()
.nth(d)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?,
.ok_or(Exceptions::indexOutOfBounds)?,
);
d += blockCount;
@@ -199,12 +199,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::indexOutOfBounds)?;
sb.replace_range(
char_index..(replace_char.len_utf8()),
&ecc.chars()
.nth(pos)
.ok_or(Exceptions::IndexOutOfBoundsException(None))?
.ok_or(Exceptions::indexOutOfBounds)?
.to_string(),
);
// sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos));
@@ -229,9 +229,9 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String> {
}
}
if table < 0 {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Illegal number of error correction codewords specified: {numECWords}"
))));
)));
}
let poly = &FACTORS[table as usize];
let mut ecc = vec![0 as char; numECWords];
@@ -245,21 +245,21 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String> {
^ codewords
.chars()
.nth(i)
.ok_or(Exceptions::IndexOutOfBoundsException(None))? as usize;
.ok_or(Exceptions::indexOutOfBounds)? 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::indexOutOfBounds)?;
} 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::indexOutOfBounds)?;
} else {
ecc[0] = 0 as char;
}

View File

@@ -222,18 +222,14 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
if forceC40 {
c40Encoder.encodeMaximalC40(&mut context)?;
encodingMode = context
.getNewEncoding()
.ok_or(Exceptions::IllegalStateException(None))?;
encodingMode = context.getNewEncoding().ok_or(Exceptions::illegalState)?;
context.resetEncoderSignal();
}
while context.hasMoreCharacters() {
encoders[encodingMode].encode(&mut context)?;
if context.getNewEncoding().is_some() {
encodingMode = context
.getNewEncoding()
.ok_or(Exceptions::IllegalStateException(None))?;
encodingMode = context.getNewEncoding().ok_or(Exceptions::illegalState)?;
context.resetEncoderSignal();
}
}
@@ -241,7 +237,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
context.updateSymbolInfo();
let capacity = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity();
if len < capacity as usize
&& encodingMode != ASCII_ENCODATION
@@ -612,7 +608,7 @@ pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 {
pub fn illegalCharacter(c: char) -> Result<()> {
// let hex = Integer.toHexString(c);
// hex = "0000".substring(0, 4 - hex.length()) + hex;
Err(Exceptions::IllegalArgumentException(Some(format!(
Err(Exceptions::illegalArgumentWith(format!(
"Illegal character: {c} (0x{c})"
))))
)))
}

View File

@@ -218,7 +218,7 @@ fn addEdge(edges: &mut [Vec<Option<Rc<Edge>>>], edge: Rc<Edge>) -> Result<()> {
if edges[vertexIndex][edge.getEndMode()?.ordinal()].is_none()
|| edges[vertexIndex][edge.getEndMode()?.ordinal()]
.as_ref()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.cachedTotalSize
> edge.cachedTotalSize
{
@@ -635,9 +635,9 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult> {
}
if minimalJ < 0 {
return Err(Exceptions::IllegalStateException(Some(format!(
return Err(Exceptions::illegalStateWith(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> {
if fromPosition + characterLength > input.length() as u32 {
return Err(Exceptions::FormatException(None));
return Err(Exceptions::format);
}
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::illegalArgument);
};
let input = solution.input.clone();
let mut size = 0;

View File

@@ -129,9 +129,9 @@ impl SymbolInfo {
2 | 4 => Ok(2),
16 => Ok(4),
36 => Ok(6),
_ => Err(Exceptions::IllegalStateException(Some(
"Cannot handle this number of data regions".to_owned(),
))),
_ => Err(Exceptions::illegalStateWith(
"Cannot handle this number of data regions",
)),
}
}
@@ -141,9 +141,9 @@ impl SymbolInfo {
4 => Ok(2),
16 => Ok(4),
36 => Ok(6),
_ => Err(Exceptions::IllegalStateException(Some(
"Cannot handle this number of data regions".to_owned(),
))),
_ => Err(Exceptions::illegalStateWith(
"Cannot handle this number of data regions",
)),
}
}
@@ -311,9 +311,9 @@ impl<'a> SymbolInfoLookup<'a> {
}
}
if fail {
return Err(Exceptions::IllegalArgumentException(Some(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Can't find a symbol arrangement that matches the message. Data codewords: {dataCodewords}"
))));
)));
}
Ok(None)
}

View File

@@ -82,7 +82,7 @@ impl X12Encoder {
context.updateSymbolInfo();
let available = context
.getSymbolInfo()
.ok_or(Exceptions::IllegalStateException(None))?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
let count = buffer.chars().count();