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(name: String, mist_path: PathBuf, rust_path: PathBuf) -> Self {
24        let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };
25        this.constructor(name, mist_path, rust_path);
26        this
27    }
28
29    /* 12:5 */
30    pub fn constructor(self: &mut Self, name: String, mist_path: PathBuf, rust_path: PathBuf) -> () {
31        /* 13:9 */
32        self.children=Vec::new();
33        /* 14:9 */
34        self.name=name;
35        /* 15:9 */
36        self.path=mist_path;
37        /* 17:9 */
38        if self.path.is_dir() {
39            /* 18:13 */
40            let dir: PathBuf = self.path.clone();
41            /* 21:13 */
42            let pkg_gate: PathBuf = self.path.join("package.mist");
43            /* 22:13 */
44            if pkg_gate.exists() {
45                /* 23:17 */
46                self.path=pkg_gate;
47            }
48            /* 26:13 */
49            self.scan_directory(&dir, &rust_path);
50        }
51    }    /* 30:5 */
52    fn scan_directory(self: &mut Self, search_dir: &Path, rust_path: &Path) -> () {
53        /* 31:9 */
54        let entries = match fs::read_dir(search_dir) {            /* 32:13 */
55
56            Ok (entries,) => {entries}            /* 33:13 */
57
58            Err (e,) => {
59                /* 34:17 */
60                eprintln!("error: failed to read directory {}\n  {}", search_dir.display(), e);
61                /* 35:17 */
62                process::exit(1);
63            }
64        };
65        /* 39:9 */
66        for entry in entries{
67            /* 40:13 */
68            let entry = match entry {                /* 41:17 */
69
70                Ok (entry,) => {entry}                /* 42:17 */
71
72                Err (e,) => {
73                    /* 43:21 */
74                    eprintln!("error: failed to read directory entry\n  {}", e);
75                    /* 44:21 */
76                    process::exit(1);
77                }
78            };
79            /* 48:13 */
80            let child_path: PathBuf = entry.path();
81            /* 50:13 */
82            if child_path==self.path {
83                /* 51:17 */
84                                continue
85;
86            }
87            /* 54:13 */
88            let child_name: String = child_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
89            /* 59:13 */
90            let child_output: PathBuf = rust_path.join(&child_name).with_extension("rs");
91            /* 61:13 */
92            if !child_path.is_dir()&&should_skip(&child_path, &child_output) {
93                /* 62:17 */
94                                continue
95;
96            }
97            /* 65:13 */
98            self.children.push(Module::new(child_name, child_path, child_output));
99        }
100    }    /* 69:5 */
101    pub fn output_dir(self: &Self, parent_dir: &PathBuf) -> PathBuf {
102        /* 70:9 */
103        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
104            /* 71:13 */
105            parent_dir.join(&self.name)
106        } else {
107            /* 73:13 */
108            parent_dir.clone()
109        }
110    }    /* 77:5 */
111    pub fn output_path(self: &Self, parent_dir: &PathBuf) -> PathBuf {
112        /* 78:9 */
113        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
114            /* 79:13 */
115            parent_dir.join(&self.name).join("mod.rs")
116        } else {
117            /* 81:13 */
118            parent_dir.join(&self.name).with_extension("rs")
119        }
120    }}
121
122/* 86:1 */
123pub fn master_package(mist_path: PathBuf, rust_path: PathBuf) -> Module {
124    /* 87:5 */
125    let name: String = mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
126    /* 92:5 */
127    let mut root_mod = Module::new(name, mist_path.clone(), rust_path.clone());
128    /* 94:5 */
129    if !mist_path.is_dir() {
130        /* 95:9 */
131        let parent_dir: PathBuf = mist_path.parent().expect("Failed to get parent directory of root file").to_path_buf();
132        /* 99:9 */
133        root_mod.scan_directory(&parent_dir, &rust_path);
134    }
135    /* 102:5 */
136    root_mod
137}/* 105:1 */
138fn should_skip(source: &Path, output: &Path) -> bool {
139    /* 106:5 */
140    let src_time = fs::metadata(source).ok().and_then(|v| v.modified().ok());
141    /* 110:5 */
142    let out_time = fs::metadata(output).ok().and_then(|v| v.modified().ok());
143    /* 114:5 */
144    out_time.map(|out_time| out_time>=src_time.unwrap()).unwrap_or_default()
145}