Files
mist/crates/mist-api/src/modules.mist
T
2026-06-11 21:13:59 +02:00

86 lines
2.5 KiB
Plaintext

use std::path::Path;
use std::path::PathBuf;
use std::fs;
use std::process;
use crate::transpiler::MistConfig;
#[derive(Debug)]
pub class Module {
pub name String;
pub path PathBuf;
pub children Vec<Box<Module>>;
pub constructor(mist_path PathBuf, config *MistConfig) {
self.children = Vec::new();
self.name = mist_path.file_stem()
.and_then(fn(s) -> s.to_str())
.expect("Failed to get module name")
.to_string();
self.path = mist_path;
if (self.path.is_dir()) {
let dir = self.path.clone();
let pkg_gate = self.path.join("package.mist");
if (pkg_gate.exists()) {
self.path = pkg_gate;
}
self.scan_directory(&dir, config);
} else if (config.should_ignore(&self.path)) {
let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf();
self.scan_directory(&dir, config);
}
}
fn scan_directory(*mut self, search_dir *Path, config *MistConfig) {
let entries = match (fs::read_dir(search_dir)) {
Ok[entries] => entries;
Err[e] => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
process::exit(1);
}
};
for (entry : entries) {
let entry = match (entry) {
Ok[entry] => entry;
Err[e] => {
eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1);
}
};
let child_path = entry.path();
if (child_path == self.path || child_path == config.package) {
continue;
}
self.children.push(Module::new(child_path, config));
}
}
pub fn is_package(*self, parent_dir *PathBuf) bool {
self.path.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() == Some("package.mist")
}
pub fn output_dir(*self, parent_dir *PathBuf) PathBuf {
if (self.path.is_dir() || self.is_package(parent_dir)) {
parent_dir.join(&self.name)
} else {
parent_dir.clone()
}
}
pub fn output_path(*self, parent_dir *PathBuf) PathBuf {
if (self.path.is_dir() || self.is_package(parent_dir)) {
parent_dir.join(&self.name).join("mod.rs")
} else {
parent_dir.join(&self.name).with_extension("rs")
}
}
}