use crate::codegen::rust::CodeGenerator;
impl CodeGenerator<'_> {
pub(in crate::codegen::rust) fn try_generate_std_import_use(
&self,
full_path: &str,
alias: Option<&str>,
) -> Option<String> {
if !(full_path.starts_with("std::") || full_path.starts_with("std.")) {
return None;
}
let normalized = full_path.replace('.', "::");
let module_name = normalized.strip_prefix("std::").unwrap();
let module_base = module_name.strip_suffix("::*").unwrap_or(module_name);
if module_base.starts_with("collections")
|| module_base.starts_with("cmp")
|| module_base.starts_with("ops")
|| module_base == "ops"
{
if let Some(alias_name) = alias {
return Some(format!("use std::{} as {};\n", module_name, alias_name));
}
return Some(format!("use std::{};\n", module_name));
}
if module_base == "ui" || module_base.starts_with("ui::") {
return Some(String::new());
}
if module_base == "game" || module_base.starts_with("game::") {
return Some(String::new());
}
if module_base == "tauri" || module_base.starts_with("tauri::") {
return Some(String::new());
}
if module_base == "fs"
|| module_base.starts_with("fs::")
|| module_base == "process"
|| module_base.starts_with("process::")
{
return Some(format!("use std::{};\n", module_name));
}
if module_base == "env" || module_base.starts_with("env::") {
return Some("use windjammer_runtime::env;\n".to_string());
}
if module_base == "dialog"
|| module_base.starts_with("dialog::")
|| module_base == "encoding"
|| module_base.starts_with("encoding::")
|| module_base == "compute"
|| module_base.starts_with("compute::")
|| module_base == "net"
|| module_base.starts_with("net::")
|| module_base == "http"
|| module_base.starts_with("http::")
|| module_base == "storage"
|| module_base.starts_with("storage::")
{
return Some(String::new());
}
let rust_import = match module_name {
"http" => "windjammer_runtime::http",
"mime" => "windjammer_runtime::mime",
"json" => "windjammer_runtime::json",
"io" => "windjammer_runtime::io",
"subprocess" => "windjammer_runtime::subprocess",
"async" | "async_runtime" => "windjammer_runtime::async_runtime",
"cli" => "windjammer_runtime::cli",
"crypto" => "windjammer_runtime::crypto",
"csv" => "windjammer_runtime::csv_mod",
"db" => "windjammer_runtime::db",
"log" => "windjammer_runtime::log_mod",
"math" => "windjammer_runtime::math",
"random" => "windjammer_runtime::random",
"regex" => "windjammer_runtime::regex_mod",
"strings" => "windjammer_runtime::strings",
"testing" => "windjammer_runtime::testing",
"time" => "windjammer_runtime::time",
"game" => "windjammer_runtime::game",
_ => {
return Some(format!("use windjammer_runtime::{};\n", module_name));
}
};
if let Some(alias_name) = alias {
return Some(format!("use {} as {};\n", rust_import, alias_name));
}
if rust_import.ends_with("_mod") {
let original_name = rust_import
.strip_suffix("_mod")
.and_then(|s| s.split("::").last())
.unwrap_or(rust_import);
let mut result = format!("use {} as {};\n", rust_import, original_name);
match original_name {
"regex" => {
result.push_str(&format!("use {}::Regex;\n", rust_import));
}
"time" => {
result.push_str(&format!("use {}::{{Duration, Instant}};\n", rust_import));
}
_ => {}
}
return Some(result);
}
Some(format!("use {};\n", rust_import))
}
}