Struct PrinterMonitor

Source
pub struct PrinterMonitor { /* private fields */ }
Expand description

Printer monitoring and querying functionality

Implementations§

Source§

impl PrinterMonitor

Source

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 supported
  • PrinterError::WmiError - If WMI initialization fails on Windows
  • PrinterError::CupsError - If CUPS initialization fails on Linux
§Example
use printer_event_handler::PrinterMonitor;

#[tokio::main]
async fn main() {
    let monitor = PrinterMonitor::new().await.unwrap();
}
Source

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 Windows
  • PrinterError::CupsError - If the CUPS query fails on Linux
  • PrinterError::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());
    }
}
Source

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 Windows
  • PrinterError::CupsError - If the CUPS query fails on Linux
  • PrinterError::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());
    }
}
Source

pub async fn monitor_printer<F>( &self, printer_name: &str, interval_secs: u64, callback: F, ) -> Result<()>
where F: FnMut(&Printer, Option<&Printer>) + Send,

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 monitor
  • interval_secs - Polling interval in seconds
  • callback - 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 initially
  • PrinterError::WmiError - If WMI queries fail on Windows
  • PrinterError::CupsError - If CUPS queries fail on Linux
  • PrinterError::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();
}
Source

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 Windows
  • PrinterError::CupsError - If the CUPS query fails on Linux
  • PrinterError::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" });
    }
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.