printers/common/base/
printer.rs1
2use std::fmt::{Debug, Error, Formatter};
3
4use super::job::PrinterJob;
5use crate::common::traits::platform::{PlatformActions, PlatformPrinterGetters};
6
7#[derive(Debug, Clone)]
8pub enum PrinterState {
9 READY,
10 PAUSED,
11 PRINTING,
12 UNKNOWN,
13}
14
15pub struct Printer {
19 pub name: String,
23
24 pub system_name: String,
28
29 pub driver_name: String,
33
34 pub uri: String,
38
39 pub port_name: String,
43
44 pub processor: String,
48
49 pub data_type: String,
53
54 pub description: String,
58
59 pub location: String,
63
64 pub is_default: bool,
68
69 pub is_shared: bool,
73
74 pub state: PrinterState,
78}
79
80impl Debug for Printer {
81 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
82 write!(
83 fmt,
84 "Printer {{
85 \r name: {:?},
86 \r state: {:?},
87 \r system_name: {:?},
88 \r is_default: {:?},
89 \r uri: {:?},
90 \r port_name: {:?},
91 \r is_shared: {:?},
92 \r location: {:?},
93 \r driver_name: {:?}
94 \r processor: {:?}
95 \r data_type: {:?}
96 \r description: {:?}
97 \r}}",
98 self.name,
99 self.state,
100 self.system_name,
101 self.is_default,
102 self.uri,
103 self.port_name,
104 self.is_shared,
105 self.location,
106 self.driver_name,
107 self.processor,
108 self.data_type,
109 self.description,
110 )
111 }
112}
113
114impl Clone for Printer {
115 fn clone(&self) -> Printer {
116 return Printer {
117 name: self.name.clone(),
118 state: self.state.clone(),
119 uri: self.uri.clone(),
120 location: self.location.clone(),
121 port_name: self.port_name.clone(),
122 is_default: self.is_default.clone(),
123 system_name: self.system_name.clone(),
124 driver_name: self.driver_name.clone(),
125 is_shared: self.is_shared.clone(),
126 data_type: self.data_type.clone(),
127 description: self.description.clone(),
128 processor: self.processor.clone(),
129 };
130 }
131}
132
133impl Printer {
134 pub(crate) fn from_platform_printer_getters(platform_printer: &dyn PlatformPrinterGetters) -> Printer {
135 let printer = Printer {
136 name: platform_printer.get_name(),
137 system_name: platform_printer.get_system_name(),
138 driver_name: platform_printer.get_marker_and_model(),
139 location: platform_printer.get_location(),
140 uri: platform_printer.get_uri(),
141 port_name: platform_printer.get_port_name(),
142 is_default: platform_printer.get_is_default(),
143 is_shared: platform_printer.get_is_shared(),
144 data_type: platform_printer.get_data_type(),
145 processor: platform_printer.get_processor(),
146 description: platform_printer.get_description(),
147 state: PrinterState::from_platform_state(platform_printer.get_state().as_str()),
148 };
149
150 return printer;
151 }
152
153 pub fn print(&self, buffer: &[u8], job_name: Option<&str>) -> Result<(), &'static str> {
157 return crate::Platform::print(self.system_name.as_str(), buffer, job_name);
158 }
159
160 pub fn print_file(&self, file_path: &str, job_name: Option<&str>) -> Result<(), &'static str> {
164 return crate::Platform::print_file(self.system_name.as_str(), file_path, job_name);
165 }
166
167 pub fn get_active_jobs(&self) -> Vec<PrinterJob> {
171 return crate::Platform::get_printer_jobs(self.system_name.as_str(), true);
172 }
173
174 pub fn get_job_history(&self) -> Vec<PrinterJob> {
178 return crate::Platform::get_printer_jobs(self.system_name.as_str(), false);
179 }
180
181}
182
183
184impl PrinterState {
185 pub(crate) fn from_platform_state(platform_state: &str) -> Self {
186 return crate::Platform::parse_printer_state(platform_state);
187 }
188}
189