extern crate modelthing; extern crate rustc_serialize; extern crate getopts; use modelthing::modelica_parser; use modelthing::transpile_scheme::TranspileScheme; use modelthing::transpile_js::TranspileJS; 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) { 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 `{}` ({}sec): ERROR\n{}", input, elapsed, modelthing::pp_parseerror(&s, err)), } } } fn print_usage(opts: Options) { let brief = "usage:\tmt-tool [options]"; println!(""); print!("{}", opts.usage(&brief)); println!(""); println!(r#"Commands: parse checks that file contains a valid modelica model list dumps all the known models in database load checks that directory contains valid model and metadata transpile_js converts modelica to javascript function transpile_scheme converts modelica to scheme function "#); } fn main() { let args: Vec = 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()); }, "transpile_scheme" => { 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.ast.repr_scheme().unwrap()); }, "transpile_js" => { 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.ast.repr_js().unwrap()); }, "list" => { let search_path = if matches.free.len() >= 2 { matches.free[1].clone() } else { "examples".to_string() }; for m in modelthing::search_models(Path::new(&search_path)) { 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); }, } }