use crate::error::Result; use serde_json::Value; use std::{fs, path::PathBuf}; pub fn read_file_to_json(path: &PathBuf) -> Result { let file = fs::read_to_string(path)?; let json: Value = serde_json::from_str(&file)?; Ok(json) } pub fn write_json_to_file(path: &PathBuf, value: &Value) -> Result<()> { let content = serde_json::to_string_pretty(value)?; fs::write(path, content)?; Ok(()) } pub fn update_json_file(path: &PathBuf, updater: F) -> Result<()> where F: FnOnce(&mut Value) -> Result<()>, { let mut json = read_file_to_json(path)?; updater(&mut json)?; write_json_to_file(path, &json)?; Ok(()) } pub fn find_project_dir(path: &Option) -> Result { let path = path.as_deref().unwrap_or("."); Ok(PathBuf::from(path)) }