rename helper methods

This commit is contained in:
Vukašin Stepanović
2023-02-15 12:52:59 +00:00
parent bfcdb397ad
commit 935519ced5
133 changed files with 858 additions and 1044 deletions

View File

@@ -31,12 +31,12 @@ impl Encoder for ASCIIEncoder {
.getMessage()
.chars()
.nth(context.pos as usize)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
.ok_or(Exceptions::indexOutOfBounds)?,
context
.getMessage()
.chars()
.nth(context.pos as usize + 1)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
.ok_or(Exceptions::indexOutOfBounds)?,
)? as u8);
context.pos += 2;
} else {
@@ -73,7 +73,9 @@ impl Encoder for ASCIIEncoder {
}
_ => {
return Err(Exceptions::illegalState(format!("Illegal mode: {newMode}")));
return Err(Exceptions::illegalStateWith(format!(
"Illegal mode: {newMode}"
)));
}
}
} else if high_level_encoder::isExtendedASCII(c) {
@@ -102,7 +104,7 @@ impl ASCIIEncoder {
let num = (digit1 as u8 - 48) * 10 + (digit2 as u8 - 48);
Ok((num + 130) as char)
} else {
Err(Exceptions::illegalArgument(format!(
Err(Exceptions::illegalArgumentWith(format!(
"not digits: {digit1}{digit2}"
)))
}

View File

@@ -53,7 +53,7 @@ impl Encoder for Base256Encoder {
context.updateSymbolInfoWithLength(currentSize);
let mustPad = (context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- currentSize as u32)
> 0;
@@ -62,27 +62,26 @@ impl Encoder for Base256Encoder {
buffer.replace_range(
0..1,
&char::from_u32(dataCount as u32)
.ok_or(Exceptions::parseEmpty())?
.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::parseEmpty())?
.ok_or(Exceptions::parse)?
.to_string(),
);
let (ci_pos, _) = buffer
.char_indices()
.nth(1)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
.ok_or(Exceptions::indexOutOfBounds)?;
buffer.insert(
ci_pos,
char::from_u32(dataCount as u32 % 250)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
char::from_u32(dataCount as u32 % 250).ok_or(Exceptions::indexOutOfBounds)?,
);
} else {
return Err(Exceptions::illegalState(format!(
return Err(Exceptions::illegalStateWith(format!(
"Message length not in valid ranges: {dataCount}"
)));
}
@@ -92,13 +91,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::indexOutOfBoundsEmpty())?,
buffer.chars().nth(i).ok_or(Exceptions::indexOutOfBounds)?,
context.getCodewordCount() as u32 + 1,
)
.ok_or(Exceptions::parseEmpty())? as u8,
.ok_or(Exceptions::parse)? as u8,
);
}
Ok(())

View File

@@ -65,7 +65,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
@@ -140,7 +140,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
let rest = buffer.chars().count() % 3;
@@ -182,7 +182,7 @@ impl C40Encoder {
context: &mut EncoderContext,
buffer: &mut String,
) -> Result<(), Exceptions> {
context.writeCodewords(&Self::encodeToCodewords(buffer).ok_or(Exceptions::formatEmpty())?);
context.writeCodewords(&Self::encodeToCodewords(buffer).ok_or(Exceptions::format)?);
buffer.replace_range(0..3, "");
// buffer.delete(0, 3);
Ok(())
@@ -205,7 +205,7 @@ impl C40Encoder {
context.updateSymbolInfoWithLength(curCodewordCount);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity() as usize
- curCodewordCount;
@@ -234,7 +234,9 @@ impl C40Encoder {
context.writeCodeword(C40_UNLATCH);
}
} else {
return Err(Exceptions::illegalState("Unexpected case. Please report!"));
return Err(Exceptions::illegalStateWith(
"Unexpected case. Please report!",
));
}
context.signalEncoderChange(ASCII_ENCODATION);

View File

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

View File

@@ -76,7 +76,7 @@ impl EdifactEncoder {
context.updateSymbolInfo();
let mut available = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.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::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
}
@@ -95,7 +95,7 @@ impl EdifactEncoder {
}
if count > 4 {
return Err(Exceptions::illegalState("Count must not exceed 4"));
return Err(Exceptions::illegalStateWith("Count must not exceed 4"));
}
let restChars = count - 1;
let encoded = Self::encodeToCodewords(buffer)?;
@@ -106,7 +106,7 @@ impl EdifactEncoder {
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
let available = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity()
- context.getCodewordCount() as u32;
if available >= 3 {
@@ -147,30 +147,23 @@ impl EdifactEncoder {
fn encodeToCodewords(sb: &str) -> Result<String, Exceptions> {
let len = sb.chars().count();
if len == 0 {
return Err(Exceptions::illegalState("StringBuilder must not be empty"));
return Err(Exceptions::illegalStateWith(
"StringBuilder must not be empty",
));
}
let c1 = sb
.chars()
.next()
.ok_or(Exceptions::indexOutOfBoundsEmpty())?;
let c1 = sb.chars().next().ok_or(Exceptions::indexOutOfBounds)?;
let c2 = if len >= 2 {
sb.chars()
.nth(1)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
sb.chars().nth(1).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
let c3 = if len >= 3 {
sb.chars()
.nth(2)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
sb.chars().nth(2).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
let c4 = if len >= 4 {
sb.chars()
.nth(3)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
sb.chars().nth(3).ok_or(Exceptions::indexOutOfBounds)?
} else {
0 as char
};
@@ -180,12 +173,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::indexOutOfBoundsEmpty())?);
res.push(char::from_u32(cw1).ok_or(Exceptions::indexOutOfBounds)?);
if len >= 2 {
res.push(char::from_u32(cw2).ok_or(Exceptions::indexOutOfBoundsEmpty())?);
res.push(char::from_u32(cw2).ok_or(Exceptions::indexOutOfBounds)?);
}
if len >= 3 {
res.push(char::from_u32(cw3).ok_or(Exceptions::indexOutOfBoundsEmpty())?);
res.push(char::from_u32(cw3).ok_or(Exceptions::indexOutOfBounds)?);
}
Ok(res)

View File

@@ -63,10 +63,10 @@ impl<'a> EncoderContext<'_> {
ISO_8859_1_ENCODER
.decode(&encoded_bytes, encoding::DecoderTrap::Strict)
.map_err(|e| {
Exceptions::parse(format!("round trip decode should always work: {e}"))
Exceptions::parseWith(format!("round trip decode should always work: {e}"))
})?
} else {
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"Message contains characters outside ISO-8859-1 encoding.",
));
};

View File

@@ -154,7 +154,7 @@ 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::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"The number of codewords does not match the selected symbol",
));
}
@@ -185,7 +185,7 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
codewords
.chars()
.nth(d)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?,
.ok_or(Exceptions::indexOutOfBounds)?,
);
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::indexOutOfBoundsEmpty())?;
.ok_or(Exceptions::indexOutOfBounds)?;
sb.replace_range(
char_index..(replace_char.len_utf8()),
&ecc.chars()
.nth(pos)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
.to_string(),
);
// sb.setCharAt(symbolInfo.getDataCapacity() + e, ecc.charAt(pos));
@@ -228,7 +228,7 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptio
}
}
if table < 0 {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Illegal number of error correction codewords specified: {numECWords}"
)));
}
@@ -244,21 +244,21 @@ fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptio
^ codewords
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBoundsEmpty())? 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::indexOutOfBoundsEmpty())?;
.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::indexOutOfBoundsEmpty())?;
.ok_or(Exceptions::indexOutOfBounds)?;
} else {
ecc[0] = 0 as char;
}

