use std::fmt::Debug; use std::process::exit; use std::io::{self, Write}; use std::env::{current_exe, current_dir}; use std::path::{PathBuf, Path}; use mktemp::Temp; #[inline] pub fn result_override(r: Result, msg: String) -> Result { return match r { Ok(t) => Ok(t), Err(_) => Err(msg), }; } #[inline] pub fn result_prefix(r: Result, prefix: String) -> Result { return match r { Ok(t) => Ok(t), Err(e) => Err(format!("{}: {:?}", prefix, e)), }; } pub fn ok_or_exit(res: Result) -> T { return match res { Ok(k) => k, Err(err) => { writeln!(io::stderr(), "Error: {}", err).unwrap(); exit(1); } }; } #[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(); } pub fn resolve_path>(path: P) -> PathBuf { let base_dir = current_dir().unwrap(); return base_dir.join(path); } pub fn get_temp_file() -> PathBuf { return Temp::new_file().expect("Failed to create temporary file").to_path_buf(); } #[inline] pub fn path_to_string<'a>(path: &'a PathBuf) -> &'a str { return path.to_str().expect("Failed to parse path to string"); }