2017-07-26 15:22:20 +01:00
|
|
|
use std::fmt::Debug;
|
2017-09-06 18:20:45 +01:00
|
|
|
use std::process::exit;
|
|
|
|
use std::io::{self, Write};
|
2017-09-10 16:23:40 +01:00
|
|
|
use std::env::{current_exe, current_dir};
|
2017-09-09 23:28:18 +01:00
|
|
|
use std::path::PathBuf;
|
2017-09-06 18:20:45 +01:00
|
|
|
|
2017-07-26 15:22:20 +01:00
|
|
|
|
2017-07-26 15:28:44 +01:00
|
|
|
#[inline]
|
2017-07-26 15:22:20 +01:00
|
|
|
pub fn result_override<T, E: Debug>(r: Result<T, E>, msg: String) -> Result<T, String> {
|
|
|
|
return match r {
|
|
|
|
Ok(t) => Ok(t),
|
2017-07-26 15:33:13 +01:00
|
|
|
Err(_) => Err(msg),
|
2017-07-26 15:22:20 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:28:44 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn result_prefix<T, E: Debug>(r: Result<T, E>, prefix: String) -> Result<T, String> {
|
2017-07-26 15:22:20 +01:00
|
|
|
return match r {
|
|
|
|
Ok(t) => Ok(t),
|
2017-07-26 15:33:13 +01:00
|
|
|
Err(e) => Err(format!("{}: {:?}", prefix, e)),
|
2017-07-26 15:22:20 +01:00
|
|
|
};
|
|
|
|
}
|
2017-09-06 18:20:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
pub fn ok_or_exit<T>(res: Result<T, String>) -> T {
|
|
|
|
return match res {
|
|
|
|
Ok(k) => k,
|
|
|
|
Err(err) => {
|
2017-09-10 16:24:26 +01:00
|
|
|
writeln!(io::stderr(), "Error: {}", err).unwrap();
|
2017-09-06 18:20:45 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-09-09 23:28:18 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get_exe_dir() -> PathBuf {
|
|
|
|
return current_exe()
|
|
|
|
.expect("Failed to get exe location")
|
|
|
|
.parent()
|
|
|
|
.expect("Failed to get exe directory")
|
|
|
|
.to_path_buf();
|
|
|
|
}
|
2017-09-10 16:23:40 +01:00
|
|
|
|
|
|
|
pub fn resolve_path(path: String) -> PathBuf {
|
|
|
|
let base_dir = current_dir().unwrap();
|
|
|
|
return base_dir.join(path);
|
|
|
|
}
|