1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Import/use statement generation for Rust code.
//!
//! This module handles the conversion of Windjammer import paths to Rust `use` statements.
//! It resolves crate::, std::, relative (./, ../), and sibling module imports, including
//! support for nested output directory structures (e.g., src/generated/core/commands/).
mod external_imports;
mod stdlib_imports;
mod use_statement_generation;
use crate::codegen::rust::CodeGenerator;
impl CodeGenerator<'_> {
/// Calculate the import prefix for cross-module imports based on output file nesting
/// Returns the number of directory levels to go up (for super:: prefixes)
pub(crate) fn get_import_prefix_for_nested_output(&self) -> Option<usize> {
if self.current_output_file.as_os_str().is_empty() {
return None;
}
// Count directory levels by checking parent directories
// For src/generated/core/commands/command.rs:
// - command.rs (file)
// - commands/ (parent 1)
// - core/ (parent 2)
// - generated/ (parent 3 - this is our module root)
// - src/ (parent 4)
// So from core/commands/ we need to go up 2 levels to get to generated/
// Get the path and count parent directories excluding the filename
let mut parent = self.current_output_file.parent();
let mut depth = 0;
while let Some(p) = parent {
let dir_name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
// Stop when we hit a known module root directory
// These are directories that typically contain the generated modules
if dir_name == "generated"
|| dir_name == "build"
|| dir_name == "out"
|| dir_name == "src"
{
// Found module root - return current depth
if depth > 0 {
return Some(depth);
}
break;
}
depth += 1;
parent = p.parent();
}
None
}
fn get_module_root_name(&self) -> Option<String> {
// Walk up the directory tree to find the module root name
// KEY DISTINCTION:
// - If directory has lib.rs: it's the crate root -> return None
// - If directory is inside src/ of another crate: it's a submodule -> return dir name
// - Known output dirs (build, generated, out) are always the crate root
// even if lib.rs hasn't been generated yet (chicken-and-egg with CLI)
let mut parent = self.current_output_file.parent();
while let Some(p) = parent {
let dir_name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
if dir_name == "generated" || dir_name == "build" || dir_name == "out" {
// If this directory contains lib.rs, it IS the crate root
if p.join("lib.rs").exists() {
return None;
}
// TDD FIX: Only treat as a submodule if this output directory is
// INSIDE another crate's src/ directory. A parent lib.rs that's NOT
// in a src/ directory belongs to a DIFFERENT crate (sibling project),
// not the one we're generating into.
if let Some(parent_of_p) = p.parent() {
let parent_name = parent_of_p
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("");
if parent_name == "src" && parent_of_p.join("lib.rs").exists() {
return Some(dir_name.to_string());
}
}
// Known output directory without lib.rs yet - it IS the crate root.
// lib.rs will be generated later by the CLI.
return None;
}
if dir_name == "src" {
break;
}
parent = p.parent();
}
None
}
/// True when the Rust file being generated is a directory module root (`mod.rs`).
pub(crate) fn is_output_mod_rs(&self) -> bool {
let name = self
.current_output_file
.file_name()
.and_then(|n| n.to_str());
name == Some("mod.rs") || name == Some("_mod_items.rs")
}
/// True if `first_segment` names a submodule living in the same directory as this `mod.rs`
/// (matches sibling `.wj` / `.rs` next to `mod.wj` / `mod.rs`, submodule directory, or
/// `name/mod.wj` under the output dir).
///
/// Multipass emits only `.rs` next to `mod.rs`; checking the **source** directory
/// (`current_wj_file.parent()`) ensures we still classify children when `mod.wj` is
/// compiled before sibling `.rs` exists, and avoids false `is_directory_prefix` when a
/// **top-level** directory shares the submodule name (E0432).
fn is_child_module_of_mod_rs_dir(&self, first_segment: &str) -> bool {
if !self.is_output_mod_rs() {
return false;
}
let Some(mod_dir) = self.current_output_file.parent() else {
return false;
};
let child = mod_dir.join(first_segment);
if child.is_dir() {
return true;
}
let out_wj = mod_dir.join(format!("{}.wj", first_segment));
if out_wj.exists() {
return true;
}
let out_rs = mod_dir.join(format!("{}.rs", first_segment));
if out_rs.exists() {
return true;
}
if let Some(wj_parent) = self.current_wj_file.parent() {
if wj_parent.join(format!("{}.wj", first_segment)).exists() {
return true;
}
if wj_parent.join(format!("{}.rs", first_segment)).exists() {
return true;
}
}
false
}
/// When the same type name exists in multiple modules, pick the defining path that extends
/// the import's parent prefix (`crate::autotile::TileId` → parent `[autotile]`).
fn select_def_mod_for_import_prefix<'a>(
candidates: &'a [Vec<String>],
logical_parent: &[String],
) -> Option<&'a Vec<String>> {
let matches: Vec<&Vec<String>> = candidates
.iter()
.filter(|c| {
c.len() > logical_parent.len() && c[..logical_parent.len()] == logical_parent[..]
})
.collect();
if matches.is_empty() {
return None;
}
if matches.len() == 1 {
return Some(matches[0]);
}
// Prefer the shortest suffix after `logical_parent` so duplicate type names resolve to the
// canonical submodule (e.g. `input::input::Input` over `input::input_interface::Input`).
Some(
*matches
.iter()
.min_by_key(|c| {
let tail = &c[logical_parent.len()..];
(tail.len(), tail.iter().map(|s| s.len()).sum::<usize>())
})
.expect("matches non-empty"),
)
}
fn split_leading_output_root(&self, segs: &[String]) -> (Option<String>, Vec<String>) {
if let Some(root) = self.get_module_root_name() {
if segs.first().map(|s| s.as_str()) == Some(root.as_str()) {
return (Some(root), segs[1..].to_vec());
}
}
(None, segs.to_vec())
}
/// Segments after `crate::` (and after optional output-root), ending with a type name.
fn expand_type_path_after_crate_root(&self, body: &[String]) -> Vec<String> {
if body.len() < 2 || self.type_defining_modules.is_empty() {
return body.to_vec();
}
let type_name = &body[body.len() - 1];
if !type_name
.chars()
.next()
.is_some_and(|c| c.is_ascii_uppercase())
{
return body.to_vec();
}
let logical_parent = &body[..body.len() - 1];
let candidates = match self.type_defining_modules.get(type_name) {
Some(c) if !c.is_empty() => c.as_slice(),
_ => return body.to_vec(),
};
let Some(def_mod) = Self::select_def_mod_for_import_prefix(candidates, logical_parent)
else {
return body.to_vec();
};
if *def_mod == *logical_parent {
return body.to_vec();
}
if def_mod.len() > logical_parent.len()
&& def_mod[..logical_parent.len()] == logical_parent[..]
{
let mut out = def_mod.clone();
out.push(type_name.clone());
return out;
}
body.to_vec()
}
/// Expand `math::Vec3`-style paths (no `crate::` prefix) for nested output / internal imports.
fn expand_bare_module_path_for_type(&self, rust_path: &str) -> String {
if self.type_defining_modules.is_empty() {
return rust_path.to_string();
}
let segs: Vec<String> = rust_path.split("::").map(String::from).collect();
self.expand_type_path_after_crate_root(&segs).join("::")
}
/// Rewrite `crate::parent::Type` → `crate::parent::submodule::Type` using multipass `type_defining_modules`.
pub(crate) fn expand_crate_path_string(&self, rust_path: &str) -> String {
if self.type_defining_modules.is_empty() {
return rust_path.to_string();
}
let trimmed = rust_path.trim();
if !trimmed.starts_with("crate::") {
return rust_path.to_string();
}
let rest = trimmed.strip_prefix("crate::").unwrap();
let segs: Vec<String> = rest
.split("::")
.filter(|s| !s.is_empty())
.map(String::from)
.collect();
if segs.is_empty() {
return rust_path.to_string();
}
let (root_opt, body) = self.split_leading_output_root(&segs);
let expanded_body = self.expand_type_path_after_crate_root(&body);
if expanded_body == body {
return rust_path.to_string();
}
let mut out = String::from("crate::");
if let Some(root) = root_opt {
out.push_str(&root);
out.push_str("::");
}
out.push_str(&expanded_body.join("::"));
out
}
fn expand_braced_crate_import(
&self,
normalized: &str,
alias: Option<&str>,
is_pub: bool,
) -> String {
let pub_prefix = if is_pub { "pub " } else { "" };
let module_root = if self.is_module {
self.get_module_root_name()
} else {
None
};
let with_root = if let Some(ref root_name) = module_root {
if normalized.starts_with("crate::") {
let w = normalized.strip_prefix("crate::").unwrap();
format!("crate::{}::{}", root_name, w)
} else {
normalized.to_string()
}
} else {
normalized.to_string()
};
let Some((base, rest)) = with_root.split_once("::{") else {
return if let Some(a) = alias {
format!("{}use {} as {};\n", pub_prefix, with_root, a)
} else {
format!("{}use {};\n", pub_prefix, with_root)
};
};
let Some(inner) = rest.strip_suffix('}') else {
return if let Some(a) = alias {
format!("{}use {} as {};\n", pub_prefix, with_root, a)
} else {
format!("{}use {};\n", pub_prefix, with_root)
};
};
let types: Vec<&str> = inner
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.collect();
if types.is_empty() {
return if let Some(a) = alias {
format!("{}use {} as {};\n", pub_prefix, with_root, a)
} else {
format!("{}use {};\n", pub_prefix, with_root)
};
}
let all_pascal = types
.iter()
.all(|t| t.chars().next().is_some_and(|c| c.is_ascii_uppercase()));
if !all_pascal {
return if let Some(a) = alias {
format!("{}use {} as {};\n", pub_prefix, with_root, a)
} else {
format!("{}use {};\n", pub_prefix, with_root)
};
}
if types.len() == 1 {
let single = format!("{}::{}", base, types[0]);
let exp = self.expand_crate_path_string(&single);
return if let Some(a) = alias {
format!("{}use {} as {};\n", pub_prefix, exp, a)
} else {
format!("{}use {};\n", pub_prefix, exp)
};
}
// Multiple types may live in different submodules — emit one `use` per type.
let mut lines = String::new();
for typ in types {
let single = format!("{}::{}", base, typ);
let exp = self.expand_crate_path_string(&single);
lines.push_str(&format!("{}use {};\n", pub_prefix, exp));
}
lines
}
}