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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::error;
use glob;
use std::path;
use std::env;
#[derive(Debug)]
pub struct Paths {
config: path::PathBuf,
cache: path::PathBuf,
data: path::PathBuf,
}
impl Paths {
pub fn new() -> error::Result<Self> {
match env::var_os("RQ_SYSTEM_DIR") {
Some(basepath) => {
let basepath = path::Path::new(&basepath);
Ok(Self {
config: basepath.join("config"),
cache: basepath.join("cache"),
data: basepath.join("data"),
})
},
None => match directories::ProjectDirs::from("io", "dflemstr", "rq") {
Some(dirs) => Ok(Self {
config: dirs.config_dir().into(),
cache: dirs.cache_dir().into(),
data: dirs.data_dir().into(),
}),
None => Err(error::Error::Message(format!(
"The environment variable RQ_SYSTEM_DIR is unspecified and no home directory is known"
))),
},
}
}
pub fn preferred_config<P>(&self, path: P) -> path::PathBuf
where
P: AsRef<path::Path>,
{
let mut result = self.config.clone();
result.push(path);
result
}
pub fn preferred_cache<P>(&self, path: P) -> path::PathBuf
where
P: AsRef<path::Path>,
{
let mut result = self.cache.clone();
result.push(path);
result
}
pub fn preferred_data<P>(&self, path: P) -> path::PathBuf
where
P: AsRef<path::Path>,
{
let mut result = self.data.clone();
result.push(path);
result
}
pub fn find_config(&self, pattern: &str) -> error::Result<Vec<path::PathBuf>> {
find(&self.config, pattern)
}
pub fn find_data(&self, pattern: &str) -> error::Result<Vec<path::PathBuf>> {
find(&self.data, pattern)
}
}
fn find(home: &path::Path, pattern: &str) -> error::Result<Vec<path::PathBuf>> {
let mut result = Vec::new();
run_pattern(home, pattern, &mut result)?;
Ok(result)
}
fn run_pattern(
dir: &path::Path,
pattern: &str,
result: &mut Vec<path::PathBuf>,
) -> error::Result<()> {
let full_pattern = format!("{}/{}", dir.to_string_lossy(), pattern);
for entry in glob::glob(&full_pattern)? {
result.push(entry?);
}
Ok(())
}