Skip to main content

Crate zpl_builder

Crate zpl_builder 

Source
Expand description

A library for building ZPL II label strings to be sent to Zebra thermal printers.

§Overview

ZPL (Zebra Programming Language) is a label description language interpreted by Zebra printers. This library lets you build valid ZPL strings in Rust without constructing raw strings by hand. All field values are validated against the ZPL II specification at construction time — an invalid value returns a ZplError rather than silently producing a malformed command.

§Quick start

use zpl_builder::{LabelBuilder, BarcodeType, Color, Orientation};

let zpl = LabelBuilder::new()
    .set_home_position(10, 10).unwrap()
    .add_text("Hello, printer!", 50, 50, 'A', 30, Orientation::Normal).unwrap()
    .add_graphical_box(10, 10, 500, 300, 3, Color::Black, 0).unwrap()
    .add_barcode(BarcodeType::Code128, "ABC-123", 50, 120, 2, 2.5, 80, Orientation::Normal).unwrap()
    .build();

assert!(zpl.starts_with("^XA"));
assert!(zpl.ends_with("^XZ"));

§Elements

The builder supports the following ZPL elements:

MethodZPL commandDescription
LabelBuilder::add_text^A / ^FDText field
LabelBuilder::add_barcode^BY / ^B*Barcode (27 types)
LabelBuilder::add_graphical_box^GBRectangle
LabelBuilder::add_graphical_circle^GCCircle
LabelBuilder::add_graphical_ellipse^GEEllipse
LabelBuilder::add_graphical_diagonal_line^GDDiagonal line

§Error handling

Every builder method returns Result<LabelBuilder, ZplError>. This means you can chain calls with ? in a function that returns Result:

use zpl_builder::{LabelBuilder, Color, Orientation, ZplError};

fn build_shipping_label(tracking: &str) -> Result<String, ZplError> {
    Ok(LabelBuilder::new()
        .set_home_position(0, 0)?
        .add_text("SHIP TO:", 10, 10, 'B', 20, Orientation::Normal)?
        .add_graphical_box(5, 5, 400, 200, 2, Color::Black, 0)?
        .build())
}

Or with .unwrap() / .expect() when the values are known to be valid:

use zpl_builder::{LabelBuilder, Orientation};

let zpl = LabelBuilder::new()
    .add_text("Fixed label", 10, 10, 'A', 20, Orientation::Normal).unwrap()
    .build();

See ZplError for the full list of validation errors and their ranges.

Structs§

LabelBuilder
Builder for ZPL II label strings.

Enums§

BarcodeType
Barcode symbology.
Color
Line and fill color for graphical elements.
DiagonalOrientation
Orientation of a diagonal line.
Orientation
Orientation of a text or barcode field.
ZplError
Errors that can occur when building a ZPL label.