diff --git a/Cargo.toml b/Cargo.toml index 2120c73..ebd4d92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,11 +21,11 @@ is-it-maintained-open-issues = { repository = "kennytm/qrcode-rust" } maintenance = { status = "passively-maintained" } [dependencies] -image = { version = "0.23", default-features = false, optional = true } -checked_int_cast = "1" +image = { version = "0.24.1", default-features = false, optional = true } +checked_int_cast = "1.0.0" [dev-dependencies] -image = "0.23" +image = "0.24.1" [features] default = ["image", "svg"] diff --git a/src/bits.rs b/src/bits.rs index 5d1f93f..fcad4df 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -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. use std::cmp::min; @@ -133,14 +134,14 @@ fn test_push_number() { assert_eq!( bytes, vec![ - 0b010__110__10, // 90 - 0b1__001_1010, // 154 - 0b1100__1011, // 203 - 0b0110_1101, // 109 - 0b01_1001_00, // 100 - 0b01__111_001, // 121 - 0b0_1110_001, // 113 - 0b1__0000000, // 128 + 0b010_110_10, // 90 + 0b1_001_1010, // 154 + 0b1100_1011, // 203 + 0b0110_1101, // 109 + 0b01_1001_00, // 100 + 0b01_111_001, // 121 + 0b0_1110_001, // 113 + 0b1_0000000, // 128 ] ); } @@ -285,21 +286,21 @@ mod eci_tests { fn test_9() { let mut bits = Bits::new(Version::Normal(1)); 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] fn test_899() { let mut bits = Bits::new(Version::Normal(1)); 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] fn test_999999() { let mut bits = Bits::new(Version::Normal(1)); 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] @@ -355,10 +356,7 @@ mod numeric_tests { fn test_iso_18004_2006_example_1() { let mut bits = Bits::new(Version::Normal(1)); assert_eq!(bits.push_numeric_data(b"01234567"), Ok(())); - assert_eq!( - bits.into_bytes(), - vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1__0000000] - ); + assert_eq!(bits.into_bytes(), vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1_0000000]); } #[test] @@ -376,7 +374,7 @@ mod numeric_tests { 0b0110_1110, 0b000101_00, 0b11101010, - 0b0101__0000, + 0b0101_0000, ] ); } @@ -387,16 +385,7 @@ mod numeric_tests { assert_eq!(bits.push_numeric_data(b"0123456789012345"), Ok(())); assert_eq!( bits.into_bytes(), - vec![ - 0b00_10000_0, - 0b00000110, - 0b0_0101011, - 0b001_10101, - 0b00110_111, - 0b0000101_0, - 0b01110101, - 0b00101__000, - ] + vec![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() { let mut bits = Bits::new(Version::Normal(1)); assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(())); - assert_eq!( - bits.into_bytes(), - vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0__0000000] - ); + assert_eq!(bits.into_bytes(), vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0_0000000]); } #[test] @@ -522,7 +508,7 @@ mod byte_tests { 0b1010_1011, 0b1100_1101, 0b1110_1111, - 0b0000__0000, + 0b0000_0000, ] ); } @@ -577,7 +563,7 @@ mod kanji_tests { fn test_iso_18004_example() { let mut bits = Bits::new(Version::Normal(1)); 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] @@ -720,7 +706,7 @@ impl Bits { pub fn push_terminator(&mut self, ec_level: EcLevel) -> QrResult<()> { let terminator_size = match self.version { Version::Micro(a) => a.as_usize() * 2 + 1, - _ => 4, + Version::Normal(_) => 4, }; let cur_length = self.len(); @@ -740,7 +726,7 @@ impl Bits { self.bit_offset = 0; let data_bytes_length = data_length / 8; 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); } @@ -783,7 +769,7 @@ mod finish_tests { let mut bits = Bits::new(Version::Micro(1)); assert_eq!(bits.push_numeric_data(b"99999"), 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] @@ -791,7 +777,7 @@ mod finish_tests { let mut bits = Bits::new(Version::Micro(1)); assert_eq!(bits.push_numeric_data(b"9999"), 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] @@ -799,7 +785,7 @@ mod finish_tests { let mut bits = Bits::new(Version::Micro(1)); assert_eq!(bits.push_numeric_data(b"999"), 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] @@ -878,7 +864,7 @@ mod encode_tests { #[test] fn test_auto_mode_switch() { 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] @@ -904,7 +890,7 @@ mod encode_tests { pub fn encode_auto(data: &[u8], ec_level: EcLevel) -> QrResult { let segments = Parser::new(data).collect::>(); for version in &[Version::Normal(9), Version::Normal(26), Version::Normal(40)] { - let opt_segments = Optimizer::new(segments.iter().cloned(), *version).collect::>(); + let opt_segments = Optimizer::new(segments.iter().copied(), *version).collect::>(); let total_len = total_encoded_len(&*opt_segments, *version); let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).expect("invalid DATA_LENGTHS"); if total_len <= data_capacity { diff --git a/src/canvas.rs b/src/canvas.rs index 9d29a67..f430f72 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -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(false), Module::Masked(Color::Dark)); /// + #[must_use] pub fn mask(self, should_invert: bool) -> Self { match (self, should_invert) { (Module::Empty, true) => Module::Masked(Color::Dark), @@ -223,9 +224,9 @@ impl Canvas { y + j, #[allow(clippy::match_same_arms)] match (i, j) { - (4, _) | (_, 4) | (-4, _) | (_, -4) => Color::Light, - (3, _) | (_, 3) | (-3, _) | (_, -3) => Color::Dark, - (2, _) | (_, 2) | (-2, _) | (_, -2) => Color::Light, + (4 | -4, _) | (_, 4 | -4) => Color::Light, + (3 | -3, _) | (_, 3 | -3) => Color::Dark, + (2 | -2, _) | (_, 2 | -2) => Color::Light, _ => Color::Dark, }, ); @@ -325,7 +326,7 @@ impl Canvas { x + i, y + j, match (i, j) { - (2, _) | (_, 2) | (-2, _) | (_, -2) | (0, 0) => Color::Dark, + (2 | -2, _) | (_, 2 | -2) | (0, 0) => Color::Dark, _ => Color::Light, }, ); @@ -693,7 +694,7 @@ mod draw_version_info_tests { #[test] fn test_draw_number() { 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!( &*c.to_debug_str(), "\n\ @@ -992,7 +993,7 @@ pub fn is_functional(version: Version, width: i16, x: i16, y: i16) -> bool { true } else if a == 1 { false - } else if 2 <= a && a <= 6 { + } else if (2..=6).contains(&a) { (width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2 } else { 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 }; 'outside: for j in (bits_end..=7).rev() { 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); if *r == Module::Empty { *r = Module::Unmasked(color); @@ -1378,10 +1379,8 @@ impl Canvas { /// Draws the encoded data and error correction codes to the empty modules. pub fn draw_data(&mut self, data: &[u8], ec: &[u8]) { - let is_half_codeword_at_end = match (self.version, self.ec_level) { - (Version::Micro(1), EcLevel::L) | (Version::Micro(3), EcLevel::M) => true, - _ => false, - }; + let is_half_codeword_at_end = + matches!((self.version, self.ec_level), (Version::Micro(1), EcLevel::L) | (Version::Micro(3), EcLevel::M)); let mut coords = DataModuleIter::new(self.version); self.draw_codewords(data, is_half_codeword_at_end, &mut coords); diff --git a/src/cast.rs b/src/cast.rs index aa21f46..9eb5c65 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -14,6 +14,7 @@ impl Truncate for u16 { } } +#[allow(clippy::wrong_self_convention)] pub trait As { fn as_u16(self) -> u16; fn as_i16(self) -> i16; diff --git a/src/ec.rs b/src/ec.rs index 857de08..06eeda7 100644 --- a/src/ec.rs +++ b/src/ec.rs @@ -179,8 +179,8 @@ pub fn max_allowed_errors(version: Version, ec_level: EcLevel) -> QrResult 3, - (Micro(_), L) | (Normal(2), L) | (Micro(2), M) | (Normal(1), M) => 2, + (Micro(2) | Normal(1), L) => 3, + (Micro(_) | Normal(2), L) | (Micro(2) | Normal(1), M) => 2, (Normal(1), _) | (Normal(3), L) => 1, _ => 0, }; diff --git a/src/lib.rs b/src/lib.rs index b748659..bef057f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -325,7 +325,7 @@ mod image_tests { fn test_annex_i_qr_as_image() { let code = QrCode::new(b"01234567").unwrap(); let image = code.render::>().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.into_raw(), expected.into_raw()); } @@ -339,7 +339,7 @@ mod image_tests { .dark_color(Rgb([128, 0, 0])) .light_color(Rgb([255, 255, 128])) .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.into_raw(), expected.into_raw()); } diff --git a/src/optimize.rs b/src/optimize.rs index 31d0029..eb7549b 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -341,7 +341,7 @@ mod optimize_tests { fn test_optimization_result(given: Vec, expected: Vec, version: Version) { let prev_len = total_encoded_len(&*given, version); - let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::>(); + let opt_segs = Optimizer::new(given.iter().copied(), version).collect::>(); let new_len = total_encoded_len(&*opt_segs, version); if given != opt_segs { assert!(prev_len > new_len, "{} > {}", prev_len, new_len); diff --git a/src/render/image.rs b/src/render/image.rs index d42776b..aeb8a5b 100644 --- a/src/render/image.rs +++ b/src/render/image.rs @@ -1,4 +1,4 @@ -#![cfg(feature="image")] +#![cfg(feature = "image")] use crate::render::{Canvas, Pixel}; use crate::types::Color; @@ -7,8 +7,12 @@ use image::{ImageBuffer, Luma, LumaA, Pixel as ImagePixel, Primitive, Rgb, Rgba} macro_rules! impl_pixel_for_image_pixel { ($p:ident<$s:ident>: $c:pat => $d:expr) => { - impl<$s: Primitive + 'static> Pixel for $p<$s> { - type Image = ImageBuffer>; + impl<$s> Pixel for $p<$s> + where + $s: Primitive + 'static, + Self: ImagePixel, + { + type Image = ImageBuffer::Subpixel>>; type Canvas = (Self, Self::Image); fn default_color(color: Color) -> Self { diff --git a/src/types.rs b/src/types.rs index 95a716c..6df7c56 100644 --- a/src/types.rs +++ b/src/types.rs @@ -275,7 +275,7 @@ impl Mode { /// pub fn max(self, other: Self) -> Self { match self.partial_cmp(&other) { - Some(Ordering::Less) | Some(Ordering::Equal) => other, + Some(Ordering::Less | Ordering::Equal) => other, Some(Ordering::Greater) => self, None => Mode::Byte, } @@ -286,15 +286,13 @@ impl PartialOrd for Mode { /// Defines a partial ordering between modes. If `a <= b`, then `b` contains /// a superset of all characters supported by `a`. fn partial_cmp(&self, other: &Self) -> Option { - match (*self, *other) { - (Mode::Numeric, Mode::Alphanumeric) - | (Mode::Numeric, Mode::Byte) - | (Mode::Alphanumeric, Mode::Byte) - | (Mode::Kanji, Mode::Byte) => Some(Ordering::Less), - (Mode::Alphanumeric, Mode::Numeric) - | (Mode::Byte, Mode::Numeric) - | (Mode::Byte, Mode::Alphanumeric) - | (Mode::Byte, Mode::Kanji) => Some(Ordering::Greater), + match (self, other) { + (Mode::Numeric, Mode::Alphanumeric) | (Mode::Numeric | Mode::Alphanumeric | Mode::Kanji, Mode::Byte) => { + Some(Ordering::Less) + } + (Mode::Alphanumeric, Mode::Numeric) | (Mode::Byte, Mode::Numeric | Mode::Alphanumeric | Mode::Kanji) => { + Some(Ordering::Greater) + } (a, b) if a == b => Some(Ordering::Equal), _ => None, } @@ -309,8 +307,7 @@ mod mode_tests { fn test_mode_order() { assert!(Numeric < Alphanumeric); assert!(Byte > Kanji); - assert!(!(Numeric < Kanji)); - assert!(!(Numeric >= Kanji)); + assert!(Numeric.partial_cmp(&Kanji).is_none()); } #[test]