Skip to main content

marlon_printers/
printer.rs

1use crate::shared::interface::PlatformPrinterGetters;
2
3/**
4 * Enum of the Printer state
5 */
6#[derive(Debug, Clone)]
7pub enum PrinterState {
8
9    /**
10     * The printer is able to receive jobs (also idle)
11     */
12    READY,
13
14    /**
15     * The printer is not accepting jobs (also stopped)
16     */
17    PAUSED,
18
19    /**
20     * The printer is now printing an document (also processing)
21     */
22    PRINTING,
23
24    /**
25     * All other status like error, resources, manual intervention, etc...
26     */
27    UNKNOWN,
28
29}
30
31
32/**
33 * Printer is a struct to representation the system printer
34 * They has an ID composed by your system_name and has printing method to print directly
35 */
36pub struct Printer {
37    /**
38     * Visual reference of system printer name
39     */
40    pub name: String,
41
42    /**
43     * Name of Printer exactly as on system
44     */
45    pub system_name: String,
46
47    /**
48     * Name of the Printer driver
49     */
50    pub driver_name: String,
51
52    /**
53     * Uri of Print (default is empty string)
54     */
55    pub uri: String,
56
57    /**
58     * Location definition of printer (default is empty string)
59     */
60    pub location: String,
61
62    /**
63     * Definition if the printer is the default printer
64     */
65    pub is_default: bool,
66
67    /**
68     * Definition if the printer is shared
69     */
70    pub is_shared: bool,
71
72    /**
73     * The state of the printer
74     */
75    pub state: PrinterState,
76
77}
78
79impl std::fmt::Debug for Printer {
80    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
81        write!(
82            fmt,
83            "Printer {{
84                \r  name: {:?},
85                \r  state: {:?},
86                \r  system_name: {:?},
87                \r  is_default: {:?},
88                \r  uri: {:?},
89                \r  is_shared: {:?},
90                \r  location: {:?},
91                \r  driver_name: {:?}
92            \r}}",
93            self.name,
94            self.state,
95            self.system_name,
96            self.is_default,
97            self.uri,
98            self.is_shared,
99            self.location,
100            self.driver_name
101        )
102    }
103}
104
105impl Clone for Printer {
106    fn clone(&self) -> Printer {
107        return Printer {
108            name: self.name.clone(),
109            state: self.state.clone(),
110            uri: self.uri.clone(),
111            location: self.location.clone(),
112            is_default: self.is_default.clone(),
113            system_name: self.system_name.clone(),
114            driver_name: self.driver_name.clone(),
115            is_shared: self.is_shared.clone(),
116        };
117    }
118}
119
120impl Printer {
121
122    pub fn from_platform_printer_getters(platform_printer: & dyn PlatformPrinterGetters, state: PrinterState) -> Printer {
123        let printer = Printer {
124            name: platform_printer.get_name(),
125            system_name: platform_printer.get_system_name(),
126            driver_name: platform_printer.get_marker_and_model(),
127            location: platform_printer.get_location(),
128            state,
129            uri: platform_printer.get_uri(),
130            is_default: platform_printer.get_is_default(),
131            is_shared: platform_printer.get_is_shared(),
132        };
133
134        return printer;
135    }
136
137    /**
138     * Print bytes with self printer instance
139     */
140    pub fn print(&self, buffer: &[u8], job_name: Option<&str>) -> Result<bool, String> {
141        return crate::print(&self.system_name, buffer, job_name);
142    }
143
144    /**
145     * Print specific file with self printer instance
146     */
147    pub fn print_file(&self, file_path: &str, job_name: Option<&str>) -> Result<bool, String> {
148        return crate::print_file(&self.system_name, file_path, job_name);
149    }
150}