use cups_rs::*;
fn main() -> Result<()> {
println!("CUPS Complete Workflow Example");
println!("==============================");
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 && args[1] == "cancel" {
return handle_cancel_command(&args);
}
if args.len() > 1 && args[1] == "list" {
return handle_list_command();
}
handle_print_workflow(&args)
}
fn handle_cancel_command(args: &[String]) -> Result<()> {
if args.len() < 3 {
println!("Usage: cargo run --example complete_workflow -- cancel <job_id>");
return handle_list_command();
}
let job_id: i32 = args[2]
.parse()
.map_err(|_| Error::JobManagementFailed("Invalid job ID".to_string()))?;
match get_job_info(job_id) {
Ok(info) => println!("Canceling: {} ({})", info.title, info.status),
Err(_) => println!("Job {} not found", job_id),
}
cancel_job(job_id)?;
println!("Job {} canceled", job_id);
Ok(())
}
fn handle_list_command() -> Result<()> {
let active_jobs = get_active_jobs(None)?;
let completed_jobs = get_completed_jobs(None)?;
println!("Active jobs: {}", active_jobs.len());
for job in &active_jobs {
println!(" Job {}: {} ({})", job.id, job.title, job.status);
}
println!("Recent completed jobs: {}", completed_jobs.len());
for job in completed_jobs.iter().take(5) {
println!(" Job {}: {} ({})", job.id, job.title, job.status);
}
Ok(())
}
fn handle_print_workflow(args: &[String]) -> Result<()> {
let file_path = if args.len() > 1 {
args[1].clone()
} else {
let content = "CUPS Workflow Test\nThis demonstrates job creation, document submission, and completion.\n";
std::fs::write("workflow_test.txt", content)?;
"workflow_test.txt".to_string()
};
let printer_name = args.get(2).cloned().unwrap_or_else(|| "PDF".to_string());
let destination = get_destination(&printer_name)?;
println!(
"Using printer: {} ({})",
destination.full_name(),
destination.state()
);
let job = create_job(&destination, "Workflow test document")?;
println!("Created job ID: {}", job.id);
let format = if file_path.ends_with(".pdf") {
FORMAT_PDF
} else {
FORMAT_TEXT
};
job.submit_file(&file_path, format)?;
println!("Document submitted");
if let Ok(info) = get_job_info(job.id) {
println!("Job status: {} ({} bytes)", info.status, info.size);
}
job.close()?;
println!("Job closed - printing started");
std::thread::sleep(std::time::Duration::from_secs(1));
match get_job_info(job.id) {
Ok(info) => println!("Final status: {}", info.status),
Err(_) => println!("Job completed and removed from queue"),
}
if file_path == "workflow_test.txt" {
std::fs::remove_file("workflow_test.txt").ok();
}
println!("Workflow completed! Check: lpstat -o");
Ok(())
}