ipp_printer_app/
printer.rs1use std::sync::Arc;
4
5use parking_lot::RwLock;
6
7use crate::flags::PrinterReason;
8
9#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
13#[allow(missing_docs)]
14pub struct PrinterConfig {
15 pub name: String,
16 pub driver_name: String,
17 pub make_and_model: String,
18 pub device_id: String,
19 pub device_uri: String,
20 pub dpi: i32,
21 pub printhead_width_dots: u32,
22 pub media_names: Vec<String>,
23 pub media_sizes: Vec<[i32; 2]>,
24 pub darkness: i32,
26}
27
28impl PrinterConfig {
29 pub fn printer_uri(&self, host: &str, port: u16) -> String {
33 let h = if host == "0.0.0.0" || host == "::" || host.is_empty() {
34 "localhost"
35 } else {
36 host
37 };
38 format!("ipp://{h}:{port}/ipp/print/{}", self.name)
39 }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44#[repr(u32)]
45#[allow(missing_docs)]
46pub enum IppPrinterState {
47 Idle = 3,
48 Processing = 4,
49 Stopped = 5,
50}
51
52#[derive(Debug, Clone)]
54#[allow(missing_docs)]
55pub struct PrinterRecord {
56 pub config: PrinterConfig,
57 pub state: IppPrinterState,
58 pub reasons: PrinterReason,
59 pub uuid: String,
60}
61
62impl PrinterRecord {
63 pub fn new(config: PrinterConfig) -> Self {
65 Self {
66 uuid: uuid::Uuid::new_v4().to_string(),
67 state: IppPrinterState::Idle,
68 reasons: PrinterReason::empty(),
69 config,
70 }
71 }
72}
73
74pub struct PrinterHandle<'a> {
79 pub record: &'a PrinterRecord,
81}
82
83impl<'a> PrinterHandle<'a> {
84 pub fn driver_name(&self) -> &str {
87 &self.record.config.driver_name
88 }
89
90 pub fn darkness(&self) -> i32 {
92 self.record.config.darkness
93 }
94
95 pub fn printhead_width_dots(&self) -> u32 {
97 self.record.config.printhead_width_dots
98 }
99}
100
101pub type PrinterRegistry = Arc<RwLock<Vec<PrinterRecord>>>;