use crate::{error::HbdResult, files::storage::Storage};
pub fn rename(from: &str, to: &str) -> HbdResult<()> {
let mut storage_birthdays = Storage::read_from_json()?;
let user_string = from.to_string();
for (_, names) in storage_birthdays.birthdays.iter_mut() {
if names.contains(&user_string) {
names.retain(|n| n != from);
names.push(to.to_owned());
if let Some(year) = storage_birthdays.ages.remove(from) {
storage_birthdays.ages.insert(to.to_owned(), year);
}
storage_birthdays.reads.remove(from);
storage_birthdays.write_to_storage()?;
return Ok(());
}
}
println!("No users with the name \x1b[1m`{from}`\x1B[0m found.");
Ok(())
}