Commit ce194017ae44953efe8f76dae7b08f070c9a09ae

Cléo Rebert 2024-07-24T11:43:27

Move qrcodegen-image to its own repo.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4f6de61..6bc5a49 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# [5.6.0](https://github.com/constantoine/totp-rs/releases/tag/v5.5.0) (19/01/2024)
+### Changes
+- [qrcodegen-image](https://crates.io/crates/qrcodegen-image) has now been moved to its own [repo](https://github.com/constantoine/qrcodegen-image).
+
 # [5.5.0](https://github.com/constantoine/totp-rs/releases/tag/v5.5.0) (19/01/2024)
 ### Changes
 - Documentation now indicates required feature.
@@ -204,4 +208,4 @@ This has been, I think, the update containing the most work. While a lot of unit
 
 ## What's coming next
 - The currently used "qrcode" library is abandonned. Preliminary work showed it was not compatible woth newer versions of the "image" library
-- I'd like to take that opportunity to rethink the way the "qr" feature is presented
\ No newline at end of file
+- I'd like to take that opportunity to rethink the way the "qr" feature is presented
diff --git a/Cargo.toml b/Cargo.toml
index eeda56e..11b5758 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "totp-rs"
-version = "5.5.1"
+version = "5.6.0"
 authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
 rust-version = "1.61"
 edition = "2021"
@@ -12,11 +12,6 @@ homepage = "https://github.com/constantoine/totp-rs"
 keywords = ["authentication", "2fa", "totp", "hmac", "otp"]
 categories = ["authentication", "web-programming"]
 
-[workspace]
-members = [
-  "qrcodegen-image"
-]
-
 [package.metadata.docs.rs]
 all-features = true
 rustdoc-args = ["--cfg", "docsrs"]
@@ -40,4 +35,4 @@ url = { version = "2.4", optional = true }
 constant_time_eq = "0.2"
 rand = { version = "0.8", features = ["std_rng", "std"], optional = true, default-features = false }
 zeroize = { version = "1.6", features = ["alloc", "derive"], optional = true }
-qrcodegen-image = { version = "1.0", features = ["base64"], optional = true, path = "qrcodegen-image" }
+qrcodegen-image = { version = "1.4", features = ["base64"], optional = true }
diff --git a/qrcodegen-image/CHANGELOG.md b/qrcodegen-image/CHANGELOG.md
deleted file mode 100644
index b4d6dfb..0000000
--- a/qrcodegen-image/CHANGELOG.md
+++ /dev/null
@@ -1,38 +0,0 @@
-# Changelog
-
-## [1.4.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.4.0) (08/03/2024)
-
-### What's new
-
-- `draw_canvas` is now 15%(!) faster on my machine after optimizing the "drawing_square" loop.
-- Updated `base64` crate to `0.22`.
-
-## [1.3.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.3.0) (19/01/2024)
-
-### What's new
-
-- `draw_canvas` is now 70%(!) faster on my machine after optimizing the "drawing_square" loop.
-
-## [1.2.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.2.0) (14/09/2023)
-
-### What's new
-
-- Added benchmarks for `draw_canvas`.
-- `draw_canvas` is now 150%(!) faster on my machine after optimizing the "border" loop.
-
-## [1.1.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.1.0) (11/09/2023)
-
-### What's new
-
-- Added documentation for `draw_png` and `draw_base64`.
-- Add changelog.
-
-## [1.0.0](https://github.com/constantoine/totp-rs/releases/tag/qrcodegen-image%2Fv1.0.0) (10/09/2023)
-
-### What's new
-
-- Creation of a new `qrcodegen-image` subcrate to handle image creation, as the wrapper is actually nice and could be used in placed not related to `totp-rs`. (#61)
-
-### Special thanks
-
-- [@tmpfs](https://github.com/tmpfs) for their work on #60 and implementation in #61.
diff --git a/qrcodegen-image/Cargo.toml b/qrcodegen-image/Cargo.toml
deleted file mode 100644
index 8cba9f4..0000000
--- a/qrcodegen-image/Cargo.toml
+++ /dev/null
@@ -1,29 +0,0 @@
-[package]
-name = "qrcodegen-image"
-version = "1.4.0"
-edition = "2021"
-authors = ["Cleo Rebert <cleo.rebert@gmail.com>"]
-rust-version = "1.61"
-readme = "README.md"
-license = "MIT"
-description = "Draw QR codes to a PNG canvas. Wrapper around the qrcodegen and image crates."
-repository = "https://github.com/constantoine/totp-rs"
-homepage = "https://github.com/constantoine/totp-rs"
-
-[package.metadata.docs.rs]
-features = [ "base64" ]
-
-[features]
-base64 = ["dep:base64"]
-
-[dependencies]
-qrcodegen = "1.8"
-image = { version = "0.25", features = ["png"], default-features = false}
-base64 = { version = "0.22", optional = true }
-
-[dev-dependencies]
-criterion = { version = "0.5", features = ["html_reports"] }
-
-[[bench]]
-name = "benches"
-harness = false
\ No newline at end of file
diff --git a/qrcodegen-image/README.md b/qrcodegen-image/README.md
deleted file mode 100644
index 39b50d3..0000000
--- a/qrcodegen-image/README.md
+++ /dev/null
@@ -1,5 +0,0 @@
-# qrcodegen-image
-
-[![docs](https://docs.rs/totp-rs/badge.svg)](https://docs.rs/qrcodegen-image) [![crates.io](https://img.shields.io/crates/v/qrcodegen-image.svg)](https://crates.io/crates/qrcodegen-image)
-
-This crate is a wrapper around the [qrcodegen](https://crates.io/crates/qrcodegen) and [image](https://crates.io/crates/image) library. For examples, see the source code of [totp-rs](https://crates.io/crates/totp-rs).
diff --git a/qrcodegen-image/benches/benches.rs b/qrcodegen-image/benches/benches.rs
deleted file mode 100644
index c16898c..0000000
--- a/qrcodegen-image/benches/benches.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
-
-fn canvas_benchmark(c: &mut Criterion) {
-    let input = qrcodegen::QrCode::encode_text("otpauth://totp/GitHub:test?issuer=GitHub&secret=KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ&digits=8&period=60&algorithm=SHA256", qrcodegen::QrCodeEcc::Medium).unwrap();
-    c.bench_with_input(
-        BenchmarkId::new("qrcodegen-image", "draw_canvas"),
-        black_box(&input),
-        |b, i| {
-            b.iter(|| {
-                let _img = qrcodegen_image::draw_canvas(i.clone());
-            })
-        },
-    );
-}
-
-fn png_benchmark(c: &mut Criterion) {
-    let input = "otpauth://totp/GitHub:test?issuer=GitHub&secret=KRSXG5CTMVRXEZLUKN2XAZLSKNSWG4TFOQ&digits=8&period=60&algorithm=SHA256";
-    c.bench_with_input(
-        BenchmarkId::new("qrcodegen-image", "draw_png"),
-        black_box(&input),
-        |b, i| {
-            b.iter(|| {
-                let _img = qrcodegen_image::draw_png(i);
-            })
-        },
-    );
-}
-
-criterion_group!(benches, canvas_benchmark, png_benchmark);
-criterion_main!(benches);
diff --git a/qrcodegen-image/src/lib.rs b/qrcodegen-image/src/lib.rs
deleted file mode 100644
index 00cfe2a..0000000
--- a/qrcodegen-image/src/lib.rs
+++ /dev/null
@@ -1,98 +0,0 @@
-//! Utility functions for drawing QR codes generated using `qrcodegen`
-//! to a canvas provided by the `image` crate.
-use image::Luma;
-
-pub use image;
-pub use qrcodegen;
-
-/// Draw a QR code to an image buffer.
-pub fn draw_canvas(qr: qrcodegen::QrCode) -> image::ImageBuffer<Luma<u8>, Vec<u8>> {
-    let size = qr.size() as u32;
-    // "+ 8 * 8" is here to add padding (the white border around the QRCode)
-    // As some QRCode readers don't work without padding
-    let image_size = size * 8 + 8 * 8;
-    let mut canvas = image::GrayImage::from_pixel(image_size, image_size, Luma([255]));
-
-    let raw = canvas.as_mut();
-
-    // The QR inside the white border
-    for x_qr in 0..size {
-        for y_qr in 0..size {
-            if !qr.get_module(x_qr as i32, y_qr as i32) {
-                continue;
-            }
-
-            // Multiply coordinates by width of pixels
-            // And take into account the 8*4 padding on top and left side
-            let x_start = x_qr * 8 + 8 * 4;
-            let y_start = y_qr * 8 + 8 * 4;
-
-            // Draw a 8-pixels-wide square
-            for y_img in y_start..y_start + 8 {
-                let start = (x_start + y_img * image_size) as usize;
-                raw[start..start + 8].copy_from_slice(&[0; 8]);
-            }
-        }
-    }
-    canvas
-}
-
-/// Draw text to a PNG QR code.
-///     
-/// # Errors
-///
-/// This will return an error in case the URL gets too long to encode into a QR code.
-/// This would require the get_url method to generate an url bigger than 2000 characters,
-/// Which would be too long for some browsers anyway.
-///
-/// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly.
-pub fn draw_png(text: &str) -> Result<Vec<u8>, String> {
-    use image::ImageEncoder;
-
-    let mut vec = Vec::new();
-
-    let qr: Result<qrcodegen::QrCode, String> =
-        match qrcodegen::QrCode::encode_text(text, qrcodegen::QrCodeEcc::Medium) {
-            Ok(qr) => Ok(qr),
-            Err(err) => Err(err.to_string()),
-        };
-
-    if qr.is_err() {
-        return Err(qr.err().unwrap());
-    }
-
-    let code = qr?;
-
-    // "+ 8 * 8" is here to add padding (the white border around the QRCode)
-    // As some QRCode readers don't work without padding
-    let image_size = (code.size() as u32) * 8 + 8 * 8;
-
-    let canvas = draw_canvas(code);
-
-    // Encode the canvas into a PNG
-    let encoder = image::codecs::png::PngEncoder::new(&mut vec);
-    match encoder.write_image(
-        &canvas.into_raw(),
-        image_size,
-        image_size,
-        image::ExtendedColorType::L8,
-    ) {
-        Ok(_) => Ok(vec),
-        Err(err) => Err(err.to_string()),
-    }
-}
-
-/// Draw text to a Base64-encoded PNG QR code.
-///
-/// # Errors
-///
-/// This will return an error in case the URL gets too long to encode into a QR code.
-/// This would require the get_url method to generate an url bigger than 2000 characters,
-/// Which would be too long for some browsers anyway.
-///
-/// It will also return an error in case it can't encode the qr into a png. This shouldn't happen unless either the qrcode library returns malformed data, or the image library doesn't encode the data correctly.
-#[cfg(feature = "base64")]
-pub fn draw_base64(text: &str) -> Result<String, String> {
-    use base64::{engine::general_purpose, Engine as _};
-    draw_png(text).map(|vec| general_purpose::STANDARD.encode(vec))
-}