cargo fmt

This commit is contained in:
Henry Schimke
2022-12-14 17:58:40 -06:00
parent 2de4061f8b
commit 14d8245568
7 changed files with 557 additions and 524 deletions

View File

@@ -24,19 +24,16 @@
* http://www.piramidepse.com/ * http://www.piramidepse.com/
*/ */
use crate::{Exceptions, common::BitArray}; use crate::{common::BitArray, Exceptions};
use super::GeneralAppIdDecoder; use super::GeneralAppIdDecoder;
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
*/ */
pub trait AbstractExpandedDecoder { pub trait AbstractExpandedDecoder {
// private final BitArray information; // private final BitArray information;
// private final GeneralAppIdDecoder generalDecoder; // private final GeneralAppIdDecoder generalDecoder;

View File

@@ -40,7 +40,9 @@ impl AbstractExpandedDecoder for AI01AndOtherAIs<'_> {
buff.push_str("(01)"); buff.push_str("(01)");
let initialGtinPosition = buff.chars().count(); let initialGtinPosition = buff.chars().count();
let firstGtinDigit = self.getGeneralDecoder().extractNumericValueFromBitArray(Self::HEADER_SIZE, 4); let firstGtinDigit = self
.getGeneralDecoder()
.extractNumericValueFromBitArray(Self::HEADER_SIZE, 4);
buff.push_str(&firstGtinDigit.to_string()); buff.push_str(&firstGtinDigit.to_string());
self.encodeCompressedGtinWithoutAI(&mut buff, Self::HEADER_SIZE + 4, initialGtinPosition); self.encodeCompressedGtinWithoutAI(&mut buff, Self::HEADER_SIZE + 4, initialGtinPosition);

View File

@@ -26,13 +26,11 @@
use super::AbstractExpandedDecoder; use super::AbstractExpandedDecoder;
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
*/ */
pub trait AI01decoder: AbstractExpandedDecoder { pub trait AI01decoder: AbstractExpandedDecoder {
const GTIN_SIZE: u32 = 40; const GTIN_SIZE: u32 = 40;
fn encodeCompressedGtin(&self, buf: &mut String, currentPos: usize) { fn encodeCompressedGtin(&self, buf: &mut String, currentPos: usize) {
@@ -43,10 +41,17 @@ pub trait AI01decoder : AbstractExpandedDecoder {
self.encodeCompressedGtinWithoutAI(buf, currentPos, initialPosition); self.encodeCompressedGtinWithoutAI(buf, currentPos, initialPosition);
} }
fn encodeCompressedGtinWithoutAI(&self, buf:&mut String, currentPos:usize, initialBufferPosition:usize) { fn encodeCompressedGtinWithoutAI(
&self,
buf: &mut String,
currentPos: usize,
initialBufferPosition: usize,
) {
for i in 0..4 { for i in 0..4 {
// for (int i = 0; i < 4; ++i) { // for (int i = 0; i < 4; ++i) {
let currentBlock = self.getGeneralDecoder().extractNumericValueFromBitArray(currentPos + 10 * i, 10); let currentBlock = self
.getGeneralDecoder()
.extractNumericValueFromBitArray(currentPos + 10 * i, 10);
if currentBlock / 100 == 0 { if currentBlock / 100 == 0 {
buf.push('0'); buf.push('0');
} }
@@ -58,9 +63,6 @@ pub trait AI01decoder : AbstractExpandedDecoder {
appendCheckDigit(buf, initialBufferPosition); appendCheckDigit(buf, initialBufferPosition);
} }
} }
pub(super) fn appendCheckDigit(buf: &mut String, currentPos: usize) { pub(super) fn appendCheckDigit(buf: &mut String, currentPos: usize) {

View File

@@ -24,13 +24,9 @@
* http://www.piramidepse.com/ * http://www.piramidepse.com/
*/ */
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
*/ */
pub trait DecodedObject { pub trait DecodedObject {
fn getNewPosition(&self) -> usize; fn getNewPosition(&self) -> usize;
} }

View File

@@ -281,8 +281,6 @@ impl DataLength {
} }
} }
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)

View File

@@ -26,14 +26,16 @@
use crate::{common::BitArray, Exceptions}; use crate::{common::BitArray, Exceptions};
use super::{CurrentParsingState, DecodedChar, DecodedNumeric, DecodedInformation, BlockParsedRXingResult, DecodedObject, field_parser}; use super::{
field_parser, BlockParsedRXingResult, CurrentParsingState, DecodedChar, DecodedInformation,
DecodedNumeric, DecodedObject,
};
/** /**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
*/ */
pub struct GeneralAppIdDecoder<'a> { pub struct GeneralAppIdDecoder<'a> {
information: &'a BitArray, information: &'a BitArray,
current: CurrentParsingState, //= new CurrentParsingState(); current: CurrentParsingState, //= new CurrentParsingState();
buffer: String, //= new StringBuilder(); buffer: String, //= new StringBuilder();
@@ -48,7 +50,11 @@ impl<'a> GeneralAppIdDecoder<'_> {
} }
} }
pub fn decodeAllCodes(&mut self, buff:String, initialPosition:usize) -> Result<String,Exceptions> { pub fn decodeAllCodes(
&mut self,
buff: String,
initialPosition: usize,
) -> Result<String, Exceptions> {
let mut buff = buff; let mut buff = buff;
let mut currentPosition = initialPosition; let mut currentPosition = initialPosition;
let mut remaining = "".to_owned(); let mut remaining = "".to_owned();
@@ -64,7 +70,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
remaining = "".to_owned(); remaining = "".to_owned();
} }
if currentPosition == info.getNewPosition() { // No step forward! if currentPosition == info.getNewPosition() {
// No step forward!
break; break;
} }
currentPosition = info.getNewPosition(); currentPosition = info.getNewPosition();
@@ -94,9 +101,17 @@ impl<'a> GeneralAppIdDecoder<'_> {
if pos + 7 > self.information.getSize() { if pos + 7 > self.information.getSize() {
let numeric = self.extractNumericValueFromBitArray(pos, 4); let numeric = self.extractNumericValueFromBitArray(pos, 4);
if numeric == 0 { if numeric == 0 {
return DecodedNumeric::new(self.information.getSize(), DecodedNumeric::FNC1, DecodedNumeric::FNC1); return DecodedNumeric::new(
self.information.getSize(),
DecodedNumeric::FNC1,
DecodedNumeric::FNC1,
);
} }
return DecodedNumeric::new(self.information.getSize(), numeric - 1, DecodedNumeric::FNC1); return DecodedNumeric::new(
self.information.getSize(),
numeric - 1,
DecodedNumeric::FNC1,
);
} }
let numeric = self.extractNumericValueFromBitArray(pos, 7); let numeric = self.extractNumericValueFromBitArray(pos, 7);
@@ -110,7 +125,11 @@ impl<'a> GeneralAppIdDecoder<'_> {
Self::extractNumericValueFromBitArrayWithInformation(&self.information, pos, bits) Self::extractNumericValueFromBitArrayWithInformation(&self.information, pos, bits)
} }
pub fn extractNumericValueFromBitArrayWithInformation( information:&BitArray, pos:usize, bits:u32) ->u32{ pub fn extractNumericValueFromBitArrayWithInformation(
information: &BitArray,
pos: usize,
bits: u32,
) -> u32 {
let mut value = 0; let mut value = 0;
for i in 0..bits { for i in 0..bits {
// for (int i = 0; i < bits; ++i) { // for (int i = 0; i < bits; ++i) {
@@ -122,7 +141,11 @@ impl<'a> GeneralAppIdDecoder<'_> {
value value
} }
pub fn decodeGeneralPurposeField(&mut self, pos:usize, remaining:&str) -> Result<DecodedInformation,Exceptions> { pub fn decodeGeneralPurposeField(
&mut self,
pos: usize,
remaining: &str,
) -> Result<DecodedInformation, Exceptions> {
self.buffer.clear(); self.buffer.clear();
if !remaining.is_empty() { if !remaining.is_empty() {
@@ -133,10 +156,17 @@ impl<'a> GeneralAppIdDecoder<'_> {
if let Ok(lastDecoded) = self.parseBlocks() { if let Ok(lastDecoded) = self.parseBlocks() {
if lastDecoded.isRemaining() { if lastDecoded.isRemaining() {
return Ok(DecodedInformation::with_remaining_value(self.current.getPosition(), return Ok(DecodedInformation::with_remaining_value(
self.buffer.clone(), lastDecoded.getRemainingValue())); self.current.getPosition(),
}} self.buffer.clone(),
Ok(DecodedInformation::new(self.current.getPosition(), self.buffer.clone())) lastDecoded.getRemainingValue(),
));
}
}
Ok(DecodedInformation::new(
self.current.getPosition(),
self.buffer.clone(),
))
} }
fn parseBlocks(&mut self) -> Result<DecodedInformation, Exceptions> { fn parseBlocks(&mut self) -> Result<DecodedInformation, Exceptions> {
@@ -151,7 +181,8 @@ impl<'a> GeneralAppIdDecoder<'_> {
} else if self.current.isIsoIec646() { } else if self.current.isIsoIec646() {
result = self.parseIsoIec646Block()?; result = self.parseIsoIec646Block()?;
isFinished = result.isFinished(); isFinished = result.isFinished();
} else { // it must be numeric } else {
// it must be numeric
result = self.parseNumericBlock()?; result = self.parseNumericBlock()?;
isFinished = result.isFinished(); isFinished = result.isFinished();
} }
@@ -161,7 +192,9 @@ impl<'a> GeneralAppIdDecoder<'_> {
break; break;
} }
if !(!isFinished) { break; } if !(!isFinished) {
break;
}
} //while (!isFinished); } //while (!isFinished);
Ok(result.getDecodedInformation().as_ref().unwrap().clone()) Ok(result.getDecodedInformation().as_ref().unwrap().clone())
@@ -173,19 +206,29 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.current.setPosition(numeric.getNewPosition()); self.current.setPosition(numeric.getNewPosition());
if numeric.isFirstDigitFNC1() { if numeric.isFirstDigitFNC1() {
let information= let information = if numeric.isSecondDigitFNC1() {
if numeric.isSecondDigitFNC1() {
DecodedInformation::new(self.current.getPosition(), self.buffer.clone()) DecodedInformation::new(self.current.getPosition(), self.buffer.clone())
} else { } else {
DecodedInformation::with_remaining_value(self.current.getPosition(), self.buffer.clone(), numeric.getSecondDigit()) DecodedInformation::with_remaining_value(
self.current.getPosition(),
self.buffer.clone(),
numeric.getSecondDigit(),
)
}; };
return Ok(BlockParsedRXingResult::with_information(Some(information), true)); return Ok(BlockParsedRXingResult::with_information(
Some(information),
true,
));
} }
self.buffer.push_str(&numeric.getFirstDigit().to_string()); self.buffer.push_str(&numeric.getFirstDigit().to_string());
if numeric.isSecondDigitFNC1() { if numeric.isSecondDigitFNC1() {
let information = DecodedInformation::new(self.current.getPosition(), self.buffer.clone()); let information =
return Ok(BlockParsedRXingResult::with_information(Some(information), true)); DecodedInformation::new(self.current.getPosition(), self.buffer.clone());
return Ok(BlockParsedRXingResult::with_information(
Some(information),
true,
));
} }
self.buffer.push_str(&numeric.getSecondDigit().to_string()); self.buffer.push_str(&numeric.getSecondDigit().to_string());
} }
@@ -204,8 +247,12 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.current.setPosition(iso.getNewPosition()); self.current.setPosition(iso.getNewPosition());
if iso.isFNC1() { if iso.isFNC1() {
let information = DecodedInformation::new(self.current.getPosition(), self.buffer.clone()); let information =
return Ok(BlockParsedRXingResult::with_information(Some(information), true)); DecodedInformation::new(self.current.getPosition(), self.buffer.clone());
return Ok(BlockParsedRXingResult::with_information(
Some(information),
true,
));
} }
self.buffer.push_str(&iso.getValue().to_string()); self.buffer.push_str(&iso.getValue().to_string());
} }
@@ -231,8 +278,12 @@ impl<'a> GeneralAppIdDecoder<'_> {
self.current.setPosition(alpha.getNewPosition()); self.current.setPosition(alpha.getNewPosition());
if alpha.isFNC1() { if alpha.isFNC1() {
let information = DecodedInformation::new(self.current.getPosition(), self.buffer.clone()); let information =
return Ok(BlockParsedRXingResult::with_information(Some(information), true)); //end of the char block DecodedInformation::new(self.current.getPosition(), self.buffer.clone());
return Ok(BlockParsedRXingResult::with_information(
Some(information),
true,
)); //end of the char block
} }
self.buffer.push_str(&alpha.getValue().to_string()); self.buffer.push_str(&alpha.getValue().to_string());
@@ -280,7 +331,6 @@ impl<'a> GeneralAppIdDecoder<'_> {
let eightBitValue = self.extractNumericValueFromBitArray(pos, 8); let eightBitValue = self.extractNumericValueFromBitArray(pos, 8);
eightBitValue >= 232 && eightBitValue < 253 eightBitValue >= 232 && eightBitValue < 253
} }
fn decodeIsoIec646(&self, pos: usize) -> Result<DecodedChar, Exceptions> { fn decodeIsoIec646(&self, pos: usize) -> Result<DecodedChar, Exceptions> {
@@ -290,66 +340,52 @@ impl<'a> GeneralAppIdDecoder<'_> {
} }
if fiveBitValue >= 5 && fiveBitValue < 15 { if fiveBitValue >= 5 && fiveBitValue < 15 {
return Ok(DecodedChar::new(pos + 5, char::from_u32 ('0' as u32 + fiveBitValue - 5).unwrap())); return Ok(DecodedChar::new(
pos + 5,
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
));
} }
let sevenBitValue = self.extractNumericValueFromBitArray(pos, 7); let sevenBitValue = self.extractNumericValueFromBitArray(pos, 7);
if sevenBitValue >= 64 && sevenBitValue < 90 { if sevenBitValue >= 64 && sevenBitValue < 90 {
return Ok(DecodedChar::new(pos + 7, char::from_u32 (sevenBitValue + 1).unwrap())); return Ok(DecodedChar::new(
pos + 7,
char::from_u32(sevenBitValue + 1).unwrap(),
));
} }
if sevenBitValue >= 90 && sevenBitValue < 116 { if sevenBitValue >= 90 && sevenBitValue < 116 {
return Ok(DecodedChar::new(pos + 7, char::from_u32 (sevenBitValue + 7).unwrap())); return Ok(DecodedChar::new(
pos + 7,
char::from_u32(sevenBitValue + 7).unwrap(),
));
} }
let eightBitValue = self.extractNumericValueFromBitArray(pos, 8); let eightBitValue = self.extractNumericValueFromBitArray(pos, 8);
let c= let c = match eightBitValue {
match eightBitValue { 232 => '!',
232=> 233 => '"',
'!', 234 => '%',
233=> 235 => '&',
'"', 236 => '\'',
234=> 237 => '(',
'%', 238 => ')',
235=> 239 => '*',
'&', 240 => '+',
236=> 241 => ',',
'\'', 242 => '-',
237=> 243 => '.',
'(', 244 => '/',
238=> 245 => ':',
')', 246 => ';',
239=> 247 => '<',
'*', 248 => '=',
240=> 249 => '>',
'+', 250 => '?',
241=> 251 => '_',
',', 252 => ' ',
242=> _ => return Err(Exceptions::FormatException("".to_owned())),
'-',
243=>
'.',
244=>
'/',
245=>
':',
246=>
';',
247=>
'<',
248=>
'=',
249=>
'>',
250=>
'?',
251=>
'_',
252=>
' ',
_=>
return Err(Exceptions::FormatException("".to_owned())),
}; };
Ok(DecodedChar::new(pos + 8, c)) Ok(DecodedChar::new(pos + 8, c))
@@ -382,29 +418,33 @@ impl<'a> GeneralAppIdDecoder<'_> {
} }
if fiveBitValue >= 5 && fiveBitValue < 15 { if fiveBitValue >= 5 && fiveBitValue < 15 {
return Ok(DecodedChar::new(pos + 5, char::from_u32 ('0' as u32 + fiveBitValue - 5).unwrap())); return Ok(DecodedChar::new(
pos + 5,
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
));
} }
let sixBitValue = self.extractNumericValueFromBitArray(pos, 6); let sixBitValue = self.extractNumericValueFromBitArray(pos, 6);
if sixBitValue >= 32 && sixBitValue < 58 { if sixBitValue >= 32 && sixBitValue < 58 {
return Ok(DecodedChar::new(pos + 6, char::from_u32 (sixBitValue + 33).unwrap())); return Ok(DecodedChar::new(
pos + 6,
char::from_u32(sixBitValue + 33).unwrap(),
));
} }
let c = let c = match sixBitValue {
match sixBitValue { 58 => '*',
58=> 59 => ',',
'*', 60 => '-',
59=> 61 => '.',
',', 62 => '/',
60=> _ => {
'-', return Err(Exceptions::IllegalStateException(format!(
61=> "Decoding invalid alphanumeric value: {}",
'.', sixBitValue
62=> )))
'/', }
_=>
return Err(Exceptions::IllegalStateException(format!("Decoding invalid alphanumeric value: {}" , sixBitValue))),
}; };
Ok(DecodedChar::new(pos + 6, c)) Ok(DecodedChar::new(pos + 6, c))

View File

@@ -1,5 +1,3 @@
pub mod abstract_expanded_decoder; pub mod abstract_expanded_decoder;
pub use abstract_expanded_decoder::AbstractExpandedDecoder; pub use abstract_expanded_decoder::AbstractExpandedDecoder;
mod ai_01_and_other_ais; mod ai_01_and_other_ais;