use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub apikey: String,
}
fn config_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
let home = dirs::home_dir().ok_or("Cannot find home directory")?;
let config_dir = home.join(".abpilot");
fs::create_dir_all(&config_dir)?;
Ok(config_dir.join("config.json"))
}
pub fn save_config(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
let path = config_path()?;
let json = serde_json::to_string_pretty(config)?;
fs::write(path, json)?;
Ok(())
}
pub fn load_config() -> Result<Config, Box<dyn std::error::Error>> {
let path = config_path()?;
let content = fs::read_to_string(path)?;
let config = serde_json::from_str(&content)?;
Ok(config)
}