ssec_cli/
lib.rs

1pub mod cli;
2
3mod file;
4mod password;
5mod enc;
6mod dec;
7
8#[cfg(test)]
9mod tests;
10
11const BAR_STYLE: &str = "[{elapsed_precise}] {binary_bytes_per_sec} {bar} {binary_bytes}/{binary_total_bytes} ({eta})";
12
13#[inline]
14fn handle_err(result: Result<(), ()>) -> std::process::ExitCode {
15	match result {
16		Ok(()) => std::process::ExitCode::SUCCESS,
17		Err(()) => std::process::ExitCode::FAILURE
18	}
19}
20
21trait GetBufRead: Send + 'static {
22	fn get_bufread(&self) -> impl std::io::BufRead;
23
24	// the `rpassword` crate won't hide input if a custom `BufRead` is passed in
25	#[inline]
26	fn is_stdin() -> bool {
27		false
28	}
29}
30
31impl GetBufRead for std::io::Stdin {
32	fn get_bufread(&self) -> impl std::io::BufRead {
33		self.lock()
34	}
35
36	fn is_stdin() -> bool {
37		true
38	}
39}
40
41async fn run_with_io(
42	cli: cli::Cli,
43	reader: impl GetBufRead,
44	writer: impl std::io::Write + Send + 'static
45) -> std::process::ExitCode {
46	match cli.command {
47		cli::Command::Enc(args) => handle_err(enc::enc(args, reader, writer).await),
48		cli::Command::Dec(args) => handle_err(dec::dec_file(args, reader, writer).await),
49		cli::Command::Fetch(args) => handle_err(dec::dec_fetch(args, reader, writer).await)
50	}
51}
52
53pub async fn run(cli: cli::Cli) -> std::process::ExitCode {
54	run_with_io(
55		cli,
56		std::io::stdin(),
57		std::io::stdout()
58	).await
59}