beet_cli/commands/
qrcode.rs

1use beet::prelude::*;
2use clap::Parser;
3use image::Rgb;
4use qrcode::QrCode;
5
6
7
8/// Build the project
9#[derive(Debug, Clone, Parser)]
10pub struct QrCodeCmd {
11	/// Input url (positional)
12	#[arg(value_name = "PROMPT", trailing_var_arg = true)]
13	pub input: Vec<String>,
14	/// Output file (-o, --output)
15	#[clap(short = 'o', long = "output",
16		default_value="qrcode.png",
17	 value_parser = clap::value_parser!(std::path::PathBuf))]
18	pub output: std::path::PathBuf,
19	/// Light color (--light)
20	#[clap(long = "light", default_value = "255,255,255")]
21	pub light: String,
22	/// Dark color (--dark)
23	#[clap(long = "dark", default_value = "0,0,0")]
24	pub dark: String,
25}
26
27
28
29impl QrCodeCmd {
30	pub async fn run(self) -> Result {
31		let Self {
32			input,
33			output,
34			light,
35			dark,
36		} = self;
37		let input = input.join(" ");
38
39		// Parse light color
40		let light_parts: Vec<&str> = light.split(',').collect();
41		let light_rgb = [
42			light_parts[0].parse::<u8>()?,
43			light_parts[1].parse::<u8>()?,
44			light_parts[2].parse::<u8>()?,
45		];
46
47		// Parse dark color
48		let dark_parts: Vec<&str> = dark.split(',').collect();
49		let dark_rgb = [
50			dark_parts[0].parse::<u8>()?,
51			dark_parts[1].parse::<u8>()?,
52			dark_parts[2].parse::<u8>()?,
53		];
54
55		let code = QrCode::new(input)?;
56		let image = code
57			.render::<Rgb<u8>>()
58			.dark_color(Rgb(dark_rgb))
59			.light_color(Rgb(light_rgb))
60			.build();
61
62		// Save the image.
63		image.save(output)?;
64
65		Ok(())
66	}
67}