mist-api 0.3.0-alpha.0

The Mist programming language module system
Documentation
/* 1:1 */
use std::path::Path;
/* 2:1 */
use std::path::PathBuf;
/* 3:1 */
use std::fs;
/* 4:1 */
use std::process;
#[derive(Debug)]
/* 7:1 */
pub struct Module {
    /* 8:5 */
    pub name: String,
    /* 9:5 */
    pub path: PathBuf,
    /* 10:5 */
    pub children: Vec<Self>,
}

impl Module<> {
    #[allow(invalid_value)]
    /* 12:5 */
    pub fn new(mist_path: PathBuf) -> Self {
        let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };
        this.constructor(mist_path);
        this
    }

    /* 12:5 */
    pub fn constructor(self: &mut Self, mist_path: PathBuf) -> () {
        /* 13:9 */
        self.children=Vec::new();
        /* 14:9 */
        self.name=mist_path.file_stem().and_then(|s| s.to_str()).expect("Failed to get module name").to_string();
        /* 18:9 */
        self.path=mist_path;
        /* 20:9 */
        if self.path.is_dir() {
            /* 21:13 */
            let dir: PathBuf = self.path.clone();
            /* 23:13 */
            let pkg_gate: PathBuf = self.path.join("package.mist");
            /* 24:13 */
            if pkg_gate.exists() {
                /* 25:17 */
                self.path=pkg_gate;
            }
            /* 28:13 */
            self.scan_directory(&dir);
        }
    }    /* 32:5 */
    fn scan_directory(self: &mut Self, search_dir: &Path) -> () {
        /* 33:9 */
        let entries = match fs::read_dir(search_dir) {            /* 34:13 */

            Ok (entries,) => {entries}            /* 35:13 */

            Err (e,) => {
                /* 36:17 */
                eprintln!("error: failed to read directory {}\n  {}", search_dir.display(), e);
                /* 37:17 */
                process::exit(1);
            }
        };
        /* 41:9 */
        for entry in entries{
            /* 42:13 */
            let entry = match entry {                /* 43:17 */

                Ok (entry,) => {entry}                /* 44:17 */

                Err (e,) => {
                    /* 45:21 */
                    eprintln!("error: failed to read directory entry\n  {}", e);
                    /* 46:21 */
                    process::exit(1);
                }
            };
            /* 50:13 */
            let child_path: PathBuf = entry.path();
            /* 52:13 */
            if child_path==self.path {
                /* 53:17 */
                                continue
;
            }
            /* 56:13 */
            self.children.push(Module::new(child_path));
        }
    }    /* 60:5 */
    pub fn output_dir(self: &Self, parent_dir: &PathBuf) -> PathBuf {
        /* 61:9 */
        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
            /* 62:13 */
            parent_dir.join(&self.name)
        } else {
            /* 64:13 */
            parent_dir.clone()
        }
    }    /* 68:5 */
    pub fn output_path(self: &Self, parent_dir: &PathBuf) -> PathBuf {
        /* 69:9 */
        if self.path.is_dir()||Some("package.mist")==self.path.file_name().unwrap().to_str() {
            /* 70:13 */
            parent_dir.join(&self.name).join("mod.rs")
        } else {
            /* 72:13 */
            parent_dir.join(&self.name).with_extension("rs")
        }
    }}

/* 77:1 */
pub fn master_package(mist_path: PathBuf) -> Module {
    /* 78:5 */
    let mut root_mod = Module::new(mist_path.clone());
    /* 80:5 */
    let parent_dir: &Path = mist_path.parent().expect("Failed to get parent directory of root file");
    /* 83:5 */
    root_mod.scan_directory(parent_dir);
    /* 85:5 */
    root_mod
}