Skip to content

Commit

Permalink
feat: Upgrade deps and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed May 20, 2024
1 parent a1ec27c commit 12081e4
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 115 deletions.
76 changes: 50 additions & 26 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
branches:
- master
- main

name: Docs

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ 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]
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"]
Expand Down
2 changes: 1 addition & 1 deletion src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
84 changes: 36 additions & 48 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -1201,71 +1200,62 @@ 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),
(18, 9), (17, 9), (18, 10), (17, 10), (18, 11), (17, 11),
(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),
(16, 11), (15, 11), (16, 10), (15, 10), (16, 9), (15, 9),
(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),
(14, 9), (13, 9), (14, 10), (13, 10), (14, 11), (13, 11),
(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),
(12, 11), (11, 11), (12, 10), (11, 10), (12, 9), (11, 9),
(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),
(10, 9), (9, 9), (10, 10), (9, 10), (10, 11), (9, 11),
(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),
(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),

(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),
(5, 9), (4, 9), (5, 10), (4, 10), (5, 11), (4, 11),
(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),
(3, 11), (2, 11), (3, 10), (2, 10), (3, 9), (2, 9),
(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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -1356,8 +1337,8 @@ mod data_iter_tests {

impl Canvas {
fn draw_codewords<I>(&mut self, codewords: &[u8], is_half_codeword_at_end: bool, coords: &mut I)
where
I: Iterator<Item = (i16, i16)>,
where
I: Iterator<Item=(i16, i16)>,
{
let length = codewords.len();
let last_word = if is_half_codeword_at_end { length - 1 } else { length };
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 12081e4

Please sign in to comment.