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

@@ -99,7 +99,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
let length = contents.chars().count();
// Check length
if !(1..=80).contains(&length) {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Contents length should be between 1 and 80 characters, but got {length}"
)));
}
@@ -107,13 +107,13 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
// Check for forced code set hint.
let mut forcedCodeSet = -1_i32;
if hints.contains_key(&EncodeHintType::FORCE_CODE_SET) {
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { return Err(Exceptions::illegalStateEmpty()) };
let Some(EncodeHintValue::ForceCodeSet(codeSetHint)) = hints.get(&EncodeHintType::FORCE_CODE_SET) else { return Err(Exceptions::illegalState) };
match codeSetHint.as_str() {
"A" => forcedCodeSet = CODE_CODE_A as i32,
"B" => forcedCodeSet = CODE_CODE_B as i32,
"C" => forcedCodeSet = CODE_CODE_C as i32,
_ => {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Unsupported code set hint: {codeSetHint}"
)))
}
@@ -134,7 +134,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
if c > 127 {
// no full Latin-1 character set available at the moment
// shift and manual code change are not supported
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Bad character in input: ASCII value={c}"
)));
}
@@ -149,7 +149,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
// allows no ascii above 95 (no lower caps, no special symbols)
{
if c > 95 && c <= 127 {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Bad character in input for forced code set A: ASCII value={c}"
)));
}
@@ -158,7 +158,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
// allows no ascii below 32 (terminal symbols)
{
if c <= 32 {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Bad character in input for forced code set B: ASCII value={c}"
)));
}
@@ -172,7 +172,7 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|| ch == ESCAPE_FNC_3
|| ch == ESCAPE_FNC_4
{
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Bad character in input for forced code set C: ASCII value={c}"
)));
}
@@ -195,7 +195,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
while position < length {
//Select code to use
let newCodeSet = if forcedCodeSet == -1 {
chooseCode(contents, position, codeSet).ok_or(Exceptions::illegalStateEmpty())?
chooseCode(contents, position, codeSet).ok_or(Exceptions::illegalState)?
} else {
forcedCodeSet as usize // THIS IS RISKY
};
@@ -208,7 +208,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
match contents
.chars()
.nth(position)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
{
ESCAPE_FNC_1 => patternIndex = CODE_FNC_1 as isize,
ESCAPE_FNC_2 => patternIndex = CODE_FNC_2 as isize,
@@ -228,7 +228,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
as isize
- ' ' as isize;
if patternIndex < 0 {
@@ -240,7 +240,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
patternIndex = contents
.chars()
.nth(position)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
as isize
- ' ' as isize
}
@@ -248,7 +248,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
// CODE_CODE_C
if position + 1 == length {
// this is the last character, but the encoding is C, which always encodes two characers
return Err(Exceptions::illegalArgument(
return Err(Exceptions::illegalArgumentWith(
"Bad number of characters for digit only encoding.",
));
}
@@ -259,7 +259,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
.map(|(_u, c)| c)
.collect();
patternIndex = s.parse::<isize>().map_err(|e| {
Exceptions::parse(format!("issue parsing {s}: {e}"))
Exceptions::parseWith(format!("issue parsing {s}: {e}"))
})?;
position += 1;
} // Also incremented below
@@ -535,7 +535,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
if contents
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
== ESCAPE_FNC_1
{
addPattern(
@@ -554,8 +554,9 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
.collect();
addPattern(
&mut patterns,
s.parse::<usize>()
.map_err(|e| Exceptions::parse(format!("unable to parse {s} {e}")))?,
s.parse::<usize>().map_err(|e| {
Exceptions::parseWith(format!("unable to parse {s} {e}"))
})?,
&mut checkSum,
&mut checkWeight,
i,
@@ -570,7 +571,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
let mut patternIndex = match contents
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
.ok_or(Exceptions::indexOutOfBounds)?
{
ESCAPE_FNC_1 => CODE_FNC_1 as isize,
ESCAPE_FNC_2 => CODE_FNC_2 as isize,
@@ -588,8 +589,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
contents
.chars()
.nth(i)
.ok_or(Exceptions::indexOutOfBoundsEmpty())?
as isize
.ok_or(Exceptions::indexOutOfBounds)? as isize
- ' ' as isize
}
};
@@ -680,7 +680,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
minPath: &mut Vec<Vec<Latch>>,
) -> Result<u32, Exceptions> {
if position >= contents.chars().count() {
return Err(Exceptions::illegalStateEmpty());
return Err(Exceptions::illegalState);
}
let mCost = memoizedCost[charset.ordinal()][position];
if mCost > 0 {
@@ -761,7 +761,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
}
}
if minCost == u32::MAX {
return Err(Exceptions::illegalArgument(format!(
return Err(Exceptions::illegalArgumentWith(format!(
"Bad character in input: ASCII value={}",
contents.chars().nth(position).unwrap_or('x')
)));