zpl_toolchain_print_client 0.1.5

Print client for sending ZPL to Zebra and ZPL-compatible printers over TCP, USB, and serial
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! ZPL Print Client — send ZPL to Zebra and ZPL-compatible printers.
//!
//! Supports TCP (port 9100), USB, and serial/Bluetooth SPP transports.
//! The core API is synchronous (`std::net`), with no async runtime required.
mod addr;
mod config;
mod error;
mod frame;
mod retry;
#[cfg(feature = "serial")]
mod serial;
mod status;
#[cfg(feature = "tcp")]
mod tcp;
#[cfg(feature = "usb")]
mod usb;

#[cfg(feature = "tcp")]
pub use addr::resolve_printer_addr;
pub use config::{BatchOptions, PrinterConfig, PrinterTimeouts, RetryConfig};
pub use error::{PrintError, PrinterErrorKind};
pub use frame::{expected_frame_count, read_frames};
pub use retry::{ReconnectRetryPrinter, RetryPrinter};
#[cfg(feature = "serial")]
pub use serial::{
    SerialDataBits, SerialFlowControl, SerialParity, SerialPrinter, SerialSettings, SerialStopBits,
};
pub use status::{HostStatus, PrintMode, PrinterInfo};
#[cfg(feature = "tcp")]
pub use tcp::TcpPrinter;
#[cfg(feature = "usb")]
pub use usb::UsbPrinter;

use std::ops::ControlFlow;
use std::time::{Duration, Instant};

// ── Traits ──────────────────────────────────────────────────────────────

/// Send data to a printer. All transports implement this.
pub trait Printer: Send {
    /// Send raw bytes to the printer.
    fn send_raw(&mut self, data: &[u8]) -> Result<(), PrintError>;

    /// Send a ZPL string to the printer (convenience wrapper over `send_raw`).
    fn send_zpl(&mut self, zpl: &str) -> Result<(), PrintError> {
        self.send_raw(zpl.as_bytes())
    }
}

/// Query printer status. Only bidirectional transports implement this.
pub trait StatusQuery: Printer {
    /// Send a command and read the raw STX/ETX framed response.
    fn query_raw(&mut self, cmd: &[u8]) -> Result<Vec<Vec<u8>>, PrintError>;

    /// Query printer status via `~HS` and parse the response.
    fn query_status(&mut self) -> Result<HostStatus, PrintError> {
        let frames = self.query_raw(b"~HS")?;
        HostStatus::parse(&frames)
    }

    /// Query printer info via `~HI` and parse the response.
    fn query_info(&mut self) -> Result<PrinterInfo, PrintError> {
        let frames = self.query_raw(b"~HI")?;
        PrinterInfo::parse(&frames)
    }
}

/// A printer that can re-establish its connection after a failure.
///
/// Implementing this trait enables [`ReconnectRetryPrinter`] to automatically
/// reconnect between retry attempts, making retries effective even after a
/// full connection drop (e.g., TCP disconnect, USB unplug/replug).
pub trait Reconnectable {
    /// Re-establish the connection.
    ///
    /// Implementations should close the old connection (if any) and open a
    /// fresh one. Errors during reconnection are non-fatal for the retry
    /// loop — the next operation attempt may still succeed or produce a
    /// more specific error.
    fn reconnect(&mut self) -> Result<(), PrintError>;
}

// ── Batch helpers ───────────────────────────────────────────────────────

/// Progress report for batch printing.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BatchProgress {
    /// How many labels have been sent so far.
    pub sent: usize,
    /// Total labels in the batch.
    pub total: usize,
    /// Latest printer status (if polling was enabled).
    pub status: Option<HostStatus>,
}

/// Result of a batch print operation.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BatchResult {
    /// Number of labels successfully sent to the printer.
    pub sent: usize,
    /// Total labels in the batch.
    pub total: usize,
}

/// Send a batch of labels with optional progress reporting.
///
/// The `on_progress` callback receives a `BatchProgress` and can return
/// `ControlFlow::Break(())` to abort the batch early.
pub fn send_batch<P, F>(
    printer: &mut P,
    labels: &[impl AsRef<[u8]>],
    mut on_progress: F,
) -> Result<BatchResult, PrintError>
where
    P: Printer,
    F: FnMut(BatchProgress) -> ControlFlow<(), ()>,
{
    let total = labels.len();
    for (i, label) in labels.iter().enumerate() {
        printer.send_raw(label.as_ref())?;

        let status = None; // Status polling requires send_batch_with_status()

        let progress = BatchProgress {
            sent: i + 1,
            total,
            status,
        };

        if let ControlFlow::Break(()) = on_progress(progress) {
            return Ok(BatchResult { sent: i + 1, total });
        }
    }

    Ok(BatchResult { sent: total, total })
}

