js2rust_bridge/config.rs
1//! js2rust.toml configuration parsing.
2//!
3//! Reads a single `js2rust.toml` from the crate root (`CARGO_MANIFEST_DIR`).
4//! This file replaces the need to duplicate group/host-function information
5//! between the `js2rust_bridge!()` macro and `build.rs`.
6//!
7//! ## Format
8//!
9//! ```toml
10//! [project]
11//! js_file = "js_src/main.js" # required: core JS entry point
12//! additional_js_files = ["js_src/extra.js"] # optional
13//!
14//! [[host_functions]]
15//! name = "host_add"
16//! params = ["i64", "i64"]
17//! returns = "i64"
18//!
19//! [[host_functions]]
20//! name = "host_concat"
21//! params = ["str", "str"]
22//! returns = "str"
23//!
24//! [[host_functions]]
25//! name = "fetch_user"
26//! params = ["str"]
27//! is_async = true
28//! async_returns = { id = "i64", name = "str" }
29//! ```
30
31use indexmap::IndexMap;
32use serde::Deserialize;
33use std::path::PathBuf;
34
35/// Root structure of `js2rust.toml`.
36#[derive(Debug, Deserialize)]
37pub struct Js2rustConfig {
38 pub project: ProjectSection,
39 #[serde(default)]
40 pub host_functions: Vec<HostFnToml>,
41}
42
43/// `[project]` section.
44#[derive(Debug, Deserialize)]
45pub struct ProjectSection {
46 /// Core JS source file path, relative to the crate root.
47 pub js_file: String,
48 /// Additional root JS files (multi-root mode), relative to the crate root.
49 #[serde(default)]
50 pub additional_js_files: Vec<String>,
51}
52
53/// A single `[[host_functions]]` entry.
54#[derive(Debug, Deserialize)]
55pub struct HostFnToml {
56 /// Host function name (must match `host_` prefix convention in JS).
57 pub name: String,
58 /// Parameter types: "i64", "i32", "f64", "bool", "str".
59 pub params: Vec<String>,
60 /// Return type (optional for async struct returns).
61 #[serde(default)]
62 pub returns: Option<String>,
63 /// Whether this is an async host function.
64 #[serde(default)]
65 pub is_async: bool,
66 /// For async functions that return a struct: field_name → field_type.
67 #[serde(default)]
68 pub async_returns: IndexMap<String, String>,
69}
70
71impl Js2rustConfig {
72 /// Load `js2rust.toml` from the crate manifest directory.
73 ///
74 /// Panics with a descriptive message if the file is missing or malformed.
75 pub fn from_manifest_dir() -> Self {
76 let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
77 let config_path = PathBuf::from(&manifest_dir).join("js2rust.toml");
78
79 let content = std::fs::read_to_string(&config_path).unwrap_or_else(|e| {
80 panic!(
81 "js2rust_bridge: failed to read {}: {}\n\
82 Create a js2rust.toml in your crate root with:\n\
83 \n\
84 [project]\n\
85 js_file = \"js_src/main.js\"\n",
86 config_path.display(),
87 e
88 );
89 });
90
91 toml::from_str(&content).unwrap_or_else(|e| {
92 panic!(
93 "js2rust_bridge: failed to parse {}: {}\n\
94 Make sure your js2rust.toml is valid TOML.",
95 config_path.display(),
96 e
97 );
98 })
99 }
100
101 /// Derive the Zig group name from the `js_file` file stem.
102 ///
103 /// The file stem is sanitized for Zig identifier rules.
104 pub fn group_name(&self) -> String {
105 let stem = std::path::Path::new(&self.project.js_file)
106 .file_stem()
107 .and_then(|s| s.to_str())
108 .unwrap_or("main");
109 js2zig_core::analyzer::sanitize_module_name(stem)
110 }
111}