65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
use onig::Regex;
|
|
use std::collections::HashMap;
|
|
pub use std::path::PathBuf;
|
|
|
|
pub type WrapperFnRet<'a> = Result<Box<dyn SubprogramWithArguments>, String>;
|
|
pub type WrapperFn<'a> = fn(&HashMap<String, String>) -> WrapperFnRet<'a>;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Subprogram<'a> {
|
|
pub name: &'a str,
|
|
pub wrapper: WrapperFn<'a>,
|
|
}
|
|
|
|
impl Subprogram<'_> {
|
|
pub fn buildable_from(&self, arg: &str) -> bool {
|
|
self.name == arg.split(':').take(1).collect::<Vec<&str>>()[0]
|
|
}
|
|
pub fn make_callable(&self, arg: &str) -> WrapperFnRet<'_> {
|
|
let re = Regex::new(r"(?<!~):").unwrap();
|
|
(self.wrapper)(
|
|
&(re.split(arg)
|
|
.skip(1)
|
|
.map(|x| x.replace("~:", ":"))
|
|
.collect::<Vec<String>>()
|
|
.chunks_exact(2)
|
|
.map(|x| (x[0].clone(), x[1].clone()))
|
|
.collect()),
|
|
)
|
|
}
|
|
}
|
|
|
|
pub trait SubprogramWithArguments {
|
|
fn call(&self) -> Result<(), String>;
|
|
}
|
|
|
|
pub fn get_from(hm: &HashMap<String, String>, key: &str, default: &str) -> Result<String, String> {
|
|
Ok(hm.get(key).unwrap_or(&default.to_string()).clone())
|
|
}
|
|
|
|
use std::str::FromStr;
|
|
|
|
pub fn parse_from<T: FromStr>(
|
|
hm: &HashMap<String, String>,
|
|
key: &str,
|
|
default: &str,
|
|
) -> Result<T, String> {
|
|
let value: String = get_from(hm, key, default).unwrap();
|
|
match value.parse::<T>() {
|
|
Ok(v) => Ok(v),
|
|
Err(_) => Err(format!("Could not parse {:?}", value)),
|
|
}
|
|
}
|
|
|
|
pub fn get_db_dir(subfolder: Option<&str>) -> PathBuf {
|
|
let new_path: PathBuf = match subfolder {
|
|
Some(n) => PathBuf::from("db").join(n),
|
|
None => PathBuf::from("db"),
|
|
};
|
|
std::fs::create_dir_all(&new_path).unwrap();
|
|
new_path
|
|
}
|
|
|
|
pub static FIREFOX_USER_AGENT: &str =
|
|
"Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0";
|