use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tai_core::Network;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CliConfig {
pub network: String,
pub signer: SignerConfig,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SignerConfig {
pub mode: String,
pub key_path: PathBuf,
}
impl CliConfig {
pub fn network(&self) -> Result<Network> {
self.network
.parse::<Network>()
.map_err(|e| anyhow!("invalid network in config: {e}"))
}
}
pub fn default_path() -> Result<PathBuf> {
let home = dirs::home_dir().ok_or_else(|| anyhow!("could not resolve $HOME"))?;
Ok(home.join(".tai").join("config.toml"))
}
pub fn default_keys_dir() -> Result<PathBuf> {
let home = dirs::home_dir().ok_or_else(|| anyhow!("could not resolve $HOME"))?;
Ok(home.join(".tai").join("keys"))
}
pub fn load(path: &Path) -> Result<CliConfig> {
let raw =
std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
let cfg: CliConfig = toml::from_str(&raw).context("parsing config.toml")?;
Ok(cfg)
}
pub fn save(path: &Path, cfg: &CliConfig) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("creating {}", parent.display()))?;
}
let toml_str = toml::to_string_pretty(cfg).context("serializing config")?;
std::fs::write(path, toml_str).with_context(|| format!("writing {}", path.display()))?;
Ok(())
}