31 lines
809 B
Rust
31 lines
809 B
Rust
use crate::error::Result;
|
|
use serde_json::Value;
|
|
use std::{fs, path::PathBuf};
|
|
|
|
pub fn read_file_to_json(path: &PathBuf) -> Result<Value> {
|
|
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<F>(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<String>) -> Result<PathBuf> {
|
|
let path = path.as_deref().unwrap_or(".");
|
|
Ok(PathBuf::from(path))
|
|
}
|