-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomplete_workflow.rs
More file actions
116 lines (91 loc) · 3.25 KB
/
complete_workflow.rs
File metadata and controls
116 lines (91 loc) · 3.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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()))?;
// Get job info before canceling
match get_job_info(job_id) {
Ok(info) => println!("Canceling: {} ({})", info.title, info.status),
Err(_) => println!("Job {} not found", job_id),
}
// Cancel the job
cancel_job(job_id)?;
println!("Job {} canceled", job_id);
Ok(())
}
fn handle_list_command() -> Result<()> {
// Get different job queues
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());
// Get the target printer
let destination = get_destination(&printer_name)?;
println!(
"Using printer: {} ({})",
destination.full_name(),
destination.state()
);
// Step 1: Create a print job
let job = create_job(&destination, "Workflow test document")?;
println!("Created job ID: {}", job.id);
// Step 2: Submit document to the job
let format = if file_path.ends_with(".pdf") {
FORMAT_PDF
} else {
FORMAT_TEXT
};
job.submit_file(&file_path, format)?;
println!("Document submitted");
// Step 3: Check job status
if let Ok(info) = get_job_info(job.id) {
println!("Job status: {} ({} bytes)", info.status, info.size);
}
// Step 4: Close job to start printing
job.close()?;
println!("Job closed - printing started");
std::thread::sleep(std::time::Duration::from_secs(1));
// Step 5: Final status check
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(())
}