1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::path::{Path, PathBuf};
use anyhow::Result;
use xreq_lib::{KeyVal, KeyValType};
pub fn parse_key_val(s: &str) -> Result<KeyVal> {
let (kv_type, input) = match s.chars().next() {
Some(c) => match c {
'%' => (KeyValType::Header, &s[1..]),
'@' => (KeyValType::Body, &s[1..]),
'A'..='Z' | 'a'..='z' => (KeyValType::Query, s),
_ => return Err(anyhow::anyhow!("invalid key val pair: {}", s)),
},
None => return Err(anyhow::anyhow!("empty key-value pair is invalid")),
};
let mut parts = input.splitn(2, '=');
let key = parts.next().ok_or_else(|| anyhow::anyhow!("missing key"))?;
let val = parts
.next()
.ok_or_else(|| anyhow::anyhow!("missing value"))?;
Ok(KeyVal::new(kv_type, key, val))
}
pub fn get_config_file(s: &str) -> Result<PathBuf> {
let path = Path::new(s);
if path.exists() {
Ok(path.to_path_buf())
} else {
Err(anyhow::anyhow!("config file not found"))
}
}
pub fn get_default_config(name: &str) -> Result<PathBuf> {
let paths = [
format!("{}/.config/{}", std::env::var("HOME").unwrap(), name),
format!("./{}", name),
format!("/etc/{}", name),
];
for path in paths.iter() {
if Path::new(path).exists() {
return Ok(Path::new(path).to_path_buf());
}
}
Err(anyhow::anyhow!("Config file not found. You can either specify it with the --config option or put it in one of the following locations: {}", paths.join(", ")))
}