Debian Codegraph @

debian/missing-sources/lopdf/examples/decrypt.rs

raw
1 use lopdf::Document;
2
3 #[cfg(not(feature = "async"))]
4 fn main() {
5 // Collect command line arguments: input_file angle output_file
6 let args: Vec<String> = std::env::args().collect();
7 assert!(args.len() >= 3, "Not enough arguments: input_file output_file <password>");
8 let input_file = &args[1];
9 let output_file = &args[2];
10 let password = if args.len() >= 4 { &args[3] } else { "" };
11
12 let mut doc = Document::load(input_file).unwrap();
13
14 // Check if the document is actually encrypted.
15 if doc.encryption_state.is_none() && !doc.is_encrypted() {
16 println!("nothing to be done");
17 return;
18 }
19
20 // Decrypt the document.
21 if doc.is_encrypted() {
22 doc.decrypt(password).unwrap();
23 }
24
25 // Store file in current working directory.
26 doc.save(output_file).unwrap();
27 }
28
29 #[cfg(feature = "async")]
30 #[tokio::main]
31 async fn main() {
32 // Collect command line arguments: input_file angle output_file
33 let args: Vec<String> = std::env::args().collect();
34 assert!(args.len() >= 3, "Not enough arguments: input_file output_file <password>");
35 let input_file = &args[1];
36 let output_file = &args[2];
37 let password = if args.len() >= 4 { &args[3] } else { "" };
38
39 let mut doc = Document::load(input_file).await.unwrap();
40
41 // Check if the document is actually encrypted.
42 if doc.encryption_state.is_none() && !doc.is_encrypted() {
43 println!("nothing to be done");
44 return;
45 }
46
47 // Decrypt the document.
48 if doc.is_encrypted() {
49 doc.decrypt(password).unwrap();
50 }
51
52 // Store file in current working directory.
53 doc.save(output_file).unwrap();
54 }