cargo fmt

This commit is contained in:
Henry Schimke
2022-12-17 18:47:54 -06:00
parent d9b0d46f71
commit 2a27b182c1
7 changed files with 675 additions and 580 deletions

View File

@@ -1,5 +1,3 @@
mod pdf_417_detector_result;
pub use pdf_417_detector_result::*;

View File

@@ -14,15 +14,12 @@
* limitations under the License.
*/
/**
* Represents possible PDF417 barcode compaction types.
*/
pub enum Compaction {
AUTO = 0,
TEXT = 1,
BYTE = 2,
NUMERIC=3
NUMERIC = 3,
}

View File

@@ -1,4 +1,3 @@
mod compaction;
pub use compaction::*;

View File

@@ -18,11 +18,14 @@
* This file has been modified from its original form in Barcode4J.
*/
use std::{fmt::Display, any::TypeId};
use std::{any::TypeId, fmt::Display};
use encoding::EncodingRef;
use crate::{Exceptions, common::{ECIInput, MinimalECIInput, CharacterSetECI}};
use crate::{
common::{CharacterSetECI, ECIInput, MinimalECIInput},
Exceptions,
};
use super::Compaction;
@@ -110,15 +113,17 @@ use super::Compaction;
* Raw code table for text compaction Mixed sub-mode
*/
const TEXT_MIXED_RAW: [u8; 30] = [
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58,
35, 45, 46, 36, 47, 43, 37, 42, 61, 94, 0, 32, 0, 0, 0];
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58, 35, 45, 46, 36, 47, 43, 37, 42, 61,
94, 0, 32, 0, 0, 0,
];
/**
* Raw code table for text compaction: Punctuation sub-mode
*/
const TEXT_PUNCTUATION_RAW: [u8; 30] = [
59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58,
10, 45, 46, 36, 47, 34, 124, 42, 40, 41, 63, 123, 125, 39, 0];
59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58, 10, 45, 46, 36, 47, 34, 124, 42,
40, 41, 63, 123, 125, 39, 0,
];
// const MIXED : [u8;128]= [0;128];//new byte[128];
@@ -187,11 +192,17 @@ punct
* then charsets will be chosen so that the byte representation is minimal.
* @return the encoded message (the char values range from 0 to 928)
*/
pub fn encodeHighLevel( msg:&str, compaction:Compaction, encoding:Option<EncodingRef>, autoECI:bool)
-> Result<String,Exceptions> {
pub fn encodeHighLevel(
msg: &str,
compaction: Compaction,
encoding: Option<EncodingRef>,
autoECI: bool,
) -> Result<String, Exceptions> {
let mut encoding = encoding;
if msg.is_empty() {
return Err(Exceptions::WriterException("Empty message not allowed".to_owned()));
return Err(Exceptions::WriterException(
"Empty message not allowed".to_owned(),
));
}
if encoding.is_none() && !autoECI {
@@ -226,19 +237,31 @@ let mut encoding = encoding;
// User selected encoding mode
match compaction {
Compaction::TEXT=>
{encodeText(&input, p, len as u32, &mut sb, textSubMode)?;},
Compaction:: BYTE=>
Compaction::TEXT => {
encodeText(&input, p, len as u32, &mut sb, textSubMode)?;
}
Compaction::BYTE => {
if autoECI {
encodeMultiECIBinary(&input, 0, input.length() as u32, TEXT_COMPACTION, &mut sb)?;
} else {
let msgBytes = encoding.as_ref().unwrap().encode(&input.to_string(), encoding::EncoderTrap::Strict).unwrap_or_default(); //input.to_string().getBytes(encoding);
encodeBinary(&msgBytes, p, msgBytes.len() as u32, BYTE_COMPACTION, &mut sb);
},
Compaction:: NUMERIC=>
{sb.push(char::from_u32( LATCH_TO_NUMERIC).unwrap());
encodeNumeric(&input, p, len as u32, &mut sb);},
let msgBytes = encoding
.as_ref()
.unwrap()
.encode(&input.to_string(), encoding::EncoderTrap::Strict)
.unwrap_or_default(); //input.to_string().getBytes(encoding);
encodeBinary(
&msgBytes,
p,
msgBytes.len() as u32,
BYTE_COMPACTION,
&mut sb,
);
}
}
Compaction::NUMERIC => {
sb.push(char::from_u32(LATCH_TO_NUMERIC).unwrap());
encodeNumeric(&input, p, len as u32, &mut sb);
}
_ => {
let mut encodingMode = TEXT_COMPACTION; //Default mode, see 4.4.2.1
while p < len as u32 {
@@ -267,13 +290,26 @@ let mut encoding = encoding;
textSubMode = encodeText(&input, p, t, &mut sb, textSubMode)?;
p += t;
} else {
let mut b = determineConsecutiveBinaryCount(&input, p, if autoECI {None} else {encoding})?;
let mut b = determineConsecutiveBinaryCount(
&input,
p,
if autoECI { None } else { encoding },
)?;
if b == 0 {
b = 1;
}
let bytes = if autoECI {None} else {
let str = input.subSequence(p as usize, (p + b) as usize)?.iter().collect::<String>();
if let Ok(enc_str) = encoding.as_ref().unwrap().encode(&str, encoding::EncoderTrap::Strict) {
let bytes = if autoECI {
None
} else {
let str = input
.subSequence(p as usize, (p + b) as usize)?
.iter()
.collect::<String>();
if let Ok(enc_str) = encoding
.as_ref()
.unwrap()
.encode(&str, encoding::EncoderTrap::Strict)
{
Some(enc_str)
} else {
None
@@ -281,20 +317,34 @@ let mut encoding = encoding;
}; //.getBytes(encoding))};
// if let Some(byts) = bytes {
let bytes_ok = if let Some(_) = bytes { true } else { false };
if ((bytes_ok && b == 1) || (!bytes_ok && bytes.as_ref().unwrap().len() == 1))
&& encodingMode == TEXT_COMPACTION {
if ((bytes_ok && b == 1)
|| (!bytes_ok && bytes.as_ref().unwrap().len() == 1))
&& encodingMode == TEXT_COMPACTION
{
//Switch for one byte (instead of latch)
if autoECI {
encodeMultiECIBinary(&input, p, 1, TEXT_COMPACTION, &mut sb)?;
} else {
encodeBinary(bytes.as_ref().unwrap(), 0, 1, TEXT_COMPACTION, &mut sb);
encodeBinary(
bytes.as_ref().unwrap(),
0,
1,
TEXT_COMPACTION,
&mut sb,
);
}
} else {
//Mode latch performed by encodeBinary()
if autoECI {
encodeMultiECIBinary(&input, p, p + b, encodingMode, &mut sb)?;
} else {
encodeBinary(bytes.as_ref().unwrap(), 0, bytes.as_ref().unwrap().len() as u32, encodingMode, &mut sb);
encodeBinary(
bytes.as_ref().unwrap(),
0,
bytes.as_ref().unwrap().len() as u32,
encodingMode,
&mut sb,
);
}
encodingMode = BYTE_COMPACTION;
textSubMode = SUBMODE_ALPHA; //Reset after latch
@@ -302,7 +352,8 @@ let mut encoding = encoding;
p += b;
}
}
}},
}
}
}
Ok(sb)
@@ -319,11 +370,13 @@ let mut encoding = encoding;
* @param initialSubmode should normally be SUBMODE_ALPHA
* @return the text submode in which this method ends
*/
fn encodeText<T:ECIInput+?Sized>( input:&Box<T>,
fn encodeText<T: ECIInput + ?Sized>(
input: &Box<T>,
startpos: u32,
count: u32,
sb: &mut String,
initialSubmode:u32) -> Result<u32,Exceptions> {
initialSubmode: u32,
) -> Result<u32, Exceptions> {
let mut tmp = String::with_capacity(count as usize);
let mut submode = initialSubmode;
let mut idx = 0;
@@ -334,7 +387,7 @@ let mut encoding = encoding;
} else {
let ch = input.charAt((startpos + idx) as usize)?;
match submode {
SUBMODE_ALPHA=>
SUBMODE_ALPHA => {
if isAlphaUpper(ch) {
if ch == ' ' {
tmp.push(26 as char); //space
@@ -355,9 +408,10 @@ let mut encoding = encoding;
tmp.push(char::from_u32(PUNCTUATION[ch as usize] as u32).unwrap());
break;
}
},
}
}
// break;
SUBMODE_LOWER=>
SUBMODE_LOWER => {
if isAlphaLower(ch) {
if ch == ' ' {
tmp.push(26 as char); //space
@@ -379,9 +433,10 @@ let mut encoding = encoding;
tmp.push(char::from_u32(PUNCTUATION[ch as usize] as u32).unwrap());
break;
}
},
}
}
// break;
SUBMODE_MIXED=>
SUBMODE_MIXED => {
if isMixed(ch) {
tmp.push(char::from_u32(MIXED[ch as usize] as u32).unwrap());
} else {
@@ -395,7 +450,9 @@ let mut encoding = encoding;
continue;
} else {
if startpos + idx + 1 < count {
if !input.isECI(startpos + idx + 1)? && isPunctuation(input.charAt((startpos + idx + 1) as usize)?) {
if !input.isECI(startpos + idx + 1)?
&& isPunctuation(input.charAt((startpos + idx + 1) as usize)?)
{
submode = SUBMODE_PUNCTUATION;
tmp.push(25 as char); //pl
continue;
@@ -404,15 +461,19 @@ let mut encoding = encoding;
tmp.push(29 as char); //ps
tmp.push(char::from_u32(PUNCTUATION[ch as usize] as u32).unwrap());
}
},
_=> //SUBMODE_PUNCTUATION
}
}
_ =>
//SUBMODE_PUNCTUATION
{
if isPunctuation(ch) {
tmp.push(char::from_u32(PUNCTUATION[ch as usize] as u32).unwrap());
} else {
submode = SUBMODE_ALPHA;
tmp.push(29 as char); //al
continue;
},
}
}
}
idx += 1;
if idx >= count {
@@ -447,11 +508,13 @@ let mut encoding = encoding;
* @param startmode the mode from which this method starts
* @param sb receives the encoded codewords
*/
fn encodeMultiECIBinary<T:ECIInput + ?Sized>( input:&Box<T>,
fn encodeMultiECIBinary<T: ECIInput + ?Sized>(
input: &Box<T>,
startpos: u32,
count: u32,
startmode: u32,
sb:&mut String) -> Result<(),Exceptions> {
sb: &mut String,
) -> Result<(), Exceptions> {
let end = (startpos + count).min(input.length() as u32);
let mut localStart = startpos;
loop {
@@ -472,8 +535,17 @@ let mut encoding = encoding;
break;
} else {
//encode the segment
encodeBinary(&subBytes(input, localStart, localEnd),
0, localCount, if localStart == startpos {startmode} else {BYTE_COMPACTION}, sb);
encodeBinary(
&subBytes(input, localStart, localEnd),
0,
localCount,
if localStart == startpos {
startmode
} else {
BYTE_COMPACTION
},
sb,
);
localStart = localEnd;
}
}
@@ -502,11 +574,7 @@ let mut encoding = encoding;
* @param startmode the mode from which this method starts
* @param sb receives the encoded codewords
*/
fn encodeBinary( bytes:&[u8],
startpos: u32,
count: u32,
startmode: u32,
sb:&mut String) {
fn encodeBinary(bytes: &[u8], startpos: u32, count: u32, startmode: u32, sb: &mut String) {
if count == 1 && startmode == TEXT_COMPACTION {
sb.push(char::from_u32(SHIFT_TO_BYTE).unwrap());
} else {
@@ -556,13 +624,25 @@ let mut encoding = encoding;
while idx < count {
tmp.clear();
let len = 44.min(count as isize - idx as isize);
let part = format!("1{}", input.subSequence((startpos + idx) as usize, (startpos + idx + len as u32) as usize).unwrap().iter().collect::<String>());
let part = format!(
"1{}",
input
.subSequence(
(startpos + idx) as usize,
(startpos + idx + len as u32) as usize
)
.unwrap()
.iter()
.collect::<String>()
);
let mut bigint: u128 = part.parse().unwrap();
loop {
tmp.push(char::from_u32((bigint % num900) as u32).unwrap());
bigint = bigint / num900;
if !(!bigint == num0) { break; }
if !(!bigint == num0) {
break;
}
} //while (!bigint.equals(num0));
//Reverse temporary string
@@ -577,7 +657,6 @@ let mut encoding = encoding;
}
}
fn isDigit(ch: char) -> bool {
return ch >= '0' && ch <= '9';
}
@@ -614,7 +693,8 @@ let mut encoding = encoding;
let len = input.length();
let mut idx = startpos as usize;
if idx < len {
while idx < len && !input.isECI(idx as u32).unwrap() && isDigit(input.charAt(idx).unwrap()) {
while idx < len && !input.isECI(idx as u32).unwrap() && isDigit(input.charAt(idx).unwrap())
{
count += 1;
idx += 1;
}
@@ -635,7 +715,11 @@ let mut encoding = encoding;
let mut idx = startpos as usize;
while idx < len {
let mut numericCount = 0;
while numericCount < 13 && idx < len && !input.isECI(idx as u32).unwrap() && isDigit(input.charAt(idx).unwrap()) {
while numericCount < 13
&& idx < len
&& !input.isECI(idx as u32).unwrap()
&& isDigit(input.charAt(idx).unwrap())
{
numericCount += 1;
idx += 1;
}
@@ -664,8 +748,11 @@ let mut encoding = encoding;
* @param encoding the charset used to convert the message to a byte array
* @return the requested character count
*/
fn determineConsecutiveBinaryCount<T: ECIInput+?Sized+'static>( input:&Box<T>, startpos:u32, encoding:Option<EncodingRef>)
-> Result<u32,Exceptions> {
fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>(
input: &Box<T>,
startpos: u32,
encoding: Option<EncodingRef>,
) -> Result<u32, Exceptions> {
// CharsetEncoder encoder = encoding == null ? null : encoding.newEncoder();
let len = input.length();
let mut idx = startpos as usize;
@@ -673,7 +760,10 @@ let mut encoding = encoding;
let mut numericCount = 0;
let mut i = idx;
while numericCount < 13 && !input.isECI(i as u32).unwrap() && isDigit(input.charAt(i).unwrap()) {
while numericCount < 13
&& !input.isECI(i as u32).unwrap()
&& isDigit(input.charAt(i).unwrap())
{
numericCount += 1;
//textCount++;
i = idx + numericCount;
@@ -686,15 +776,24 @@ let mut encoding = encoding;
}
if let Some(encoder) = encoding {
let can_encode = encoder.encode(&input.charAt(idx)?.to_string(), encoding::EncoderTrap::Strict).is_ok();
let can_encode = encoder
.encode(
&input.charAt(idx)?.to_string(),
encoding::EncoderTrap::Strict,
)
.is_ok();
// if encoder != null && !encoder.canEncode(input.charAt(idx)) {
if !can_encode {
assert!(TypeId::of::<T>() == TypeId::of::<NoECIInput>());
// assert!(input instanceof NoECIInput);
let ch = input.charAt(idx).unwrap();
return Err(Exceptions::WriterException(format!("Non-encodable character detected: {} (Unicode: {})", ch, ch as u32)));
}}
return Err(Exceptions::WriterException(format!(
"Non-encodable character detected: {} (Unicode: {})",
ch, ch as u32
)));
}
}
idx += 1;
}
Ok(idx as u32 - startpos)
@@ -712,7 +811,10 @@ let mut encoding = encoding;
sb.push(char::from_u32(ECI_USER_DEFINED).unwrap());
sb.push(char::from_u32((810900 - eci) as u32).unwrap());
} else {
return Err(Exceptions::WriterException(format!("ECI number not in valid range from 0..811799, but was {}" , eci)));
return Err(Exceptions::WriterException(format!(
"ECI number not in valid range from 0..811799, but was {}",
eci
)));
}
Ok(())
}
@@ -749,7 +851,6 @@ let mut encoding = encoding;
}
}
impl NoECIInput {
pub fn new(input: String) -> Self {
Self(input)
}