1use std::path::Path;
3use std::path::PathBuf;
5use std::fs;
7use std::process;
9pub struct Module {
11 pub name: String,
13 pub path: PathBuf,
15 pub children: Vec<Self>,
17}
18
19impl Module<> {
20 #[allow(invalid_value)]
21 pub fn new(name: String, mist_path: PathBuf, rust_path: PathBuf) -> Self {
23 let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };
24 this.constructor(name, mist_path, rust_path);
25 this
26 }
27
28 pub fn constructor(self: &mut Self, name: String, mist_path: PathBuf, rust_path: PathBuf) -> () {
30 self.children=Vec::new();
32 self.name=name;
34 self.path=mist_path;
36 if self.path.is_dir() {
38 let entries = match fs::read_dir(&self.path) {
40 Ok (entries,) => {entries}
41 Err (e,) => {
42 eprintln!("error: failed to read directory {}\n {}",
44 self.path.display(),
45 e);
46 process::exit(1);
48 }
49 };
50 for entry in entries{
52 let entry = match entry {
54 Ok (entry,) => {entry}
55 Err (e,) => {
56 eprintln!("error: failed to read directory entry\n {}", e);
58 process::exit(1);
60 }
61 };
62 let child_path: PathBuf = entry.path();
64 let child_name: String = child_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
66 let child_output: PathBuf = rust_path.join(&child_name).with_extension("rs");
68 if should_skip(&child_path, &child_output) {
70 continue
72;
73 }
74 self.children.push(Module::new(child_name, child_path, child_output));
76 }
77 }
78 }}
79
80pub fn new_package(mist_path: PathBuf, rust_path: PathBuf) -> Module {
82 let name: String = mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
84 Module::new(name, mist_path, rust_path)
86}fn should_skip(source: &Path, output: &Path) -> bool {
88 let src_time = fs::metadata(source).ok().and_then(|v| v.modified().ok());
90 let out_time = fs::metadata(output).ok().and_then(|v| v.modified().ok());
92 out_time.map(|out_time| out_time>=src_time.unwrap()).unwrap_or_default()
94}