js2zig_core/
toml_config.rs1use crate::analyzer::sanitize_module_name;
11use crate::{HostConfig, HostFunction, HostType};
12use indexmap::IndexMap;
13use serde::Deserialize;
14use std::path::PathBuf;
15
16#[derive(Debug, Deserialize)]
18pub struct Js2rustConfig {
19 pub project: ProjectSection,
21 #[serde(default)]
23 pub build: BuildSection,
24 #[serde(default)]
26 pub host_functions: Vec<HostFnToml>,
27}
28
29#[derive(Debug, Deserialize)]
31pub struct ProjectSection {
32 #[serde(default = "default_js_dir")]
35 pub js_dir: String,
36 pub js_files: Vec<String>,
39}
40
41fn default_js_dir() -> String {
42 "js_src".to_string()
43}
44
45#[derive(Debug, Deserialize)]
47pub struct BuildSection {
48 #[serde(default)]
50 pub force_rebuild: bool,
51 #[serde(default = "default_run_zig_build")]
53 pub run_zig_build: bool,
54 #[serde(default)]
60 pub zig_optimize: Option<String>,
61}
62
63fn default_run_zig_build() -> bool {
64 true
65}
66
67impl Default for BuildSection {
68 fn default() -> Self {
69 Self {
70 force_rebuild: false,
71 run_zig_build: true,
72 zig_optimize: None,
73 }
74 }
75}
76
77#[derive(Debug, Deserialize)]
79pub struct HostFnToml {
80 pub name: String,
82 pub params: Vec<String>,
84 #[serde(default)]
86 pub returns: Option<String>,
87 #[serde(default)]
89 pub is_async: bool,
90 #[serde(default)]
92 pub async_returns: IndexMap<String, String>,
93}
94
95impl Js2rustConfig {
96 pub fn from_manifest_dir() -> Self {
100 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
101 let config_path = PathBuf::from(&manifest_dir).join("js2rust.toml");
102
103 let content = std::fs::read_to_string(&config_path).unwrap_or_else(|e| {
104 panic!(
105 "js2rust_bridge: failed to read {}: {}\n\
106 Create a js2rust.toml in your crate root with:\n\
107 \n\
108 [project]\n\
109 js_dir = \"js_src\"\n\
110 js_files = [\"main.js\"]\n",
111 config_path.display(),
112 e
113 );
114 });
115
116 toml::from_str(&content).unwrap_or_else(|e| {
117 panic!(
118 "js2rust_bridge: failed to parse {}: {}\n\
119 Make sure your js2rust.toml is valid TOML.",
120 config_path.display(),
121 e
122 );
123 })
124 }
125
126 pub fn project_name(&self) -> String {
128 let default = "main.js".to_string();
129 let first = self.project.js_files.first().unwrap_or(&default);
130 let stem = std::path::Path::new(first)
132 .file_stem()
133 .and_then(|s| s.to_str())
134 .unwrap_or("main");
135 sanitize_module_name(stem)
136 }
137
138 pub fn to_host_config(&self) -> Option<HostConfig> {
142 if self.host_functions.is_empty() {
143 return None;
144 }
145 let functions: Vec<HostFunction> = self
146 .host_functions
147 .iter()
148 .map(|hf| {
149 let params: Vec<HostType> = hf
150 .params
151 .iter()
152 .map(|t| HostType::from_toml_str(t))
153 .collect();
154
155 let return_type = hf.returns.as_deref().and_then(|t| {
156 if t == "void" {
157 None
158 } else {
159 Some(HostType::from_toml_str(t))
160 }
161 });
162
163 let async_return_fields: Vec<(String, HostType)> = hf
164 .async_returns
165 .iter()
166 .map(|(name, ty)| (name.clone(), HostType::from_toml_str(ty)))
167 .collect();
168
169 HostFunction {
170 name: hf.name.clone(),
171 params,
172 return_type,
173 is_async: hf.is_async,
174 async_return_fields,
175 }
176 })
177 .collect();
178
179 Some(HostConfig { functions })
180 }
181}
182
183pub fn pascal_case(name: &str) -> String {
188 let mut result = String::with_capacity(name.len());
189 let mut capitalize = true;
190 for ch in name.chars() {
191 if ch == '_' {
192 capitalize = true;
193 } else if capitalize {
194 result.push(ch.to_ascii_uppercase());
195 capitalize = false;
196 } else {
197 result.push(ch);
198 }
199 }
200 result
201}
202
203impl HostType {
204 pub fn from_toml_str(name: &str) -> HostType {
209 match name {
210 "i64" => HostType::I64,
211 "i32" => HostType::I32,
212 "f64" => HostType::F64,
213 "bool" => HostType::Bool,
214 "str" => HostType::Str,
215 "void" => HostType::Void,
216 other => panic!(
217 "js2rust.toml: unknown host type '{}'. \
218 Valid types: i64, i32, f64, bool, str, void",
219 other
220 ),
221 }
222 }
223}