1use std::path::Path;
3use std::path::PathBuf;
5use std::fs;
7use std::process;
9#[derive(Debug)]
10pub struct Module {
12 pub name: String,
14 pub path: PathBuf,
16 pub children: Vec<Self>,
18}
19
20impl Module<> {
21 #[allow(invalid_value)]
22 pub fn new(mist_path: PathBuf) -> Self {
24 let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };
25 this.constructor(mist_path);
26 this
27 }
28
29 pub fn constructor(self: &mut Self, mist_path: PathBuf) -> () {
31 self.children=Vec::new();
33 self.name=mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
35 self.path=mist_path;
37 if self.path.is_dir() {
39 let dir: PathBuf = self.path.clone();
41 let pkg_gate: PathBuf = self.path.join("package.mist");
43 if pkg_gate.exists() {
45 self.path=pkg_gate;
47 }
48 self.scan_directory(&dir);
50 }
51 } fn scan_directory(self: &mut Self, search_dir: &Path) -> () {
53 let entries = match fs::read_dir(search_dir) { Ok (entries,) => {entries} Err (e,) => {
59 eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
61 process::exit(1);
63 }
64 };
65 for entry in entries{
67 let entry = match entry { Ok (entry,) => {entry} Err (e,) => {
73 eprintln!("error: failed to read directory entry\n {}", e);
75 process::exit(1);
77 }
78 };
79 let child_path: PathBuf = entry.path();
81 if child_path==self.path {
83 continue
85;
86 }
87 self.children.push(Module::new(child_path));
89 }
90 } pub fn output_dir(self: &Self, parent_dir: &PathBuf) -> PathBuf {
92 if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
94 parent_dir.join(&self.name)
96 } else {
97 parent_dir.clone()
99 }
100 } pub fn output_path(self: &Self, parent_dir: &PathBuf) -> PathBuf {
102 if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
104 parent_dir.join(&self.name).join("mod.rs")
106 } else {
107 parent_dir.join(&self.name).with_extension("rs")
109 }
110 }}
111
112pub fn master_package(mist_path: PathBuf) -> Module {
114 let mut root_mod = Module::new(mist_path.clone());
116 let parent_dir: &Path = mist_path.parent().expect("Failed to get parent directory of root file");
118 root_mod.scan_directory(parent_dir);
120 root_mod
122}