many small rustifications

This commit is contained in:
Henry Schimke
2023-01-10 14:33:47 -06:00
parent 752d8c87b9
commit 9f7a41f81c
23 changed files with 218 additions and 187 deletions

View File

@@ -52,14 +52,14 @@ impl DetectorRXingResult for AztecDetectorRXingResult {
impl AztecDetectorRXingResult {
pub fn new(
bits: BitMatrix,
points: Vec<RXingResultPoint>,
points: [RXingResultPoint; 4],
compact: bool,
nbDatablocks: u32,
nbLayers: u32,
) -> Self {
Self {
bits,
points,
points: points.to_vec(),
compact,
nbDatablocks,
nbLayers,

View File

@@ -38,7 +38,7 @@ use super::{decoder, AztecDetectorResult::AztecDetectorRXingResult};
* Tests {@link Decoder}.
*/
const NO_POINTS: &[RXingResultPoint] = &[RXingResultPoint { x: 0.0, y: 0.0 }; 0];
const NO_POINTS: [RXingResultPoint; 4] = [RXingResultPoint { x: 0.0, y: 0.0 }; 4];
#[test]
fn test_high_level_decode() {
@@ -113,7 +113,7 @@ X X X X X X X X X X X X X X
" ",
)
.expect("Bitmatrix should init");
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.to_vec(), false, 30, 2);
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.clone(), false, 30, 2);
let result = decoder::decode(&r).expect("decoder should init");
assert_eq!("88888TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT", result.getText());
assert_eq!(
@@ -173,7 +173,7 @@ X X X X X X X X ",
" ",
)
.expect("string parse success");
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.to_vec(), false, 15, 1);
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.clone(), false, 15, 1);
let result = decoder::decode(&r).expect("decode success");
assert_eq!("Français", result.getText());
}
@@ -214,7 +214,7 @@ X X . X . X . . . X . X . . . . X X . X . . X X . . . ",
". ",
)
.expect("parse string failed");
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.to_vec(), true, 16, 4);
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.clone(), true, 16, 4);
assert!(decoder::decode(&r).is_ok());
}
@@ -254,7 +254,7 @@ X X . . . X X . . X . X . . . . X X . X . . X . X . X ",
". ",
)
.expect("String Parse failed");
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.to_vec(), true, 16, 4);
let r = AztecDetectorRXingResult::new(matrix, NO_POINTS.clone(), true, 16, 4);
assert!(decoder::decode(&r).is_ok());
}

View File

