bindings_generat/
config.rs1use anyhow::{Context, Result};
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Config {
8 pub source: String,
10
11 pub output_path: PathBuf,
13
14 pub lib_name: Option<String>,
16
17 pub headers: Vec<PathBuf>,
19
20 pub use_llm: bool,
22
23 pub llm_model: String,
25
26 pub interactive: bool,
28
29 pub style: CodeStyle,
31
32 pub cache_dir: PathBuf,
34
35 pub dry_run: bool,
37
38 pub verbose: bool,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43pub enum CodeStyle {
44 Minimal,
46 Ergonomic,
48 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 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(), 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 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 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