-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiscover_printers.rs
More file actions
83 lines (67 loc) · 2.93 KB
/
discover_printers.rs
File metadata and controls
83 lines (67 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use cups_rs::{
get_all_destinations, get_default_destination, find_destinations,
Destinations, PRINTER_LOCAL, PRINTER_REMOTE, Result,
};
fn main() -> Result<()> {
println!("CUPS Printer Discovery & Management Example");
println!("===========================================");
// Method 1: Simple discovery (existing API)
let destinations = get_all_destinations()?;
println!("Found {} printer(s)", destinations.len());
if destinations.is_empty() {
println!(
"No printers found. Try: sudo lpadmin -p TestPDF -E -v file:///tmp/ -m everywhere"
);
return Ok(());
}
println!("\nBasic printer information:");
for (i, dest) in destinations.iter().enumerate() {
println!("\nPrinter #{}: {}", i + 1, dest.name);
println!(
" State: {} | Accepting jobs: {}",
dest.state(),
dest.is_accepting_jobs()
);
if let Some(info) = dest.info() {
println!(" Description: {}", info);
}
if let Some(location) = dest.location() {
println!(" Location: {}", location);
}
let reasons = dest.state_reasons();
if !reasons.is_empty() {
println!(" Issues: {}", reasons.join(", "));
}
}
// Method 2: Advanced management (new API)
println!("\n--- Advanced Destination Management ---");
let mut managed_destinations = Destinations::get_all()?;
println!("Managing {} destinations with new API", managed_destinations.len());
// Add a test destination to demonstrate management
println!("Adding test destination 'MyTestPrinter'...");
managed_destinations.add_destination("MyTestPrinter", None)?;
// Search for it using new find method
if let Some(found) = managed_destinations.find_destination("MyTestPrinter", None) {
println!(" ✓ Found added destination: {}", found.name);
}
// Set a new default if we have printers
if let Some(first_dest) = destinations.first() {
println!("Setting '{}' as default...", first_dest.name);
managed_destinations.set_default_destination(&first_dest.name, first_dest.instance.as_deref())?;
println!(" ✓ Default updated");
}
// Clean up test destination
let removed = managed_destinations.remove_destination("MyTestPrinter", None)?;
println!("Test destination removed: {}", if removed { "✓" } else { "✗" });
// Show current default (existing API)
match get_default_destination() {
Ok(default) => println!("\nCurrent default printer: {}", default.full_name()),
Err(_) => println!("\nNo default printer set"),
}
// Filter by type (existing API)
let local_printers = find_destinations(PRINTER_LOCAL, PRINTER_REMOTE)?;
println!("Local printers found: {}", local_printers.len());
// Note about persistence
println!("\nNote: Use managed_destinations.save_to_lpoptions() to persist changes");
Ok(())
}