From 12081e41101e252f7dbc1c778843433f8d4dd494 Mon Sep 17 00:00:00 2001 From: houseme Date: Mon, 20 May 2024 16:11:26 +0800 Subject: [PATCH] feat: Upgrade deps and improve code --- .github/workflows/Build.yml | 76 +++++++++++++++++++++------------ .github/workflows/docs.yml | 2 +- Cargo.toml | 5 ++- src/bits.rs | 2 +- src/canvas.rs | 84 ++++++++++++++++--------------------- src/cast.rs | 22 +++------- src/render/image.rs | 8 ++-- src/types.rs | 26 +++++------- 8 files changed, 110 insertions(+), 115 deletions(-) diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index e8ddd61..6e5c2d1 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -10,8 +10,26 @@ on: branches: [ "main" ] jobs: + check: + name: Check + runs-on: ubuntu-latest + timeout-minutes: 5 + strategy: + fail-fast: true + matrix: + rustc: ["1.67.1", stable] # MSVR and current stable rustc + steps: + - uses: actions/checkout@v4 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.rustc }} + override: true + - uses: actions-rs/cargo@v1 + with: + command: check rust_fmt_check: - name: RustFmt check + name: RustFmt runs-on: ubuntu-latest steps: - name: Checkout code @@ -26,35 +44,39 @@ jobs: - name: Install rust uses: actions-rs/toolchain@v1 with: - toolchain: nightly + profile: minimal + toolchain: stable + override: true components: rustfmt - name: Run cargo fmt uses: actions-rs/cargo@v1 with: command: fmt - args: -- --check -# clippy_check: -# name: Clippy check -# runs-on: ubuntu-latest -# steps: -# - name: Checkout code -# uses: actions/checkout@v4 -# - uses: actions/cache@v4 -# with: -# path: | -# ~/.cargo/registry -# ~/.cargo/git -# target -# key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} -# - name: Install clippy -# run: rustup toolchain install nightly --component clippy -# - name: Add clippy to path -# run: rustup component add clippy -# - name: Run clippy check -# uses: actions-rs/clippy-check@v1 -# with: -# token: ${{ secrets.GITHUB_TOKEN }} -# args: --all-features + args: --all -- --check + clippy_check: + name: Clippy + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: clippy + - name: Run clippy check + uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + #args: --all-features test: name: Run tests runs-on: ubuntu-latest @@ -71,7 +93,9 @@ jobs: - name: Install rust uses: actions-rs/toolchain@v1 with: - toolchain: nightly + profile: minimal + toolchain: stable + override: true - name: Run cargo test uses: actions-rs/cargo@v1 with: diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 7c420b8..c08ad52 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -1,7 +1,7 @@ on: push: branches: - - master + - main name: Docs diff --git a/Cargo.toml b/Cargo.toml index 688a338..4ae3c37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ categories = ["encoding", "multimedia::images", "multimedia::encoding"] readme = "README.md" # Crate build related exclude = ["tests/*", ".gitignore"] +rust-version = "1.67.1" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [profile.release] @@ -19,10 +20,10 @@ opt-level = 3 debug = false [dependencies] -image = { version = "0.24", default-features = false, optional = true } +image = { version = "0.25", default-features = false, optional = true } [dev-dependencies] -image = "0.24" +image = "0.25" [features] default = ["image", "svg"] diff --git a/src/bits.rs b/src/bits.rs index 96fb76a..1a795a1 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -64,7 +64,7 @@ impl Bits { /// that the number does not overflow the bits. /// /// Returns `Err(QrError::DataTooLong)` on overflow. - fn push_number_checked(&mut self, n: usize, number: usize) -> QrResult<()> { + pub fn push_number_checked(&mut self, n: usize, number: usize) -> QrResult<()> { if n > 16 || number >= (1 << n) { Err(QrError::DataTooLong) } else { diff --git a/src/canvas.rs b/src/canvas.rs index 922d089..6ec748a 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -344,8 +344,8 @@ impl Canvas { Version::Normal(2..=6) => self.draw_alignment_pattern_at(-7, -7), Version::Normal(a) => { let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()]; - for x in positions.iter() { - for y in positions.iter() { + for x in positions { + for y in positions { self.draw_alignment_pattern_at(*x, *y); } } @@ -987,28 +987,27 @@ pub fn is_functional(version: Version, width: i16, x: i16, y: i16) -> bool { Version::Normal(a) => { let non_alignment_test = x == 6 || y == 6 || // Timing patterns (x < 9 && y < 9) || // Top-left finder pattern - (x < 9 && y >= width-8) || // Bottom-left finder pattern - (x >= width-8 && y < 9); // Top-right finder pattern - if non_alignment_test { - true - } else if a == 1 { - false - } 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()]; - let last = positions.len() - 1; - for (i, align_x) in positions.iter().enumerate() { - for (j, align_y) in positions.iter().enumerate() { - if i == 0 && (j == 0 || j == last) || (i == last && j == 0) { - continue; - } - if (*align_x - x).abs() <= 2 && (*align_y - y).abs() <= 2 { - return true; + (x < 9 && y >= width - 8) || // Bottom-left finder pattern + (x >= width - 8 && y < 9); // Top-right finder pattern + match a { + _ if non_alignment_test => true, + 1 => false, + 2..=6 => (width - 7 - x).abs() <= 2 && (width - 7 - y).abs() <= 2, + _ => { + let positions = ALIGNMENT_PATTERN_POSITIONS[(a - 7).as_usize()]; + let last = positions.len() - 1; + for (i, align_x) in positions.iter().enumerate() { + for (j, align_y) in positions.iter().enumerate() { + if i == 0 && (j == 0 || j == last) || (i == last && j == 0) { + continue; + } + if (*align_x - x).abs() <= 2 && (*align_y - y).abs() <= 2 { + return true; + } } } + false } - false } } } @@ -1201,7 +1200,6 @@ mod data_iter_tests { (20, 8), (19, 8), (20, 7), (19, 7), (20, 6), (19, 6), (20, 5), (19, 5), (20, 4), (19, 4), (20, 3), (19, 3), (20, 2), (19, 2), (20, 1), (19, 1), (20, 0), (19, 0), - (18, 0), (17, 0), (18, 1), (17, 1), (18, 2), (17, 2), (18, 3), (17, 3), (18, 4), (17, 4), (18, 5), (17, 5), (18, 6), (17, 6), (18, 7), (17, 7), (18, 8), (17, 8), @@ -1209,7 +1207,6 @@ mod data_iter_tests { (18, 12), (17, 12), (18, 13), (17, 13), (18, 14), (17, 14), (18, 15), (17, 15), (18, 16), (17, 16), (18, 17), (17, 17), (18, 18), (17, 18), (18, 19), (17, 19), (18, 20), (17, 20), - (16, 20), (15, 20), (16, 19), (15, 19), (16, 18), (15, 18), (16, 17), (15, 17), (16, 16), (15, 16), (16, 15), (15, 15), (16, 14), (15, 14), (16, 13), (15, 13), (16, 12), (15, 12), @@ -1217,7 +1214,6 @@ mod data_iter_tests { (16, 8), (15, 8), (16, 7), (15, 7), (16, 6), (15, 6), (16, 5), (15, 5), (16, 4), (15, 4), (16, 3), (15, 3), (16, 2), (15, 2), (16, 1), (15, 1), (16, 0), (15, 0), - (14, 0), (13, 0), (14, 1), (13, 1), (14, 2), (13, 2), (14, 3), (13, 3), (14, 4), (13, 4), (14, 5), (13, 5), (14, 6), (13, 6), (14, 7), (13, 7), (14, 8), (13, 8), @@ -1225,7 +1221,6 @@ mod data_iter_tests { (14, 12), (13, 12), (14, 13), (13, 13), (14, 14), (13, 14), (14, 15), (13, 15), (14, 16), (13, 16), (14, 17), (13, 17), (14, 18), (13, 18), (14, 19), (13, 19), (14, 20), (13, 20), - (12, 20), (11, 20), (12, 19), (11, 19), (12, 18), (11, 18), (12, 17), (11, 17), (12, 16), (11, 16), (12, 15), (11, 15), (12, 14), (11, 14), (12, 13), (11, 13), (12, 12), (11, 12), @@ -1233,7 +1228,6 @@ mod data_iter_tests { (12, 8), (11, 8), (12, 7), (11, 7), (12, 6), (11, 6), (12, 5), (11, 5), (12, 4), (11, 4), (12, 3), (11, 3), (12, 2), (11, 2), (12, 1), (11, 1), (12, 0), (11, 0), - (10, 0), (9, 0), (10, 1), (9, 1), (10, 2), (9, 2), (10, 3), (9, 3), (10, 4), (9, 4), (10, 5), (9, 5), (10, 6), (9, 6), (10, 7), (9, 7), (10, 8), (9, 8), @@ -1241,7 +1235,6 @@ mod data_iter_tests { (10, 12), (9, 12), (10, 13), (9, 13), (10, 14), (9, 14), (10, 15), (9, 15), (10, 16), (9, 16), (10, 17), (9, 17), (10, 18), (9, 18), (10, 19), (9, 19), (10, 20), (9, 20), - (8, 20), (7, 20), (8, 19), (7, 19), (8, 18), (7, 18), (8, 17), (7, 17), (8, 16), (7, 16), (8, 15), (7, 15), (8, 14), (7, 14), (8, 13), (7, 13), (8, 12), (7, 12), @@ -1249,7 +1242,6 @@ mod data_iter_tests { (8, 8), (7, 8), (8, 7), (7, 7), (8, 6), (7, 6), (8, 5), (7, 5), (8, 4), (7, 4), (8, 3), (7, 3), (8, 2), (7, 2), (8, 1), (7, 1), (8, 0), (7, 0), - (5, 0), (4, 0), (5, 1), (4, 1), (5, 2), (4, 2), (5, 3), (4, 3), (5, 4), (4, 4), (5, 5), (4, 5), (5, 6), (4, 6), (5, 7), (4, 7), (5, 8), (4, 8), @@ -1257,7 +1249,6 @@ mod data_iter_tests { (5, 12), (4, 12), (5, 13), (4, 13), (5, 14), (4, 14), (5, 15), (4, 15), (5, 16), (4, 16), (5, 17), (4, 17), (5, 18), (4, 18), (5, 19), (4, 19), (5, 20), (4, 20), - (3, 20), (2, 20), (3, 19), (2, 19), (3, 18), (2, 18), (3, 17), (2, 17), (3, 16), (2, 16), (3, 15), (2, 15), (3, 14), (2, 14), (3, 13), (2, 13), (3, 12), (2, 12), @@ -1265,7 +1256,6 @@ mod data_iter_tests { (3, 8), (2, 8), (3, 7), (2, 7), (3, 6), (2, 6), (3, 5), (2, 5), (3, 4), (2, 4), (3, 3), (2, 3), (3, 2), (2, 2), (3, 1), (2, 1), (3, 0), (2, 0), - (1, 0), (0, 0), (1, 1), (0, 1), (1, 2), (0, 2), (1, 3), (0, 3), (1, 4), (0, 4), (1, 5), (0, 5), (1, 6), (0, 6), (1, 7), (0, 7), (1, 8), (0, 8), @@ -1284,22 +1274,18 @@ mod data_iter_tests { (10, 7), (9, 7), (10, 6), (9, 6), (10, 5), (9, 5), (10, 4), (9, 4), (10, 3), (9, 3), (10, 2), (9, 2), (10, 1), (9, 1), (10, 0), (9, 0), - (8, 0), (7, 0), (8, 1), (7, 1), (8, 2), (7, 2), (8, 3), (7, 3), (8, 4), (7, 4), (8, 5), (7, 5), (8, 6), (7, 6), (8, 7), (7, 7), (8, 8), (7, 8), (8, 9), (7, 9), (8, 10), (7, 10), - (6, 10), (5, 10), (6, 9), (5, 9), (6, 8), (5, 8), (6, 7), (5, 7), (6, 6), (5, 6), (6, 5), (5, 5), (6, 4), (5, 4), (6, 3), (5, 3), (6, 2), (5, 2), (6, 1), (5, 1), (6, 0), (5, 0), - (4, 0), (3, 0), (4, 1), (3, 1), (4, 2), (3, 2), (4, 3), (3, 3), (4, 4), (3, 4), (4, 5), (3, 5), (4, 6), (3, 6), (4, 7), (3, 7), (4, 8), (3, 8), (4, 9), (3, 9), (4, 10), (3, 10), - (2, 10), (1, 10), (2, 9), (1, 9), (2, 8), (1, 8), (2, 7), (1, 7), (2, 6), (1, 6), (2, 5), (1, 5), (2, 4), (1, 4), (2, 3), (1, 3), (2, 2), (1, 2), @@ -1316,31 +1302,26 @@ mod data_iter_tests { (12, 6), (11, 6), (12, 5), (11, 5), (12, 4), (11, 4), (12, 3), (11, 3), (12, 2), (11, 2), (12, 1), (11, 1), (12, 0), (11, 0), - (10, 0), (9, 0), (10, 1), (9, 1), (10, 2), (9, 2), (10, 3), (9, 3), (10, 4), (9, 4), (10, 5), (9, 5), (10, 6), (9, 6), (10, 7), (9, 7), (10, 8), (9, 8), (10, 9), (9, 9), (10, 10), (9, 10), (10, 11), (9, 11), (10, 12), (9, 12), - (8, 12), (7, 12), (8, 11), (7, 11), (8, 10), (7, 10), (8, 9), (7, 9), (8, 8), (7, 8), (8, 7), (7, 7), (8, 6), (7, 6), (8, 5), (7, 5), (8, 4), (7, 4), (8, 3), (7, 3), (8, 2), (7, 2), (8, 1), (7, 1), (8, 0), (7, 0), - (6, 0), (5, 0), (6, 1), (5, 1), (6, 2), (5, 2), (6, 3), (5, 3), (6, 4), (5, 4), (6, 5), (5, 5), (6, 6), (5, 6), (6, 7), (5, 7), (6, 8), (5, 8), (6, 9), (5, 9), (6, 10), (5, 10), (6, 11), (5, 11), (6, 12), (5, 12), - (4, 12), (3, 12), (4, 11), (3, 11), (4, 10), (3, 10), (4, 9), (3, 9), (4, 8), (3, 8), (4, 7), (3, 7), (4, 6), (3, 6), (4, 5), (3, 5), (4, 4), (3, 4), (4, 3), (3, 3), (4, 2), (3, 2), (4, 1), (3, 1), (4, 0), (3, 0), - (2, 0), (1, 0), (2, 1), (1, 1), (2, 2), (1, 2), (2, 3), (1, 3), (2, 4), (1, 4), (2, 5), (1, 5), (2, 6), (1, 6), (2, 7), (1, 7), (2, 8), (1, 8), @@ -1356,8 +1337,8 @@ mod data_iter_tests { impl Canvas { fn draw_codewords(&mut self, codewords: &[u8], is_half_codeword_at_end: bool, coords: &mut I) - where - I: Iterator, + where + I: Iterator, { let length = codewords.len(); let last_word = if is_half_codeword_at_end { length - 1 } else { length }; @@ -1492,24 +1473,31 @@ mod mask_functions { pub fn checkerboard(x: i16, y: i16) -> bool { (x + y) % 2 == 0 } + pub fn horizontal_lines(_: i16, y: i16) -> bool { y % 2 == 0 } + pub fn vertical_lines(x: i16, _: i16) -> bool { x % 3 == 0 } + pub fn diagonal_lines(x: i16, y: i16) -> bool { (x + y) % 3 == 0 } + pub fn large_checkerboard(x: i16, y: i16) -> bool { ((y / 2) + (x / 3)) % 2 == 0 } + pub fn fields(x: i16, y: i16) -> bool { (x * y) % 2 + (x * y) % 3 == 0 } + pub fn diamonds(x: i16, y: i16) -> bool { ((x * y) % 2 + (x * y) % 3) % 2 == 0 } + pub fn meadow(x: i16, y: i16) -> bool { ((x + y) % 2 + (x * y) % 3) % 2 == 0 } @@ -1977,13 +1965,13 @@ impl Canvas { Version::Normal(_) => ALL_PATTERNS_QR.iter(), Version::Micro(_) => ALL_PATTERNS_MICRO_QR.iter(), } - .map(|ptn| { - let mut c = self.clone(); - c.apply_mask(*ptn); - c - }) - .min_by_key(Self::compute_total_penalty_scores) - .expect("at least one pattern") + .map(|ptn| { + let mut c = self.clone(); + c.apply_mask(*ptn); + c + }) + .min_by_key(Self::compute_total_penalty_scores) + .expect("at least one pattern") } /// Convert the modules into a vector of booleans. diff --git a/src/cast.rs b/src/cast.rs index 11bce44..6715e1a 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -20,40 +20,28 @@ pub trait As { fn as_isize(self) -> isize; } -trait ExpectOrOverflow { - type Output; - fn expect_or_overflow(self, value: D, ty: &str) -> Self::Output; -} - -impl ExpectOrOverflow for Result { - type Output = T; - fn expect_or_overflow(self, value: D, ty: &str) -> Self::Output { - self.unwrap_or_else(|_err| panic!("{value} overflows {ty}")) - } -} - macro_rules! impl_as { ($ty:ty) => { #[cfg(debug_assertions)] impl As for $ty { fn as_u16(self) -> u16 { - self.try_into().expect_or_overflow(self, "u16") + u16::try_from(self).unwrap() } fn as_i16(self) -> i16 { - self.try_into().expect_or_overflow(self, "i16") + i16::try_from(self).unwrap() } fn as_u32(self) -> u32 { - self.try_into().expect_or_overflow(self, "u32") + u32::try_from(self).unwrap() } fn as_usize(self) -> usize { - self.try_into().expect_or_overflow(self, "usize") + usize::try_from(self).unwrap() } fn as_isize(self) -> isize { - self.try_into().expect_or_overflow(self, "usize") + isize::try_from(self).unwrap() } } diff --git a/src/render/image.rs b/src/render/image.rs index b24975f..2282920 100644 --- a/src/render/image.rs +++ b/src/render/image.rs @@ -3,16 +3,16 @@ use crate::render::{Canvas, Pixel}; use crate::types::Color; -use image::{ImageBuffer, Luma, LumaA, Pixel as ImagePixel, Primitive, Rgb, Rgba}; +use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba}; macro_rules! impl_pixel_for_image_pixel { ($p:ident<$s:ident>: $c:pat => $d:expr) => { impl<$s> Pixel for $p<$s> where $s: Primitive + 'static, - Self: ImagePixel, + $p<$s>: image::Pixel, { - type Image = ImageBuffer::Subpixel>>; + type Image = ImageBuffer>; type Canvas = (Self, Self::Image); fn default_color(color: Color) -> Self { @@ -29,7 +29,7 @@ impl_pixel_for_image_pixel! { LumaA: p => [p, S::max_value()] } impl_pixel_for_image_pixel! { Rgb: p => [p, p, p] } impl_pixel_for_image_pixel! { Rgba: p => [p, p, p, S::max_value()] } -impl Canvas for (P, ImageBuffer>) { +impl Canvas for (P, ImageBuffer>) { type Pixel = P; type Image = ImageBuffer>; diff --git a/src/types.rs b/src/types.rs index d5b451e..f9f9352 100644 --- a/src/types.rs +++ b/src/types.rs @@ -150,8 +150,8 @@ impl Version { /// If the entry compares equal to the default value of `T`, this method /// returns `Err(QrError::InvalidVersion)`. pub fn fetch(self, ec_level: EcLevel, table: &[[T; 4]]) -> QrResult - where - T: PartialEq + Default + Copy, + where + T: PartialEq + Default + Copy, { match self { Version::Normal(v @ 1..=40) => { @@ -170,18 +170,16 @@ impl Version { /// The number of bits needed to encode the mode indicator. pub fn mode_bits_count(self) -> usize { - match self { - Version::Micro(a) => (a - 1).as_usize(), - Version::Normal(_) => 4, + if let Version::Micro(a) = self { + (a - 1).as_usize() + } else { + 4 } } /// Checks whether is version refers to a Micro QR code. pub fn is_micro(self) -> bool { - match self { - Version::Normal(_) => false, - Version::Micro(_) => true, - } + matches!(self, Version::Micro(_)) } } @@ -275,8 +273,8 @@ impl Mode { #[must_use] pub fn max(self, other: Self) -> Self { match self.partial_cmp(&other) { - Some(Ordering::Less | Ordering::Equal) => other, Some(Ordering::Greater) => self, + Some(_) => other, None => Mode::Byte, } } @@ -287,13 +285,9 @@ impl PartialOrd for Mode { /// 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::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), + (Mode::Numeric, Mode::Alphanumeric) | (_, Mode::Byte) => Some(Ordering::Less), + (Mode::Alphanumeric, Mode::Numeric) | (Mode::Byte, _) => Some(Ordering::Greater), _ => None, } }