aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/consts.rs4
-rw-r--r--src/main.rs33
-rw-r--r--src/renderer.rs36
3 files changed, 16 insertions, 57 deletions
diff --git a/src/consts.rs b/src/consts.rs
index f10570a..1a105f7 100644
--- a/src/consts.rs
+++ b/src/consts.rs
@@ -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;
diff --git a/src/main.rs b/src/main.rs
index 558dabb..11193cc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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);
-}
diff --git a/src/renderer.rs b/src/renderer.rs
index 614672f..77b9eed 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -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)),
}
}
}