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
//// -- env-substitute
//// 
//// This crate provides the substitute function.
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,
    }
}

/// Takes a path, finds any components that begin with a `$` and then attempts to 
/// substitute the remainder of the component with an environment variable named
/// with that remainder.
///
/// An attempt to duplicate the functionality of shells.
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::*;

    /// Demonstrating that $TEST in a path is replaced with the value of the
    /// environment variable named TEST
    #[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"));
    }

    /// Demonstrating that if the environment variable is undefined or the
    /// substitution fails for some reason the path is unchanged
    #[test]
    fn no_env() {
        let path = PathBuf::from("$FAKE/file.txt");
        let substituted = substitute(path.clone());
        assert_eq!(substituted, path);
    }
}