pub mod compression;
pub mod parallelism;
use crate::context::compression::infer_compression;
use crate::context::parallelism::infer_parallelism;
use crate::errors::EnkryptitError;
use crate::types::{CompressionType, ParallelismType};
use std::fs::File;
use zeroize::Zeroizing;
pub const LOW_BOUNDARY: u64 = 50 << 20; pub const MID_INFERIOR_BOUNDARY: u64 = 250 << 20; pub const MID_SUPERIOR_BOUNDARY: u64 = 1 << 30; pub const SUPERIOR_BOUNDARY: u64 = 5 << 30;
pub struct EnkryptitContext {
pub password: Option<Zeroizing<String>>,
pub compression_type: CompressionType,
pub parallelism: ParallelismType,
}
impl EnkryptitContext {
pub fn new(
password: Option<String>,
compression_type: CompressionType,
parallelism: ParallelismType,
) -> Self {
Self {
password: password.map(Zeroizing::new),
compression_type,
parallelism,
}
}
pub fn resolve_password(&mut self) -> Result<&Zeroizing<String>, EnkryptitError> {
if self.password.is_none() {
let pwd = rpassword::prompt_password("Enter password: ")?;
self.password = Some(Zeroizing::new(pwd));
}
Ok(self.password.as_ref().unwrap())
}
pub fn resolve_compression(&self, path: &str) -> Result<CompressionType, EnkryptitError> {
match self.compression_type {
CompressionType::Auto => infer_compression(path),
compression => Ok(compression),
}
}
pub fn resolve_parallelism(&self, path: &str) -> Result<ParallelismType, EnkryptitError> {
let file = File::open(path)?;
let len = file.metadata()?.len();
self.resolve_parallelism_with_size(len)
}
pub fn resolve_parallelism_with_size(
&self,
size: u64,
) -> Result<ParallelismType, EnkryptitError> {
match self.parallelism {
ParallelismType::Auto => infer_parallelism(size),
parallelism => Ok(parallelism),
}
}
}