Skip to main content

teaql_tool_extra/
qrcode.rs

1use teaql_tool_core::{Result, TeaQLToolError};
2use qrcode::QrCode;
3use image::Luma;
4
5#[derive(Debug, Clone)]
6pub struct QrcodeTool;
7
8impl QrcodeTool {
9    pub fn new() -> Self { Self }
10
11    pub fn generate_png(&self, data: &str, output_path: &str) -> Result<()> {
12        let code = QrCode::new(data).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
13        let image = code.render::<Luma<u8>>().build();
14        image.save(output_path).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
15        Ok(())
16    }
17
18    pub fn generate_svg(&self, data: &str) -> Result<String> {
19        let code = QrCode::new(data).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
20        let svg = code.render()
21            .min_dimensions(200, 200)
22            .dark_color(qrcode::render::svg::Color("#000000"))
23            .light_color(qrcode::render::svg::Color("#ffffff"))
24            .build();
25        Ok(svg)
26    }
27}
28
29impl Default for QrcodeTool {
30    fn default() -> Self { Self::new() }
31}