pub mod exporters;
pub mod generators;
pub mod readers;
pub mod types;
#[cfg(feature = "python")]
pub mod python;
pub use exporters::*;
pub use generators::*;
pub use types::*;
use anyhow::Result as AnyhowResult;
pub fn generate(
barcode_type: BarcodeType,
data: &str,
format: ExportFormat,
) -> AnyhowResult<Vec<u8>> {
let barcode = match barcode_type {
BarcodeType::QRCode => generators::qr::generate_qr(data)?,
BarcodeType::EAN13 => generators::ean13::generate_ean13(data)?,
BarcodeType::UPCA => generators::upc::generate_upc_a(data)?,
BarcodeType::Code128 => generators::code128::generate_code128(data)?,
_ => return Err(anyhow::anyhow!("Barcode type not yet implemented")),
};
match format {
ExportFormat::SVG => Ok(exporters::svg::export_svg(&barcode)?),
ExportFormat::PNG => Ok(exporters::png::export_png(&barcode)?),
ExportFormat::PDF => Err(anyhow::anyhow!(
"PDF export not yet implemented - coming in Phase 2"
)),
}
}
pub fn generate_to_file(
barcode_type: BarcodeType,
data: &str,
output_path: &str,
) -> AnyhowResult<()> {
let format = ExportFormat::from_extension(output_path)?;
let barcode_data = generate(barcode_type, data, format)?;
std::fs::write(output_path, barcode_data)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_qr_generation() {
let result = generate(BarcodeType::QRCode, "test", ExportFormat::SVG);
assert!(result.is_ok());
}
#[test]
fn test_invalid_barcode_type() {
let result = generate(BarcodeType::DataMatrix, "test", ExportFormat::SVG);
assert!(result.is_err());
}
}