Skip to main content

teaql_tool_extra/
barcode.rs

1use teaql_tool_core::{Result, TeaQLToolError};
2use barcoders::sym::code128::Code128;
3use barcoders::generators::image::Image;
4
5#[derive(Debug, Clone)]
6pub struct BarcodeTool;
7
8impl BarcodeTool {
9    pub fn new() -> Self { Self }
10
11    pub fn generate_code128_png(&self, data: &str, output_path: &str) -> Result<()> {
12        let barcode = Code128::new(data).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
13        let png = Image::png(80);
14        let bytes = png.generate(&barcode.encode()[..]).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
15        std::fs::write(output_path, bytes).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
16        Ok(())
17    }
18
19    pub fn generate_code128_svg(&self, data: &str) -> Result<String> {
20        let barcode = Code128::new(data).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
21        let svg = barcoders::generators::svg::SVG::new(80);
22        let data = svg.generate(&barcode.encode()[..]).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))?;
23        Ok(data)
24    }
25}
26
27impl Default for BarcodeTool {
28    fn default() -> Self { Self::new() }
29}