use ansi_term::Color;
use chrono::Local;
use spinners::{Spinner, Spinners};
use crate::{
errors::HimitsuError,
models::metadata::{Anatomy, LookupTable},
};
use super::{secure, utils};
pub fn write_to_lookup_table(
anatomy: Anatomy,
lookup_table: &mut LookupTable,
password: &str,
secret_hash: &str,
) -> Result<(), HimitsuError> {
lookup_table.table.insert(secret_hash.to_string(), anatomy);
secure::encrypt_lookup_table(password, lookup_table)?;
Ok(())
}
pub fn update_last_accessed(hash_id: &str, password: &str) -> Result<(), HimitsuError> {
let mut lookup_table = secure::decrypt_lookup_table(password)?;
match lookup_table.table.get_mut(hash_id) {
Some(mut anatomy) => {
anatomy.last_accessed = Some(Local::now().format("%m-%d-%Y %H:%M:%S").to_string());
secure::encrypt_lookup_table(password, &mut lookup_table)?;
Ok(())
}
None => Err(HimitsuError::LookupError(
"Could not update 'last_accessed' within this secret's Anatomy!".to_string(),
)),
}
}
pub enum RemovalEvent {
Remove,
Replace,
}
pub fn remove_in_lookup_table(
hash_id: &str,
password: &str,
removal_event: RemovalEvent,
) -> Result<(), HimitsuError> {
let mut removal_spinner = Spinner::new(
Spinners::Aesthetic,
match removal_event {
RemovalEvent::Remove => "Removing your secret...".into(),
RemovalEvent::Replace => "Removing your old secret...".into(),
},
);
let mut lookup_table = secure::decrypt_lookup_table(password)?;
match lookup_table.table.get(hash_id) {
Some(_found_match) => {
lookup_table.table.remove(hash_id);
utils::remove_hash_directory(hash_id)?;
secure::encrypt_lookup_table(password, &mut lookup_table)?;
removal_spinner.stop_and_persist(
"✅",
Color::Green
.bold()
.paint(match removal_event {
RemovalEvent::Remove => "Successfully removed your secret.",
RemovalEvent::Replace => "Successfully removed your old secret.",
})
.to_string(),
);
Ok(())
}
None => {
removal_spinner.stop_and_persist(
"❗️",
Color::Red
.bold()
.paint("SECRET REMOVAL FAILED.")
.to_string(),
);
Err(HimitsuError::LookupError(
"Could not find an existing Anatomy for this secret!".to_string(),
))
}
}
}