Skip to content

Commit

Permalink
Merge pull request #70 from sorairolake/apply-more-clippy-lints
Browse files Browse the repository at this point in the history
chore(clippy): Apply more clippy lints
  • Loading branch information
kennytm authored Jul 13, 2024
2 parents 227b5c0 + 733e3f5 commit 0d1a336
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 216 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
rust-version = "1.67.1"
authors = ["kennytm <[email protected]>"]
keywords = ["qrcode"]
categories = ["encoding", "multimedia::images"]
repository = "https://github.com/kennytm/qrcode-rust"
readme = "README.md"
documentation = "http://docs.rs/qrcode"
Expand Down
2 changes: 1 addition & 1 deletion examples/encode_pic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ use qrcode::QrCode;
fn main() {
let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<pic::Color>().min_dimensions(1, 1).build();
println!("{}", image);
println!("{image}");
}
2 changes: 1 addition & 1 deletion examples/encode_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use qrcode::QrCode;
fn main() {
let code = QrCode::new(b"Hello").unwrap();
let string = code.render::<char>().quiet_zone(false).module_dimensions(2, 1).build();
println!("{}", string);
println!("{string}");
}
2 changes: 1 addition & 1 deletion examples/encode_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fn main() {
.dark_color(svg::Color("#800000"))
.light_color(svg::Color("#ffff80"))
.build();
println!("{}", image);
println!("{image}");
}
2 changes: 1 addition & 1 deletion examples/encode_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ use qrcode::QrCode;
fn main() {
let code = QrCode::new(b"Hello").unwrap();
let string = code.render::<unicode::Dense1x2>().quiet_zone(false).build();
println!("{}", string);
println!("{string}");
}
71 changes: 38 additions & 33 deletions src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Bits {

impl Bits {
/// Constructs a new, empty bits structure.
pub fn new(version: Version) -> Self {
pub const fn new(version: Version) -> Self {
Self { data: Vec::new(), bit_offset: 0, version }
}

Expand Down Expand Up @@ -110,7 +110,7 @@ impl Bits {
}

/// Version of the QR code.
pub fn version(&self) -> Version {
pub const fn version(&self) -> Version {
self.version
}
}
Expand Down Expand Up @@ -223,29 +223,30 @@ impl Bits {
/// the following binary data. After calling this method, one could call
/// `.push_byte_data()` or similar methods to insert the actual data, e.g.
///
/// #![allow(unused_must_use)]
///
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
/// ```
/// #![allow(unused_must_use)]
///
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_eci_designator(9); // 9 = ISO-8859-7 (Greek).
/// bits.push_byte_data(b"\xa1\xa2\xa3\xa4\xa5"); // ΑΒΓΔΕ
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
///
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_eci_designator(9); // 9 = ISO-8859-7 (Greek).
/// bits.push_byte_data(b"\xa1\xa2\xa3\xa4\xa5"); // ΑΒΓΔΕ
/// ```
///
/// The full list of ECI designator values can be found from
/// <http://strokescribe.com/en/ECI.html>. Some example values are:
///
/// ECI # | Character set
/// ------|-------------------------------------
/// 3 | ISO-8859-1 (Western European)
/// 20 | Shift JIS (Japanese)
/// 23 | Windows 1252 (Latin 1) (Western European)
/// 25 | UTF-16 Big Endian
/// 26 | UTF-8
/// 28 | Big 5 (Traditional Chinese)
/// 29 | GB-18030 (Simplified Chinese)
/// 30 | EUC-KR (Korean)
/// | ECI # | Character set |
/// | ----- | ----------------------------------------- |
/// | 3 | ISO-8859-1 (Western European) |
/// | 20 | Shift JIS (Japanese) |
/// | 23 | Windows 1252 (Latin 1) (Western European) |
/// | 25 | UTF-16 Big Endian |
/// | 26 | UTF-8 |
/// | 28 | Big 5 (Traditional Chinese) |
/// | 29 | GB-18030 (Simplified Chinese) |
/// | 30 | EUC-KR (Korean) |
///
/// # Errors
///
Expand Down Expand Up @@ -601,15 +602,17 @@ impl Bits {
/// Encodes an indicator that the following data are formatted according to
/// the UCC/EAN Application Identifiers standard.
///
/// #![allow(unused_must_use)]
/// ```
/// #![allow(unused_must_use)]
///
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
///
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_fnc1_first_position();
/// bits.push_numeric_data(b"01049123451234591597033130128");
/// bits.push_alphanumeric_data(b"%10ABC123");
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_fnc1_first_position();
/// bits.push_numeric_data(b"01049123451234591597033130128");
/// bits.push_alphanumeric_data(b"%10ABC123");
/// ```
///
/// In QR code, the character `%` is used as the data field separator (0x1D).
///
Expand All @@ -625,15 +628,17 @@ impl Bits {
/// with specific industry or application specifications previously agreed
/// with AIM International.
///
/// #![allow(unused_must_use)]
/// ```
/// #![allow(unused_must_use)]
///
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
/// use qrcode::bits::Bits;
/// use qrcode::types::Version;
///
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_fnc1_second_position(37);
/// bits.push_alphanumeric_data(b"AA1234BBB112");
/// bits.push_byte_data(b"text text text text\r");
/// let mut bits = Bits::new(Version::Normal(1));
/// bits.push_fnc1_second_position(37);
/// bits.push_alphanumeric_data(b"AA1234BBB112");
/// bits.push_byte_data(b"text text text text\r");
/// ```
///
/// If the application indicator is a single Latin alphabet (a–z / A–Z),
/// please pass in its ASCII value + 100:
Expand Down
66 changes: 34 additions & 32 deletions src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! The `canvas` module puts raw bits into the QR code canvas.
//!
//! use qrcode::types::{Version, EcLevel};
//! use qrcode::canvas::{Canvas, MaskPattern};
//! ```
//! use qrcode::canvas::{Canvas, MaskPattern};
//! use qrcode::types::{EcLevel, Version};
//!
//! let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
//! c.draw_all_functional_patterns();
//! c.draw_data(b"data_here", b"ec_code_here");
//! c.apply_mask(MaskPattern::Checkerboard);
//! let bools = c.to_bools();
//! let mut c = Canvas::new(Version::Normal(1), EcLevel::L);
//! c.draw_all_functional_patterns();
//! c.draw_data(b"data_here", b"ec_code_here");
//! c.apply_mask(MaskPattern::Checkerboard);
//! let bools = c.to_bools();
//! ```
use std::cmp::max;
use std::{cmp::max, iter};

use crate::cast::As;
use crate::types::{Color, EcLevel, Version};
Expand All @@ -34,7 +36,7 @@ pub enum Module {
impl From<Module> for Color {
fn from(module: Module) -> Self {
match module {
Module::Empty => Color::Light,
Module::Empty => Self::Light,
Module::Masked(c) | Module::Unmasked(c) => c,
}
}
Expand All @@ -48,22 +50,23 @@ impl Module {

/// Apply a mask to the unmasked modules.
///
/// use qrcode::canvas::Module;
/// use qrcode::types::Color;
///
/// assert_eq!(Module::Unmasked(Color::Light).mask(true), Module::Masked(Color::Dark));
/// assert_eq!(Module::Unmasked(Color::Dark).mask(true), Module::Masked(Color::Light));
/// assert_eq!(Module::Unmasked(Color::Light).mask(false), Module::Masked(Color::Light));
/// assert_eq!(Module::Masked(Color::Dark).mask(true), Module::Masked(Color::Dark));
/// assert_eq!(Module::Masked(Color::Dark).mask(false), Module::Masked(Color::Dark));
/// ```
/// use qrcode::canvas::Module;
/// use qrcode::types::Color;
///
/// assert_eq!(Module::Unmasked(Color::Light).mask(true), Module::Masked(Color::Dark));
/// assert_eq!(Module::Unmasked(Color::Dark).mask(true), Module::Masked(Color::Light));
/// assert_eq!(Module::Unmasked(Color::Light).mask(false), Module::Masked(Color::Light));
/// 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),
(Module::Empty, false) => Module::Masked(Color::Light),
(Module::Unmasked(c), true) => Module::Masked(!c),
(Module::Unmasked(c), false) | (Module::Masked(c), _) => Module::Masked(c),
(Self::Empty, true) => Self::Masked(Color::Dark),
(Self::Empty, false) => Self::Masked(Color::Light),
(Self::Unmasked(c), true) => Self::Masked(!c),
(Self::Unmasked(c), false) | (Self::Masked(c), _) => Self::Masked(c),
}
}
}
Expand Down Expand Up @@ -542,7 +545,6 @@ impl Canvas {
/// On even coordinates, `color_even` will be plotted; on odd coordinates,
/// `color_odd` will be plotted instead. Thus the timing pattern can be
/// drawn using this method.
///
fn draw_line(&mut self, x1: i16, y1: i16, x2: i16, y2: i16, color_even: Color, color_odd: Color) {
debug_assert!(x1 == x2 || y1 == y2);

Expand Down Expand Up @@ -1136,7 +1138,7 @@ struct DataModuleIter {
}

impl DataModuleIter {
fn new(version: Version) -> Self {
const fn new(version: Version) -> Self {
let width = version.width();
Self {
x: width - 1,
Expand Down Expand Up @@ -1487,28 +1489,28 @@ pub enum MaskPattern {
}

mod mask_functions {
pub fn checkerboard(x: i16, y: i16) -> bool {
pub const fn checkerboard(x: i16, y: i16) -> bool {
(x + y) % 2 == 0
}
pub fn horizontal_lines(_: i16, y: i16) -> bool {
pub const fn horizontal_lines(_: i16, y: i16) -> bool {
y % 2 == 0
}
pub fn vertical_lines(x: i16, _: i16) -> bool {
pub const fn vertical_lines(x: i16, _: i16) -> bool {
x % 3 == 0
}
pub fn diagonal_lines(x: i16, y: i16) -> bool {
pub const fn diagonal_lines(x: i16, y: i16) -> bool {
(x + y) % 3 == 0
}
pub fn large_checkerboard(x: i16, y: i16) -> bool {
pub const fn large_checkerboard(x: i16, y: i16) -> bool {
((y / 2) + (x / 3)) % 2 == 0
}
pub fn fields(x: i16, y: i16) -> bool {
pub const fn fields(x: i16, y: i16) -> bool {
(x * y) % 2 + (x * y) % 3 == 0
}
pub fn diamonds(x: i16, y: i16) -> bool {
pub const fn diamonds(x: i16, y: i16) -> bool {
((x * y) % 2 + (x * y) % 3) % 2 == 0
}
pub fn meadow(x: i16, y: i16) -> bool {
pub const fn meadow(x: i16, y: i16) -> bool {
((x + y) % 2 + (x * y) % 3) % 2 == 0
}
}
Expand Down Expand Up @@ -1700,7 +1702,7 @@ impl Canvas {
for i in 0..self.width {
let map_fn = |j| if is_horizontal { self.get(j, i) } else { self.get(i, j) };

let colors = (0..self.width).map(map_fn).chain(Some(Module::Empty).into_iter());
let colors = (0..self.width).map(map_fn).chain(iter::once(Module::Empty));
let mut last_color = Module::Empty;
let mut consecutive_len = 1_u16;

Expand Down
Loading

0 comments on commit 0d1a336

Please sign in to comment.