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/* 6:1 */
10pub struct Module {
11    /* 7:5 */
12    pub name: String,
13    /* 8:5 */
14    pub path: PathBuf,
15    /* 9:5 */
16    pub children: Vec<Self>,
17}
18
19impl Module<> {
20    #[allow(invalid_value)]
21    /* 11:5 */
22    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    /* 11:5 */
29    pub fn constructor(self: &mut Self, name: String, mist_path: PathBuf, rust_path: PathBuf) -> () {
30        /* 12:9 */
31        self.children=Vec::new();
32        /* 13:9 */
33        self.name=name;
34        /* 14:9 */
35        self.path=mist_path;
36        /* 16:9 */
37        if self.path.is_dir() {
38            /* 17:13 */
39            let entries = match fs::read_dir(&self.path) {
40                Ok (entries,) => {entries}
41                Err (e,) => {
42                    /* 20:21 */
43                    eprintln!("error: failed to read directory {}\n  {}",
44                        self.path.display(),
45                        e);
46                    /* 26:21 */
47                    process::exit(1);
48                }
49            };
50            /* 30:13 */
51            for entry in entries{
52                /* 31:17 */
53                let entry = match entry {
54                    Ok (entry,) => {entry}
55                    Err (e,) => {
56                        /* 34:25 */
57                        eprintln!("error: failed to read directory entry\n  {}", e);
58                        /* 36:25 */
59                        process::exit(1);
60                    }
61                };
62                /* 40:17 */
63                let child_path: PathBuf = entry.path();
64                /* 42:17 */
65                let child_name: String = child_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
66                /* 47:17 */
67                let child_output: PathBuf = rust_path.join(&child_name).with_extension("rs");
68                /* 49:17 */
69                if should_skip(&child_path, &child_output) {
70                    /* 50:21 */
71                                        continue
72;
73                }
74                /* 53:17 */
75                self.children.push(Module::new(child_name, child_path, child_output));
76            }
77        }
78    }}
79
80/* 59:1 */
81pub fn new_package(mist_path: PathBuf, rust_path: PathBuf) -> Module {
82    /* 60:5 */
83    let name: String = mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
84    /* 65:5 */
85    Module::new(name, mist_path, rust_path)
86}/* 68:1 */
87fn should_skip(source: &Path, output: &Path) -> bool {
88    /* 69:5 */
89    let src_time = fs::metadata(source).ok().and_then(|v| v.modified().ok());
90    /* 73:5 */
91    let out_time = fs::metadata(output).ok().and_then(|v| v.modified().ok());
92    /* 77:5 */
93    out_time.map(|out_time| out_time>=src_time.unwrap()).unwrap_or_default()
94}