View File

@@ -221,18 +221,14 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
if forceC40 {
c40Encoder.encodeMaximalC40(&mut context)?;
encodingMode = context
.getNewEncoding()
.ok_or(Exceptions::illegalStateEmpty())?;
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::illegalStateEmpty())?;
encodingMode = context.getNewEncoding().ok_or(Exceptions::illegalState)?;
context.resetEncoderSignal();
}
}
@@ -240,7 +236,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
context.updateSymbolInfo();
let capacity = context
.getSymbolInfo()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.getDataCapacity();
if len < capacity as usize
&& encodingMode != ASCII_ENCODATION
@@ -611,7 +607,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::illegalArgument(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<(), Ex
if edges[vertexIndex][edge.getEndMode()?.ordinal()].is_none()
|| edges[vertexIndex][edge.getEndMode()?.ordinal()]
.as_ref()
.ok_or(Exceptions::illegalStateEmpty())?
.ok_or(Exceptions::illegalState)?
.cachedTotalSize
> edge.cachedTotalSize
{
@@ -635,7 +635,7 @@ fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
}
if minimalJ < 0 {
return Err(Exceptions::illegalState(format!(
return Err(Exceptions::illegalStateWith(format!(
"Internal error: failed to encode \"{input}\""
)));
}
@@ -669,7 +669,7 @@ impl Edge {
previous: Option<Rc<Edge>>,
) -> Result<Self, Exceptions> {
if fromPosition + characterLength > input.length() as u32 {
return Err(Exceptions::formatEmpty());
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::illegalArgumentEmpty());
return Err(Exceptions::illegalArgument);
};
let input = solution.input.clone();
let mut size = 0;

View File

@@ -128,7 +128,7 @@ impl SymbolInfo {
2 | 4 => Ok(2),
16 => Ok(4),
36 => Ok(6),
_ => Err(Exceptions::illegalState(
_ => Err(Exceptions::illegalStateWith(
"Cannot handle this number of data regions",
)),
}
@@ -140,7 +140,7 @@ impl SymbolInfo {
4 => Ok(2),
16 => Ok(4),
36 => Ok(6),
_ => Err(Exceptions::illegalState(
_ => Err(Exceptions::illegalStateWith(
"Cannot handle this number of data regions",
)),
}
@@ -310,7 +310,7 @@ impl<'a> SymbolInfoLookup<'a> {
}
}
if fail {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Can't find a symbol arrangement that matches the message. Data codewords: {dataCodewords}"
)));
}

View File

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