use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub source: String,
pub output_path: PathBuf,
pub lib_name: Option<String>,
pub headers: Vec<PathBuf>,
pub use_llm: bool,
pub llm_model: String,
pub interactive: bool,
pub style: CodeStyle,
pub cache_dir: PathBuf,
pub dry_run: bool,
pub verbose: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum CodeStyle {
Minimal,
Ergonomic,
ZeroCost,
}
impl std::str::FromStr for CodeStyle {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"minimal" => Ok(CodeStyle::Minimal),
"ergonomic" => Ok(CodeStyle::Ergonomic),
"zero-cost" => Ok(CodeStyle::ZeroCost),
_ => Err(anyhow::anyhow!(
"Invalid style: {}. Use minimal, ergonomic, or zero-cost",
s
)),
}
}
}
impl Config {
pub fn from_cli(cli: &crate::cli::Cli) -> Result<Self> {
let cache_dir = cli.cache_dir.clone().unwrap_or_else(|| {
dirs::cache_dir()
.unwrap_or_else(|| PathBuf::from(".cache"))
.join("bindings-generat")
});
let style = cli.style.parse()?;
Ok(Self {
source: cli.source.clone(),
output_path: cli.get_output_dir(),
lib_name: cli.lib_name.clone(),
headers: Vec::new(), use_llm: !cli.no_llm,
llm_model: cli.model.clone(),
interactive: cli.interactive || (!cli.non_interactive),
style,
cache_dir,
dry_run: cli.dry_run,
verbose: cli.verbose,
})
}
pub fn from_file(path: &PathBuf) -> Result<Self> {
let content = std::fs::read_to_string(path).context("Failed to read config file")?;
let config: Self = toml::from_str(&content).context("Failed to parse config file")?;
Ok(config)
}
pub fn save(&self, path: &PathBuf) -> Result<()> {
let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
std::fs::write(path, content).context("Failed to write config file")?;
Ok(())
}
}