use anyhow::{Context, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
pub fn app_dir() -> Result<PathBuf> {
let home = dirs::home_dir().context("Could not find home directory")?;
let dir = home.join(".region-proxy");
fs::create_dir_all(&dir)?;
Ok(dir)
}
pub fn load_json<T: DeserializeOwned>(path: &Path) -> Result<Option<T>> {
match fs::read_to_string(path) {
Ok(content) => Ok(Some(serde_json::from_str(&content)?)),
Err(e) if e.kind() == ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
pub fn save_json<T: Serialize>(path: &Path, value: &T) -> Result<()> {
fs::write(path, serde_json::to_string_pretty(value)?)?;
Ok(())
}
pub fn remove_file_if_exists(path: &Path) -> Result<bool> {
match fs::remove_file(path) {
Ok(()) => Ok(true),
Err(e) if e.kind() == ErrorKind::NotFound => Ok(false),
Err(e) => Err(e.into()),
}
}