@@ -50,7 +50,7 @@ const WINDOWS_1252: EncodingRef = encoding::all::WINDOWS_1252; //Charset.forName
// const DOTX: &str = "[^.X]";
// const SPACES: &str = "\\s+";
const NO_POINTS: Vec<RXingResultPoint> = Vec::new();
const NO_POINTS: [RXingResultPoint; 4] = [RXingResultPoint { x: 0.0, y: 0.0 }; 4];
// real life tests
@@ -671,7 +671,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) {
let mut matrix = aztec.getMatrix().clone();
let mut r = AztecDetectorRXingResult::new(
matrix.clone(),
NO_POINTS,
NO_POINTS.clone(),
aztec.isCompact(),
aztec.getCodeWords(),
aztec.getLayers(),
@@ -698,7 +698,7 @@ fn testEncodeDecode(data: &str, compact: bool, layers: u32) {
);
r = AztecDetectorRXingResult::new(
matrix,
NO_POINTS,
NO_POINTS.clone(),
aztec.isCompact(),
aztec.getCodeWords(),
aztec.getLayers(),
@@ -751,7 +751,7 @@ fn testWriter(
let mut r = AztecDetectorRXingResult::new(
matrix.clone(),
NO_POINTS,
NO_POINTS.clone(),
aztec.isCompact(),
aztec.getCodeWords(),
aztec.getLayers(),
@@ -780,7 +780,7 @@ fn testWriter(
}
r = AztecDetectorRXingResult::new(
matrix,
NO_POINTS,
NO_POINTS.clone(),
aztec.isCompact(),
aztec.getCodeWords(),
aztec.getLayers(),
@@ -794,7 +794,7 @@ fn getPseudoRandom() -> rand::rngs::ThreadRng {
}
fn testModeMessageComplex(compact: bool, layers: u32, words: u32, expected: &str) {
let indata = encoder::generateModeMessage(compact, layers, words);
let indata = encoder::generateModeMessage(compact, layers, words).expect("generate mode");
assert_eq!(
stripSpace(expected),
stripSpace(&indata.to_string()),

View File

@@ -107,7 +107,7 @@ fn encode(
) -> Result<BitMatrix, Exceptions> {
if format != BarcodeFormat::AZTEC {
return Err(Exceptions::IllegalArgumentException(Some(format!(
"Can only encode AZTEC, but got {:?}",
"can only encode AZTEC, but got {:?}",
format
))));
}

View File

@@ -272,7 +272,7 @@ impl<'a> Detector<'_> {
fn get_bulls_eye_corners(
&mut self,
pCenter: Point,
) -> Result<Vec<RXingResultPoint>, Exceptions> {
) -> Result<[RXingResultPoint; 4], Exceptions> {
let mut pina = pCenter;
let mut pinb = pCenter;
let mut pinc = pCenter;
@@ -355,10 +355,10 @@ impl<'a> Detector<'_> {
* @return the center point
*/
fn get_matrix_center(&self) -> Point {
let mut point_a = RXingResultPoint { x: 0.0, y: 0.0 };
let mut point_b = RXingResultPoint { x: 0.0, y: 0.0 };
let mut point_c = RXingResultPoint { x: 0.0, y: 0.0 };
let mut point_d = RXingResultPoint { x: 0.0, y: 0.0 };
let mut point_a = RXingResultPoint::default(); // { x: 0.0, y: 0.0 };
let mut point_b = RXingResultPoint::default(); // { x: 0.0, y: 0.0 };
let mut point_c = RXingResultPoint::default(); // { x: 0.0, y: 0.0 };
let mut point_d = RXingResultPoint::default(); // { x: 0.0, y: 0.0 };
let mut fnd = false;
@@ -376,20 +376,20 @@ impl<'a> Detector<'_> {
// This exception can be in case the initial rectangle is white
// In that case, surely in the bull's eye, we try to expand the rectangle.
if !fnd {
let cx: i32 = (self.image.getWidth() / 2).try_into().unwrap();
let cy: i32 = (self.image.getHeight() / 2).try_into().unwrap();
let cx: i32 = (self.image.getWidth() / 2) as i32;
let cy: i32 = (self.image.getHeight() / 2) as i32;
point_a = self
.get_first_different(&Point::new(cx + 7, cy - 7), false, 1, -1)
.to_rxing_result_point();
.into();
point_b = self
.get_first_different(&Point::new(cx + 7, cy + 7), false, 1, 1)
.to_rxing_result_point();
.into();
point_c = self
.get_first_different(&Point::new(cx - 7, cy + 7), false, -1, 1)
.to_rxing_result_point();
.into();
point_d = self
.get_first_different(&Point::new(cx - 7, cy - 7), false, -1, -1)
.to_rxing_result_point();
.into();
}
// try {
@@ -438,16 +438,16 @@ impl<'a> Detector<'_> {
if !fnd {
point_a = self
.get_first_different(&Point::new(cx + 7, cy - 7), false, 1, -1)
.to_rxing_result_point();
.into();
point_b = self
.get_first_different(&Point::new(cx + 7, cy + 7), false, 1, 1)
.to_rxing_result_point();
.into();
point_c = self
.get_first_different(&Point::new(cx - 7, cy + 7), false, -1, 1)
.to_rxing_result_point();
.into();
point_d = self
.get_first_different(&Point::new(cx - 7, cy - 7), false, -1, -1)
.to_rxing_result_point();
.into();
}
// try {
// RXingResultPoint[] cornerPoints = new WhiteRectangleDetector(image, 15, cx, cy).detect();
@@ -484,7 +484,7 @@ impl<'a> Detector<'_> {
fn get_matrix_corner_points(
&self,
bulls_eye_corners: &[RXingResultPoint],
) -> Vec<RXingResultPoint> {
) -> [RXingResultPoint; 4] {
Self::expand_square(
bulls_eye_corners,
2 * self.nb_center_layers,
@@ -505,7 +505,7 @@ impl<'a> Detector<'_> {
bottom_right: &RXingResultPoint,
bottom_left: &RXingResultPoint,
) -> Result<BitMatrix, Exceptions> {
let sampler = DefaultGridSampler {};
let sampler = DefaultGridSampler::default();
let dimension = self.get_dimension();
let low = dimension as f32 / 2.0f32 - self.nb_center_layers as f32;
@@ -700,7 +700,7 @@ impl<'a> Detector<'_> {
corner_points: &[RXingResultPoint],
old_side: u32,
new_side: u32,
) -> Vec<RXingResultPoint> {
) -> [RXingResultPoint; 4] {
let ratio = new_side as f32 / (2.0f32 * old_side as f32);
let mut dx = corner_points[0].getX() - corner_points[2].getX();
let mut dy = corner_points[0].getY() - corner_points[2].getY();
@@ -717,14 +717,11 @@ impl<'a> Detector<'_> {
let result1 = RXingResultPoint::new(centerx + ratio * dx, centery + ratio * dy);
let result3 = RXingResultPoint::new(centerx - ratio * dx, centery - ratio * dy);
vec![result0, result1, result2, result3]
[result0, result1, result2, result3]
}
fn is_valid_points(&self, x: i32, y: i32) -> bool {
x >= 0
&& x < self.image.getWidth().try_into().unwrap()
&& y >= 0
&& y < self.image.getHeight().try_into().unwrap()
x >= 0 && x < self.image.getWidth() as i32 && y >= 0 && y < self.image.getHeight() as i32
}
fn is_valid(&self, point: &RXingResultPoint) -> bool {
@@ -743,9 +740,10 @@ impl<'a> Detector<'_> {
fn get_dimension(&self) -> u32 {
if self.compact {
return 4 * self.nb_layers + 11;
4 * self.nb_layers + 11
} else {
4 * self.nb_layers + 2 * ((2 * self.nb_layers + 6) / 15) + 15
}
4 * self.nb_layers + 2 * ((2 * self.nb_layers + 6) / 15) + 15
}
}
@@ -756,10 +754,6 @@ pub struct Point {
}
impl Point {
pub fn to_rxing_result_point(&self) -> RXingResultPoint {
RXingResultPoint::new(self.x as f32, self.y as f32)
}
pub fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
@@ -773,6 +767,12 @@ impl Point {
}
}
impl From<Point> for RXingResultPoint {
fn from(value: Point) -> Self {
RXingResultPoint::new(value.x as f32, value.y as f32)
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{} {}>", &self.x, &self.y)

View File

@@ -51,9 +51,10 @@ pub const WORD_SIZE: [u32; 33] = [
* @return Aztec symbol matrix with metadata
*/
pub fn encode_simple(data: &str) -> Result<AztecCode, Exceptions> {
let bytes = encoding::all::ISO_8859_1
.encode(data, encoding::EncoderTrap::Replace)
.unwrap();
let Ok(bytes) = encoding::all::ISO_8859_1
.encode(data, encoding::EncoderTrap::Replace) else {
return Err(Exceptions::IllegalArgumentException(Some(format!("'{}' cannot be encoded as ISO_8859_1", data))));
};
encode_bytes_simple(&bytes)
}
@@ -71,10 +72,14 @@ pub fn encode(
minECCPercent: u32,
userSpecifiedLayers: i32,
) -> Result<AztecCode, Exceptions> {
let bytes = encoding::all::ISO_8859_1
.encode(data, encoding::EncoderTrap::Strict)
.expect("must encode cleanly in ISO_8859_1");
encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
if let Ok(bytes) = encoding::all::ISO_8859_1.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes(&bytes, minECCPercent, userSpecifiedLayers)
} else {
Err(Exceptions::IllegalArgumentException(Some(format!(
"'{}' cannot be encoded as ISO_8859_1",
data
))))
}
}
/**
@@ -95,10 +100,14 @@ pub fn encode_with_charset(
userSpecifiedLayers: i32,
charset: encoding::EncodingRef,
) -> Result<AztecCode, Exceptions> {
let bytes = charset
.encode(data, encoding::EncoderTrap::Strict)
.expect("must be encodeable"); //data.getBytes(null != charset ? charset : StandardCharsets.ISO_8859_1);
encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
if let Ok(bytes) = charset.encode(data, encoding::EncoderTrap::Strict) {
encode_bytes_with_charset(&bytes, minECCPercent, userSpecifiedLayers, charset)
} else {
Err(Exceptions::IllegalArgumentException(Some(format!(
"'{}' cannot be encoded as ISO_8859_1",
data
))))
}
}
/**
@@ -235,11 +244,11 @@ pub fn encode_bytes_with_charset(
&stuffed_bits,
total_bits_in_layer_var as usize,
word_size as usize,
);
)?;
// generate mode message
let messageSizeInWords = stuffed_bits.getSize() as u32 / word_size;
let modeMessage = generateModeMessage(compact, layers, messageSizeInWords);
let modeMessage = generateModeMessage(compact, layers, messageSizeInWords)?;
// allocate symbol
let baseMatrixSize = (if compact { 11 } else { 14 }) + layers * 4; // not including alignment lines
@@ -372,7 +381,11 @@ fn drawBullsEye(matrix: &mut BitMatrix, center: u32, size: u32) {
matrix.set(center + size, center + size - 1);
}
pub fn generateModeMessage(compact: bool, layers: u32, messageSizeInWords: u32) -> BitArray {
pub fn generateModeMessage(
compact: bool,
layers: u32,
messageSizeInWords: u32,
) -> Result<BitArray, Exceptions> {
let mut mode_message = BitArray::new();
if compact {
mode_message
@@ -381,7 +394,7 @@ pub fn generateModeMessage(compact: bool, layers: u32, messageSizeInWords: u32)
mode_message
.appendBits(messageSizeInWords - 1, 6)
.expect("should append");
mode_message = generateCheckWords(&mode_message, 28, 4);
mode_message = generateCheckWords(&mode_message, 28, 4)?;
} else {
mode_message
.appendBits(layers - 1, 5)
@@ -389,9 +402,9 @@ pub fn generateModeMessage(compact: bool, layers: u32, messageSizeInWords: u32)
mode_message
.appendBits(messageSizeInWords - 1, 11)
.expect("should append");
mode_message = generateCheckWords(&mode_message, 40, 4);
mode_message = generateCheckWords(&mode_message, 40, 4)?;
}
mode_message
Ok(mode_message)
}
fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeMessage: BitArray) {
@@ -433,25 +446,26 @@ fn drawModeMessage(matrix: &mut BitMatrix, compact: bool, matrixSize: u32, modeM
}
}
fn generateCheckWords(bitArray: &BitArray, totalBits: usize, wordSize: usize) -> BitArray {
fn generateCheckWords(
bitArray: &BitArray,
totalBits: usize,
wordSize: usize,
) -> Result<BitArray, Exceptions> {
// 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).expect("Should never have bad value"));
let mut rs = ReedSolomonEncoder::new(getGF(wordSize)?);
let total_words = totalBits / wordSize;
let mut message_words = bitsToWords(bitArray, wordSize, total_words);
rs.encode(&mut message_words, total_words - message_size_in_words)
.expect("must encode ok");
rs.encode(&mut message_words, total_words - message_size_in_words)?;
let start_pad = totalBits % wordSize;
let mut message_bits = BitArray::new();
message_bits.appendBits(0, start_pad).expect("must append");
for message_word in message_words {
// for (int messageWord : messageWords) {
message_bits
.appendBits(message_word as u32, wordSize)
.expect("must append");
message_bits.appendBits(message_word as u32, wordSize)?
}
// dbg!(message_bits.to_string());
message_bits
Ok(message_bits)
}
fn bitsToWords(stuffedBits: &BitArray, wordSize: usize, totalWords: usize) -> Vec<i32> {