Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions examples/authentication.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cups_rs::{
auth::{set_password_callback, get_password, do_authentication},
get_destination, create_job, Result,
Result,
auth::{do_authentication, get_password, set_password_callback},
create_job, get_destination,
};
use std::io::{self, Write};

Expand All @@ -14,14 +15,14 @@ fn main() -> Result<()> {
println!("Prompt: {}", prompt);
println!("Method: {}", method);
println!("Resource: {}", resource);

print!("Enter password (or 'q' to quit): ");
io::stdout().flush().unwrap();

let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let input = input.trim();

if input == "q" || input.is_empty() {
println!("Authentication cancelled");
None
Expand All @@ -38,16 +39,14 @@ fn main() -> Result<()> {
}

// Try to access a printer that might require authentication
let printer_name = std::env::args()
.nth(1)
.unwrap_or_else(|| "PDF".to_string());
let printer_name = std::env::args().nth(1).unwrap_or_else(|| "PDF".to_string());

println!("\nTrying to access printer: {}", printer_name);

match get_destination(&printer_name) {
Ok(destination) => {
println!("Successfully connected to: {}", destination.full_name());

// Try to create a job (this might trigger authentication)
println!("Attempting to create a job...");
match create_job(&destination, "Authentication test job") {
Expand All @@ -56,7 +55,7 @@ fn main() -> Result<()> {
}
Err(e) => {
println!("Job creation failed: {}", e);

// Try manual authentication
println!("Attempting manual authentication...");
match do_authentication(None, "POST", "/") {
Expand All @@ -74,12 +73,12 @@ fn main() -> Result<()> {
// Test removing the callback
println!("\nRemoving password callback...");
set_password_callback(None)?;

match get_password("Test prompt after removal:", None, "GET", "/test") {
Some(password) => println!("Unexpected password: {}", password),
None => println!("Callback correctly removed - no password provided"),
}

println!("\nAuthentication example completed!");
Ok(())
}
}
17 changes: 12 additions & 5 deletions examples/discover_printers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cups_rs::{
get_all_destinations, get_default_destination, find_destinations,
Destinations, PRINTER_LOCAL, PRINTER_REMOTE, Result,
Destinations, PRINTER_LOCAL, PRINTER_REMOTE, Result, find_destinations, get_all_destinations,
get_default_destination,
};

fn main() -> Result<()> {
Expand Down Expand Up @@ -44,7 +44,10 @@ fn main() -> Result<()> {
// 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());
println!(
"Managing {} destinations with new API",
managed_destinations.len()
);

// Add a test destination to demonstrate management
println!("Adding test destination 'MyTestPrinter'...");
Expand All @@ -58,13 +61,17 @@ fn main() -> Result<()> {
// 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())?;
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 { "✗" });
println!(
"Test destination removed: {}",
if removed { "✓" } else { "✗" }
);

// Show current default (existing API)
match get_default_destination() {
Expand Down
29 changes: 16 additions & 13 deletions examples/multi_document.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use cups_rs::{
create_job, get_destination, get_job_info, Result, FORMAT_TEXT,
};
use cups_rs::{FORMAT_TEXT, Result, create_job, get_destination, get_job_info};

fn main() -> Result<()> {
println!("CUPS Multi-Document Job Example");

let printer_name = std::env::args()
.nth(1)
.unwrap_or_else(|| "PDF".to_string());
let printer_name = std::env::args().nth(1).unwrap_or_else(|| "PDF".to_string());

let destination = get_destination(&printer_name)?;
println!("Using printer: {}", destination.full_name());

let job = create_job(&destination, "Multi-document job")?;
println!("Created job ID: {}", job.id);

std::fs::write("doc1.txt", "Document 1: First page of the multi-document job\n")?;
std::fs::write("doc2.txt", "Document 2: Second page of the multi-document job\n")?;
std::fs::write("doc3.txt", "Document 3: Final page of the multi-document job\n")?;
std::fs::write(
"doc1.txt",
"Document 1: First page of the multi-document job\n",
)?;
std::fs::write(
"doc2.txt",
"Document 2: Second page of the multi-document job\n",
)?;
std::fs::write(
"doc3.txt",
"Document 3: Final page of the multi-document job\n",
)?;

println!("Submitting document 1 (not last)...");
job.submit_file_with_options("doc1.txt", FORMAT_TEXT, &[], false)?;

println!("Submitting document 2 (not last)...");
let doc2_options = vec![
("print-quality".to_string(), "high".to_string()),
];
let doc2_options = vec![("print-quality".to_string(), "high".to_string())];
job.submit_file_with_options("doc2.txt", FORMAT_TEXT, &doc2_options, false)?;

println!("Submitting document 3 (last document)...");
Expand All @@ -47,4 +50,4 @@ fn main() -> Result<()> {

println!("Multi-document job completed! Check: lpstat -o");
Ok(())
}
}
22 changes: 13 additions & 9 deletions examples/server_config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use cups_rs::{
Result,
config::{
get_server, set_server, get_user, set_user, get_encryption, set_encryption,
get_user_agent, set_user_agent, CupsConfig, EncryptionMode,
CupsConfig, EncryptionMode, get_encryption, get_server, get_user, get_user_agent,
set_encryption, set_server, set_user, set_user_agent,
},
get_all_destinations, Result,
get_all_destinations,
};

fn main() -> Result<()> {
Expand All @@ -17,7 +18,7 @@ fn main() -> Result<()> {

// Test individual configuration functions
println!("\n--- Testing Individual Configuration ---");

// Test server configuration
println!("Setting server to 'print.example.com:8631'...");
set_server(Some("print.example.com:8631"))?;
Expand All @@ -44,7 +45,7 @@ fn main() -> Result<()> {
set_user(None)?;
set_encryption(EncryptionMode::IfRequested);
set_user_agent(None)?;

println!("Server restored to: {}", get_server());
println!("User restored to: {}", get_user());
println!("Encryption restored to: {:?}", get_encryption());
Expand All @@ -69,7 +70,10 @@ fn main() -> Result<()> {
// Note: This will likely fail since scoped.example.com doesn't exist
match get_all_destinations() {
Ok(destinations) => {
println!(" Successfully connected! Found {} printers", destinations.len());
println!(
" Successfully connected! Found {} printers",
destinations.len()
);
}
Err(e) => {
println!(" Expected connection failure: {}", e);
Expand All @@ -95,13 +99,13 @@ fn main() -> Result<()> {
.with_user("developer")?
.with_encryption(EncryptionMode::Never)
.with_user_agent("DevApp/1.0-debug")?;

let dev_summary = _dev_config.current_config();
println!(" {}", dev_summary);

// The configuration will be automatically restored when _dev_config is dropped
println!("\nServer configuration demo completed!");
println!("Note: All configurations are thread-local in CUPS");

Ok(())
}
}
Loading