mars/src/client.rs

56 lines
1.5 KiB
Rust

#[macro_use]
extern crate log;
use std::env;
use std::process::exit;
use percent_encoding::{percent_decode, percent_encode, DEFAULT_ENCODE_SET};
use hyper::Client;
use hyper::rt::{self, Future};
use mars::{GeneralBuilder, builder_arguments_from_string};
fn main() {
beautylog::init(log::LevelFilter::Info).ok();
let current_dir = match env::current_dir() {
Err(e) => {
error!("couldn't find current directory: {}", e);
exit(1);
},
Ok(path) => {
let p = path.to_str();
p.unwrap().to_owned()
},
};
let args = env::args().skip(1).collect::<Vec<_>>().join("&");
let url = if args.is_empty() {
format!("http://localhost:1500{}", current_dir)
} else {
format!("http://localhost:1500{}?{}", current_dir, args)
};
let url = percent_encode(url.as_bytes(), DEFAULT_ENCODE_SET).to_string();
let url = url.parse::<hyper::Uri>().unwrap();
rt::run(fetch_url(url));
}
fn fetch_url(uri: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new();
client.get(uri.clone())
.map(|_| ())
// If there was an error, build in client
.map_err(move |_| {
warn!("{}", "server not listening, building in client...");
let mut builder = GeneralBuilder::new();
let uri = percent_decode(uri.path().as_bytes()).decode_utf8().unwrap();
let (path, args) = builder_arguments_from_string(&*uri);
let _ = builder.build(&path, &args);
})
}