43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
use crate::escpos::{errors::EscPosError, job::Job};
|
|
use std::io::Write;
|
|
|
|
pub struct Printer {
|
|
pub queue: Vec<Job>,
|
|
pub max_width: u16,
|
|
}
|
|
impl Printer {
|
|
pub fn new(max_width: u16) -> Self {
|
|
Printer {
|
|
queue: Vec::new(),
|
|
max_width: max_width,
|
|
}
|
|
}
|
|
|
|
pub fn new_job(&mut self) -> Result<&mut Job, EscPosError> {
|
|
self.queue.push(Job::new(self.max_width));
|
|
self.queue.last_mut().ok_or(EscPosError::InvalidQueueIndex)
|
|
}
|
|
|
|
fn extract_job(&mut self) -> Result<Job, EscPosError> {
|
|
self.queue
|
|
.extract_if(.., |j| j.ready)
|
|
.next()
|
|
.ok_or(EscPosError::EmptyQueue)
|
|
}
|
|
pub fn print_job(&mut self, writer: &mut impl Write) -> Result<(), EscPosError> {
|
|
let mut job = self.extract_job()?;
|
|
job.write_feed(Some(2));
|
|
|
|
writer.write(&job.content).unwrap();
|
|
Ok(())
|
|
}
|
|
pub fn export_job(&mut self) -> Result<Vec<u8>, EscPosError> {
|
|
let mut job = self.extract_job()?;
|
|
job.write_feed(Some(2));
|
|
|
|
let mut out = Vec::with_capacity(2 + job.content.len());
|
|
out.extend(job.content);
|
|
|
|
Ok(out)
|
|
}
|
|
}
|