Skip to main content

thermoprint/
lib.rs

1/*!
2# thermoprint
3
4A fluent, correct ESC/POS receipt builder for thermal printers.
5
6- **Native** (Rust / Tauri / CLI): outputs `Vec<u8>` you send to any printer
7- **WASM / npm**: outputs `Uint8Array` you pass to WebUSB, WebSerial, or Node.js
8
9## Quickstart (native)
10
11```rust
12use thermoprint::{ReceiptBuilder, PrintWidth};
13use rust_decimal::prelude::*;
14
15let bytes = ReceiptBuilder::new(PrintWidth::Mm80)
16    .init()
17    .align_center()
18    .bold(true).double_size(true)
19    .text_line("MA BOUTIQUE")
20    .bold(false).normal_size()
21    .text_line("Tel: +221 77 000 00 00")
22    .divider('=')
23    .align_left()
24    .item("Polo shirt", 2, dec!(15_000), None)
25    .item("Jean Levis 501", 1, dec!(25_000), Some(dec!(2_000)))
26    .divider('-')
27    .subtotal_ht(dec!(53_000))
28    .taxes(&[thermoprint::TaxEntry::new("TVA 18%", dec!(9_540), true)])
29    .total(dec!(62_540))
30    .received(dec!(70_000))
31    .change(dec!(7_460))
32    .divider('=')
33    .barcode_code128("ORD-2024-001")
34    .feed(3)
35    .cut()
36    .build();
37
38// `bytes` is Vec<u8> — send it to your printer however you like
39```
40
41## WASM / TypeScript
42
43```typescript
44import init, { ReceiptBuilder, PrintWidth } from 'thermoprint';
45await init();
46
47const bytes = new ReceiptBuilder(PrintWidth.Mm80)
48  .init()
49  .alignCenter()
50  .bold(true).doubleSize(true)
51  .textLine("MA BOUTIQUE")
52  .bold(false).normalSize()
53  .divider("=")
54  .item("Polo shirt", 2, "15000", null)
55  .total("62540")
56  .barcodeCode128("ORD-2024-001")
57  .cut()
58  .build(); // → Uint8Array
59```
60*/
61
62#![forbid(unsafe_code)]
63#![warn(missing_docs)]
64
65/// Fluent receipt builder API.
66pub mod builder;
67/// Raw ESC/POS command byte sequences.
68pub mod commands;
69/// CP-858 text encoding and layout helpers.
70pub mod encoding;
71/// Error types.
72pub mod error;
73/// Shared domain types (alignment, print width, tax entries).
74pub mod types;
75/// Internationalisation — receipt label translations.
76pub mod i18n;
77/// JSON template engine for receipt generation.
78pub mod template;
79/// Image dithering — pure Rust, works in native and WASM.
80pub mod dither;
81
82/// Image rasterisation (native builds only).
83#[cfg(feature = "native")]
84pub mod image;
85
86// Convenient top-level re-exports
87pub use builder::ReceiptBuilder;
88pub use dither::{dither_rgba, DitherMethod};
89pub use error::ThermoprintError;
90pub use i18n::{Language, ReceiptLabels};
91pub use template::{render_json, ReceiptTemplate};
92pub use types::{Align, PrintWidth, TaxEntry};
93
94// ── WASM public surface ───────────────────────────────────────────────────────
95#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
96pub use crate::builder::wasm::WasmReceiptBuilder;