chore: version update and cargo fmt

This commit is contained in:
Henry Schimke
2024-01-02 17:10:56 -06:00
parent e04354cb4a
commit c3618919d7
10 changed files with 89 additions and 93 deletions

View File

@@ -126,7 +126,6 @@ pub enum DecodeHintType {
* Will translate the ASCII values parsed by the Telepen reader into the Telepen Numeric form.
*/
TELEPEN_AS_NUMERIC,
/*
* Data type the hint is expecting.
* Among the possible values the {@link Void} stands out as being used for

View File

@@ -92,4 +92,4 @@ pub use ean_8_writer::*;
mod upc_e_writer;
pub use upc_e_writer::*;
mod telepen_common;
mod telepen_common;

View File

@@ -1,5 +1,5 @@
use crate::Exceptions;
use crate::common::Result;
use crate::Exceptions;
pub fn calculate_checksum(contents: &str) -> char {
let mut sum = 0;
@@ -12,8 +12,7 @@ pub fn calculate_checksum(contents: &str) -> char {
let diff = 127 - remainder;
return if diff != 127 {
diff as u8 as char
}
else {
} else {
0 as char
};
}
@@ -24,18 +23,19 @@ pub fn ascii_to_numeric(contents: &str) -> String {
for c in contents.chars().map(|x| x as u32) {
if c >= 27 {
number.push_str(&format!("{:0>2}", (c - 27)));
}
else {
} else {
number.push_str(&format!("{:0>2}", (c - 17)));
}
}
return number;
}
pub fn numeric_to_ascii(contents: &str) -> Result<String> {
if contents.len() % 2 != 0 {
return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters."));
return Err(Exceptions::illegal_argument_with(
"Input must contain an even number of characters.",
));
}
let mut ascii = String::new();
@@ -47,12 +47,13 @@ pub fn numeric_to_ascii(contents: &str) -> Result<String> {
if second == 88 && first >= 48 && first <= 57 {
ascii.push((17 + first - 48) as char);
}
else if second >= 48 && second <= 57 && first >= 48 && first <= 57 {
} else if second >= 48 && second <= 57 && first >= 48 && first <= 57 {
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char);
}
else {
return Err(Exceptions::illegal_argument_with(format!("Input contains an invalid character around position {}.", i.to_string())));
} else {
return Err(Exceptions::illegal_argument_with(format!(
"Input contains an invalid character around position {}.",
i.to_string()
)));
}
i += 2;
@@ -95,4 +96,4 @@ fn telepen_numeric_to_ascii_test() {
numeric = "11058474";
result = numeric_to_ascii(numeric).unwrap();
assert_eq!("& oe", result);
}
}

View File

@@ -17,10 +17,10 @@
use rxing_one_d_proc_derive::OneDReader;
use crate::common::{BitArray, Result};
use crate::oned::telepen_common;
use crate::DecodeHintValue;
use crate::Exceptions;
use crate::RXingResult;
use crate::DecodeHintValue;
use crate::oned::telepen_common;
use crate::{point_f, BarcodeFormat};
use bit_reverse::ParallelReverse;
@@ -65,11 +65,11 @@ impl OneDReader for TelepenReader {
let mut minBar = u32::MAX;
// 0 position value will be whitespace prior to the barcode beginning
let mut j = startOffset;
let mut j = startOffset;
// Calculate a median bar / gap width by establishing the smallest and
// largest gaps.
while j <= end {
while j <= end {
let currentCounter = theCounters[j];
if currentCounter < minBar {
minBar = currentCounter;
@@ -118,19 +118,15 @@ impl OneDReader for TelepenReader {
if isBlack {
// Narrow black: B
pattern[patternLength] = 1;
}
else {
} else {
// Narrow white: .
pattern[patternLength] = 0;
}
}
else {
} else {
if isBlack {
// Wide black: BBB
pattern[patternLength] = 3;
}
else {
} else {
// Wide white: ...
pattern[patternLength] = 2;
}
@@ -147,36 +143,31 @@ impl OneDReader for TelepenReader {
// Convert narrow-wide sequence into bit array.
while j < patternLength - 1 {
if pattern[j] == 3 && pattern[j + 1] == 2 {
// BBB... = 010
bits.appendBit(false);
bits.appendBit(true);
bits.appendBit(false);
}
else if pattern[j] == 3 && pattern[j + 1] == 0 {
} else if pattern[j] == 3 && pattern[j + 1] == 0 {
// BBB. = 00
bits.appendBit(false);
bits.appendBit(false);
}
else if pattern[j] == 1 && pattern[j + 1] == 2 && state == 0 {
} else if pattern[j] == 1 && pattern[j + 1] == 2 && state == 0 {
// B... = 01
bits.appendBit(false);
bits.appendBit(true);
state = 1;
}
else if pattern[j] == 1 && pattern[j + 1] == 2 && state == 1 {
} else if pattern[j] == 1 && pattern[j + 1] == 2 && state == 1 {
// B... = 10
bits.appendBit(true);
bits.appendBit(false);
state = 0;
}
else if pattern[j] == 1 && pattern[j + 1] == 0 {
} else if pattern[j] == 1 && pattern[j + 1] == 0 {
// B. = 1
bits.appendBit(true);
}
j += 2;
j += 2;
}
let byteLength = bits.getSizeInBytes();
@@ -192,8 +183,8 @@ impl OneDReader for TelepenReader {
j = 0;
// Tweak our byte array to clean things up a little.
while j < byteLength {
// Telepen is little-endian, so need to swap the
while j < byteLength {
// Telepen is little-endian, so need to swap the
// bits around for each byte to be correct.
bytes[j] = bytes[j].swap_bits();
@@ -220,14 +211,14 @@ impl OneDReader for TelepenReader {
}
// Content bytes
let contentBytes = bytes[1 .. byteLength - 2].to_vec();
let contentBytes = bytes[1..byteLength - 2].to_vec();
let mut contentString = String::from_utf8_lossy(&contentBytes).to_string();
// Penultimate byte is a block check character.
let check = bytes[byteLength - 2];
let checksum = telepen_common::calculate_checksum(&contentString);
// Validate checksum
if check != checksum as u8 {
return Err(Exceptions::NOT_FOUND);
@@ -272,7 +263,6 @@ impl OneDReader for TelepenReader {
}
}
impl TelepenReader {
pub fn new() -> Self {
Self {
counters: vec![0; 80], //Vec::with_capacity(80),
@@ -302,15 +292,13 @@ impl TelepenReader {
while i < end {
if row.get(i) == currentColor {
count += 1;
}
else {
} else {
if count >= minToleratedWidth || self.counterLength == 0 {
self.counterAppend(count);
}
else {
// Noise from previous bar. Treat it as the
} else {
// Noise from previous bar. Treat it as the
// previous colour.
self.counters[self.counterLength - 1] += count;
self.counters[self.counterLength - 1] += count;
}
count = 1;
@@ -322,11 +310,10 @@ impl TelepenReader {
if count >= minToleratedWidth {
self.counterAppend(count);
}
else {
// Noise from previous bar. Treat it as the
} else {
// Noise from previous bar. Treat it as the
// previous colour.
self.counters[self.counterLength - 1] += count;
self.counters[self.counterLength - 1] += count;
}
Ok(())
@@ -349,19 +336,19 @@ impl TelepenReader {
let mut i = 0;
while i < self.counterLength - 20 {
// Read next 20 in sequence. All 20 must be either between 28% and 38%
// Read next 20 in sequence. All 20 must be either between 28% and 38%
// of biggest, or between 90% to 100% of biggest.
let mut j = 0;
let mut maxBar: f32 = 0.0;
let mut minBar: f32 = f32::MAX;
while i + j < self.counterLength && j < 20 {
while i + j < self.counterLength && j < 20 {
if (self.counters[i + j] as f32) > maxBar {
maxBar = self.counters[i + j] as f32;
}
if (self.counters[ i + j] as f32) < minBar {
minBar = self.counters[ i + j] as f32;
if (self.counters[i + j] as f32) < minBar {
minBar = self.counters[i + j] as f32;
}
j += 1;
@@ -374,14 +361,13 @@ impl TelepenReader {
// First 10 items must be:
// N-N-N-N-N-N-N-N-N-N-W
while i + j < self.counterLength && j < 11 {
while i + j < self.counterLength && j < 11 {
if j < 10 {
// Narrow
if (self.counters[i + j] as f32) > median {
passed = false;
break;
}
else if j == 10 {
} else if j == 10 {
// Wide
if (self.counters[i + j] as f32) < median {
passed = false;
@@ -408,12 +394,12 @@ impl TelepenReader {
let mut i = start;
while i < self.counterLength {
// Read next 20 in sequence. All 20 must be either between 28% and 38%
// Read next 20 in sequence. All 20 must be either between 28% and 38%
// of biggest, or between 90% to 100% of biggest.
let mut j = 0;
let mut maxBar: f32 = 0.0;
while i + j < self.counterLength && j < 20 {
while i + j < self.counterLength && j < 20 {
if (self.counters[i + j] as f32) > maxBar {
maxBar = self.counters[i + j] as f32;
}

View File

@@ -14,11 +14,11 @@
* limitations under the License.
*/
use rxing_one_d_proc_derive::OneDWriter;
use regex::Regex;
use crate::common::Result;
use crate::BarcodeFormat;
use crate::oned::telepen_common;
use crate::BarcodeFormat;
use regex::Regex;
use rxing_one_d_proc_derive::OneDWriter;
use super::OneDimensionalCodeWriter;
@@ -35,9 +35,11 @@ impl OneDimensionalCodeWriter for TelepenWriter {
self.encode_oned_with_hints(contents, &HashMap::new())
}
fn encode_oned_with_hints(&self, contents: &str,
hints: &crate::EncodingHintDictionary) -> Result<Vec<bool>> {
fn encode_oned_with_hints(
&self,
contents: &str,
hints: &crate::EncodingHintDictionary,
) -> Result<Vec<bool>> {
let mut decodedContents = contents.to_string();
if matches!(
@@ -49,7 +51,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
// Calculate the checksum character
let checksum = telepen_common::calculate_checksum(&decodedContents);
// Build binary string
let mut binary = String::new();
@@ -91,7 +93,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 5] = false;
resultPosition += 6;
},
}
"00" => {
// BBB.
result[resultPosition] = true;
@@ -100,14 +102,14 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false;
resultPosition += 4;
},
}
"1" => {
// B.
result[resultPosition] = true;
result[resultPosition + 1] = false;
resultPosition += 2;
},
}
"01" => {
// B...
result[resultPosition] = true;
@@ -116,7 +118,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false;
resultPosition += 4;
},
}
"10" => {
// B...
result[resultPosition] = true;
@@ -125,12 +127,12 @@ impl OneDimensionalCodeWriter for TelepenWriter {
result[resultPosition + 3] = false;
resultPosition += 4;
},
}
"0" => {
return Err(Exceptions::illegal_argument_with(format!(
"Invalid bit combination!"
)));
},
}
_ => {
// B... (B.)* B...
result[resultPosition] = true;
@@ -140,7 +142,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
resultPosition += 4;
for _j in 2 .. b.len() - 2 {
for _j in 2..b.len() - 2 {
result[resultPosition] = true;
result[resultPosition + 1] = false;
@@ -157,7 +159,7 @@ impl OneDimensionalCodeWriter for TelepenWriter {
}
}
Ok(result[0 .. resultPosition - 1].to_vec())
Ok(result[0..resultPosition - 1].to_vec())
}
fn getSupportedWriteFormats(&self) -> Option<Vec<crate::BarcodeFormat>> {
@@ -189,14 +191,9 @@ impl TelepenWriter {
fn add_to_binary(&self, c: char, mut binary: String) -> String {
let byte = c as u8;
let bits = self.get_bits(byte);
for i in 0..8 {
binary.push(if bits[i] {
'1'
}
else {
'0'
});
binary.push(if bits[i] { '1' } else { '0' });
}
return binary;
@@ -212,7 +209,7 @@ mod TelepenWriterTestCase {
use crate::{
common::{bit_matrix_test_case, BitMatrix},
BarcodeFormat, Writer, EncodeHintType, EncodeHintValue,
BarcodeFormat, EncodeHintType, EncodeHintValue, Writer,
};
use super::TelepenWriter;
@@ -245,14 +242,13 @@ mod TelepenWriterTestCase {
true
);
}
fn doTest(input: &str, expected: &str, numeric: bool) {
let result: BitMatrix;
if numeric {
result = encode_with_hints(input);
}
else {
} else {
result = encode(input);
};
@@ -267,7 +263,10 @@ mod TelepenWriterTestCase {
fn encode_with_hints(input: &str) -> BitMatrix {
let mut hints = HashMap::new();
hints.insert(EncodeHintType::TELEPEN_AS_NUMERIC, EncodeHintValue::TelepenAsNumeric(true));
hints.insert(
EncodeHintType::TELEPEN_AS_NUMERIC,
EncodeHintValue::TelepenAsNumeric(true),
);
TelepenWriter
.encode_with_hints(input, &BarcodeFormat::TELEPEN, 0, 0, &hints)