use serde::{Deserialize, Serialize};
use std::path::Path;
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BarcodeType {
EAN13,
UPCA,
Code128,
Code39,
ITF14,
Codabar,
QRCode,
DataMatrix,
PDF417,
Aztec,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExportFormat {
PNG,
SVG,
PDF,
}
impl ExportFormat {
pub fn from_extension(path: &str) -> Result<Self> {
let path = Path::new(path);
match path.extension().and_then(|ext| ext.to_str()) {
Some("png") => Ok(ExportFormat::PNG),
Some("svg") => Ok(ExportFormat::SVG),
Some("pdf") => Ok(ExportFormat::PDF),
_ => Err(QuickCodesError::UnsupportedFormat(
"Unsupported file extension. Use .png, .svg, or .pdf".to_string(),
)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum QRErrorCorrection {
Low, #[default]
Medium, Quartile, High, }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BarcodeConfig {
pub width: Option<u32>,
pub height: Option<u32>,
pub dpi: Option<u32>,
pub qr_config: QRConfig,
pub include_text: bool,
pub margin: u32,
}
impl Default for BarcodeConfig {
fn default() -> Self {
Self {
width: None,
height: None,
dpi: Some(300),
qr_config: QRConfig::default(),
include_text: true,
margin: 10,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QRConfig {
pub error_correction: QRErrorCorrection,
pub version: Option<u8>, }
impl Default for QRConfig {
fn default() -> Self {
Self {
error_correction: QRErrorCorrection::Medium,
version: None,
}
}
}
#[derive(Debug, Clone)]
pub struct Barcode {
pub barcode_type: BarcodeType,
pub data: String,
pub modules: BarcodeModules,
pub config: BarcodeConfig,
}
#[derive(Debug, Clone)]
pub enum BarcodeModules {
Linear(Vec<bool>),
Matrix(Vec<Vec<bool>>),
}
#[derive(Error, Debug)]
pub enum QuickCodesError {
#[error("Invalid data: {0}")]
InvalidData(String),
#[error("Unsupported barcode type: {0:?}")]
UnsupportedBarcodeType(BarcodeType),
#[error("Unsupported export format: {0}")]
UnsupportedFormat(String),
#[error("Generation error: {0}")]
GenerationError(String),
#[error("Export error: {0}")]
ExportError(String),
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Image processing error: {0}")]
ImageError(String),
}
pub type Result<T> = std::result::Result<T, QuickCodesError>;