From 3865c955f11acc76413f661b10655a08d379ab1c Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Sat, 13 Jul 2024 01:13:30 +0900 Subject: [PATCH 1/2] chore(clippy): Apply more clippy lints --- Cargo.toml | 1 + examples/encode_pic.rs | 2 +- examples/encode_string.rs | 2 +- examples/encode_svg.rs | 2 +- examples/encode_unicode.rs | 2 +- src/bits.rs | 183 +++++++++++++++++++++---------------- src/canvas.rs | 71 +++++++------- src/ec.rs | 12 +-- src/lib.rs | 87 ++++++++++-------- src/optimize.rs | 109 +++++++++++----------- src/render/image.rs | 5 +- src/render/mod.rs | 5 +- src/render/pic.rs | 4 +- src/render/unicode.rs | 16 ++-- src/types.rs | 113 ++++++++++++----------- 15 files changed, 335 insertions(+), 279 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6da3292..e2b33df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" rust-version = "1.67.1" authors = ["kennytm "] keywords = ["qrcode"] +categories = ["encoding", "multimedia::images"] repository = "https://github.com/kennytm/qrcode-rust" readme = "README.md" documentation = "http://docs.rs/qrcode" diff --git a/examples/encode_pic.rs b/examples/encode_pic.rs index 790b0d5..1e0f0ea 100644 --- a/examples/encode_pic.rs +++ b/examples/encode_pic.rs @@ -4,5 +4,5 @@ use qrcode::QrCode; fn main() { let code = QrCode::new(b"01234567").unwrap(); let image = code.render::().min_dimensions(1, 1).build(); - println!("{}", image); + println!("{image}"); } diff --git a/examples/encode_string.rs b/examples/encode_string.rs index 56007e2..c0213b4 100644 --- a/examples/encode_string.rs +++ b/examples/encode_string.rs @@ -3,5 +3,5 @@ use qrcode::QrCode; fn main() { let code = QrCode::new(b"Hello").unwrap(); let string = code.render::().quiet_zone(false).module_dimensions(2, 1).build(); - println!("{}", string); + println!("{string}"); } diff --git a/examples/encode_svg.rs b/examples/encode_svg.rs index 22e438c..90b0101 100644 --- a/examples/encode_svg.rs +++ b/examples/encode_svg.rs @@ -9,5 +9,5 @@ fn main() { .dark_color(svg::Color("#800000")) .light_color(svg::Color("#ffff80")) .build(); - println!("{}", image); + println!("{image}"); } diff --git a/examples/encode_unicode.rs b/examples/encode_unicode.rs index 795eb79..3918cc2 100644 --- a/examples/encode_unicode.rs +++ b/examples/encode_unicode.rs @@ -4,5 +4,5 @@ use qrcode::QrCode; fn main() { let code = QrCode::new(b"Hello").unwrap(); let string = code.render::().quiet_zone(false).build(); - println!("{}", string); + println!("{string}"); } diff --git a/src/bits.rs b/src/bits.rs index 3751358..b124a6c 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -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 } } @@ -110,7 +110,7 @@ impl Bits { } /// Version of the QR code. - pub fn version(&self) -> Version { + pub const fn version(&self) -> Version { self.version } } @@ -133,14 +133,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 + 0b0101_1010, // 90 + 0b1001_1010, // 154 + 0b1100_1011, // 203 + 0b0110_1101, // 109 + 0b0110_0100, // 100 + 0b0111_1001, // 121 + 0b0111_0001, // 113 + 0b1000_0000, // 128 ] ); } @@ -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 /// . 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 /// @@ -285,27 +286,27 @@ 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_1000, 0b0011_1000, 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.push_eci_designator(999_999), Ok(())); + assert_eq!(bits.into_bytes(), vec![0b0111_1100, 0b1111_0100, 0b0010_0011, 0b1111_0000]); } #[test] fn test_invalid_designator() { let mut bits = Bits::new(Version::Normal(1)); - assert_eq!(bits.push_eci_designator(1000000), Err(QrError::InvalidEciDesignator)); + assert_eq!(bits.push_eci_designator(1_000_000), Err(QrError::InvalidEciDesignator)); } #[test] @@ -357,7 +358,7 @@ mod numeric_tests { 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] + vec![0b0001_0000, 0b0010_0000, 0b0000_1100, 0b0101_0110, 0b0110_0001, 0b1000_0000] ); } @@ -369,14 +370,14 @@ mod numeric_tests { bits.into_bytes(), vec![ 0b0001_0000, - 0b010000_00, - 0b00001100, - 0b01010110, - 0b01_101010, + 0b0100_0000, + 0b0000_1100, + 0b0101_0110, + 0b0110_1010, 0b0110_1110, - 0b000101_00, - 0b11101010, - 0b0101__0000, + 0b0001_0100, + 0b1110_1010, + 0b0101_0000, ] ); } @@ -388,14 +389,14 @@ mod numeric_tests { assert_eq!( bits.into_bytes(), vec![ - 0b00_10000_0, - 0b00000110, - 0b0_0101011, - 0b001_10101, - 0b00110_111, - 0b0000101_0, - 0b01110101, - 0b00101__000, + 0b0010_0000, + 0b0000_0110, + 0b0010_1011, + 0b0011_0101, + 0b0011_0111, + 0b0000_1010, + 0b0111_0101, + 0b0010_1000, ] ); } @@ -465,7 +466,7 @@ mod alphanumeric_tests { 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] + vec![0b0010_0000, 0b0010_1001, 0b1100_1110, 0b1110_0111, 0b0010_0001, 0b0000_0000] ); } @@ -522,7 +523,7 @@ mod byte_tests { 0b1010_1011, 0b1100_1101, 0b1110_1111, - 0b0000__0000, + 0b0000_0000, ] ); } @@ -577,7 +578,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, 0b1100_1111, 0b1110_1010, 0b1010_1000]); } #[test] @@ -601,17 +602,20 @@ 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). + /// In QR code, the character `%` is used as the data field separator + /// (0x1D). /// /// # Errors /// @@ -625,15 +629,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: @@ -762,8 +768,19 @@ mod finish_tests { assert_eq!( bits.into_bytes(), vec![ - 0b00100000, 0b01011011, 0b00001011, 0b01111000, 0b11010001, 0b01110010, 0b11011100, 0b01001101, - 0b01000011, 0b01000000, 0b11101100, 0b00010001, 0b11101100, + 0b0010_0000, + 0b0101_1011, + 0b0000_1011, + 0b0111_1000, + 0b1101_0001, + 0b0111_0010, + 0b1101_1100, + 0b0100_1101, + 0b0100_0011, + 0b0100_0000, + 0b1110_1100, + 0b0001_0001, + 0b1110_1100, ] ); } @@ -780,7 +797,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![0b1011_1111, 0b0011_1110, 0b0011_0000]); } #[test] @@ -788,7 +805,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![0b1001_1111, 0b0011_1100, 0b1000_0000]); } #[test] @@ -796,7 +813,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![0b0111_1111, 0b0011_1000, 0b0000_0000]); } #[test] @@ -804,7 +821,7 @@ mod finish_tests { let mut bits = Bits::new(Version::Micro(1)); assert_eq!(bits.push_numeric_data(b""), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); - assert_eq!(bits.into_bytes(), vec![0b000_000_00, 0b11101100, 0]); + assert_eq!(bits.into_bytes(), vec![0b0000_0000, 0b1110_1100, 0]); } } @@ -866,8 +883,19 @@ mod encode_tests { assert_eq!( res, Ok(vec![ - 0b00100000, 0b01011011, 0b00001011, 0b01111000, 0b11010001, 0b01110010, 0b11011100, 0b01001101, - 0b01000011, 0b01000000, 0b11101100, 0b00010001, 0b11101100, + 0b0010_0000, + 0b0101_1011, + 0b0000_1011, + 0b0111_1000, + 0b1101_0001, + 0b0111_0010, + 0b1101_1100, + 0b0100_1101, + 0b0100_0011, + 0b0100_0000, + 0b1110_1100, + 0b0001_0001, + 0b1110_1100, ]) ); } @@ -875,7 +903,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![0b0001_1000, 0b1111_0111, 0b0010_0101, 0b0000_0000, 0b1110_1100])); } #[test] @@ -898,7 +926,8 @@ mod encode_tests { /// /// Returns `Err(QrError::DataTooLong)` if the data is too long to fit even the /// highest QR code version. -#[allow(clippy::missing_panics_doc)] // the panic caused by the expect() will never actually happen since the `version`s are known good constants. +// the panic caused by the expect() will never actually happen since the `version`s are known good constants. +#[allow(clippy::missing_panics_doc)] 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)] { @@ -949,7 +978,7 @@ mod encode_auto_tests { assert_eq!(find_min_version(20000, EcLevel::L), Version::Normal(37)); assert_eq!(find_min_version(640, EcLevel::L), Version::Normal(4)); assert_eq!(find_min_version(641, EcLevel::L), Version::Normal(5)); - assert_eq!(find_min_version(999999, EcLevel::H), Version::Normal(40)); + assert_eq!(find_min_version(999_999, EcLevel::H), Version::Normal(40)); } #[test] diff --git a/src/canvas.rs b/src/canvas.rs index 5874bc0..99462eb 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -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}; @@ -34,7 +36,7 @@ pub enum Module { impl From for Color { fn from(module: Module) -> Self { match module { - Module::Empty => Color::Light, + Module::Empty => Self::Light, Module::Masked(c) | Module::Unmasked(c) => c, } } @@ -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), } } } @@ -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); @@ -694,7 +696,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\ @@ -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, @@ -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 } } @@ -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; @@ -1792,7 +1794,8 @@ impl Canvas { if ratio >= 100 { ratio - 100 } else { 100 - ratio }.as_u16() } - /// Compute the penalty score for having too many light modules on the sides. + /// Compute the penalty score for having too many light modules on the + /// sides. /// /// This penalty score is exclusive to Micro QR code. /// diff --git a/src/ec.rs b/src/ec.rs index 25f170d..51eb852 100644 --- a/src/ec.rs +++ b/src/ec.rs @@ -13,9 +13,9 @@ use crate::types::{EcLevel, QrResult, Version}; /// 69 bytes. Longer blocks will result in task panic. /// /// This method treats the data as a polynomial of the form -/// (a\[0\] xm+n + a\[1\] xm+n-1 + … + a\[m\] xn) -/// in GF(28), and then computes the polynomial modulus with a -/// generator polynomial of degree N. +/// (a\[0\] xm+n + a\[1\] xm+n-1 + … + a\[m\] +/// xn) in GF(28), and then computes the polynomial +/// modulus with a generator polynomial of degree N. pub fn create_error_correction_code(data: &[u8], ec_code_size: usize) -> Vec { let data_len = data.len(); let log_den = GENERATOR_POLYNOMIALS[ec_code_size]; @@ -431,9 +431,9 @@ static EC_BYTES_PER_BLOCK: [[usize; 4]; 44] = [ /// This is a copy of ISO/IEC 18004:2006, §6.5.1, Table 9 (The value "k" of the /// 7th column, followed by the 6th column). /// -/// Every entry is a 4-tuple. Take `DATA_BYTES_PER_BLOCK[39][3] == (15, 20, 16, 61)` -/// as an example, this means in version 40 with correction level H, there are -/// 20 blocks with 15 bytes in size, and 61 blocks with 16 bytes in size. +/// Every entry is a 4-tuple. Take `DATA_BYTES_PER_BLOCK[39][3] == (15, 20, 16, +/// 61)` as an example, this means in version 40 with correction level H, there +/// are 20 blocks with 15 bytes in size, and 61 blocks with 16 bytes in size. static DATA_BYTES_PER_BLOCK: [[(usize, usize, usize, usize); 4]; 44] = [ // Normal versions. [(19, 1, 0, 0), (16, 1, 0, 0), (13, 1, 0, 0), (9, 1, 0, 0)], // 1 diff --git a/src/lib.rs b/src/lib.rs index e37107d..ba66d4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,12 @@ -//! QRCode encoder +//! QR code encoder //! //! This crate provides a QR code and Micro QR code encoder for binary data. //! -#![cfg_attr(feature = "image", doc = "```rust")] -#![cfg_attr(not(feature = "image"), doc = "```ignore")] -//! use qrcode::QrCode; +//! ``` +//! # #[cfg(feature = "image")] +//! # { //! use image::Luma; +//! use qrcode::QrCode; //! //! // Encode some data into bits. //! let code = QrCode::new(b"01234567").unwrap(); @@ -19,11 +20,9 @@ //! # } //! //! // You can also render it into a string. -//! let string = code.render() -//! .light_color(' ') -//! .dark_color('#') -//! .build(); +//! let string = code.render().light_color(' ').dark_color('#').build(); //! println!("{}", string); +//! # } //! ``` #![cfg_attr(feature = "bench", feature(test))] // Unstable libraries @@ -64,9 +63,11 @@ impl QrCode { /// This method uses the "medium" error correction level and automatically /// chooses the smallest QR code. /// - /// use qrcode::QrCode; + /// ``` + /// use qrcode::QrCode; /// - /// let code = QrCode::new(b"Some data").unwrap(); + /// let code = QrCode::new(b"Some data").unwrap(); + /// ``` /// /// # Errors /// @@ -81,9 +82,11 @@ impl QrCode { /// /// This method automatically chooses the smallest QR code. /// - /// use qrcode::{QrCode, EcLevel}; + /// ``` + /// use qrcode::{EcLevel, QrCode}; /// - /// let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap(); + /// let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap(); + /// ``` /// /// # Errors /// @@ -97,15 +100,19 @@ impl QrCode { /// Constructs a new QR code for the given version and error correction /// level. /// - /// use qrcode::{QrCode, Version, EcLevel}; + /// ``` + /// use qrcode::{EcLevel, QrCode, Version}; /// - /// let code = QrCode::with_version(b"Some data", Version::Normal(5), EcLevel::M).unwrap(); + /// let code = QrCode::with_version(b"Some data", Version::Normal(5), EcLevel::M).unwrap(); + /// ``` /// /// This method can also be used to generate Micro QR code. /// - /// use qrcode::{QrCode, Version, EcLevel}; + /// ``` + /// use qrcode::{EcLevel, QrCode, Version}; /// - /// let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap(); + /// let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap(); + /// ``` /// /// # Errors /// @@ -130,16 +137,18 @@ impl QrCode { /// /// See the `Bits` structure for detail. /// - /// #![allow(unused_must_use)] + /// ``` + /// #![allow(unused_must_use)] /// - /// use qrcode::{QrCode, Version, EcLevel}; - /// use qrcode::bits::Bits; + /// use qrcode::bits::Bits; + /// use qrcode::{EcLevel, QrCode, Version}; /// - /// let mut bits = Bits::new(Version::Normal(1)); - /// bits.push_eci_designator(9); - /// bits.push_byte_data(b"\xca\xfe\xe4\xe9\xea\xe1\xf2 QR"); - /// bits.push_terminator(EcLevel::L); - /// let qrcode = QrCode::with_bits(bits, EcLevel::L); + /// let mut bits = Bits::new(Version::Normal(1)); + /// bits.push_eci_designator(9); + /// bits.push_byte_data(b"\xca\xfe\xe4\xe9\xea\xe1\xf2 QR"); + /// bits.push_terminator(EcLevel::L); + /// let qrcode = QrCode::with_bits(bits, EcLevel::L); + /// ``` /// /// # Errors /// @@ -158,26 +167,27 @@ impl QrCode { } /// Gets the version of this QR code. - pub fn version(&self) -> Version { + pub const fn version(&self) -> Version { self.version } /// Gets the error correction level of this QR code. - pub fn error_correction_level(&self) -> EcLevel { + pub const fn error_correction_level(&self) -> EcLevel { self.ec_level } /// Gets the number of modules per side, i.e. the width of this QR code. /// /// The width here does not contain the quiet zone paddings. - pub fn width(&self) -> usize { + pub const fn width(&self) -> usize { self.width } /// Gets the maximum number of allowed erratic modules can be introduced /// before the data becomes corrupted. Note that errors should not be /// introduced to functional modules. - #[allow(clippy::missing_panics_doc)] // the version and ec_level should have been checked when calling `.with_version()`. + // the version and ec_level should have been checked when calling `.with_version()`. + #[allow(clippy::missing_panics_doc)] pub fn max_allowed_errors(&self) -> usize { ec::max_allowed_errors(self.version, self.ec_level).expect("invalid version or ec_level") } @@ -230,18 +240,21 @@ impl QrCode { /// /// # Examples /// - #[cfg_attr(feature = "image", doc = " ```rust")] - #[cfg_attr(not(feature = "image"), doc = " ```ignore")] + /// ``` + /// # #[cfg(feature = "image")] + /// # { /// # use qrcode::QrCode; /// # use image::Rgb; /// - /// let image = QrCode::new(b"hello").unwrap() - /// .render() - /// .dark_color(Rgb([0, 0, 128])) - /// .light_color(Rgb([224, 224, 224])) // adjust colors - /// .quiet_zone(false) // disable quiet zone (white border) - /// .min_dimensions(300, 300) // sets minimum image size - /// .build(); + /// let image = QrCode::new(b"hello") + /// .unwrap() + /// .render() + /// .dark_color(Rgb([0, 0, 128])) + /// .light_color(Rgb([224, 224, 224])) // adjust colors + /// .quiet_zone(false) // disable quiet zone (white border) + /// .min_dimensions(300, 300) // sets minimum image size + /// .build(); + /// # } /// ``` /// /// Note: the `image` crate itself also provides method to rotate the image, diff --git a/src/optimize.rs b/src/optimize.rs index 31d0029..8393702 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -49,7 +49,6 @@ impl Segment { /// ``` /// /// But the type is too hard to write, thus the new type. -/// struct EcsIter { base: I, index: usize, @@ -90,14 +89,20 @@ impl<'a> Parser<'a> { /// Creates a new iterator which parse the data into segments that only /// contains their exclusive subsets. No optimization is done at this point. /// - /// use qrcode::optimize::{Parser, Segment}; - /// use qrcode::types::Mode::{Alphanumeric, Numeric, Byte}; - /// - /// let parse_res = Parser::new(b"ABC123abcd").collect::>(); - /// assert_eq!(parse_res, vec![Segment { mode: Alphanumeric, begin: 0, end: 3 }, - /// Segment { mode: Numeric, begin: 3, end: 6 }, - /// Segment { mode: Byte, begin: 6, end: 10 }]); + /// ``` + /// use qrcode::optimize::{Parser, Segment}; + /// use qrcode::types::Mode::{Alphanumeric, Byte, Numeric}; /// + /// let parse_res = Parser::new(b"ABC123abcd").collect::>(); + /// assert_eq!( + /// parse_res, + /// &[ + /// Segment { mode: Alphanumeric, begin: 0, end: 3 }, + /// Segment { mode: Numeric, begin: 3, end: 6 }, + /// Segment { mode: Byte, begin: 6, end: 10 } + /// ] + /// ); + /// ``` pub fn new(data: &[u8]) -> Parser { Parser { ecs_iter: EcsIter { base: data.iter(), index: 0, ended: false }, @@ -165,7 +170,7 @@ mod parse_tests { let segs = parse(b"01049123451234591597033130128%10ABC123"); assert_eq!( segs, - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 29 }, Segment { mode: Mode::Alphanumeric, begin: 29, end: 30 }, Segment { mode: Mode::Numeric, begin: 30, end: 32 }, @@ -180,7 +185,7 @@ mod parse_tests { let segs = parse(b"\x82\xa0\x81\x41\x41\xb1\x81\xf0"); // "あ、AアÅ" assert_eq!( segs, - vec![ + &[ Segment { mode: Mode::Kanji, begin: 0, end: 4 }, Segment { mode: Mode::Alphanumeric, begin: 4, end: 5 }, Segment { mode: Mode::Byte, begin: 5, end: 6 }, @@ -195,7 +200,7 @@ mod parse_tests { let segs = parse(b"\xe3\x81\x82\xe3\x80\x81A\xef\xbd\xb1\xe2\x84\xab"); assert_eq!( segs, - vec![ + &[ Segment { mode: Mode::Kanji, begin: 0, end: 4 }, Segment { mode: Mode::Byte, begin: 4, end: 5 }, Segment { mode: Mode::Kanji, begin: 5, end: 7 }, @@ -211,7 +216,7 @@ mod parse_tests { let segs = parse(b"\x81\x30"); assert_eq!( segs, - vec![Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Numeric, begin: 1, end: 2 },] + &[Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Numeric, begin: 1, end: 2 },] ); } @@ -222,7 +227,7 @@ mod parse_tests { let segs = parse(b"\xeb\xc0"); assert_eq!( segs, - vec![Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Byte, begin: 1, end: 2 },] + &[Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Byte, begin: 1, end: 2 },] ); } @@ -231,7 +236,7 @@ mod parse_tests { let segs = parse(b"\x81\x7f"); assert_eq!( segs, - vec![Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Byte, begin: 1, end: 2 },] + &[Segment { mode: Mode::Byte, begin: 0, end: 1 }, Segment { mode: Mode::Byte, begin: 1, end: 2 },] ); } @@ -240,7 +245,7 @@ mod parse_tests { let segs = parse(b"\x81\x40\x81"); assert_eq!( segs, - vec![Segment { mode: Mode::Kanji, begin: 0, end: 2 }, Segment { mode: Mode::Byte, begin: 2, end: 3 },] + &[Segment { mode: Mode::Kanji, begin: 0, end: 2 }, Segment { mode: Mode::Byte, begin: 2, end: 3 },] ); } } @@ -263,7 +268,6 @@ impl> Optimizer { /// Currently this method uses a greedy algorithm by combining segments from /// left to right until the new segment is longer than before. This method /// does *not* use Annex J from the ISO standard. - /// pub fn new(mut segments: I, version: Version) -> Self { match segments.next() { None => Self { @@ -285,7 +289,7 @@ impl> Optimizer { } impl<'a> Parser<'a> { - pub fn optimize(self, version: Version) -> Optimizer> { + pub fn optimize(self, version: Version) -> Optimizer { Optimizer::new(self, version) } } @@ -339,18 +343,18 @@ mod optimize_tests { use crate::optimize::{total_encoded_len, Optimizer, Segment}; use crate::types::{Mode, Version}; - 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 new_len = total_encoded_len(&*opt_segs, version); + fn test_optimization_result(given: &[Segment], expected: &[Segment], version: Version) { + let prev_len = total_encoded_len(given, version); + 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); + assert!(prev_len > new_len, "{prev_len} > {new_len}"); } assert!( opt_segs == expected, "Optimization gave something better: {} < {} ({:?})", new_len, - total_encoded_len(&*expected, version), + total_encoded_len(expected, version), opt_segs ); } @@ -358,15 +362,12 @@ mod optimize_tests { #[test] fn test_example_1() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Alphanumeric, begin: 0, end: 3 }, Segment { mode: Mode::Numeric, begin: 3, end: 6 }, Segment { mode: Mode::Byte, begin: 6, end: 10 }, ], - vec![ - Segment { mode: Mode::Alphanumeric, begin: 0, end: 6 }, - Segment { mode: Mode::Byte, begin: 6, end: 10 }, - ], + &[Segment { mode: Mode::Alphanumeric, begin: 0, end: 6 }, Segment { mode: Mode::Byte, begin: 6, end: 10 }], Version::Normal(1), ); } @@ -374,14 +375,14 @@ mod optimize_tests { #[test] fn test_example_2() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 29 }, Segment { mode: Mode::Alphanumeric, begin: 29, end: 30 }, Segment { mode: Mode::Numeric, begin: 30, end: 32 }, Segment { mode: Mode::Alphanumeric, begin: 32, end: 35 }, Segment { mode: Mode::Numeric, begin: 35, end: 38 }, ], - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 29 }, Segment { mode: Mode::Alphanumeric, begin: 29, end: 38 }, ], @@ -392,13 +393,13 @@ mod optimize_tests { #[test] fn test_example_3() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Kanji, begin: 0, end: 4 }, Segment { mode: Mode::Alphanumeric, begin: 4, end: 5 }, Segment { mode: Mode::Byte, begin: 5, end: 6 }, Segment { mode: Mode::Kanji, begin: 6, end: 8 }, ], - vec![Segment { mode: Mode::Byte, begin: 0, end: 8 }], + &[Segment { mode: Mode::Byte, begin: 0, end: 8 }], Version::Normal(1), ); } @@ -406,8 +407,8 @@ mod optimize_tests { #[test] fn test_example_4() { test_optimization_result( - vec![Segment { mode: Mode::Kanji, begin: 0, end: 10 }, Segment { mode: Mode::Byte, begin: 10, end: 11 }], - vec![Segment { mode: Mode::Kanji, begin: 0, end: 10 }, Segment { mode: Mode::Byte, begin: 10, end: 11 }], + &[Segment { mode: Mode::Kanji, begin: 0, end: 10 }, Segment { mode: Mode::Byte, begin: 10, end: 11 }], + &[Segment { mode: Mode::Kanji, begin: 0, end: 10 }, Segment { mode: Mode::Byte, begin: 10, end: 11 }], Version::Normal(1), ); } @@ -415,11 +416,11 @@ mod optimize_tests { #[test] fn test_annex_j_guideline_1a() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 3 }, Segment { mode: Mode::Alphanumeric, begin: 3, end: 4 }, ], - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 3 }, Segment { mode: Mode::Alphanumeric, begin: 3, end: 4 }, ], @@ -430,11 +431,11 @@ mod optimize_tests { #[test] fn test_annex_j_guideline_1b() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 2 }, Segment { mode: Mode::Alphanumeric, begin: 2, end: 4 }, ], - vec![Segment { mode: Mode::Alphanumeric, begin: 0, end: 4 }], + &[Segment { mode: Mode::Alphanumeric, begin: 0, end: 4 }], Version::Micro(2), ); } @@ -442,11 +443,11 @@ mod optimize_tests { #[test] fn test_annex_j_guideline_1c() { test_optimization_result( - vec![ + &[ Segment { mode: Mode::Numeric, begin: 0, end: 3 }, Segment { mode: Mode::Alphanumeric, begin: 3, end: 4 }, ], - vec![Segment { mode: Mode::Alphanumeric, begin: 0, end: 4 }], + &[Segment { mode: Mode::Alphanumeric, begin: 0, end: 4 }], Version::Micro(3), ); } @@ -490,8 +491,8 @@ enum ExclCharSet { /// The end of string. End = 0, - /// All symbols supported by the Alphanumeric encoding, i.e. space, `$`, `%`, - /// `*`, `+`, `-`, `.`, `/` and `:`. + /// All symbols supported by the Alphanumeric encoding, i.e. space, `$`, + /// `%`, `*`, `+`, `-`, `.`, `/` and `:`. Symbol = 1, /// All numbers (0–9). @@ -513,8 +514,8 @@ enum ExclCharSet { KanjiHi3 = 6, /// The second byte of a Shift JIS 2-byte encoding, in the range 0x40–0xbf, - /// excluding letters (covered by `Alpha`), 0x81–0x9f (covered by `KanjiHi1`), - /// and the invalid byte 0x7f. + /// excluding letters (covered by `Alpha`), 0x81–0x9f (covered by + /// `KanjiHi1`), and the invalid byte 0x7f. KanjiLo1 = 7, /// The second byte of a Shift JIS 2-byte encoding, in the range 0xc0–0xfc, @@ -529,17 +530,17 @@ enum ExclCharSet { impl ExclCharSet { /// Determines which character set a byte is in. - fn from_u8(c: u8) -> Self { + const fn from_u8(c: u8) -> Self { match c { - 0x20 | 0x24 | 0x25 | 0x2a | 0x2b | 0x2d..=0x2f | 0x3a => ExclCharSet::Symbol, - 0x30..=0x39 => ExclCharSet::Numeric, - 0x41..=0x5a => ExclCharSet::Alpha, - 0x81..=0x9f => ExclCharSet::KanjiHi1, - 0xe0..=0xea => ExclCharSet::KanjiHi2, - 0xeb => ExclCharSet::KanjiHi3, - 0x40 | 0x5b..=0x7e | 0x80 | 0xa0..=0xbf => ExclCharSet::KanjiLo1, - 0xc0..=0xdf | 0xec..=0xfc => ExclCharSet::KanjiLo2, - _ => ExclCharSet::Byte, + 0x20 | 0x24 | 0x25 | 0x2a | 0x2b | 0x2d..=0x2f | 0x3a => Self::Symbol, + 0x30..=0x39 => Self::Numeric, + 0x41..=0x5a => Self::Alpha, + 0x81..=0x9f => Self::KanjiHi1, + 0xe0..=0xea => Self::KanjiHi2, + 0xeb => Self::KanjiHi3, + 0x40 | 0x5b..=0x7e | 0x80 | 0xa0..=0xbf => Self::KanjiLo1, + 0xc0..=0xdf | 0xec..=0xfc => Self::KanjiLo2, + _ => Self::Byte, } } } diff --git a/src/render/image.rs b/src/render/image.rs index e468ff0..518e1d2 100644 --- a/src/render/image.rs +++ b/src/render/image.rs @@ -5,8 +5,9 @@ use crate::types::Color; use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba}; -// need to keep using this macro to implement Pixel separately for each color model, -// otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷 +// need to keep using this macro to implement Pixel separately for each color +// model, otherwise we'll have conflicting impl with `impl Pixel for impl +// Element` 🤷 macro_rules! impl_pixel_for_image_pixel { ($p:ident<$s:ident>: $c:pat => $d:expr) => { impl<$s> Pixel for $p<$s> diff --git a/src/render/mod.rs b/src/render/mod.rs index 2c584b8..26e3865 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -76,8 +76,9 @@ impl<'a, P: Pixel> Renderer<'a, P> { /// /// # Panics /// - /// Panics if the length of `content` is not exactly `modules_count * modules_count`. - pub fn new(content: &'a [Color], modules_count: usize, quiet_zone: u32) -> Renderer<'a, P> { + /// Panics if the length of `content` is not exactly `modules_count * + /// modules_count`. + pub fn new(content: &'a [Color], modules_count: usize, quiet_zone: u32) -> Self { assert!(modules_count * modules_count == content.len()); Renderer { content, diff --git a/src/render/pic.rs b/src/render/pic.rs index b715ce5..89ac0c8 100644 --- a/src/render/pic.rs +++ b/src/render/pic.rs @@ -26,7 +26,7 @@ impl Pixel for Color { type Image = String; fn default_color(_color: ModuleColor) -> Self { - Color + Self } } @@ -40,7 +40,7 @@ impl RenderCanvas for Canvas { type Image = String; fn new(width: u32, height: u32, _dark_pixel: Color, _light_pixel: Color) -> Self { - Canvas { + Self { pic: format!( concat!( "maxpswid={w};maxpsht={h};movewid=0;moveht=1;boxwid=1;boxht=1\n", diff --git a/src/render/unicode.rs b/src/render/unicode.rs index 4ae97b8..4470cee 100644 --- a/src/render/unicode.rs +++ b/src/render/unicode.rs @@ -4,7 +4,7 @@ use crate::render::{Canvas as RenderCanvas, Color, Pixel}; const CODEPAGE: [&str; 4] = [" ", "\u{2584}", "\u{2580}", "\u{2588}"]; -#[derive(Copy, Clone, PartialEq)] +#[derive(Copy, Clone, PartialEq, Eq)] pub enum Dense1x2 { Dark, Light, @@ -13,8 +13,8 @@ pub enum Dense1x2 { impl Pixel for Dense1x2 { type Image = String; type Canvas = Canvas1x2; - fn default_color(color: Color) -> Dense1x2 { - color.select(Dense1x2::Dark, Dense1x2::Light) + fn default_color(color: Color) -> Self { + color.select(Self::Dark, Self::Light) } fn default_unit_size() -> (u32, u32) { (1, 1) @@ -22,10 +22,10 @@ impl Pixel for Dense1x2 { } impl Dense1x2 { - fn value(self) -> u8 { + const fn value(self) -> u8 { match self { - Dense1x2::Dark => 1, - Dense1x2::Light => 0, + Self::Dark => 1, + Self::Light => 0, } } fn parse_2_bits(sym: u8) -> &'static str { @@ -45,7 +45,7 @@ impl RenderCanvas for Canvas1x2 { fn new(width: u32, height: u32, dark_pixel: Dense1x2, light_pixel: Dense1x2) -> Self { let a = vec![light_pixel.value(); (width * height) as usize]; - Canvas1x2 { width, canvas: a, dark_pixel: dark_pixel.value() } + Self { width, canvas: a, dark_pixel: dark_pixel.value() } } fn draw_dark_pixel(&mut self, x: u32, y: u32) { @@ -111,7 +111,7 @@ fn integration_render_utf8_1x2() { + " ▀███▄ ▀▀ █ ██ \n" + " ▀▀▀ ▀ ▀▀ ▀ ▀ \n" + " " - ) + ); } #[test] diff --git a/src/types.rs b/src/types.rs index 4ff2b14..4f0e46c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -31,11 +31,11 @@ pub enum QrError { impl Display for QrError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> { let msg = match *self { - QrError::DataTooLong => "data too long", - QrError::InvalidVersion => "invalid version", - QrError::UnsupportedCharacterSet => "unsupported character set", - QrError::InvalidEciDesignator => "invalid ECI designator", - QrError::InvalidCharacter => "invalid character", + Self::DataTooLong => "data too long", + Self::InvalidVersion => "invalid version", + Self::UnsupportedCharacterSet => "unsupported character set", + Self::InvalidEciDesignator => "invalid ECI designator", + Self::InvalidCharacter => "invalid character", }; fmt.write_str(msg) } @@ -65,15 +65,15 @@ impl Color { /// /// # Examples /// - /// ```rust + /// ``` /// # use qrcode::types::Color; /// assert_eq!(Color::Light.select(1, 0), 0); /// assert_eq!(Color::Dark.select("black", "white"), "black"); /// ``` pub fn select(self, dark: T, light: T) -> T { match self { - Color::Light => light, - Color::Dark => dark, + Self::Light => light, + Self::Dark => dark, } } } @@ -82,8 +82,8 @@ impl Not for Color { type Output = Self; fn not(self) -> Self { match self { - Color::Light => Color::Dark, - Color::Dark => Color::Light, + Self::Light => Self::Dark, + Self::Dark => Self::Light, } } } @@ -131,10 +131,10 @@ pub enum Version { impl Version { /// Get the number of "modules" on each size of the QR code, i.e. the width /// and height of the code. - pub fn width(self) -> i16 { + pub const fn width(self) -> i16 { match self { - Version::Normal(v) => v * 4 + 17, - Version::Micro(v) => v * 2 + 9, + Self::Normal(v) => v * 4 + 17, + Self::Micro(v) => v * 2 + 9, } } @@ -155,10 +155,10 @@ impl Version { T: PartialEq + Default + Copy, { match self { - Version::Normal(v @ 1..=40) => { + Self::Normal(v @ 1..=40) => { return Ok(table[(v - 1).as_usize()][ec_level as usize]); } - Version::Micro(v @ 1..=4) => { + Self::Micro(v @ 1..=4) => { let obj = table[(v + 39).as_usize()][ec_level as usize]; if obj != T::default() { return Ok(obj); @@ -171,7 +171,7 @@ impl Version { /// The number of bits needed to encode the mode indicator. pub fn mode_bits_count(self) -> usize { - if let Version::Micro(a) = self { + if let Self::Micro(a) = self { (a - 1).as_usize() } else { 4 @@ -179,8 +179,8 @@ impl Version { } /// Checks whether is version refers to a Micro QR code. - pub fn is_micro(self) -> bool { - matches!(self, Version::Micro(_)) + pub const fn is_micro(self) -> bool { + matches!(self, Self::Micro(_)) } } @@ -208,9 +208,11 @@ pub enum Mode { impl Mode { /// Computes the number of bits needed to encode the data length. /// - /// use qrcode::types::{Version, Mode}; + /// ``` + /// use qrcode::types::{Mode, Version}; /// - /// assert_eq!(Mode::Numeric.length_bits_count(Version::Normal(1)), 10); + /// assert_eq!(Mode::Numeric.length_bits_count(Version::Normal(1)), 10); + /// ``` /// /// This method will return `Err(QrError::UnsupportedCharacterSet)` if the /// mode is not supported in the given version. @@ -219,64 +221,67 @@ impl Mode { Version::Micro(a) => { let a = a.as_usize(); match self { - Mode::Numeric => 2 + a, - Mode::Alphanumeric | Mode::Byte => 1 + a, - Mode::Kanji => a, + Self::Numeric => 2 + a, + Self::Alphanumeric | Self::Byte => 1 + a, + Self::Kanji => a, } } Version::Normal(1..=9) => match self { - Mode::Numeric => 10, - Mode::Alphanumeric => 9, - Mode::Byte | Mode::Kanji => 8, + Self::Numeric => 10, + Self::Alphanumeric => 9, + Self::Byte | Self::Kanji => 8, }, Version::Normal(10..=26) => match self { - Mode::Numeric => 12, - Mode::Alphanumeric => 11, - Mode::Byte => 16, - Mode::Kanji => 10, + Self::Numeric => 12, + Self::Alphanumeric => 11, + Self::Byte => 16, + Self::Kanji => 10, }, Version::Normal(_) => match self { - Mode::Numeric => 14, - Mode::Alphanumeric => 13, - Mode::Byte => 16, - Mode::Kanji => 12, + Self::Numeric => 14, + Self::Alphanumeric => 13, + Self::Byte => 16, + Self::Kanji => 12, }, } } /// Computes the number of bits needed to some data of a given raw length. /// - /// use qrcode::types::Mode; + /// ``` + /// use qrcode::types::Mode; /// - /// assert_eq!(Mode::Numeric.data_bits_count(7), 24); + /// assert_eq!(Mode::Numeric.data_bits_count(7), 24); + /// ``` /// /// Note that in Kanji mode, the `raw_data_len` is the number of Kanjis, /// i.e. half the total size of bytes. - pub fn data_bits_count(self, raw_data_len: usize) -> usize { + pub const fn data_bits_count(self, raw_data_len: usize) -> usize { match self { - Mode::Numeric => (raw_data_len * 10 + 2) / 3, - Mode::Alphanumeric => (raw_data_len * 11 + 1) / 2, - Mode::Byte => raw_data_len * 8, - Mode::Kanji => raw_data_len * 13, + Self::Numeric => (raw_data_len * 10 + 2) / 3, + Self::Alphanumeric => (raw_data_len * 11 + 1) / 2, + Self::Byte => raw_data_len * 8, + Self::Kanji => raw_data_len * 13, } } /// Find the lowest common mode which both modes are compatible with. /// - /// use qrcode::types::Mode; - /// - /// let a = Mode::Numeric; - /// let b = Mode::Kanji; - /// let c = a.max(b); - /// assert!(a <= c); - /// assert!(b <= c); + /// ``` + /// use qrcode::types::Mode; /// + /// let a = Mode::Numeric; + /// let b = Mode::Kanji; + /// let c = a.max(b); + /// assert!(a <= c); + /// assert!(b <= c); + /// ``` #[must_use] pub fn max(self, other: Self) -> Self { match self.partial_cmp(&other) { Some(Ordering::Greater) => self, Some(_) => other, - None => Mode::Byte, + None => Self::Byte, } } } @@ -287,8 +292,8 @@ impl PartialOrd for Mode { fn partial_cmp(&self, other: &Self) -> Option { match (*self, *other) { (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), + (Self::Numeric, Self::Alphanumeric) | (_, Self::Byte) => Some(Ordering::Less), + (Self::Alphanumeric, Self::Numeric) | (Self::Byte, _) => Some(Ordering::Greater), _ => None, } } @@ -296,14 +301,16 @@ impl PartialOrd for Mode { #[cfg(test)] mod mode_tests { + use std::cmp::Ordering; + use crate::types::Mode::{Alphanumeric, Byte, Kanji, Numeric}; #[test] fn test_mode_order() { assert!(Numeric < Alphanumeric); assert!(Byte > Kanji); - assert!(!(Numeric < Kanji)); - assert!(!(Numeric >= Kanji)); + assert!(matches!(Numeric.partial_cmp(&Kanji), None | Some(Ordering::Equal | Ordering::Greater))); + assert!(matches!(Numeric.partial_cmp(&Kanji), None | Some(Ordering::Less))); } #[test] From 733e3f5b95e80c0a9b29f8a0da59abfbb0d7fd13 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Sat, 13 Jul 2024 04:36:58 +0900 Subject: [PATCH 2/2] revert: Revert some changes --- src/bits.rs | 112 +++++++++++++++++--------------------------- src/canvas.rs | 5 +- src/ec.rs | 12 ++--- src/lib.rs | 5 +- src/optimize.rs | 8 ++-- src/render/image.rs | 5 +- src/render/mod.rs | 3 +- src/types.rs | 6 +-- 8 files changed, 63 insertions(+), 93 deletions(-) diff --git a/src/bits.rs b/src/bits.rs index b124a6c..c5b5ee0 100644 --- a/src/bits.rs +++ b/src/bits.rs @@ -133,14 +133,14 @@ fn test_push_number() { assert_eq!( bytes, vec![ - 0b0101_1010, // 90 - 0b1001_1010, // 154 - 0b1100_1011, // 203 - 0b0110_1101, // 109 - 0b0110_0100, // 100 - 0b0111_1001, // 121 - 0b0111_0001, // 113 - 0b1000_0000, // 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 ] ); } @@ -286,27 +286,27 @@ 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_1000, 0b0011_1000, 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(999_999), Ok(())); - assert_eq!(bits.into_bytes(), vec![0b0111_1100, 0b1111_0100, 0b0010_0011, 0b1111_0000]); + assert_eq!(bits.push_eci_designator(999999), Ok(())); + assert_eq!(bits.into_bytes(), vec![0b0111__110_0, 0b11110100, 0b00100011, 0b1111__0000]); } #[test] fn test_invalid_designator() { let mut bits = Bits::new(Version::Normal(1)); - assert_eq!(bits.push_eci_designator(1_000_000), Err(QrError::InvalidEciDesignator)); + assert_eq!(bits.push_eci_designator(1000000), Err(QrError::InvalidEciDesignator)); } #[test] @@ -358,7 +358,7 @@ mod numeric_tests { assert_eq!(bits.push_numeric_data(b"01234567"), Ok(())); assert_eq!( bits.into_bytes(), - vec![0b0001_0000, 0b0010_0000, 0b0000_1100, 0b0101_0110, 0b0110_0001, 0b1000_0000] + vec![0b0001_0000, 0b001000_00, 0b00001100, 0b01010110, 0b01_100001, 0b1__0000000] ); } @@ -370,14 +370,14 @@ mod numeric_tests { bits.into_bytes(), vec![ 0b0001_0000, - 0b0100_0000, - 0b0000_1100, - 0b0101_0110, - 0b0110_1010, + 0b010000_00, + 0b00001100, + 0b01010110, + 0b01_101010, 0b0110_1110, - 0b0001_0100, - 0b1110_1010, - 0b0101_0000, + 0b000101_00, + 0b11101010, + 0b0101__0000, ] ); } @@ -389,14 +389,14 @@ mod numeric_tests { assert_eq!( bits.into_bytes(), vec![ - 0b0010_0000, - 0b0000_0110, - 0b0010_1011, - 0b0011_0101, - 0b0011_0111, - 0b0000_1010, - 0b0111_0101, - 0b0010_1000, + 0b00_10000_0, + 0b00000110, + 0b0_0101011, + 0b001_10101, + 0b00110_111, + 0b0000101_0, + 0b01110101, + 0b00101__000, ] ); } @@ -466,7 +466,7 @@ mod alphanumeric_tests { assert_eq!(bits.push_alphanumeric_data(b"AC-42"), Ok(())); assert_eq!( bits.into_bytes(), - vec![0b0010_0000, 0b0010_1001, 0b1100_1110, 0b1110_0111, 0b0010_0001, 0b0000_0000] + vec![0b0010_0000, 0b00101_001, 0b11001110, 0b11100111, 0b001_00001, 0b0__0000000] ); } @@ -523,7 +523,7 @@ mod byte_tests { 0b1010_1011, 0b1100_1101, 0b1110_1111, - 0b0000_0000, + 0b0000__0000, ] ); } @@ -578,7 +578,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, 0b1100_1111, 0b1110_1010, 0b1010_1000]); + assert_eq!(bits.into_bytes(), vec![0b1000_0000, 0b0010_0110, 0b11001111, 0b1_1101010, 0b101010__00]); } #[test] @@ -614,8 +614,7 @@ impl Bits { /// bits.push_alphanumeric_data(b"%10ABC123"); /// ``` /// - /// In QR code, the character `%` is used as the data field separator - /// (0x1D). + /// In QR code, the character `%` is used as the data field separator (0x1D). /// /// # Errors /// @@ -768,19 +767,8 @@ mod finish_tests { assert_eq!( bits.into_bytes(), vec![ - 0b0010_0000, - 0b0101_1011, - 0b0000_1011, - 0b0111_1000, - 0b1101_0001, - 0b0111_0010, - 0b1101_1100, - 0b0100_1101, - 0b0100_0011, - 0b0100_0000, - 0b1110_1100, - 0b0001_0001, - 0b1110_1100, + 0b00100000, 0b01011011, 0b00001011, 0b01111000, 0b11010001, 0b01110010, 0b11011100, 0b01001101, + 0b01000011, 0b01000000, 0b11101100, 0b00010001, 0b11101100, ] ); } @@ -797,7 +785,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![0b1011_1111, 0b0011_1110, 0b0011_0000]); + assert_eq!(bits.into_bytes(), vec![0b101_11111, 0b00111_110, 0b0011__0000]); } #[test] @@ -805,7 +793,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![0b1001_1111, 0b0011_1100, 0b1000_0000]); + assert_eq!(bits.into_bytes(), vec![0b100_11111, 0b00111_100, 0b1_000__0000]); } #[test] @@ -813,7 +801,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![0b0111_1111, 0b0011_1000, 0b0000_0000]); + assert_eq!(bits.into_bytes(), vec![0b011_11111, 0b00111_000, 0b0000__0000]); } #[test] @@ -821,7 +809,7 @@ mod finish_tests { let mut bits = Bits::new(Version::Micro(1)); assert_eq!(bits.push_numeric_data(b""), Ok(())); assert_eq!(bits.push_terminator(EcLevel::L), Ok(())); - assert_eq!(bits.into_bytes(), vec![0b0000_0000, 0b1110_1100, 0]); + assert_eq!(bits.into_bytes(), vec![0b000_000_00, 0b11101100, 0]); } } @@ -883,19 +871,8 @@ mod encode_tests { assert_eq!( res, Ok(vec![ - 0b0010_0000, - 0b0101_1011, - 0b0000_1011, - 0b0111_1000, - 0b1101_0001, - 0b0111_0010, - 0b1101_1100, - 0b0100_1101, - 0b0100_0011, - 0b0100_0000, - 0b1110_1100, - 0b0001_0001, - 0b1110_1100, + 0b00100000, 0b01011011, 0b00001011, 0b01111000, 0b11010001, 0b01110010, 0b11011100, 0b01001101, + 0b01000011, 0b01000000, 0b11101100, 0b00010001, 0b11101100, ]) ); } @@ -903,7 +880,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![0b0001_1000, 0b1111_0111, 0b0010_0101, 0b0000_0000, 0b1110_1100])); + assert_eq!(res, Ok(vec![0b0_0011_000, 0b1111011_1, 0b001_00101, 0b0_00000__00, 0b11101100])); } #[test] @@ -926,8 +903,7 @@ mod encode_tests { /// /// Returns `Err(QrError::DataTooLong)` if the data is too long to fit even the /// highest QR code version. -// the panic caused by the expect() will never actually happen since the `version`s are known good constants. -#[allow(clippy::missing_panics_doc)] +#[allow(clippy::missing_panics_doc)] // the panic caused by the expect() will never actually happen since the `version`s are known good constants. 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)] { @@ -978,7 +954,7 @@ mod encode_auto_tests { assert_eq!(find_min_version(20000, EcLevel::L), Version::Normal(37)); assert_eq!(find_min_version(640, EcLevel::L), Version::Normal(4)); assert_eq!(find_min_version(641, EcLevel::L), Version::Normal(5)); - assert_eq!(find_min_version(999_999, EcLevel::H), Version::Normal(40)); + assert_eq!(find_min_version(999999, EcLevel::H), Version::Normal(40)); } #[test] diff --git a/src/canvas.rs b/src/canvas.rs index 99462eb..e160b09 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -696,7 +696,7 @@ mod draw_version_info_tests { #[test] fn test_draw_number() { let mut c = Canvas::new(Version::Micro(1), EcLevel::L); - c.draw_number(0b1010_1101, 8, Color::Dark, Color::Light, &[(0, 0), (0, -1), (-2, -2), (-2, 0)]); + c.draw_number(0b10101101, 8, Color::Dark, Color::Light, &[(0, 0), (0, -1), (-2, -2), (-2, 0)]); assert_eq!( &*c.to_debug_str(), "\n\ @@ -1794,8 +1794,7 @@ impl Canvas { if ratio >= 100 { ratio - 100 } else { 100 - ratio }.as_u16() } - /// Compute the penalty score for having too many light modules on the - /// sides. + /// Compute the penalty score for having too many light modules on the sides. /// /// This penalty score is exclusive to Micro QR code. /// diff --git a/src/ec.rs b/src/ec.rs index 51eb852..25f170d 100644 --- a/src/ec.rs +++ b/src/ec.rs @@ -13,9 +13,9 @@ use crate::types::{EcLevel, QrResult, Version}; /// 69 bytes. Longer blocks will result in task panic. /// /// This method treats the data as a polynomial of the form -/// (a\[0\] xm+n + a\[1\] xm+n-1 + … + a\[m\] -/// xn) in GF(28), and then computes the polynomial -/// modulus with a generator polynomial of degree N. +/// (a\[0\] xm+n + a\[1\] xm+n-1 + … + a\[m\] xn) +/// in GF(28), and then computes the polynomial modulus with a +/// generator polynomial of degree N. pub fn create_error_correction_code(data: &[u8], ec_code_size: usize) -> Vec { let data_len = data.len(); let log_den = GENERATOR_POLYNOMIALS[ec_code_size]; @@ -431,9 +431,9 @@ static EC_BYTES_PER_BLOCK: [[usize; 4]; 44] = [ /// This is a copy of ISO/IEC 18004:2006, §6.5.1, Table 9 (The value "k" of the /// 7th column, followed by the 6th column). /// -/// Every entry is a 4-tuple. Take `DATA_BYTES_PER_BLOCK[39][3] == (15, 20, 16, -/// 61)` as an example, this means in version 40 with correction level H, there -/// are 20 blocks with 15 bytes in size, and 61 blocks with 16 bytes in size. +/// Every entry is a 4-tuple. Take `DATA_BYTES_PER_BLOCK[39][3] == (15, 20, 16, 61)` +/// as an example, this means in version 40 with correction level H, there are +/// 20 blocks with 15 bytes in size, and 61 blocks with 16 bytes in size. static DATA_BYTES_PER_BLOCK: [[(usize, usize, usize, usize); 4]; 44] = [ // Normal versions. [(19, 1, 0, 0), (16, 1, 0, 0), (13, 1, 0, 0), (9, 1, 0, 0)], // 1 diff --git a/src/lib.rs b/src/lib.rs index ba66d4c..3b2a00f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -//! QR code encoder +//! QRCode encoder //! //! This crate provides a QR code and Micro QR code encoder for binary data. //! @@ -186,8 +186,7 @@ impl QrCode { /// Gets the maximum number of allowed erratic modules can be introduced /// before the data becomes corrupted. Note that errors should not be /// introduced to functional modules. - // the version and ec_level should have been checked when calling `.with_version()`. - #[allow(clippy::missing_panics_doc)] + #[allow(clippy::missing_panics_doc)] // the version and ec_level should have been checked when calling `.with_version()`. pub fn max_allowed_errors(&self) -> usize { ec::max_allowed_errors(self.version, self.ec_level).expect("invalid version or ec_level") } diff --git a/src/optimize.rs b/src/optimize.rs index 8393702..494d1a7 100644 --- a/src/optimize.rs +++ b/src/optimize.rs @@ -491,8 +491,8 @@ enum ExclCharSet { /// The end of string. End = 0, - /// All symbols supported by the Alphanumeric encoding, i.e. space, `$`, - /// `%`, `*`, `+`, `-`, `.`, `/` and `:`. + /// All symbols supported by the Alphanumeric encoding, i.e. space, `$`, `%`, + /// `*`, `+`, `-`, `.`, `/` and `:`. Symbol = 1, /// All numbers (0–9). @@ -514,8 +514,8 @@ enum ExclCharSet { KanjiHi3 = 6, /// The second byte of a Shift JIS 2-byte encoding, in the range 0x40–0xbf, - /// excluding letters (covered by `Alpha`), 0x81–0x9f (covered by - /// `KanjiHi1`), and the invalid byte 0x7f. + /// excluding letters (covered by `Alpha`), 0x81–0x9f (covered by `KanjiHi1`), + /// and the invalid byte 0x7f. KanjiLo1 = 7, /// The second byte of a Shift JIS 2-byte encoding, in the range 0xc0–0xfc, diff --git a/src/render/image.rs b/src/render/image.rs index 518e1d2..e468ff0 100644 --- a/src/render/image.rs +++ b/src/render/image.rs @@ -5,9 +5,8 @@ use crate::types::Color; use image::{ImageBuffer, Luma, LumaA, Primitive, Rgb, Rgba}; -// need to keep using this macro to implement Pixel separately for each color -// model, otherwise we'll have conflicting impl with `impl Pixel for impl -// Element` 🤷 +// need to keep using this macro to implement Pixel separately for each color model, +// otherwise we'll have conflicting impl with `impl Pixel for impl Element` 🤷 macro_rules! impl_pixel_for_image_pixel { ($p:ident<$s:ident>: $c:pat => $d:expr) => { impl<$s> Pixel for $p<$s> diff --git a/src/render/mod.rs b/src/render/mod.rs index 26e3865..a38c2bb 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -76,8 +76,7 @@ impl<'a, P: Pixel> Renderer<'a, P> { /// /// # Panics /// - /// Panics if the length of `content` is not exactly `modules_count * - /// modules_count`. + /// Panics if the length of `content` is not exactly `modules_count * modules_count`. pub fn new(content: &'a [Color], modules_count: usize, quiet_zone: u32) -> Self { assert!(modules_count * modules_count == content.len()); Renderer { diff --git a/src/types.rs b/src/types.rs index 4f0e46c..feeae52 100644 --- a/src/types.rs +++ b/src/types.rs @@ -301,16 +301,14 @@ impl PartialOrd for Mode { #[cfg(test)] mod mode_tests { - use std::cmp::Ordering; - use crate::types::Mode::{Alphanumeric, Byte, Kanji, Numeric}; #[test] fn test_mode_order() { assert!(Numeric < Alphanumeric); assert!(Byte > Kanji); - assert!(matches!(Numeric.partial_cmp(&Kanji), None | Some(Ordering::Equal | Ordering::Greater))); - assert!(matches!(Numeric.partial_cmp(&Kanji), None | Some(Ordering::Less))); + assert!(!(Numeric < Kanji)); + assert!(!(Numeric >= Kanji)); } #[test]