Skip to main content

mist_api/
modules.rs

1/* 1:1 */
2use std::path::Path;
3/* 2:1 */
4use std::path::PathBuf;
5/* 3:1 */
6use std::fs;
7/* 4:1 */
8use std::process;
9#[derive(Debug)]
10/* 7:1 */
11pub struct Module {
12    /* 8:5 */
13    pub name: String,
14    /* 9:5 */
15    pub path: PathBuf,
16    /* 10:5 */
17    pub children: Vec<Self>,
18}
19
20impl Module<> {
21    #[allow(invalid_value)]
22    /* 12:5 */
23    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    /* 12:5 */
30    pub fn constructor(self: &mut Self, mist_path: PathBuf) -> () {
31        /* 13:9 */
32        self.children=Vec::new();
33        /* 14:9 */
34        self.name=mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
35        /* 18:9 */
36        self.path=mist_path;
37        /* 20:9 */
38        if self.path.is_dir() {
39            /* 21:13 */
40            let dir: PathBuf = self.path.clone();
41            /* 23:13 */
42            let pkg_gate: PathBuf = self.path.join("package.mist");
43            /* 24:13 */
44            if pkg_gate.exists() {
45                /* 25:17 */
46                self.path=pkg_gate;
47            }
48            /* 28:13 */
49            self.scan_directory(&dir);
50        }
51    }    /* 32:5 */
52    fn scan_directory(self: &mut Self, search_dir: &Path) -> () {
53        /* 33:9 */
54        let entries = match fs::read_dir(search_dir) {            /* 34:13 */
55
56            Ok (entries,) => {entries}            /* 35:13 */
57
58            Err (e,) => {
59                /* 36:17 */
60                eprintln!("error: failed to read directory {}\n  {}", search_dir.display(), e);
61                /* 37:17 */
62                process::exit(1);
63            }
64        };
65        /* 41:9 */
66        for entry in entries{
67            /* 42:13 */
68            let entry = match entry {                /* 43:17 */
69
70                Ok (entry,) => {entry}                /* 44:17 */
71
72                Err (e,) => {
73                    /* 45:21 */
74                    eprintln!("error: failed to read directory entry\n  {}", e);
75                    /* 46:21 */
76                    process::exit(1);
77                }
78            };
79            /* 50:13 */
80            let child_path: PathBuf = entry.path();
81            /* 52:13 */
82            if child_path==self.path {
83                /* 53:17 */
84                                continue
85;
86            }
87            /* 56:13 */
88            self.children.push(Module::new(child_path));
89        }
90    }    /* 60:5 */
91    pub fn output_dir(self: &Self, parent_dir: &PathBuf) -> PathBuf {
92        /* 61:9 */
93        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
94            /* 62:13 */
95            parent_dir.join(&self.name)
96        } else {
97            /* 64:13 */
98            parent_dir.clone()
99        }
100    }    /* 68:5 */
101    pub fn output_path(self: &Self, parent_dir: &PathBuf) -> PathBuf {
102        /* 69:9 */
103        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
104            /* 70:13 */
105            parent_dir.join(&self.name).join("mod.rs")
106        } else {
107            /* 72:13 */
108            parent_dir.join(&self.name).with_extension("rs")
109        }
110    }}
111
112/* 77:1 */
113pub fn master_package(mist_path: PathBuf) -> Module {
114    /* 78:5 */
115    let mut root_mod = Module::new(mist_path.clone());
116    /* 80:5 */
117    let parent_dir: &Path = mist_path.parent().expect("Failed to get parent directory of root file");
118    /* 83:5 */
119    root_mod.scan_directory(parent_dir);
120    /* 85:5 */
121    root_mod
122}