Skip to main content

bindings_generat/
config.rs

1use anyhow::{Context, Result};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5/// Configuration for the bindings generator
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Config {
8    /// Source identifier (path, URL, or archive)
9    pub source: String,
10
11    /// Output directory for the generated crate
12    pub output_path: PathBuf,
13
14    /// Library name (auto-detected if None)
15    pub lib_name: Option<String>,
16
17    /// Specific header files to process
18    pub headers: Vec<PathBuf>,
19
20    /// Whether to use LLM enhancement
21    pub use_llm: bool,
22
23    /// LLM model name (for Ollama)
24    pub llm_model: String,
25
26    /// Whether to ask questions interactively
27    pub interactive: bool,
28
29    /// Code style profile
30    pub style: CodeStyle,
31
32    /// Cache directory for LLM responses
33    pub cache_dir: PathBuf,
34
35    /// Dry run mode (don't write files)
36    pub dry_run: bool,
37
38    /// Verbose logging
39    pub verbose: bool,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43pub enum CodeStyle {
44    /// Minimal wrappers, close to raw FFI
45    Minimal,
46    /// Ergonomic Rust API with builders and conveniences
47    Ergonomic,
48    /// Zero-cost abstractions, no overhead
49    ZeroCost,
50}
51
52impl std::str::FromStr for CodeStyle {
53    type Err = anyhow::Error;
54
55    fn from_str(s: &str) -> Result<Self> {
56        match s.to_lowercase().as_str() {
57            "minimal" => Ok(CodeStyle::Minimal),
58            "ergonomic" => Ok(CodeStyle::Ergonomic),
59            "zero-cost" => Ok(CodeStyle::ZeroCost),
60            _ => Err(anyhow::anyhow!(
61                "Invalid style: {}. Use minimal, ergonomic, or zero-cost",
62                s
63            )),
64        }
65    }
66}
67
68impl Config {
69    /// Create a new configuration from CLI arguments
70    pub fn from_cli(cli: &crate::cli::Cli) -> Result<Self> {
71        let cache_dir = cli.cache_dir.clone().unwrap_or_else(|| {
72            dirs::cache_dir()
73                .unwrap_or_else(|| PathBuf::from(".cache"))
74                .join("bindings-generat")
75        });
76
77        let style = cli.style.parse()?;
78
79        Ok(Self {
80            source: cli.source.clone(),
81            output_path: cli.get_output_dir(),
82            lib_name: cli.lib_name.clone(),
83            headers: Vec::new(), // Will be populated during discovery
84            use_llm: !cli.no_llm,
85            llm_model: cli.model.clone(),
86            interactive: cli.interactive || (!cli.non_interactive),
87            style,
88            cache_dir,
89            dry_run: cli.dry_run,
90            verbose: cli.verbose,
91        })
92    }
93
94    /// Load configuration from a TOML file
95    pub fn from_file(path: &PathBuf) -> Result<Self> {
96        let content = std::fs::read_to_string(path).context("Failed to read config file")?;
97        let config: Self = toml::from_str(&content).context("Failed to parse config file")?;
98        Ok(config)
99    }
100
101    /// Save configuration to a TOML file
102    pub fn save(&self, path: &PathBuf) -> Result<()> {
103        let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
104        std::fs::write(path, content).context("Failed to write config file")?;
105        Ok(())
106    }
107}
108
109// Add dirs crate to Cargo.toml dependencies