printers/common/base/
printer.rs

1
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
15/**
16 * Printer is a struct to representation the system printer
17 */
18pub struct Printer {
19    /**
20     * Visual reference of system printer name
21     */
22    pub name: String,
23
24    /**
25     * Name of Printer exactly as on system
26     */
27    pub system_name: String,
28
29    /**
30     * Name of the Printer driver
31     */
32    pub driver_name: String,
33
34    /**
35     * Uri of printer (default is empty string)
36     */
37    pub uri: String,
38
39    /**
40     * Name of printer port (default is empty string)
41     */
42    pub port_name: String,
43
44    /**
45     * Name of printer port (default is empty string)
46     */
47    pub processor: String,
48
49    /**
50     * Name of printer port (default is RAW)
51     */
52    pub data_type: String,
53
54    /**
55     * Name of printer port (default is empty string)
56     */
57    pub description: String,
58
59    /**
60     * Location definition of printer (default is empty string)
61     */
62    pub location: String,
63
64    /**
65     * Definition if the printer is the default printer
66     */
67    pub is_default: bool,
68
69    /**
70     * Definition if the printer is shared
71     */
72    pub is_shared: bool,
73
74    /**
75     * The state of the printer
76     */
77    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    /**
154     * Print bytes with self printer instance
155     */
156    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    /**
161     * Print specific file with self printer instance
162     */
163    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    /**
168     * Return vec of active jobs of printer
169     */
170    pub fn get_active_jobs(&self) -> Vec<PrinterJob> {
171        return crate::Platform::get_printer_jobs(self.system_name.as_str(), true);
172    }
173
174    /**
175     * Return vec of a historic jobs of printer
176     */
177    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