1use std::error::Error;
9
10mod args;
11mod encrypt;
12mod files;
13
14pub fn parse_arguments(args: impl Iterator<Item = String>) -> Result<args::Args, &'static str> {
16 args::Args::arguments(args)
17}
18
19pub fn run(args: args::Args) -> Result<(), Box<dyn Error>> {
24 let cipher = encrypt::Cipher::new(&args.password);
25 let filepaths = files::get_filepaths(args.input_path, args.output_path)?;
26
27 for path in filepaths {
28 let contents = files::read_file(&path.input_path)?;
29
30 let transformed = cipher.apply_codec(contents, &args.codec_type);
31 files::write_file(&path.output_path, transformed)?;
32 }
33
34 Ok(())
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40 use std::fs;
41
42 #[test]
43 fn test_transform_integration() {
44 let inner_contents = b"
45The Matrix is everywhere. It is all around us. Even now,
46in this very room. You can see it when you look out your
47window or when you turn on your television"
48 .to_vec();
49
50 let base_contents = b"
51You can feel it when you go to work... when you go to
52church... when you pay your taxes. It is the world that
53has been pulled over your eyes to blind you from the truth."
54 .to_vec();
55
56 let base_dir_path = String::from("./tests/directory");
57
58 let input_dir_path = files::extend_dir_path(&base_dir_path, "input");
59 let enc_dir_path = files::extend_dir_path(&base_dir_path, "encrypted");
60 let dec_dir_path = files::extend_dir_path(&base_dir_path, "decrypted");
61
62 let inner_file_ext = "go/to/zion.txt";
63 let inner_file_path = files::extend_dir_path(&input_dir_path, inner_file_ext);
64 files::write_file(&inner_file_path, inner_contents.clone()).unwrap();
65
66 let base_file_ext = "machine-city.txt";
67 let base_file_path = files::extend_dir_path(&input_dir_path, base_file_ext);
68 files::write_file(&base_file_path, base_contents.clone()).unwrap();
69
70 let password = String::from("red pill or blue pill?");
71
72 let enc_args = args::Args {
73 codec_type: args::CodecType::Encrypt,
74 input_path: input_dir_path.clone(),
75 output_path: enc_dir_path.clone(),
76 password: password.clone(),
77 };
78 run(enc_args).unwrap();
79
80 let dec_args = args::Args {
81 codec_type: args::CodecType::Decrypt,
82 input_path: enc_dir_path,
83 output_path: dec_dir_path.clone(),
84 password: password,
85 };
86 run(dec_args).unwrap();
87
88 let dec_inner_file_path = files::extend_dir_path(&dec_dir_path, inner_file_ext);
89 let dec_inner_contents = files::read_file(&dec_inner_file_path).unwrap();
90 assert_eq!(dec_inner_contents, inner_contents);
91
92 let dec_base_file_path = files::extend_dir_path(&dec_dir_path, base_file_ext);
93 let dec_base_contents = files::read_file(&dec_base_file_path).unwrap();
94 assert_eq!(dec_base_contents, base_contents);
95
96 fs::remove_dir_all(&base_dir_path).unwrap();
97 }
98}