Skip to content

Commit

Permalink
chore: unify comments & derive macro usage
Browse files Browse the repository at this point in the history
All becomes

    // Simple comments
    /// Doc comments
    #[cfg(...)]
  • Loading branch information
erwanvivien committed Sep 9, 2023
1 parent a9e5d6d commit 501fc6c
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 32 deletions.
20 changes: 10 additions & 10 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use core::fmt::{Display, Formatter};

use crate::Version;

#[rustfmt::skip]
#[cfg(not(target_arch = "wasm32"))]
/// Values to keep last X bits of a u8
/// `KEEP_LAST[i]` equates `(1 << i) - 1`
///
Expand All @@ -31,6 +29,8 @@ use crate::Version;
/// let mut b = 0b1010_1010;
/// assert_eq!(b & KEEP_LAST[3], 0b010)
/// ```
#[rustfmt::skip]
#[cfg(not(target_arch = "wasm32"))]
pub const KEEP_LAST: [usize; 65] = [
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383,
32767, 65535, 131_071, 262_143, 524_287, 1_048_575, 2_097_151, 4_194_303, 8_388_607,
Expand All @@ -46,10 +46,10 @@ pub const KEEP_LAST: [usize; 65] = [
9_223_372_036_854_775_807, 18_446_744_073_709_551_615,
];

#[rustfmt::skip]
#[cfg(target_arch = "wasm32")]
/// Values to keep last X bits of a u8
/// `KEEP_LAST[i]` equates `(1 << i) - 1`
#[rustfmt::skip]
#[cfg(target_arch = "wasm32")]
pub const KEEP_LAST: [usize; 33] = [
0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1_023, 2_047, 4_095, 8_191, 16_383,
32_767, 65_535, 131_071, 262_143, 524_287, 1_048_575, 2_097_151, 4_194_303, 8_388_607,
Expand Down Expand Up @@ -88,8 +88,8 @@ impl Display for CompactQR {

#[allow(clippy::cast_possible_truncation)]
impl CompactQR {
#[allow(dead_code)]
/// Instantiates a new `CompactQR`, should not be used, reduces performance.
#[allow(dead_code)]
pub const fn new() -> Self {
CompactQR {
len: 0,
Expand All @@ -104,9 +104,9 @@ impl CompactQR {
CompactQR { len: 0, data }
}

/// Instantiates a new `CompactQR`, with a given length, expects the length to be a multiple of 8.
#[allow(dead_code)]
#[cfg(test)]
/// Instantiates a new `CompactQR`, with a given length, expects the length to be a multiple of 8.
pub fn with_len(data_length: usize) -> Self {
let length = data_length / 8 + usize::from(data_length % 8 != 0);
CompactQR {
Expand Down Expand Up @@ -140,9 +140,9 @@ impl CompactQR {
&self.data
}

/// Pushes eight values in the `CompactQR`, if the array is not big enough, it will be resized.
#[inline(always)]
#[allow(dead_code)]
/// Pushes eight values in the `CompactQR`, if the array is not big enough, it will be resized.
pub fn push_u8(&mut self, bits: u8) {
self.increase_len(self.len + 8);

Expand All @@ -160,9 +160,9 @@ impl CompactQR {
self.len += 8;
}

#[inline(always)]
/// Pushes the u8 array in the `CompactQR`, using the `push_u8` function. \
/// If the array is not big enough, it will be resized.
#[inline(always)]
pub fn push_u8_slice(&mut self, slice: &[u8]) {
self.increase_len(self.len + 8 * slice.len());

Expand All @@ -171,9 +171,9 @@ impl CompactQR {
}
}

#[inline(always)]
/// Pushes `len` values to the `CompactQR`. \
/// If the array is not big enough, it will be resized.
#[inline(always)]
pub fn push_bits(&mut self, bits: usize, len: usize) {
self.increase_len(self.len + len);

Expand Down Expand Up @@ -207,9 +207,9 @@ impl CompactQR {
self.len += remaining;
}

#[inline(always)]
/// Fills the `CompactQR`'s remaining space with `[236, 17]`.
/// Expects the `CompactQR` `len` to be a multiple of 8.
#[inline(always)]
pub fn fill(&mut self) {
const PAD_BYTES: [u8; 2] = [0b1110_1100, 0b0001_0001]; //[236, 17]

Expand Down
2 changes: 1 addition & 1 deletion src/convert/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ImageBuilder {
self
}

// From https://github.com/RazrFalcon/resvg/blob/master/tests/integration/main.rs
// From https://github.com/RazrFalcon/resvg/blob/374a25f/crates/resvg/tests/integration/main.rs
/// Return a pixmap containing the svg for a QRCode
pub fn to_pixmap(&self, qr: &QRCode) -> Pixmap {
let opt = usvg::Options::default();
Expand Down
4 changes: 3 additions & 1 deletion src/ecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#![deny(unsafe_code)]
#![warn(missing_docs)]

use std::fmt::Write;

/// Error Correction Coding has 4 levels
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
/// Error Correction Coding has 4 levels
pub enum ECL {
/// Low, 7%
L,
Expand Down
4 changes: 2 additions & 2 deletions src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::ecl::ECL;
use crate::hardcode;
use crate::version::Version;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// Enum for the 3 encoding mode
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Mode {
Numeric,
Alphanumeric,
Expand Down Expand Up @@ -150,7 +150,7 @@ fn pad_to_8(compact: &mut CompactQR) {
compact.push_bits(0, len);
}

/// Converts ascii number to it's value in usize
/// Converts ascii number to it's value in usize \
/// "5" -> 5
const fn ascii_to_digit(c: u8) -> usize {
(c - b'0') as usize
Expand Down
13 changes: 6 additions & 7 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ pub fn print_matrix_with_margin(qr: &QRCode) -> String {
#[cfg(test)]
use crate::{compact::CompactQR, Version};

/// Convert a vector of u8 to it's representation in bits
///
/// If bits are required by the QR code (referring to 8.6 of the spec), they are added to the end of the vector.
///
/// ## Example
/// { 101 } => "01100101"
#[cfg(test)]
/**
* Convert a vector of u8 to it's representation in bits
*
* If bits are required by the QR code (referring to 8.6 of the spec), they are added to the end of the vector.
*
* Example: { 101 } => "01100101"
*/
pub fn binary_to_binarystring_version(binary: [u8; 5430], version: Version) -> CompactQR {
let max = version.max_bytes() * 8;
CompactQR::from_array(&binary, max + version.missing_bits())
Expand Down
8 changes: 3 additions & 5 deletions src/polynomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ const ANTILOG: [u8; 256] = [
232, 116, 214, 244, 234, 168, 80, 88, 175,
];

/**
* Return a string of human readable polynomial (ex: below)
*
* [0, 75, 249, 78, 6] => "α0x4 + α75x3 + α249x2 + α78x + α6"
*/
/// Return a string of human readable polynomial
///
/// `[0, 75, 249, 78, 6]` => "α0x4 + α75x3 + α249x2 + α78x + α6"
#[cfg(test)]
pub fn generated_to_string(poly: &[u8]) -> String {
let mut s = String::new();
Expand Down
4 changes: 2 additions & 2 deletions src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ impl QRCode {
Ok(out)
}

#[cfg(not(target_arch = "wasm32"))]
/// Prints the `QRCode` to the terminal
#[must_use]
#[cfg(not(target_arch = "wasm32"))]
pub fn to_str(&self) -> String {
helpers::print_matrix_with_margin(self)
}

#[cfg(not(target_arch = "wasm32"))]
/// Prints the `QRCode` to the terminal
#[cfg(not(target_arch = "wasm32"))]
pub fn print(&self) {
println!("{}", helpers::print_matrix_with_margin(self));
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use crate::ecl::ECL;
use crate::encode::Mode;

#[derive(Clone, Copy, Debug)]
/// Enum containing all possible `QRCode` versions
#[derive(Clone, Copy, Debug)]
pub enum Version {
/// Version n°01
V01 = 0,
Expand Down
6 changes: 3 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fn bool_to_u8(qr: QRCode) -> Vec<u8> {
.collect()
}

/// Generate a QR code from a string. All parameters are automatically set.
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
#[must_use]
/// Generate a QR code from a string. All parameters are automatically set.
pub fn qr(content: &str) -> Vec<u8> {
let qrcode = QRCode::new(content.as_bytes(), None, None, None);
qrcode.map(bool_to_u8).unwrap_or(Vec::new())
Expand Down Expand Up @@ -145,8 +145,8 @@ impl SvgOptions {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
#[cfg(feature = "svg")]
impl SvgOptions {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
/// Creates a new SvgOptions object.
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn new() -> Self {
Self {
shape: convert::Shape::Square,
Expand All @@ -164,9 +164,9 @@ impl SvgOptions {
}
}

/// Generate a QR code from a string. All parameters are automatically set.
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
#[cfg(feature = "svg")]
/// Generate a QR code from a string. All parameters are automatically set.
pub fn qr_svg(content: &str, options: SvgOptions) -> String {
use crate::convert::svg::SvgBuilder;
use crate::convert::Builder;
Expand Down

0 comments on commit 501fc6c

Please sign in to comment.