use indexmap::IndexMap;
use serde::Deserialize;
use std::path::PathBuf;
#[derive(Debug, Deserialize)]
pub struct Js2rustConfig {
pub project: ProjectSection,
#[serde(default)]
pub host_functions: Vec<HostFnToml>,
}
#[derive(Debug, Deserialize)]
pub struct ProjectSection {
pub js_file: String,
#[serde(default)]
pub additional_js_files: Vec<String>,
}
#[derive(Debug, Deserialize)]
pub struct HostFnToml {
pub name: String,
pub params: Vec<String>,
#[serde(default)]
pub returns: Option<String>,
#[serde(default)]
pub is_async: bool,
#[serde(default)]
pub async_returns: IndexMap<String, String>,
}
impl Js2rustConfig {
pub fn from_manifest_dir() -> Self {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
let config_path = PathBuf::from(&manifest_dir).join("js2rust.toml");
let content = std::fs::read_to_string(&config_path).unwrap_or_else(|e| {
panic!(
"js2rust_bridge: failed to read {}: {}\n\
Create a js2rust.toml in your crate root with:\n\
\n\
[project]\n\
js_file = \"js_src/main.js\"\n",
config_path.display(),
e
);
});
toml::from_str(&content).unwrap_or_else(|e| {
panic!(
"js2rust_bridge: failed to parse {}: {}\n\
Make sure your js2rust.toml is valid TOML.",
config_path.display(),
e
);
})
}
pub fn group_name(&self) -> String {
let stem = std::path::Path::new(&self.project.js_file)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("main");
js2zig_core::analyzer::sanitize_module_name(stem)
}
}