Skip to main content

tauri_plugin_thermoprint/
lib.rs

1//! # tauri-plugin-thermoprint
2//!
3//! Tauri v2 plugin for thermal receipt printing via serial ports.
4//!
5//! ## Setup
6//!
7//! **Rust side** (`src-tauri/src/main.rs`):
8//!
9//! ```rust,ignore
10//! fn main() {
11//!     tauri::Builder::default()
12//!         .plugin(tauri_plugin_thermoprint::init())
13//!         .run(tauri::generate_context!())
14//!         .expect("error running app");
15//! }
16//! ```
17//!
18//! **JavaScript side**:
19//!
20//! ```js
21//! import { invoke } from '@tauri-apps/api/core';
22//!
23//! // List available serial ports
24//! const ports = await invoke('plugin:thermoprint|list_ports');
25//!
26//! // Print ESC/POS bytes to a serial port
27//! await invoke('plugin:thermoprint|print_serial', {
28//!   port: '/dev/ttyUSB0',
29//!   baudRate: 9600,
30//!   data: Array.from(receiptBytes),
31//! });
32//!
33//! // Render a JSON template and print in one call
34//! await invoke('plugin:thermoprint|print_template', {
35//!   port: '/dev/ttyUSB0',
36//!   baudRate: 9600,
37//!   template: JSON.stringify({ width: "80mm", elements: [...] }),
38//! });
39//! ```
40
41use serde::{Deserialize, Serialize};
42use tauri::{
43    plugin::{Builder, TauriPlugin},
44    Runtime,
45};
46
47mod commands;
48
49/// Initialise the thermoprint plugin.
50///
51/// ```rust,ignore
52/// tauri::Builder::default()
53///     .plugin(tauri_plugin_thermoprint::init())
54/// ```
55pub fn init<R: Runtime>() -> TauriPlugin<R> {
56    Builder::new("thermoprint")
57        .invoke_handler(tauri::generate_handler![
58            commands::list_ports,
59            commands::print_serial,
60            commands::print_template,
61        ])
62        .build()
63}
64
65/// Information about an available serial port.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct PortInfo {
68    /// System port name (e.g. `/dev/ttyUSB0`, `COM3`).
69    pub name: String,
70    /// Port type description.
71    pub port_type: String,
72}