1#[macro_use]
2extern crate num_derive;
3
4use crate::error::Error;
5use crate::content::EncryptedContent;
6use crate::decrypt::{decrypt, PlainContent};
7use std::fs;
8use std::path::{Path, PathBuf};
9use std::io::Write;
10
11pub mod content;
12pub mod key;
13pub mod header;
14pub mod error;
15pub mod decrypt;
16pub mod cli;
17
18pub fn decrypt_file<P: AsRef<Path>>(path: P, passphrase: &str) -> Result<PlainContent, Error> {
19 let input = fs::read(&path).map_err(Error::Io)?;
20 let data = EncryptedContent::parse(&input);
21 decrypt(&data, passphrase)
22}
23
24pub fn create_target_path<P: AsRef<Path>>(path: P, decrypted: &PlainContent) -> PathBuf {
25 let target_path = PathBuf::from(&decrypted.file_name);
26 path.as_ref()
27 .parent()
28 .map(|parent| parent.join(&target_path))
29 .unwrap_or(target_path)
30}
31
32pub fn save_decrypted<P: AsRef<Path>>(decrypted: PlainContent, target_path: P) -> Result<P, Error> {
33 let mut file = fs::File::create(&target_path).map_err(Error::Io)?;
34 file.write_all(&decrypted.content).map(|_| target_path).map_err(Error::Io)
35}