smart-package-tracker 0.1.0

Generate package tracking IDs and render them as Code 128 barcodes (PNG and SVG)
Documentation
//! Mint a tracking ID and write a shipping-label barcode as PNG and SVG.
//!
//! Run with: `cargo run --example label`

use smart_package_tracker::{
    Barcode, Checksum, Color, IdGenerator, Length, QuietZone, RenderOptions,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // A production-grade policy: 64 bits of entropy plus a check character.
    let generator = IdGenerator::builder()
        .prefix("PKG")
        .entropy_bits(64)
        .checksum(Checksum::Iso7064Mod37_36)
        .build()?;

    let id = generator.generate()?;
    println!("tracking id: {id}");

    let barcode = Barcode::code128(&id)?;

    // 300 dpi thermal label printer, 13 mil X-dimension.
    let options = RenderOptions::builder()
        .module_width(Length::Mils(13.0))
        .height(Length::Mm(25.0))
        .quiet_zone(QuietZone::Standard)
        .dpi(300)
        .colors(Color::BLACK, Color::WHITE)
        .human_readable(true)
        .build()?;

    let layout = options.layout(barcode.symbol())?;
    println!(
        "label size: {}x{} px ({:.1}x{:.1} mm at {} dpi)",
        layout.width_px,
        layout.height_px,
        Length::Px(f64::from(layout.width_px)).to_mm(options.dpi()),
        Length::Px(f64::from(layout.height_px)).to_mm(options.dpi()),
        options.dpi()
    );

    barcode.to_png_file("label.png", &options)?;
    barcode.to_svg_file("label.svg", &options)?;
    println!("wrote label.png and label.svg");

    // The barcode decodes back to the identifier it was built from.
    assert_eq!(barcode.decode()?, id.as_str());
    println!("round-trip verified");

    Ok(())
}