pub struct PrinterMonitor { /* private fields */ }Expand description
Printer monitoring and querying functionality
Implementations§
Source§impl PrinterMonitor
impl PrinterMonitor
Sourcepub async fn new() -> Result<Self>
pub async fn new() -> Result<Self>
Creates a new PrinterMonitor instance with the appropriate platform backend.
This function automatically selects and initializes the correct backend for the current platform (WMI for Windows, CUPS for Linux).
§Returns
Result<Self>- A new PrinterMonitor instance or an error if initialization fails
§Errors
PrinterError::PlatformNotSupported- If the current platform is not supportedPrinterError::WmiError- If WMI initialization fails on WindowsPrinterError::CupsError- If CUPS initialization fails on Linux
§Example
use printer_event_handler::PrinterMonitor;
#[tokio::main]
async fn main() {
let monitor = PrinterMonitor::new().await.unwrap();
}Sourcepub async fn list_printers(&self) -> Result<Vec<Printer>>
pub async fn list_printers(&self) -> Result<Vec<Printer>>
Retrieves a list of all printers available on the system.
This method queries the platform-specific printer service to get information about all installed and available printers.
§Returns
Result<Vec<Printer>>- A vector of all printers found on the system
§Errors
PrinterError::WmiError- If the WMI query fails on WindowsPrinterError::CupsError- If the CUPS query fails on LinuxPrinterError::IoError- If there are system I/O issues
§Example
use printer_event_handler::PrinterMonitor;
#[tokio::main]
async fn main() {
let monitor = PrinterMonitor::new().await.unwrap();
let printers = monitor.list_printers().await.unwrap();
for printer in printers {
println!("{}: {}", printer.name(), printer.status_description());
}
}Sourcepub async fn find_printer(&self, name: &str) -> Result<Option<Printer>>
pub async fn find_printer(&self, name: &str) -> Result<Option<Printer>>
Searches for a specific printer by name using case-insensitive matching.
This method searches through all available printers to find one with a name that matches the provided string (case-insensitive).
§Arguments
name- The name of the printer to search for
§Returns
Result<Option<Printer>>- The found printer or None if not found
§Errors
PrinterError::WmiError- If the WMI query fails on WindowsPrinterError::CupsError- If the CUPS query fails on LinuxPrinterError::IoError- If there are system I/O issues
§Example
use printer_event_handler::PrinterMonitor;
#[tokio::main]
async fn main() {
let monitor = PrinterMonitor::new().await.unwrap();
if let Some(printer) = monitor.find_printer("HP LaserJet").await.unwrap() {
println!("Found printer: {}", printer.name());
}
}Sourcepub async fn monitor_printer<F>(
&self,
printer_name: &str,
interval_secs: u64,
callback: F,
) -> Result<()>
pub async fn monitor_printer<F>( &self, printer_name: &str, interval_secs: u64, callback: F, ) -> Result<()>
Continuously monitors a specific printer for status changes.
This function runs indefinitely, polling the specified printer every interval_secs
seconds and calling the provided callback function whenever the printer’s status changes.
The callback receives both the current printer state and the previous state (if any).
§Arguments
printer_name- The name of the printer to monitorinterval_secs- Polling interval in secondscallback- Function called when printer status changes, receives (current, previous)
§Returns
Result<()>- Never returns Ok normally (runs indefinitely), only Err on failure
§Errors
PrinterError::PrinterNotFound- If the specified printer is not found initiallyPrinterError::WmiError- If WMI queries fail on WindowsPrinterError::CupsError- If CUPS queries fail on LinuxPrinterError::IoError- If there are system I/O issues
§Behavior
- If the printer disappears during monitoring, the callback is called with a synthetic “unknown” status to indicate the printer is no longer available
- The first check always triggers the callback to provide the initial status
- Subsequent calls only trigger the callback if the status actually changes
§Example
use printer_event_handler::PrinterMonitor;
#[tokio::main]
async fn main() {
let monitor = PrinterMonitor::new().await.unwrap();
monitor.monitor_printer("HP LaserJet", 30, |current, previous| {
if let Some(prev) = previous {
if prev != current {
println!("Status changed: {} -> {}",
prev.status_description(),
current.status_description());
}
} else {
println!("Initial status: {}", current.status_description());
}
}).await.unwrap();
}Sourcepub async fn printer_summary(&self) -> Result<HashMap<String, PrinterSummary>>
pub async fn printer_summary(&self) -> Result<HashMap<String, PrinterSummary>>
Retrieves a comprehensive summary of all printers and their current states.
This method provides a convenient way to get an overview of all printers in a structured format, useful for status dashboards or reports.
§Returns
Result<HashMap<String, PrinterSummary>>- Map of printer names to their summaries
§Errors
PrinterError::WmiError- If the WMI query fails on WindowsPrinterError::CupsError- If the CUPS query fails on LinuxPrinterError::IoError- If there are system I/O issues
§Example
use printer_event_handler::PrinterMonitor;
#[tokio::main]
async fn main() {
let monitor = PrinterMonitor::new().await.unwrap();
let summary = monitor.printer_summary().await.unwrap();
for (name, info) in summary {
println!("{}: {} ({})", name, info.status,
if info.has_error { "ERROR" } else { "OK" });
}
}