-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
895821f
commit cb29de1
Showing
9 changed files
with
65 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod dir; | ||
pub mod file; | ||
pub mod qr_code; | ||
pub mod random; | ||
pub mod retry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::io::Cursor; | ||
|
||
use base64::{engine::general_purpose::STANDARD, Engine}; | ||
use image::{ImageOutputFormat, Luma}; | ||
use qrcode::QrCode; | ||
|
||
pub fn generate_text_qr_code(input: &str) -> anyhow::Result<String> { | ||
Ok( | ||
QrCode::new(input.as_bytes())? | ||
.render::<char>() | ||
.quiet_zone(true) | ||
.module_dimensions(2, 1) | ||
.build(), | ||
) | ||
} | ||
|
||
pub fn generate_base64_text_qr_code(input: &str) -> anyhow::Result<String> { | ||
Ok(STANDARD.encode(generate_text_qr_code(input)?)) | ||
} | ||
|
||
pub fn generate_base64_png_qr_code(input: &str) -> anyhow::Result<String> { | ||
let qr_code: image::ImageBuffer<Luma<u8>, Vec<u8>> = QrCode::new(input.as_bytes())? | ||
.render::<Luma<u8>>() | ||
.quiet_zone(true) | ||
.module_dimensions(20, 20) | ||
.build(); | ||
let mut buff = Cursor::new(Vec::new()); | ||
qr_code.write_to(&mut buff, ImageOutputFormat::Png)?; | ||
Ok(STANDARD.encode(buff.into_inner())) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use fake::{Fake, Faker}; | ||
|
||
#[test] | ||
pub fn test_generate_base64_text_qr_code() { | ||
let qr_code = generate_base64_text_qr_code(&Faker.fake::<String>()).unwrap(); | ||
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap(); | ||
} | ||
|
||
#[test] | ||
pub fn test_generate_base64_png_qr_code() { | ||
let qr_code = generate_base64_png_qr_code(&Faker.fake::<String>()).unwrap(); | ||
let _content = STANDARD.decode(qr_code.as_bytes()).unwrap(); | ||
} | ||
} |