1pub mod analyzer;
4pub mod builtins;
5pub mod host;
6pub mod parser;
7pub mod project;
8pub mod sourcemap;
9pub mod testgen;
10
11pub mod native_proto;
14
15pub mod pipeline;
17
18use std::path::PathBuf;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum HostType {
23 Void,
24 Bool,
25 I32,
26 I64,
27 F64,
28 Str, }
30
31#[derive(Debug, Clone)]
33pub struct HostFunction {
34 pub name: String,
35 pub params: Vec<HostType>,
36 pub return_type: Option<HostType>,
37 pub is_async: bool,
39 pub async_return_fields: Vec<(String, HostType)>,
42}
43
44impl HostFunction {
45 pub fn struct_zig_name(&self) -> String {
48 let pascal: String = self
49 .name
50 .split('_')
51 .map(|word| {
52 let mut chars = word.chars();
53 match chars.next() {
54 Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
55 None => String::new(),
56 }
57 })
58 .collect();
59 format!("{}Result", pascal)
60 }
61
62 pub fn struct_c_name(&self) -> String {
65 format!("Host{}", self.struct_zig_name())
66 }
67}
68
69impl HostType {
70 pub fn to_zig_field_type(self) -> &'static str {
72 match self {
73 HostType::Void => "void",
74 HostType::Bool => "bool",
75 HostType::I32 => "i32",
76 HostType::I64 => "i64",
77 HostType::F64 => "f64",
78 HostType::Str => "[]const u8",
79 }
80 }
81
82 pub fn to_c_field_type(self) -> &'static str {
84 match self {
85 HostType::Void => "void",
86 HostType::Bool => "bool",
87 HostType::I32 => "i32",
88 HostType::I64 => "i64",
89 HostType::F64 => "f64",
90 HostType::Str => "[256]u8",
91 }
92 }
93}
94
95#[derive(Debug, Clone)]
97pub struct HostConfig {
98 pub functions: Vec<HostFunction>,
99}
100
101#[derive(Debug, Clone)]
103pub struct ProjectConfig {
104 pub name: String,
106 pub js_file: PathBuf,
108 pub additional_js_files: Vec<PathBuf>,
110 pub out_dir: PathBuf,
112 pub host_config: Option<HostConfig>,
114 pub force_rebuild: bool,
116 pub run_zig_build: bool,
118}
119
120impl Default for ProjectConfig {
121 fn default() -> Self {
122 Self {
123 name: "js2zig_lib".into(),
124 js_file: PathBuf::from("main.js"),
125 additional_js_files: Vec::new(),
126 out_dir: PathBuf::from("out"),
127 host_config: None,
128 force_rebuild: false,
129 run_zig_build: false,
130 }
131 }
132}
133
134#[derive(Debug, Clone)]
136pub struct GroupResult {
137 pub name: String,
139 pub is_test: bool,
141 pub cabi_exports_json: String,
143 pub diagnostics: Vec<String>,
145 pub output_files: Vec<PathBuf>,
147}
148
149#[derive(Debug, Default)]
151pub struct ProjectResult {
152 pub groups: Vec<GroupResult>,
154 pub diagnostics: Vec<String>,
156}
157
158pub fn transpile_project(config: &ProjectConfig) -> Result<ProjectResult, String> {
165 pipeline::transpile_project(config)
166}
167
168pub fn write_cabi_metadata(
171 out_dir: &std::path::Path,
172 group_name: &str,
173 cabi_exports: &[native_proto::NativeCabiExport],
174 host_fns: &host::HostFnRegistry,
175 include_init: bool,
176) {
177 pipeline::write_cabi_metadata(out_dir, group_name, cabi_exports, host_fns, include_init)
178}