Switch to fast_qr crate

This commit is contained in:
cyqsimon 2022-09-01 20:20:27 +08:00
parent f762672b09
commit aa2dda7885
No known key found for this signature in database
GPG Key ID: 1D8CE2F297390D65
6 changed files with 29 additions and 130 deletions

78
Cargo.lock generated
View File

@ -474,12 +474,6 @@ version = "3.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3"
[[package]]
name = "bytemuck"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f5715e491b5a1598fc2bef5a606847b5dc1d48ea625bd3c02c00de8285591da"
[[package]]
name = "byteorder"
version = "1.4.3"
@ -528,12 +522,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "checked_int_cast"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17cc5e6b5ab06331c33589842070416baa137e8b0eb912b008cfd4a78ada7919"
[[package]]
name = "chrono"
version = "0.4.22"
@ -638,12 +626,6 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e769b5c8c8283982a987c6e948e540254f1058d5a74b8794914d4ef5fc2a24"
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "comrak"
version = "0.14.0"
@ -833,6 +815,15 @@ dependencies = [
"regex",
]
[[package]]
name = "fast_qr"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b798dfd6e29b85c0bcf434272db4cde0100ab4d82c5db0a4f422e77b30d0b4e4"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "fastrand"
version = "1.8.0"
@ -1096,7 +1087,7 @@ dependencies = [
"indexmap",
"lasso",
"num-bigint",
"num-rational 0.4.1",
"num-rational",
"num-traits",
"once_cell",
"phf 0.9.0",
@ -1291,20 +1282,6 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "image"
version = "0.23.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24ffcb7e7244a9bf19d35bf2883b9c080c4ced3c07a9895572178cdb8f13f6a1"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"num-iter",
"num-rational 0.3.2",
"num-traits",
]
[[package]]
name = "indexmap"
version = "1.9.1"
@ -1570,13 +1547,13 @@ dependencies = [
"clap_mangen",
"comrak",
"fake-tty",
"fast_qr",
"futures",
"get_if_addrs",
"grass",
"hex",
"http",
"httparse",
"lazy_static",
"libflate",
"log",
"maud",
@ -1586,7 +1563,6 @@ dependencies = [
"port_check",
"predicates",
"pretty_assertions",
"qrcode",
"regex",
"reqwest",
"rstest",
@ -1669,28 +1645,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
@ -2085,16 +2039,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "qrcode"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16d2f1455f3630c6e5107b4f2b94e74d76dea80736de0981fd27644216cff57f"
dependencies = [
"checked_int_cast",
"image",
]
[[package]]
name = "quote"
version = "1.0.21"

View File

@ -32,12 +32,12 @@ clap = { version = "3.2", features = ["derive", "cargo", "wrap_help"] }
clap_complete = "3.2.3"
clap_mangen = "0.1"
comrak = "0.14.0"
fast_qr = "0.3.1"
futures = "0.3"
get_if_addrs = "0.5"
hex = "0.4"
http = "0.2"
httparse = "1"
lazy_static = "1.4.0"
libflate = "1"
log = "0.4"
maud = "0.23"
@ -45,8 +45,6 @@ mime = "0.3"
nanoid = "0.4"
percent-encoding = "2"
port_check = "0.1"
qrcode = "0.12.0"
regex = "1"
rustls = { version = "0.20", optional = true }
rustls-pemfile = { version = "1.0", optional = true }
serde = { version = "1", features = ["derive"] }
@ -73,6 +71,7 @@ assert_cmd = "2"
assert_fs = "1"
predicates = "2"
pretty_assertions = "1.2"
regex = "1"
reqwest = { version = "0.11", features = ["blocking", "multipart", "rustls-tls"], default-features = false }
rstest = "0.15"
select = "0.5"

View File

@ -180,9 +180,6 @@ nav .qrcode {
nav .qrcode svg {
display: block;
border: 0.3rem;
border-style: solid;
border-color: #ffffff;
}
nav .theme {

View File

@ -1,4 +1,4 @@
use qrcode::EcLevel;
use fast_qr::ECL;
/// The error correction level to use for all QR code generation.
pub const QR_EC_LEVEL: EcLevel = EcLevel::L;
pub const QR_EC_LEVEL: ECL = ECL::L;

View File

@ -12,8 +12,8 @@ use actix_web_httpauth::middleware::HttpAuthentication;
use anyhow::Result;
use clap::{crate_version, IntoApp, Parser};
use clap_complete::generate;
use fast_qr::QRBuilder;
use log::{error, warn};
use qrcode::QrCode;
use yansi::{Color, Paint};
mod archive;
@ -240,13 +240,13 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
.iter()
.filter(|url| !url.contains("//127.0.0.1:") && !url.contains("//[::1]:"))
{
match QrCode::with_error_correction_level(url, consts::QR_EC_LEVEL) {
match QRBuilder::new(url.clone()).ecl(consts::QR_EC_LEVEL).build() {
Ok(qr) => {
println!("QR code for {}:", Color::Green.paint(url).bold());
print_qr(&qr);
qr.print();
}
Err(e) => {
error!("Failed to render QR to terminal: {}", e);
error!("Failed to render QR to terminal: {:?}", e);
}
};
}
@ -352,28 +352,3 @@ async fn css() -> impl Responder {
.insert_header(ContentType(mime::TEXT_CSS))
.body(css)
}
// Prints to the console a normal and an inverted QrCode side by side.
fn print_qr(qr: &QrCode) {
use qrcode::render::unicode::Dense1x2;
let normal = qr
.render()
.quiet_zone(true)
.dark_color(Dense1x2::Dark)
.light_color(Dense1x2::Light)
.build();
let inverted = qr
.render()
.quiet_zone(true)
.dark_color(Dense1x2::Light)
.light_color(Dense1x2::Dark)
.build();
let codes = normal
.lines()
.zip(inverted.lines())
.map(|(l, r)| format!("{} {}", l, r))
.collect::<Vec<_>>()
.join("\n");
println!("{}", codes);
}

View File

@ -2,10 +2,10 @@ use actix_web::http::StatusCode;
use chrono::{DateTime, Utc};
use chrono_humanize::Humanize;
use clap::{crate_name, crate_version};
use lazy_static::lazy_static;
use fast_qr::convert::svg::SvgBuilder;
use fast_qr::qr::QRCodeError;
use fast_qr::QRBuilder;
use maud::{html, Markup, PreEscaped, DOCTYPE};
use qrcode::{types::QrError, QrCode};
use regex::Regex;
use std::time::SystemTime;
use strum::IntoEnumIterator;
@ -229,27 +229,11 @@ pub fn raw(entries: Vec<Entry>, is_root: bool) -> Markup {
}
/// Renders the QR code SVG
fn qr_code_svg(url: impl AsRef<str>, no_width_height_attr: bool) -> Result<String, QrError> {
use qrcode::render::svg;
let qr = QrCode::with_error_correction_level(url.as_ref(), consts::QR_EC_LEVEL)?;
let mut svg = qr
.render()
.quiet_zone(false)
.dark_color(svg::Color("#000000"))
.light_color(svg::Color("#ffffff"))
.build();
if no_width_height_attr {
// HACK: qrcode crate hard-codes height and width into SVG's attributes.
// This behaviour may be undesirable because we want it to fit its HTML container.
// The proper way to remove them is to use a XML parser, but regex is good enough for a
// simple case like this.
lazy_static! {
static ref RE: Regex =
Regex::new(r#"(?P<front><svg.+? )width=".+?" height=".+?"(?P<aft>.+?>)"#).unwrap();
}
svg = RE.replace(&svg, "$front$aft").to_string();
}
fn qr_code_svg(url: impl AsRef<str>, margin: usize) -> Result<String, QRCodeError> {
let qr = QRBuilder::new(url.as_ref().into())
.ecl(consts::QR_EC_LEVEL)
.build()?;
let svg = SvgBuilder::new().margin(margin).build_qr(qr);
Ok(svg)
}
@ -338,9 +322,9 @@ fn qr_spoiler(show_qrcode: bool, content: impl AsRef<str>) -> Markup {
"QR code"
}
div.qrcode #qrcode {
@match qr_code_svg(content, true) {
@match qr_code_svg(content, 1) {
Ok(svg) => (PreEscaped(svg)),
Err(err) => (format!("QR generation error: {}", err)),
Err(err) => (format!("QR generation error: {:?}", err)),
}
}
}