this_me/utils/
update.rs

1use std::fs;
2use std::io;
3use serde_json::Value;
4use crate::me::Me;
5
6pub fn update_me<F>(me: &Me, hash: &str, mutator: F) -> io::Result<()>
7where
8    F: FnOnce(&mut Value),
9{
10    let contents = fs::read(&me.file_path)?;
11    let decrypted = me.decrypt(&contents, hash)
12        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "❌ Decryption failed"))?;
13
14    let mut json: Value = serde_json::from_str(&decrypted)?;
15    mutator(&mut json); // Apply transformation
16
17    let updated_str = serde_json::to_string(&json)?;
18    let encrypted = me.encrypt(&updated_str, hash)?;
19    fs::write(&me.file_path, encrypted)?;
20    Ok(())
21}