use beet::prelude::*;
use image::Rgb;
use qrcode::QrCode;
#[derive(Debug, Clone)]
pub struct QrCodeCmd {
pub input: Vec<String>,
pub output: std::path::PathBuf,
pub light: String,
pub dark: String,
}
impl Default for QrCodeCmd {
fn default() -> Self {
Self {
input: Vec::new(),
output: "qrcode.png".into(),
light: "255,255,255".to_string(),
dark: "0,0,0".to_string(),
}
}
}
impl QrCodeCmd {
pub async fn run(self) -> Result {
let Self {
input,
output,
light,
dark,
} = self;
let input = input.join(" ");
let light_parts: Vec<&str> = light.split(',').collect();
let light_rgb = [
light_parts[0].parse::<u8>()?,
light_parts[1].parse::<u8>()?,
light_parts[2].parse::<u8>()?,
];
let dark_parts: Vec<&str> = dark.split(',').collect();
let dark_rgb = [
dark_parts[0].parse::<u8>()?,
dark_parts[1].parse::<u8>()?,
dark_parts[2].parse::<u8>()?,
];
let code = QrCode::new(input)?;
let image = code
.render::<Rgb<u8>>()
.dark_color(Rgb(dark_rgb))
.light_color(Rgb(light_rgb))
.build();
image.save(output)?;
Ok(())
}
}