cargo fmt

This commit is contained in:
Henry Schimke
2022-10-17 10:51:08 -05:00
parent 111fe47566
commit ae11af8c5b
136 changed files with 2117 additions and 1776 deletions

View File

@@ -415,7 +415,8 @@ fn testAppendLengthInfo() {
Version::getVersionForNumber(1).unwrap(),
Mode::NUMERIC,
&mut bits,
).expect("ok");
)
.expect("ok");
assert_eq!(" ........ .X", bits.to_string()); // 10 bits.
let mut bits = BitArray::new();
encoder::appendLengthInfo(
@@ -423,7 +424,8 @@ fn testAppendLengthInfo() {
Version::getVersionForNumber(10).unwrap(),
Mode::ALPHANUMERIC,
&mut bits,
).expect("ok");
)
.expect("ok");
assert_eq!(" ........ .X.", bits.to_string()); // 11 bits.
let mut bits = BitArray::new();
encoder::appendLengthInfo(
@@ -431,7 +433,8 @@ fn testAppendLengthInfo() {
Version::getVersionForNumber(27).unwrap(),
Mode::BYTE,
&mut bits,
).expect("ok");
)
.expect("ok");
assert_eq!(" ........ XXXXXXXX", bits.to_string()); // 16 bits.
let mut bits = BitArray::new();
encoder::appendLengthInfo(
@@ -439,7 +442,8 @@ fn testAppendLengthInfo() {
Version::getVersionForNumber(40).unwrap(),
Mode::KANJI,
&mut bits,
).expect("ok");
)
.expect("ok");
assert_eq!(" ..X..... ....", bits.to_string()); // 12 bits.
}
@@ -453,7 +457,8 @@ fn testAppendBytes() {
Mode::NUMERIC,
&mut bits,
encoder::DEFAULT_BYTE_MODE_ENCODING,
).expect("ok");
)
.expect("ok");
assert_eq!(" ...X", bits.to_string());
// Should use appendAlphanumericBytes.
// A = 10 = 0xa = 001010 in 6 bits
@@ -463,7 +468,8 @@ fn testAppendBytes() {
Mode::ALPHANUMERIC,
&mut bits,
encoder::DEFAULT_BYTE_MODE_ENCODING,
).expect("ok");
)
.expect("ok");
assert_eq!(" ..X.X.", bits.to_string());
// Lower letters such as 'a' cannot be encoded in MODE_ALPHANUMERIC.
//try {
@@ -488,7 +494,8 @@ fn testAppendBytes() {
Mode::BYTE,
&mut bits,
encoder::DEFAULT_BYTE_MODE_ENCODING,
).expect("ok");
)
.expect("ok");
assert_eq!(" .XX....X .XX...X. .XX...XX", bits.to_string());
// Anything can be encoded in QRCode.MODE_8BIT_BYTE.
encoder::appendBytes(
@@ -496,7 +503,8 @@ fn testAppendBytes() {
Mode::BYTE,
&mut bits,
encoder::DEFAULT_BYTE_MODE_ENCODING,
).expect("ok");
)
.expect("ok");
// Should use appendKanjiBytes.
// 0x93, 0x5f
let mut bits = BitArray::new();
@@ -505,7 +513,8 @@ fn testAppendBytes() {
Mode::KANJI,
&mut bits,
encoder::DEFAULT_BYTE_MODE_ENCODING,
).expect("ok");
)
.expect("ok");
assert_eq!(" .XX.XX.. XXXXX", bits.to_string());
}
@@ -540,80 +549,45 @@ fn testTerminateBits() {
#[test]
fn testGetNumDataBytesAndNumECBytesForBlockID() {
// Version 1-H.
let (numDataBytes,numEcBytes) = encoder::getNumDataBytesAndNumECBytesForBlockID(
26,
9,
1,
0,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(26, 9, 1, 0).expect("ok");
assert_eq!(9, numDataBytes);
assert_eq!(17, numEcBytes);
// Version 3-H. 2 blocks.
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
70,
26,
2,
0,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 0).expect("ok");
assert_eq!(13, numDataBytes);
assert_eq!(22, numEcBytes);
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
70,
26,
2,
1,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(70, 26, 2, 1).expect("ok");
assert_eq!(13, numDataBytes);
assert_eq!(22, numEcBytes);
// Version 7-H. (4 + 1) blocks.
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
196,
66,
5,
0,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 0).expect("ok");
assert_eq!(13, numDataBytes);
assert_eq!(26, numEcBytes);
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
196,
66,
5,
4,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(196, 66, 5, 4).expect("ok");
assert_eq!(14, numDataBytes);
assert_eq!(26, numEcBytes);
// Version 40-H. (20 + 61) blocks.
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
3706,
1276,
81,
0,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 0).expect("ok");
assert_eq!(15, numDataBytes);
assert_eq!(30, numEcBytes);
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
3706,
1276,
81,
20,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 20).expect("ok");
assert_eq!(16, numDataBytes);
assert_eq!(30, numEcBytes);
let (numDataBytes,numEcBytes) =encoder::getNumDataBytesAndNumECBytesForBlockID(
3706,
1276,
81,
80,
).expect("ok");
let (numDataBytes, numEcBytes) =
encoder::getNumDataBytesAndNumECBytesForBlockID(3706, 1276, 81, 80).expect("ok");
assert_eq!(16, numDataBytes);
assert_eq!(30, numEcBytes);
}
@@ -738,7 +712,8 @@ fn testAppendAlphanumericBytes() {
fn testAppend8BitBytes() {
// 0x61, 0x62, 0x63
let mut bits = BitArray::new();
encoder::append8BitBytes("abc", &mut bits, encoder::DEFAULT_BYTE_MODE_ENCODING).expect("append");
encoder::append8BitBytes("abc", &mut bits, encoder::DEFAULT_BYTE_MODE_ENCODING)
.expect("append");
assert_eq!(" .XX....X .XX...X. .XX...XX", bits.to_string());
// Empty.
let mut bits = BitArray::new();

View File

@@ -254,7 +254,8 @@ fn testBuildMatrix() {
Version::getVersionForNumber(1).expect("version"), // Version 1
3, // Mask pattern 3
&mut matrix,
).expect("append");
)
.expect("append");
let expected = r" 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1
1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1

View File

@@ -14,20 +14,21 @@
* limitations under the License.
*/
use crate::qrcode::{decoder::{Mode, ErrorCorrectionLevel, Version}, encoder::ByteMatrix};
use crate::qrcode::{
decoder::{ErrorCorrectionLevel, Mode, Version},
encoder::ByteMatrix,
};
use super::QRCode;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++
*/
#[test]
fn test() {
let mut qrCode = QRCode::new();
fn test() {
let mut qrCode = QRCode::new();
// First, test simple setters and getters.
// We use numbers of version 7-H.
@@ -42,43 +43,43 @@ use super::QRCode;
assert_eq!(3, qrCode.getMaskPattern());
// Prepare the matrix.
let mut matrix = ByteMatrix::new(45, 45);
let mut matrix = ByteMatrix::new(45, 45);
// Just set bogus zero/one values.
for y in 0..45 {
// for (int y = 0; y < 45; ++y) {
for x in 0..45 {
// for (int x = 0; x < 45; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
// for (int y = 0; y < 45; ++y) {
for x in 0..45 {
// for (int x = 0; x < 45; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
}
// Set the matrix.
qrCode.setMatrix(matrix.clone());
assert_eq!(&matrix, qrCode.getMatrix().as_ref().unwrap());
}
}
#[test]
fn testToString1() {
let qrCode = QRCode::new();
#[test]
fn testToString1() {
let qrCode = QRCode::new();
let expected =
"<<\n mode: null\n ecLevel: null\n version: null\n maskPattern: -1\n matrix: null\n>>\n";
assert_eq!(expected, qrCode.to_string());
}
assert_eq!(expected, qrCode.to_string());
}
#[test]
fn testToString2() {
let mut qrCode = QRCode::new();
#[test]
fn testToString2() {
let mut qrCode = QRCode::new();
qrCode.setMode(Mode::BYTE);
qrCode.setECLevel(ErrorCorrectionLevel::H);
qrCode.setVersion(Version::getVersionForNumber(1).expect("predefined value must exist"));
qrCode.setMaskPattern(3);
let mut matrix = ByteMatrix::new(21, 21);
let mut matrix = ByteMatrix::new(21, 21);
for y in 0..21 {
// for (int y = 0; y < 21; ++y) {
for x in 0..21 {
// for (int x = 0; x < 21; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
// for (int y = 0; y < 21; ++y) {
for x in 0..21 {
// for (int x = 0; x < 21; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
}
qrCode.setMatrix(matrix);
let expected = "<<\n \
@@ -110,13 +111,12 @@ use super::QRCode;
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
>>\n";
assert_eq!(expected, qrCode.to_string());
}
}
#[test]
fn testIsValidMaskPattern() {
#[test]
fn testIsValidMaskPattern() {
assert!(!QRCode::isValidMaskPattern(-1));
assert!(QRCode::isValidMaskPattern(0));
assert!(QRCode::isValidMaskPattern(7));
assert!(!QRCode::isValidMaskPattern(8));
}
}

View File

@@ -43,7 +43,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::{
common::{
reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonEncoder},
BitArray, CharacterSetECI,
BitArray, CharacterSetECI,
},
qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef},
EncodeHintType, EncodeHintValue, EncodingHintDictionary, Exceptions,
@@ -155,9 +155,11 @@ pub fn encode_with_hints(
let encoding = if encoding.is_some() {
encoding.unwrap()
} else {
if let Ok(_encs) = DEFAULT_BYTE_MODE_ENCODING.encode(content, encoding::EncoderTrap::Strict){
if let Ok(_encs) =
DEFAULT_BYTE_MODE_ENCODING.encode(content, encoding::EncoderTrap::Strict)
{
DEFAULT_BYTE_MODE_ENCODING
}else {
} else {
has_encoding_hint = true;
encoding::all::UTF_8
}
@@ -302,8 +304,12 @@ fn recommendVersion(
// Hard part: need to know version to know how many bits length takes. But need to know how many
// bits it takes to know version. First we take a guess at version by assuming version will be
// the minimum, 1:
let provisional_bits_needed =
calculateBitsNeeded(mode, header_bits, data_bits, Version::getVersionForNumber(1)?);
let provisional_bits_needed = calculateBitsNeeded(
mode,
header_bits,
data_bits,
Version::getVersionForNumber(1)?,
);
let provisional_version = chooseVersion(provisional_bits_needed, ec_level)?;
// Use that guess to calculate the right version. I am still not sure this works in 100% of cases.

View File

@@ -48,10 +48,10 @@ pub fn applyMaskPenaltyRule2(matrix: &ByteMatrix) -> u32 {
let array = matrix.getArray();
let width = matrix.getWidth();
let height = matrix.getHeight();
for y in 0..(height -1) as usize {
for y in 0..(height - 1) as usize {
// for (int y = 0; y < height - 1; y++) {
let arrayY = &array[y];
for x in 0..(width -1) as usize {
for x in 0..(width - 1) as usize {
// for (int x = 0; x < width - 1; x++) {
let value = arrayY[x];
if value == arrayY[x + 1] && value == array[y + 1][x] && value == array[y + 1][x + 1] {
@@ -154,7 +154,8 @@ pub fn applyMaskPenaltyRule4(matrix: &ByteMatrix) -> u32 {
}
}
let numTotalCells = matrix.getHeight() * matrix.getWidth();
let fivePercentVariances = (numDarkCells as i64 * 2 - numTotalCells as i64).abs() as u32 * 10 / numTotalCells;
let fivePercentVariances =
(numDarkCells as i64 * 2 - numTotalCells as i64).abs() as u32 * 10 / numTotalCells;
return fivePercentVariances * N4;
}

View File

@@ -24,7 +24,7 @@ use crate::{
Exceptions,
};
use unicode_segmentation::{ UnicodeSegmentation};
use unicode_segmentation::UnicodeSegmentation;
use super::encoder;
@@ -834,14 +834,15 @@ impl RXingResultList {
}
let first = list.get(0);
// prepend or insert a FNC1_FIRST_POSITION after the ECI (if any)
if first.is_some() && first.as_ref().unwrap().mode != Mode::ECI {//&& containsECI {
if first.is_some() && first.as_ref().unwrap().mode != Mode::ECI {
//&& containsECI {
list.insert(
if first.as_ref().unwrap().mode != Mode::ECI {
//first
list.len()
} else {
//second
list.len()-1
list.len() - 1
},
RXingResultNode::new(
Mode::FNC1_FIRST_POSITION,

View File

@@ -1,23 +1,23 @@
mod qr_code;
mod byte_matrix;
mod block_pair;
mod byte_matrix;
pub mod encoder;
pub mod mask_util;
pub mod matrix_util;
mod minimal_encoder;
pub mod encoder;
mod qr_code;
pub use qr_code::*;
pub use byte_matrix::*;
pub use block_pair::*;
pub use byte_matrix::*;
pub use minimal_encoder::*;
pub use qr_code::*;
#[cfg(test)]
mod QRCodeTestCase;
#[cfg(test)]
mod BitVectorTestCase;
#[cfg(test)]
mod EncoderTestCase;
#[cfg(test)]
mod MaskUtilTestCase;
#[cfg(test)]
mod MatrixUtilTestCase;
#[cfg(test)]
mod EncoderTestCase;
mod QRCodeTestCase;

View File

@@ -16,131 +16,124 @@
use std::fmt;
use crate::qrcode::decoder::{Mode, ErrorCorrectionLevel, Version, VersionRef};
use crate::qrcode::decoder::{ErrorCorrectionLevel, Mode, Version, VersionRef};
use super::ByteMatrix;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/
#[derive(Debug, Clone)]
pub struct QRCode {
// public static final int NUM_MASK_PATTERNS = 8;
mode:Option<Mode>,
ecLevel:Option<ErrorCorrectionLevel>,
version:Option<VersionRef>,
maskPattern:i32,
matrix:Option<ByteMatrix>,
// public static final int NUM_MASK_PATTERNS = 8;
mode: Option<Mode>,
ecLevel: Option<ErrorCorrectionLevel>,
version: Option<VersionRef>,
maskPattern: i32,
matrix: Option<ByteMatrix>,
}
impl QRCode {
pub const NUM_MASK_PATTERNS : i32 = 8;
pub const NUM_MASK_PATTERNS: i32 = 8;
pub fn new()->Self {
Self {
mode: None,
ecLevel: None,
version: None,
maskPattern: -1,
matrix: None,
pub fn new() -> Self {
Self {
mode: None,
ecLevel: None,
version: None,
maskPattern: -1,
matrix: None,
}
}
}
/**
* @return the mode. Not relevant if {@link com.google.zxing.EncodeHintType#QR_COMPACT} is selected.
*/
pub fn getMode(&self) -> &Option<Mode>{
&self.mode
}
/**
* @return the mode. Not relevant if {@link com.google.zxing.EncodeHintType#QR_COMPACT} is selected.
*/
pub fn getMode(&self) -> &Option<Mode> {
&self.mode
}
pub fn getECLevel(&self) -> &Option<ErrorCorrectionLevel>{
&self.ecLevel
}
pub fn getECLevel(&self) -> &Option<ErrorCorrectionLevel> {
&self.ecLevel
}
pub fn getVersion(&self) -> &Option<&'static Version>{
&self.version
}
pub fn getVersion(&self) -> &Option<&'static Version> {
&self.version
}
pub fn getMaskPattern(&self) -> i32{
self.maskPattern
}
pub fn getMaskPattern(&self) -> i32 {
self.maskPattern
}
pub fn getMatrix(&self) ->&Option<ByteMatrix>{
&self.matrix
}
pub fn getMatrix(&self) -> &Option<ByteMatrix> {
&self.matrix
}
pub fn setMode(&mut self, value: Mode) {
self.mode = Some(value);
}
pub fn setMode(&mut self, value:Mode) {
self.mode = Some(value);
}
pub fn setECLevel(&mut self, value: ErrorCorrectionLevel) {
self.ecLevel = Some(value);
}
pub fn setECLevel(&mut self, value:ErrorCorrectionLevel) {
self.ecLevel = Some(value);
}
pub fn setVersion(&mut self, version: &'static Version) {
self.version = Some(version);
}
pub fn setVersion(&mut self, version:&'static Version) {
self.version = Some(version);
}
pub fn setMaskPattern(&mut self, value: i32) {
self.maskPattern = value;
}
pub fn setMaskPattern(&mut self, value:i32) {
self.maskPattern = value;
}
pub fn setMatrix(&mut self, value:ByteMatrix) {
self.matrix = Some(value);
}
// Check if "mask_pattern" is valid.
pub fn isValidMaskPattern( maskPattern:i32) -> bool{
maskPattern >= 0 && maskPattern < Self::NUM_MASK_PATTERNS
}
pub fn setMatrix(&mut self, value: ByteMatrix) {
self.matrix = Some(value);
}
// Check if "mask_pattern" is valid.
pub fn isValidMaskPattern(maskPattern: i32) -> bool {
maskPattern >= 0 && maskPattern < Self::NUM_MASK_PATTERNS
}
}
impl fmt::Display for QRCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::with_capacity(200);
result.push_str("<<\n");
result.push_str(" mode: ");
if self.mode.is_some() {
result.push_str(&format!("{:?}",self.mode.as_ref().unwrap()));
}else {
result.push_str("null");
}
// result.push_str(&format!("{:?}", self.mode));
result.push_str("\n ecLevel: ");
if self.ecLevel.is_some() {
result.push_str(&format!("{:?}",self.ecLevel.as_ref().unwrap()));
}else {
result.push_str("null");
}
// result.push_str(&format!("{:?}", self.ecLevel));
result.push_str("\n version: ");
if self.version.is_some() {
result.push_str(&format!("{}",self.version.as_ref().unwrap()));
}else {
result.push_str("null");
}
result.push_str("\n maskPattern: ");
result.push_str(&format!("{}",self.maskPattern));
if self.matrix.is_none() {
result.push_str("\n matrix: null\n");
} else {
result.push_str("\n matrix:\n");
if self.matrix.is_some() {
result.push_str(&format!("{}",self.matrix.as_ref().unwrap()));
}else {
result.push_str("null");
let mut result = String::with_capacity(200);
result.push_str("<<\n");
result.push_str(" mode: ");
if self.mode.is_some() {
result.push_str(&format!("{:?}", self.mode.as_ref().unwrap()));
} else {
result.push_str("null");
}
}
result.push_str(">>\n");
write!(f, "{}", result)
// result.push_str(&format!("{:?}", self.mode));
result.push_str("\n ecLevel: ");
if self.ecLevel.is_some() {
result.push_str(&format!("{:?}", self.ecLevel.as_ref().unwrap()));
} else {
result.push_str("null");
}
// result.push_str(&format!("{:?}", self.ecLevel));
result.push_str("\n version: ");
if self.version.is_some() {
result.push_str(&format!("{}", self.version.as_ref().unwrap()));
} else {
result.push_str("null");
}
result.push_str("\n maskPattern: ");
result.push_str(&format!("{}", self.maskPattern));
if self.matrix.is_none() {
result.push_str("\n matrix: null\n");
} else {
result.push_str("\n matrix:\n");
if self.matrix.is_some() {
result.push_str(&format!("{}", self.matrix.as_ref().unwrap()));
} else {
result.push_str("null");
}
}
result.push_str(">>\n");
write!(f, "{}", result)
}
}
}