aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: a83975a54da24e7f34094ff1ba312be802c50bca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#[macro_use]
extern crate log;

#[macro_use]
extern crate error_chain;

extern crate toml;
pub extern crate modelica_parser;

pub mod modelica_model;
pub mod transpile_scheme;
pub mod transpile_js;
pub mod repr_latex;

use std::path::Path;
use std::fs;
use std::io::Read;
use std::fs::File;

mod errors {
    // Create the Error, ErrorKind, ResultExt, and Result types
    error_chain! { }
}
pub use errors::*;

#[derive(Debug, PartialEq)]
pub enum ModelVarType {
    Independent,
    Dependent,
    Constant,
    Time,
    State,
    Parameter,
}

#[derive(Debug, PartialEq)]
pub struct ModelVar {
    pub slug: String,
    pub name_en: Option<String>,
    pub vtype: ModelVarType,
    pub latex: Option<String>,
    pub units_si: Option<String>,
}

#[derive(Debug, PartialEq)]
pub struct ModelEntry {
    pub ast: modelica_parser::ModelicaModel,
    pub markdown: String,
}

pub fn load_model_entry(p: &Path) -> Result<ModelEntry> {
    debug!("Attempting to load model from: {:?}", p);

    let ast = {
        let mut s = String::new();
        try!(File::open(p.join("model.modelica"))
            .and_then(|mut f| f.read_to_string(&mut s))
            .map_err(|e| e.to_string()));
        try!(modelica_parser::parse_model(&s).map_err(|e| format!("{:?}", e)))
    };

    let markdown = {
        let mut s = String::new();
        try!(File::open(p.join("page.md"))
            .and_then(|mut f| f.read_to_string(&mut s))
            .map_err(|e| e.to_string()));
        s
    };

    Ok(ModelEntry {
        ast: ast,
        markdown: markdown,
    })
}

pub fn search_models(p: &Path) -> Vec<String> {
    let metadata = match fs::metadata(p) {
        Err(_) => return vec![],
        Ok(m) => m,
    };
    if metadata.is_dir() {
        fs::read_dir(p)
            .unwrap()   // exists and is_dir(); could be a race condition though?
            .map(|x| x.unwrap())
            .filter(|x| x.metadata().unwrap().is_dir())
            .filter(|x| x.path().join("model.modelica").exists())
            .map(|x| x.path().to_string_lossy().to_string())
            .collect()
    } else {
        vec![]
    }
}