aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/mt-tool.rs
blob: 803bc4638e7bc44e04a3e2f1d6bab4b0eac122ee (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
94
95
96
97
98
99

extern crate modelthing;
extern crate rustc_serialize;
extern crate getopts;

use modelthing::modelica_parser;
use getopts::Options;
use std::env;
use std::io::Read;
use std::fs::File;
use std::path::Path;
use std::time::Instant;
use std::process::exit;


fn parse_modelica_files(paths: Vec<String>) {
    for input in &paths {
        let mut s = String::new();
        if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut s)) {
            println!("Input `{}`: I/O Error {}",
                     input, err);
            continue;
        }

        let time_stamp = Instant::now();
        let result = modelica_parser::parse_model(&s);
        let elapsed = time_stamp.elapsed();
        let elapsed = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1000_000_000.0;

        match result {
            Ok(_) => println!("Input `{}` ({}s): OK", input, elapsed),
            Err(err) => println!("Input `{}` ({}s): parse error {:?}", input, elapsed, err),
        }
    }
}

fn print_usage(opts: Options) {
    let brief = "usage:\tmt-tool <CMD> [options]";
    println!("");
    print!("{}", opts.usage(&brief));
    println!("Commands:\n\
        \tparse <file>  checks that file contains a valid modelica model\n\
        \tlist          dumps all the known models in database\n\
        \tload <dir>    checks that directory contains valid model and metadata\n\
");
}

fn main() {

    let args: Vec<String> = env::args().collect();

    let mut opts = Options::new();
    opts.optflag("h", "help", "print this help menu");
    opts.optflag("", "version", "print the version");

    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(f) => {
            println!("{}\n", f.to_string());
            print_usage(opts);
            exit(-1);
        }
    };

    if matches.opt_present("help") {
        print_usage(opts);
        return;
    }

    if matches.opt_present("version") {
        println!("modelthing {}", env!("CARGO_PKG_VERSION"));
        return;
    }

    if matches.free.is_empty() {
        println!("At least a CMD is required; see --help");
        exit(-1);
    }

    let cmd = matches.free[0].clone();
    match cmd.as_str() {
        "parse" => { parse_modelica_files(matches.free[1..].iter().map(|x| x.to_string()).collect()); },
        "list" => {
            // XXX: search path?
            for m in modelthing::search_models(Path::new("examples")) {
                println!("{}", m)
            }
        },
        "load" => {
            if matches.free.len() != 2 {
                println!("Expected a single path to load");
                exit(-1);
            }
            let me = modelthing::load_model_entry(Path::new(&matches.free[1])).unwrap();
            println!("{:?}", me);
        },
        unknown => { println!("Unknown command: {}", unknown); exit(-1); },
    }
}