use bitflags::bitflags;
use std::{ffi::OsString, io::Result};
use super::{
util::{FunctionDir, Status},
Function, Handle,
};
pub const GADGET_GET_PRINTER_STATUS: u8 = 0x21;
pub const GADGET_SET_PRINTER_STATUS: u8 = 0x22;
bitflags! {
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct StatusFlags: u8 {
const NOT_ERROR = (1 << 3);
const SELECTED = (1 << 4);
const PAPER_EMPTY = (1 << 5);
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PrinterBuilder {
pub pnp_string: Option<String>,
pub qlen: Option<u8>,
}
impl PrinterBuilder {
pub fn build(self) -> (Printer, Handle) {
let dir = FunctionDir::new();
(Printer { dir: dir.clone() }, Handle::new(PrinterFunction { builder: self, dir }))
}
}
#[derive(Debug)]
struct PrinterFunction {
builder: PrinterBuilder,
dir: FunctionDir,
}
impl Function for PrinterFunction {
fn driver(&self) -> OsString {
"printer".into()
}
fn dir(&self) -> FunctionDir {
self.dir.clone()
}
fn register(&self) -> Result<()> {
if let Some(pnp_string) = &self.builder.pnp_string {
self.dir.write("pnp_string", pnp_string)?;
}
if let Some(qlen) = self.builder.qlen {
self.dir.write("q_len", qlen.to_string())?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct Printer {
dir: FunctionDir,
}
impl Printer {
pub fn builder() -> PrinterBuilder {
PrinterBuilder { pnp_string: None, qlen: None }
}
pub fn new(self) -> (Printer, Handle) {
Self::builder().build()
}
pub fn status(&self) -> Status {
self.dir.status()
}
}