Get it to compile

This commit is contained in:
qm3ster 2022-03-01 22:40:08 +02:00
parent 59a30b9d12
commit 1d82f60465
9 changed files with 60 additions and 73 deletions

View file

@ -21,11 +21,11 @@ is-it-maintained-open-issues = { repository = "kennytm/qrcode-rust" }
maintenance = { status = "passively-maintained" } maintenance = { status = "passively-maintained" }
[dependencies] [dependencies]
image = { version = "0.23", default-features = false, optional = true } image = { version = "0.24.1", default-features = false, optional = true }
checked_int_cast = "1" checked_int_cast = "1.0.0"
[dev-dependencies] [dev-dependencies]
image = "0.23" image = "0.24.1"
[features] [features]
default = ["image", "svg"] default = ["image", "svg"]

View file

@ -1,3 +1,4 @@
#![allow(clippy::unreadable_literal, clippy::unusual_byte_groupings)]
//! The `bits` module encodes binary data into raw bits used in a QR code. //! The `bits` module encodes binary data into raw bits used in a QR code.
use std::cmp::min; use std::cmp::min;
@ -133,14 +134,14 @@ fn test_push_number() {
assert_eq!( assert_eq!(
bytes, bytes,
vec![ vec![
0b010__110__10, // 90 0b010_110_10, // 90
0b1__001_1010, // 154 0b1_001_1010, // 154
0b1100__1011, // 203 0b1100_1011, // 203
0b0110_1101, // 109 0b0110_1101, // 109
0b01_1001_00, // 100 0b01_1001_00, // 100
0b01__111_001, // 121 0b01_111_001, // 121
0b0_1110_001, // 113 0b0_1110_001, // 113
0b1__0000000, // 128 0b1_0000000, // 128
] ]
); );
} }
@ -285,21 +286,21 @@ mod eci_tests {
fn test_9() { fn test_9() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(9), Ok(())); assert_eq!(bits.push_eci_designator(9), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__0000, 0b1001__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_0000, 0b1001_0000]);
} }
#[test] #[test]
fn test_899() { fn test_899() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(899), Ok(())); assert_eq!(bits.push_eci_designator(899), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__10_00, 0b00111000, 0b0011__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_10_00, 0b00111000, 0b0011_0000]);
} }
#[test] #[test]
fn test_999999() { fn test_999999() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_eci_designator(999999), Ok(())); assert_eq!(bits.push_eci_designator(999999), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b0111__110_0, 0b11110100, 0b00100011, 0b1111__0000]); assert_eq!(bits.into_bytes(), vec![0b0111_110_0, 0b11110100, 0b00100011, 0b1111_0000]);
} }
#[test] #[test]
@ -355,10 +356,7 @@ mod numeric_tests {
fn test_iso_18004_2006_example_1() { fn test_iso_18004_2006_example_1() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_numeric_data(b"01234567"), Ok(())); assert_eq!(bits.push_numeric_data(b"01234567"), Ok(()));
assert_eq!( assert_eq!(bits.into_bytes(), vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1_0000000]);
bits.into_bytes(),
vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1__0000000]
);
} }
#[test] #[test]
@ -376,7 +374,7 @@ mod numeric_tests {
0b0110_1110, 0b0110_1110,
0b000101_00, 0b000101_00,
0b11101010, 0b11101010,
0b0101__0000, 0b0101_0000,
] ]
); );
} }
@ -387,16 +385,7 @@ mod numeric_tests {
assert_eq!(bits.push_numeric_data(b"0123456789012345"), Ok(())); assert_eq!(bits.push_numeric_data(b"0123456789012345"), Ok(()));
assert_eq!( assert_eq!(
bits.into_bytes(), bits.into_bytes(),
vec![ vec![0b00_10000_0, 0b00000110, 0b0_0101011, 0b001_10101, 0b00110_111, 0b0000101_0, 0b01110101, 0b00101_000,]
0b00_10000_0,
0b00000110,
0b0_0101011,
0b001_10101,
0b00110_111,
0b0000101_0,
0b01110101,
0b00101__000,
]
); );
} }
@ -463,10 +452,7 @@ mod alphanumeric_tests {
fn test_iso_18004_2006_example() { fn test_iso_18004_2006_example() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(())); assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(()));
assert_eq!( assert_eq!(bits.into_bytes(), vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0_0000000]);
bits.into_bytes(),
vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0__0000000]
);
} }
#[test] #[test]
@ -522,7 +508,7 @@ mod byte_tests {
0b1010_1011, 0b1010_1011,
0b1100_1101, 0b1100_1101,
0b1110_1111, 0b1110_1111,
0b0000__0000, 0b0000_0000,
] ]
); );
} }
@ -577,7 +563,7 @@ mod kanji_tests {
fn test_iso_18004_example() { fn test_iso_18004_example() {
let mut bits = Bits::new(Version::Normal(1)); let mut bits = Bits::new(Version::Normal(1));
assert_eq!(bits.push_kanji_data(b"\x93\x5f\xe4\xaa"), Ok(())); assert_eq!(bits.push_kanji_data(b"\x93\x5f\xe4\xaa"), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b1000_0000, 0b0010_0110, 0b11001111, 0b1_1101010, 0b101010__00]); assert_eq!(bits.into_bytes(), vec![0b1000_0000, 0b0010_0110, 0b11001111, 0b1_1101010, 0b101010_00]);
} }
#[test] #[test]
@ -720,7 +706,7 @@ impl Bits {
pub fn push_terminator(&mut self, ec_level: EcLevel) -> QrResult<()> { pub fn push_terminator(&mut self, ec_level: EcLevel) -> QrResult<()> {
let terminator_size = match self.version { let terminator_size = match self.version {
Version::Micro(a) => a.as_usize() * 2 + 1, Version::Micro(a) => a.as_usize() * 2 + 1,
_ => 4, Version::Normal(_) => 4,
}; };
let cur_length = self.len(); let cur_length = self.len();
@ -740,7 +726,7 @@ impl Bits {
self.bit_offset = 0; self.bit_offset = 0;
let data_bytes_length = data_length / 8; let data_bytes_length = data_length / 8;
let padding_bytes_count = data_bytes_length - self.data.len(); let padding_bytes_count = data_bytes_length - self.data.len();
let padding = PADDING_BYTES.iter().cloned().cycle().take(padding_bytes_count); let padding = PADDING_BYTES.iter().copied().cycle().take(padding_bytes_count);
self.data.extend(padding); self.data.extend(padding);
} }
@ -783,7 +769,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"99999"), Ok(())); assert_eq!(bits.push_numeric_data(b"99999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b101_11111, 0b00111_110, 0b0011__0000]); assert_eq!(bits.into_bytes(), vec![0b101_11111, 0b00111_110, 0b0011_0000]);
} }
#[test] #[test]
@ -791,7 +777,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"9999"), Ok(())); assert_eq!(bits.push_numeric_data(b"9999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b100_11111, 0b00111_100, 0b1_000__0000]); assert_eq!(bits.into_bytes(), vec![0b100_11111, 0b00111_100, 0b1_000_0000]);
} }
#[test] #[test]
@ -799,7 +785,7 @@ mod finish_tests {
let mut bits = Bits::new(Version::Micro(1)); let mut bits = Bits::new(Version::Micro(1));
assert_eq!(bits.push_numeric_data(b"999"), Ok(())); assert_eq!(bits.push_numeric_data(b"999"), Ok(()));
assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(()));
assert_eq!(bits.into_bytes(), vec![0b011_11111, 0b00111_000, 0b0000__0000]); assert_eq!(bits.into_bytes(), vec![0b011_11111, 0b00111_000, 0b0000_0000]);
} }
#[test] #[test]
@ -878,7 +864,7 @@ mod encode_tests {
#[test] #[test]
fn test_auto_mode_switch() { fn test_auto_mode_switch() {
let res = encode(b"123A", Version::Micro(2), EcLevel::L); let res = encode(b"123A", Version::Micro(2), EcLevel::L);
assert_eq!(res, Ok(vec![0b0_0011_000, 0b1111011_1, 0b001_00101, 0b0_00000__00, 0b11101100])); assert_eq!(res, Ok(vec![0b0_0011_000, 0b1111011_1, 0b001_00101, 0b0_00000_00, 0b11101100]));
} }
#[test] #[test]
@ -904,7 +890,7 @@ mod encode_tests {
pub fn encode_auto(data: &[u8], ec_level: EcLevel) -> QrResult<Bits> { pub fn encode_auto(data: &[u8], ec_level: EcLevel) -> QrResult<Bits> {
let segments = Parser::new(data).collect::<Vec<Segment>>(); let segments = Parser::new(data).collect::<Vec<Segment>>();
for version in &[Version::Normal(9), Version::Normal(26), Version::Normal(40)] { for version in &[Version::Normal(9), Version::Normal(26), Version::Normal(40)] {
let opt_segments = Optimizer::new(segments.iter().cloned(), *version).collect::<Vec<_>>(); let opt_segments = Optimizer::new(segments.iter().copied(), *version).collect::<Vec<_>>();
let total_len = total_encoded_len(&*opt_segments, *version); let total_len = total_encoded_len(&*opt_segments, *version);
let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).expect("invalid DATA_LENGTHS"); let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).expect("invalid DATA_LENGTHS");
if total_len <= data_capacity { if total_len <= data_capacity {

View file

@ -57,6 +57,7 @@ impl Module {
/// assert_eq!(Module::Masked(Color::Dark).mask(true), Module::Masked(Color::Dark)); /// assert_eq!(Module::Masked(Color::Dark).mask(true), Module::Masked(Color::Dark));
/// assert_eq!(Module::Masked(Color::Dark).mask(false), Module::Masked(Color::Dark)); /// assert_eq!(Module::Masked(Color::Dark).mask(false), Module::Masked(Color::Dark));
/// ///
#[must_use]
pub fn mask(self, should_invert: bool) -> Self { pub fn mask(self, should_invert: bool) -> Self {
match (self, should_invert) { match (self, should_invert) {
(Module::Empty, true) => Module::Masked(Color::Dark), (Module::Empty, true) => Module::Masked(Color::Dark),
@ -223,9 +224,9 @@ impl Canvas {
y + j, y + j,
#[allow(clippy::match_same_arms)] #[allow(clippy::match_same_arms)]
match (i, j) { match (i, j) {
(4, _) | (_, 4) | (-4, _) | (_, -4) => Color::Light, (4 | -4, _) | (_, 4 | -4) => Color::Light,
(3, _) | (_, 3) | (-3, _) | (_, -3) => Color::Dark, (3 | -3, _) | (_, 3 | -3) => Color::Dark,
(2, _) | (_, 2) | (-2, _) | (_, -2) => Color::Light, (2 | -2, _) | (_, 2 | -2) => Color::Light,
_ => Color::Dark, _ => Color::Dark,
}, },
); );
@ -325,7 +326,7 @@ impl Canvas {
x + i, x + i,
y + j, y + j,
match (i, j) { match (i, j) {
(2, _) | (_, 2) | (-2, _) | (_, -2) | (0, 0) => Color::Dark, (2 | -2, _) | (_, 2 | -2) | (0, 0) => Color::Dark,
_ => Color::Light, _ => Color::Light,
}, },
); );
@ -693,7 +694,7 @@ mod draw_version_info_tests {
#[test] #[test]
fn test_draw_number() { fn test_draw_number() {
let mut c = Canvas::new(Version::Micro(1), EcLevel::L); let mut c = Canvas::new(Version::Micro(1), EcLevel::L);
c.draw_number(0b10101101, 8, Color::Dark, Color::Light, &[(0, 0), (0, -1), (-2, -2), (-2, 0)]); c.draw_number(0b1010_1101, 8, Color::Dark, Color::Light, &[(0, 0), (0, -1), (-2, -2), (-2, 0)]);
assert_eq!( assert_eq!(
&*c.to_debug_str(), &*c.to_debug_str(),
"\n\ "\n\
@ -992,7 +993,7 @@ pub fn is_functional(version: Version, width: i16, x: i16, y: i16) -> bool {
true true
} else if a == 1 { } else if a == 1 {
false false
} else if 2 <= a && a <= 6 { } else if (2..=6).contains(&a) {
(width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2 (width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2
} else { } else {
let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()]; let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()];
@ -1364,7 +1365,7 @@ impl Canvas {
let bits_end = if i == last_word { 4 } else { 0 }; let bits_end = if i == last_word { 4 } else { 0 };
'outside: for j in (bits_end..=7).rev() { 'outside: for j in (bits_end..=7).rev() {
let color = if (*b & (1 << j)) == 0 { Color::Light } else { Color::Dark }; let color = if (*b & (1 << j)) == 0 { Color::Light } else { Color::Dark };
while let Some((x, y)) = coords.next() { for (x, y) in coords.by_ref() {
let r = self.get_mut(x, y); let r = self.get_mut(x, y);
if *r == Module::Empty { if *r == Module::Empty {
*r = Module::Unmasked(color); *r = Module::Unmasked(color);
@ -1378,10 +1379,8 @@ impl Canvas {
/// Draws the encoded data and error correction codes to the empty modules. /// Draws the encoded data and error correction codes to the empty modules.
pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) { pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) {
let is_half_codeword_at_end = match (self.version, self.ec_level) { let is_half_codeword_at_end =
(Version::Micro(1), EcLevel::L) | (Version::Micro(3), EcLevel::M) => true, matches!((self.version, self.ec_level), (Version::Micro(1), EcLevel::L) | (Version::Micro(3), EcLevel::M));
_ => false,
};
let mut coords = DataModuleIter::new(self.version); let mut coords = DataModuleIter::new(self.version);
self.draw_codewords(data, is_half_codeword_at_end, &mut coords); self.draw_codewords(data, is_half_codeword_at_end, &mut coords);

View file

@ -14,6 +14,7 @@ impl Truncate for u16 {
} }
} }
#[allow(clippy::wrong_self_convention)]
pub trait As { pub trait As {
fn as_u16(self) -> u16; fn as_u16(self) -> u16;
fn as_i16(self) -> i16; fn as_i16(self) -> i16;

View file

@ -179,8 +179,8 @@ pub fn max_allowed_errors(version: Version, ec_level: EcLevel) -> QrResult<usize
use crate::Version::{Micro, Normal}; use crate::Version::{Micro, Normal};
let p = match (version, ec_level) { let p = match (version, ec_level) {
(Micro(2), L) | (Normal(1), L) => 3, (Micro(2) | Normal(1), L) => 3,
(Micro(_), L) | (Normal(2), L) | (Micro(2), M) | (Normal(1), M) => 2, (Micro(_) | Normal(2), L) | (Micro(2) | Normal(1), M) => 2,
(Normal(1), _) | (Normal(3), L) => 1, (Normal(1), _) | (Normal(3), L) => 1,
_ => 0, _ => 0,
}; };

View file

@ -325,7 +325,7 @@ mod image_tests {
fn test_annex_i_qr_as_image() { fn test_annex_i_qr_as_image() {
let code = QrCode::new(b"01234567").unwrap(); let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<Luma<u8>>().build(); let image = code.render::<Luma<u8>>().build();
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma(); let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma8();
assert_eq!(image.dimensions(), expected.dimensions()); assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw()); assert_eq!(image.into_raw(), expected.into_raw());
} }
@ -339,7 +339,7 @@ mod image_tests {
.dark_color(Rgb([128, 0, 0])) .dark_color(Rgb([128, 0, 0]))
.light_color(Rgb([255, 255, 128])) .light_color(Rgb([255, 255, 128]))
.build(); .build();
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb(); let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb8();
assert_eq!(image.dimensions(), expected.dimensions()); assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw()); assert_eq!(image.into_raw(), expected.into_raw());
} }

View file

@ -341,7 +341,7 @@ mod optimize_tests {
fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) { fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) {
let prev_len = total_encoded_len(&*given, version); let prev_len = total_encoded_len(&*given, version);
let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::<Vec<_>>(); let opt_segs = Optimizer::new(given.iter().copied(), version).collect::<Vec<_>>();
let new_len = total_encoded_len(&*opt_segs, version); let new_len = total_encoded_len(&*opt_segs, version);
if given != opt_segs { if given != opt_segs {
assert!(prev_len > new_len, "{} > {}", prev_len, new_len); assert!(prev_len > new_len, "{} > {}", prev_len, new_len);

View file

@ -7,8 +7,12 @@ use image::{ImageBuffer, Luma, LumaA, Pixel as ImagePixel, Primitive, Rgb, Rgba}
macro_rules! impl_pixel_for_image_pixel { macro_rules! impl_pixel_for_image_pixel {
($p:ident<$s:ident>: $c:pat => $d:expr) => { ($p:ident<$s:ident>: $c:pat => $d:expr) => {
impl<$s: Primitive + 'static> Pixel for $p<$s> { impl<$s> Pixel for $p<$s>
type Image = ImageBuffer<Self, Vec<S>>; where
$s: Primitive + 'static,
Self: ImagePixel,
{
type Image = ImageBuffer<Self, Vec<<Self as ImagePixel>::Subpixel>>;
type Canvas = (Self, Self::Image); type Canvas = (Self, Self::Image);
fn default_color(color: Color) -> Self { fn default_color(color: Color) -> Self {

View file

@ -275,7 +275,7 @@ impl Mode {
/// ///
pub fn max(self, other: Self) -> Self { pub fn max(self, other: Self) -> Self {
match self.partial_cmp(&other) { match self.partial_cmp(&other) {
Some(Ordering::Less) | Some(Ordering::Equal) => other, Some(Ordering::Less | Ordering::Equal) => other,
Some(Ordering::Greater) => self, Some(Ordering::Greater) => self,
None => Mode::Byte, None => Mode::Byte,
} }
@ -286,15 +286,13 @@ impl PartialOrd for Mode {
/// Defines a partial ordering between modes. If `a <= b`, then `b` contains /// Defines a partial ordering between modes. If `a <= b`, then `b` contains
/// a superset of all characters supported by `a`. /// a superset of all characters supported by `a`.
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (*self, *other) { match (self, other) {
(Mode::Numeric, Mode::Alphanumeric) (Mode::Numeric, Mode::Alphanumeric) | (Mode::Numeric | Mode::Alphanumeric | Mode::Kanji, Mode::Byte) => {
| (Mode::Numeric, Mode::Byte) Some(Ordering::Less)
| (Mode::Alphanumeric, Mode::Byte) }
| (Mode::Kanji, Mode::Byte) => Some(Ordering::Less), (Mode::Alphanumeric, Mode::Numeric) | (Mode::Byte, Mode::Numeric | Mode::Alphanumeric | Mode::Kanji) => {
(Mode::Alphanumeric, Mode::Numeric) Some(Ordering::Greater)
| (Mode::Byte, Mode::Numeric) }
| (Mode::Byte, Mode::Alphanumeric)
| (Mode::Byte, Mode::Kanji) => Some(Ordering::Greater),
(a, b) if a == b => Some(Ordering::Equal), (a, b) if a == b => Some(Ordering::Equal),
_ => None, _ => None,
} }
@ -309,8 +307,7 @@ mod mode_tests {
fn test_mode_order() { fn test_mode_order() {
assert!(Numeric < Alphanumeric); assert!(Numeric < Alphanumeric);
assert!(Byte > Kanji); assert!(Byte > Kanji);
assert!(!(Numeric < Kanji)); assert!(Numeric.partial_cmp(&Kanji).is_none());
assert!(!(Numeric >= Kanji));
} }
#[test] #[test]