aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: f93f7fad7638168de13673a839185a6d7826a3c1 (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

#[macro_use]
extern crate log;

extern crate toml;
extern crate modelica_parser;

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

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

#[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, String> {
    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> {
    if fs::metadata(p).unwrap().is_dir() {
        fs::read_dir(p)
            .unwrap()
            .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![]
    }
}