use jstime_core as jstime;
use std::env;
use std::process;
use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(name = "jstime", rename_all = "kebab-case")]
struct Opt {
#[structopt()]
filename: Option<String>,
#[structopt(short, long)]
version: bool,
#[structopt(long)]
v8_options: Option<Vec<String>>,
}
fn main() {
let opt = Opt::from_args();
if opt.version {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
process::exit(0);
}
jstime::init(opt.v8_options);
let options = jstime::Options::default();
let mut jstime = jstime::JSTime::new(options);
if let Some(filename) = opt.filename {
match jstime.import(&filename) {
Ok(_) => {}
Err(e) => eprintln!("{}", e),
}
} else {
repl(jstime);
}
}
fn repl(mut jstime: jstime::JSTime) {
use rustyline::{error::ReadlineError, Editor};
let mut rl = Editor::<()>::new();
println!("Welcome to jstime v{}!", env!("CARGO_PKG_VERSION"));
loop {
let readline = rl.readline(">> ");
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str());
match jstime.run_script(&line, "REPL") {
Ok(v) => println!("{}", v),
Err(e) => eprintln!("Uncaught: {}", e),
}
}
Err(ReadlineError::Interrupted) => {
println!("Thanks for stopping by!");
break;
}
Err(ReadlineError::Eof) => {
println!("Eof'd");
break;
}
Err(err) => {
eprintln!("Error: {:?}", err);
break;
}
}
}
}