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
use std::{
ffi::OsString,
path::{Component, PathBuf},
};
fn check_and_swap(component: impl ToString) -> Option<impl ToString> {
let component = component.to_string();
if component.starts_with('$') {
let trimmed = component.strip_prefix('$')?;
std::env::var(trimmed).ok()
} else {
Some(component)
}
}
fn handle_components<'a>(c: Component<'a>) -> Option<OsString> {
match c {
Component::Normal(normal) => {
let comp_str = normal.to_str()?;
let swapped = check_and_swap(comp_str)?.to_string();
let os_str = OsString::from(swapped);
Some(os_str)
}
_ => None,
}
}
pub fn substitute(path: PathBuf) -> PathBuf {
let mut output = PathBuf::new();
for c in path.components() {
match handle_components(c) {
Some(s) => output.push(s),
None => output.push(c),
};
}
output
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic() {
std::env::set_var("TEST", "TEST_VAL");
let path = PathBuf::from("$TEST/file.txt");
let substituted = substitute(path);
assert_eq!(substituted, PathBuf::from("TEST_VAL/file.txt"));
}
#[test]
fn no_env() {
let path = PathBuf::from("$FAKE/file.txt");
let substituted = substitute(path.clone());
assert_eq!(substituted, path);
}
}