refactor to use common result type

This commit is contained in:
Vukašin Stepanović
2023-02-14 22:56:03 +00:00
parent 145cf704fe
commit 8ee616d96b
160 changed files with 829 additions and 901 deletions

View File

@@ -21,7 +21,7 @@ use crate::{
reedsolomon::{
get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonEncoder,
},
BitArray, BitMatrix,
BitArray, BitMatrix, Result,
},
exceptions::Exceptions,
};
@@ -50,7 +50,7 @@ pub const WORD_SIZE: [u32; 33] = [
* @param data input data string; must be encodable as ISO/IEC 8859-1 (Latin-1)
* @return Aztec symbol matrix with metadata
*/
pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
pub fn encode_simple(data: &str) -> Result<AztecCode> {
let Ok(bytes) = encoding::all::ISO_8859_1
.encode(data, encoding::EncoderTrap::Replace) else {
return Err(Exceptions::IllegalArgumentException(Some(format!("'{data}' cannot be encoded as ISO_8859_1"))));
@@ -67,11 +67,7 @@ pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
* @param userSpecifiedLayers if non-zero, a user-specified value for the number of layers
* @return Aztec symbol matrix with metadata
*/
pub fn encode(
data: &str,
minECCPercent: u32,
userSpecifiedLayers: i32,
) -> Result<AztecCode, Exceptions> {
pub fn encode(data: &str, minECCPercent: u32, userSpecifiedLayers: i32) -> Result<AztecCode> {
if let Ok(bytes) = encoding::all::ISO_8859_1.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
} else {
@@ -98,7 +94,7 @@ pub fn encode_with_charset(
minECCPercent: u32,
userSpecifiedLayers: i32,
charset: encoding::EncodingRef,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
if let Ok(bytes) = charset.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
} else {
@@ -114,7 +110,7 @@ pub fn encode_with_charset(
* @param data input data string
* @return Aztec symbol matrix with metadata
*/
pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode, Exceptions> {
pub fn encode_bytes_simple(data: &[u8]) -> Result<AztecCode> {
encode_bytes(data, DEFAULT_EC_PERCENT, DEFAULT_AZTEC_LAYERS)
}
@@ -131,7 +127,7 @@ pub fn encode_bytes(
data: &[u8],
minECCPercent: u32,
userSpecifiedLayers: i32,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
encode_bytes_with_charset(
data,
minECCPercent,
@@ -156,7 +152,7 @@ pub fn encode_bytes_with_charset(
min_eccpercent: u32,
user_specified_layers: i32,
charset: encoding::EncodingRef,
) -> Result<AztecCode, Exceptions> {
) -> Result<AztecCode> {
// High-level encode
let bits = HighLevelEncoder::with_charset(data.into(), charset).encode()?;
@@ -378,7 +374,7 @@ pub fn generateModeMessage(
compact: bool,
layers: u32,
messageSizeInWords: u32,
) -> Result<BitArray, Exceptions> {
) -> Result<BitArray> {
let mut mode_message = BitArray::new();
if compact {
mode_message.appendBits(layers - 1, 2)?;
@@ -431,11 +427,7 @@ fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeM
}
}
fn generateCheckWords(
bitArray: &BitArray,
totalBits: usize,
wordSize: usize,
) -> Result<BitArray, Exceptions> {
fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) -> Result<BitArray> {
// bitArray is guaranteed to be a multiple of the wordSize, so no padding needed
let message_size_in_words = bitArray.getSize() / wordSize;
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?)?;
@@ -475,7 +467,7 @@ fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Ve
message
}
fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
fn getGF(wordSize: usize) -> Result<GenericGFRef> {
match wordSize {
4 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecParam)),
6 => Ok(get_predefined_genericgf(PredefinedGenericGF::AztecData6)),
@@ -488,7 +480,7 @@ fn getGF(wordSize: usize) -> Result<GenericGFRef, Exceptions> {
}
}
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray, Exceptions> {
pub fn stuffBits(bits: &BitArray, word_size: usize) -> Result<BitArray> {
let mut out = BitArray::new();
let n = bits.getSize() as isize;

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::{common::BitArray, Exceptions};
use crate::common::{BitArray, Result};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BinaryShiftToken {
@@ -32,7 +32,7 @@ impl BinaryShiftToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<()> {
let bsbc = self.binary_shift_byte_count as usize;
for i in 0..bsbc {
// for (int i = 0; i < bsbc; i++) {

View File

@@ -15,7 +15,7 @@
*/
use crate::{
common::{BitArray, CharacterSetECI},
common::{BitArray, CharacterSetECI, Result},
exceptions::Exceptions,
};
@@ -240,7 +240,7 @@ impl HighLevelEncoder {
/**
* @return text represented by this encoder encoded as a {@link BitArray}
*/
pub fn encode(&self) -> Result<BitArray, Exceptions> {
pub fn encode(&self) -> Result<BitArray> {
let mut initial_state = State::new(Token::new(), Self::MODE_UPPER as u32, 0, 0);
if let Some(eci) = CharacterSetECI::getCharacterSetECI(self.charset) {
if eci != CharacterSetECI::ISO8859_1 {

View File

@@ -16,7 +16,7 @@
use std::fmt;
use crate::{common::BitArray, Exceptions};
use crate::common::{BitArray, Result};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SimpleToken {
@@ -33,7 +33,7 @@ impl SimpleToken {
}
}
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, _text: &[u8]) -> Result<()> {
bit_array.appendBits(self.value as u32, self.bit_count as usize)
}

View File

@@ -18,7 +18,10 @@ use std::fmt;
use encoding::Encoding;
use crate::{common::BitArray, exceptions::Exceptions};
use crate::{
common::{BitArray, Result},
exceptions::Exceptions,
};
use super::{HighLevelEncoder, Token};
@@ -70,7 +73,7 @@ impl State {
self.bit_count
}
pub fn appendFLGn(self, eci: u32) -> Result<Self, Exceptions> {
pub fn appendFLGn(self, eci: u32) -> Result<Self> {
let bit_count = self.bit_count;
let mode = self.mode;
let result = self.shiftAndAppend(HighLevelEncoder::MODE_PUNCT as u32, 0); // 0: FLG(n)
@@ -207,7 +210,7 @@ impl State {
new_mode_bit_count <= other.bit_count
}
pub fn toBitArray(self, text: &[u8]) -> Result<BitArray, Exceptions> {
pub fn toBitArray(self, text: &[u8]) -> Result<BitArray> {
let mut symbols = Vec::new();
let tok = self.endBinaryShift(text.len() as u32).token;
for tkn in tok.into_iter() {

View File

@@ -14,7 +14,10 @@
* limitations under the License.
*/
use crate::{common::BitArray, Exceptions};
use crate::{
common::{BitArray, Result},
Exceptions,
};
use super::{BinaryShiftToken, SimpleToken};
@@ -26,7 +29,7 @@ pub enum TokenType {
}
impl TokenType {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<(), Exceptions> {
pub fn appendTo(&self, bit_array: &mut BitArray, text: &[u8]) -> Result<()> {
// let token = &self.tokens[self.current_pointer];
match self {
TokenType::Simple(a) => a.appendTo(bit_array, text),