/// Send a batch of labels with status polling (requires bidirectional transport).
pub fn send_batch_with_status<P, F>(
    printer: &mut P,
    labels: &[impl AsRef<[u8]>],
    opts: &BatchOptions,
    mut on_progress: F,
) -> Result<BatchResult, PrintError>
where
    P: StatusQuery,
    F: FnMut(BatchProgress) -> ControlFlow<(), ()>,
{
    let total = labels.len();
    for (i, label) in labels.iter().enumerate() {
        printer.send_raw(label.as_ref())?;

        let status = if let Some(interval) = opts.status_interval {
            if (i + 1) % interval.get() == 0 {
                printer.query_status().ok()
            } else {
                None
            }
        } else {
            None
        };

        let progress = BatchProgress {
            sent: i + 1,
            total,
            status,
        };

        if let ControlFlow::Break(()) = on_progress(progress) {
            return Ok(BatchResult { sent: i + 1, total });
        }
    }

    Ok(BatchResult { sent: total, total })
}

// ── Completion polling ─────────────────────────────────────────────────

/// Poll `~HS` until the printer reports no formats in buffer and no
/// labels remaining, or until the timeout elapses.
///
/// This is useful after sending a batch of labels to wait until the
/// physical printer has finished printing all of them.
///
/// Works with any transport that implements [`StatusQuery`].
pub fn wait_for_completion<S: StatusQuery>(
    printer: &mut S,
    poll_interval: Duration,
    timeout: Duration,
) -> Result<(), PrintError> {
    let now = Instant::now();
    let deadline = now
        .checked_add(timeout)
        .unwrap_or_else(|| now + Duration::from_secs(86400));

    loop {
        let status = printer.query_status()?;

        if status.formats_in_buffer == 0 && status.labels_remaining == 0 {
            return Ok(());
        }

        if Instant::now() >= deadline {
            return Err(PrintError::CompletionTimeout {
                formats_in_buffer: status.formats_in_buffer,
                labels_remaining: status.labels_remaining,
            });
        }

        std::thread::sleep(poll_interval);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ops::ControlFlow;

    struct MockBatchPrinter {
        sent: Vec<Vec<u8>>,
        fail_on: Option<usize>,
    }

    impl Printer for MockBatchPrinter {
        fn send_raw(&mut self, data: &[u8]) -> Result<(), PrintError> {
            if Some(self.sent.len()) == self.fail_on {
                return Err(PrintError::WriteFailed(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "mock error",
                )));
            }
            self.sent.push(data.to_vec());
            Ok(())
        }
    }

    #[test]
    fn batch_happy_path() {
        let mut printer = MockBatchPrinter {
            sent: Vec::new(),
            fail_on: None,
        };
        let labels = vec!["^XA^FDOne^FS^XZ", "^XA^FDTwo^FS^XZ", "^XA^FDThree^FS^XZ"];
        let result = send_batch(&mut printer, &labels, |_| ControlFlow::Continue(())).unwrap();
        assert_eq!(result.sent, 3);
        assert_eq!(result.total, 3);
        assert_eq!(printer.sent.len(), 3);
    }

    #[test]
    fn batch_empty() {
        let mut printer = MockBatchPrinter {
            sent: Vec::new(),
            fail_on: None,
        };
        let labels: Vec<&str> = vec![];
        let result = send_batch(&mut printer, &labels, |_| ControlFlow::Continue(())).unwrap();
        assert_eq!(result.sent, 0);
        assert_eq!(result.total, 0);
    }

    #[test]
    fn batch_early_abort() {
        let mut printer = MockBatchPrinter {
            sent: Vec::new(),
            fail_on: None,
        };
        let labels = vec!["one", "two", "three", "four", "five"];
        let result = send_batch(&mut printer, &labels, |progress| {
            if progress.sent >= 2 {
                ControlFlow::Break(())
            } else {
                ControlFlow::Continue(())
            }
        })
        .unwrap();
        assert_eq!(result.sent, 2);
        assert_eq!(result.total, 5);
    }

    #[test]
    fn batch_error_propagates() {
        let mut printer = MockBatchPrinter {
            sent: Vec::new(),
            fail_on: Some(1),
        };
        let labels = vec!["ok", "fail", "never"];
        let result = send_batch(&mut printer, &labels, |_| ControlFlow::Continue(()));
        assert!(result.is_err());
        assert_eq!(printer.sent.len(), 1);
    }

    // ── MockStatusPrinter (for send_batch_with_status tests) ─────────

    struct MockStatusPrinter {
        sent: Vec<Vec<u8>>,
        fail_on: Option<usize>,
        status_queries: usize,
    }

    impl Printer for MockStatusPrinter {
        fn send_raw(&mut self, data: &[u8]) -> Result<(), PrintError> {
            if Some(self.sent.len()) == self.fail_on {
                return Err(PrintError::WriteFailed(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "mock error",
                )));
            }
            self.sent.push(data.to_vec());
            Ok(())
        }
    }

    impl StatusQuery for MockStatusPrinter {
        fn query_raw(&mut self, _cmd: &[u8]) -> Result<Vec<Vec<u8>>, PrintError> {
            self.status_queries += 1;
            // Return a valid ~HS response (3 frames)
            Ok(vec![
                b"030,0,0,1245,000,0,0,0,000,0,0,0".to_vec(),
                b"000,0,0,0,0,2,0,0,00000000,0,000".to_vec(),
                b"1234,0".to_vec(),
            ])
        }
    }

    #[test]
    fn batch_with_status_happy_path() {
        use std::num::NonZeroUsize;

        let mut printer = MockStatusPrinter {
            sent: Vec::new(),
            fail_on: None,
            status_queries: 0,
        };
        let labels: Vec<&str> = vec!["L1", "L2", "L3", "L4", "L5"];
        let opts = BatchOptions {
            status_interval: Some(NonZeroUsize::new(2).unwrap()),
            ..BatchOptions::default()
        };

        let mut progresses = Vec::new();
        let result = send_batch_with_status(&mut printer, &labels, &opts, |p| {
            progresses.push(p.clone());
            ControlFlow::Continue(())
        })
        .unwrap();

        assert_eq!(result.sent, 5);
        assert_eq!(result.total, 5);
        assert_eq!(printer.sent.len(), 5);
        // Status polled after label 2 and 4 (every 2 labels)
        assert_eq!(printer.status_queries, 2);

        // Labels at sent=2 and sent=4 should have status
        assert!(progresses[1].status.is_some()); // sent=2
        assert!(progresses[3].status.is_some()); // sent=4

        // Labels at sent=1, 3, 5 should NOT have status
        assert!(progresses[0].status.is_none()); // sent=1
        assert!(progresses[2].status.is_none()); // sent=3
        assert!(progresses[4].status.is_none()); // sent=5
    }

    #[test]
    fn batch_with_status_no_interval() {
        let mut printer = MockStatusPrinter {
            sent: Vec::new(),
            fail_on: None,
            status_queries: 0,
        };
        let labels: Vec<&str> = vec!["L1", "L2", "L3"];
        let opts = BatchOptions {
            status_interval: None,
            ..BatchOptions::default()
        };

        let result =
            send_batch_with_status(&mut printer, &labels, &opts, |_| ControlFlow::Continue(()))
                .unwrap();

        assert_eq!(result.sent, 3);
        assert_eq!(result.total, 3);
        assert_eq!(printer.status_queries, 0);
    }

    #[test]
    fn batch_with_status_early_abort() {
        use std::num::NonZeroUsize;

        let mut printer = MockStatusPrinter {
            sent: Vec::new(),
            fail_on: None,
            status_queries: 0,
        };
        let labels: Vec<&str> = vec!["L1", "L2", "L3", "L4", "L5"];
        let opts = BatchOptions {
            status_interval: Some(NonZeroUsize::new(2).unwrap()),
            ..BatchOptions::default()
        };

        let result = send_batch_with_status(&mut printer, &labels, &opts, |p| {
            if p.sent >= 3 {
                ControlFlow::Break(())
            } else {
                ControlFlow::Continue(())
            }
        })
        .unwrap();

        assert_eq!(result.sent, 3);
        assert_eq!(result.total, 5);
        assert_eq!(printer.sent.len(), 3);
        assert_eq!(printer.status_queries, 1); // polled after label 2 only
    }

    #[test]
    fn batch_with_status_error_propagates() {
        use std::num::NonZeroUsize;

        let mut printer = MockStatusPrinter {
            sent: Vec::new(),
            fail_on: Some(1), // fail on second label
            status_queries: 0,
        };
        let labels: Vec<&str> = vec!["L1", "L2", "L3"];
        let opts = BatchOptions {
            status_interval: Some(NonZeroUsize::new(1).unwrap()),
            ..BatchOptions::default()
        };

        let result =
            send_batch_with_status(&mut printer, &labels, &opts, |_| ControlFlow::Continue(()));

        assert!(result.is_err());
        assert_eq!(printer.sent.len(), 1);
    }

    // ── MockCompletionPrinter (for wait_for_completion tests) ────────

    struct MockCompletionPrinter {
        polls: usize,
        complete_after: usize,
    }

    impl Printer for MockCompletionPrinter {
        fn send_raw(&mut self, _data: &[u8]) -> Result<(), PrintError> {
            Ok(())
        }
    }

    impl StatusQuery for MockCompletionPrinter {
        fn query_raw(&mut self, _cmd: &[u8]) -> Result<Vec<Vec<u8>>, PrintError> {
            self.polls += 1;
            let remaining = if self.polls >= self.complete_after {
                0
            } else {
                5
            };
            Ok(vec![
                b"030,0,0,1245,000,0,0,0,000,0,0,0".to_vec(),
                format!("000,0,0,0,0,2,0,{remaining},00000000,0,000").into_bytes(),
                b"1234,0".to_vec(),
            ])
        }
    }

    #[test]
    fn wait_for_completion_immediate() {
        let mut printer = MockCompletionPrinter {
            polls: 0,
            complete_after: 1, // complete on first poll
        };
        let result = wait_for_completion(
            &mut printer,
            Duration::from_millis(10),
            Duration::from_secs(5),
        );
        assert!(result.is_ok());
        assert_eq!(printer.polls, 1);
    }

    #[test]
    fn wait_for_completion_after_polls() {
        let mut printer = MockCompletionPrinter {
            polls: 0,
            complete_after: 3, // complete on third poll
        };
        let result = wait_for_completion(
            &mut printer,
            Duration::from_millis(10), // short interval for test speed
            Duration::from_secs(5),
        );
        assert!(result.is_ok());
        assert_eq!(printer.polls, 3);
    }

    #[test]
    fn wait_for_completion_timeout() {
        let mut printer = MockCompletionPrinter {
            polls: 0,
            complete_after: 999, // never completes
        };
        let result = wait_for_completion(
            &mut printer,
            Duration::from_millis(1),
            Duration::from_millis(10),
        );
        match result {
            Err(PrintError::CompletionTimeout {
                formats_in_buffer,
                labels_remaining,
            }) => {
                assert_eq!(formats_in_buffer, 0);
                assert_eq!(labels_remaining, 5);
            }
            other => panic!("expected CompletionTimeout, got {:?}", other),
        }
    }

    // ── MockFormatsInBufferPrinter (formats_in_buffer blocks completion) ──

    struct MockFormatsInBufferPrinter {
        polls: usize,
    }

    impl Printer for MockFormatsInBufferPrinter {
        fn send_raw(&mut self, _data: &[u8]) -> Result<(), PrintError> {
            Ok(())
        }
    }

    impl StatusQuery for MockFormatsInBufferPrinter {
        fn query_raw(&mut self, _cmd: &[u8]) -> Result<Vec<Vec<u8>>, PrintError> {
            self.polls += 1;
            // labels_remaining always 0, but formats_in_buffer > 0 until poll 3
            let formats = if self.polls >= 3 { 0 } else { 2 };
            Ok(vec![
                format!("030,0,0,1245,{formats:03},0,0,0,000,0,0,0").into_bytes(),
                b"000,0,0,0,0,2,0,0,00000000,0,000".to_vec(),
                b"1234,0".to_vec(),
            ])
        }
    }

    #[test]
    fn wait_for_completion_waits_for_formats_in_buffer() {
        let mut printer = MockFormatsInBufferPrinter { polls: 0 };
        let result = wait_for_completion(
            &mut printer,
            Duration::from_millis(1),
            Duration::from_secs(5),
        );
        assert!(result.is_ok());
        assert!(
            printer.polls >= 3,
            "should have polled until formats cleared"
        );
    }
}