cups_rs
A safe Rust wrapper for the Common UNIX Printing System (CUPS) API.
Features
- Safe CUPS Integration: Memory-safe Rust bindings to the CUPS printing system
- Printer Discovery: Enumerate and filter available printers with callbacks
- Comprehensive Printer Information: Access printer capabilities, media support, and status
- Job Management: Create, submit, monitor, and cancel print jobs
- Print Options: Type-safe configuration for copies, quality, color mode, duplex, and media
- Media Handling: Query supported paper sizes with detailed margin information
- Error Handling: Comprehensive error types with recovery suggestions
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
System Requirements
CUPS development libraries must be installed:
Ubuntu/Debian:
RHEL/CentOS/Fedora:
# or: sudo yum install cups-devel
macOS:
# CUPS is included with macOS
Usage
Discovering Printers
use *;
// Get all available printers
let printers = get_all_destinations?;
println!;
// Get default printer
let default = get_default_destination?;
println!;
// Get specific printer by name
let printer = get_destination?;
Printer Information and Status
// Check printer state and capabilities
println!;
println!;
if let Some = printer.info
if let Some = printer.location
// Check for issues
let reasons = printer.state_reasons;
if !reasons.is_empty
Printer Capabilities and Media
use ptr;
// Get detailed printer capabilities
let info = printer.get_detailed_info?;
// Check option support
let supports_duplex = printer.is_option_supported;
let supports_color = printer.is_option_supported;
// Check media support
let supports_a4 = info.is_value_supported;
// Get available media sizes
let media_sizes = info.get_all_media?;
for media in &media_sizes
Creating and Managing Print Jobs
// Create a simple print job
let job = create_job?;
println!;
// Submit a document
job.submit_file?;
// Close job to start printing
job.close?;
Print Options and Configuration
// Create job with specific options
let options = new
.copies
.color_mode
.quality
.duplex
.media
.orientation;
let job = create_job_with_options?;
// Submit document with custom format
job.submit_file?;
job.close?;
Job Monitoring and Management
// Get job information
let job_info = get_job_info?;
println!;
// List active jobs
let active_jobs = get_active_jobs?;
for job in &active_jobs
// Cancel a specific job
cancel_job?;
// Or cancel via job instance
job.cancel?;
Advanced Printer Discovery
// Find printers with specific capabilities
let color_printers = find_destinations?;
let local_printers = find_destinations?;
// Use callback-based enumeration for real-time updates
enum_destinations?;
Error Handling
use ;
match create_job
Media Size Details
// Get default media with detailed information
let default_media = info.get_default_media?;
println!;
println!;
println!;
println!;
Examples
The examples directory contains complete working examples:
discover_printers.rs: Basic printer discovery and informationprinter_capabilities.rs: Exploring printer features and media supportprint_with_options.rs: Advanced printing with various optionscomplete_workflow.rs: Full job lifecycle management
Run examples with:
Supported Print Options
| Option | Type | Values |
|---|---|---|
copies |
u32 |
Number of copies |
media |
&str |
MEDIA_A4, MEDIA_LETTER, MEDIA_LEGAL, etc. |
color_mode |
ColorMode |
Auto, Color, Monochrome |
quality |
PrintQuality |
Draft, Normal, High |
duplex |
DuplexMode |
OneSided, TwoSidedPortrait, TwoSidedLandscape |
orientation |
Orientation |
Portrait, Landscape |
Supported Document Formats
- PDF:
FORMAT_PDF(application/pdf) - PostScript:
FORMAT_POSTSCRIPT(application/postscript) - Plain Text:
FORMAT_TEXT(text/plain) - JPEG Images:
FORMAT_JPEG(image/jpeg)
Error Types
The library provides detailed error information:
DestinationNotFound: Printer not availableJobCreationFailed: Cannot create print jobPrinterNotAccepting: Printer rejecting jobsAuthenticationRequired: Credentials neededDocumentTooLarge: File size limits exceededNetworkError: CUPS server communication issues
Thread Safety
CUPS operations are not thread-safe by default. For multi-threaded applications, consider:
- Using a single thread for all CUPS operations
- Implementing proper synchronization around CUPS calls
- Creating separate connections per thread where possible
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Requirements
- Rust 1.70+
- CUPS 2.0+ development libraries
- Linux, macOS, or other UNIX-like system with CUPS support