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
use crate::errors;

/// Return the current directoy as a PathBuf.
pub(crate) fn current_dir() -> std::path::PathBuf {
    std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
}

/// Return the current directory as a string.
pub(crate) fn current_dir_string() -> String {
    current_dir().to_string_lossy().to_string()
}

/// Return the home directory for the current user.
pub(crate) fn home_dir() -> std::path::PathBuf {
    dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("/tmp"))
}

/// Convert a Path into an absolute path.
pub fn abspath(path: &std::path::Path) -> std::path::PathBuf {
    path.to_path_buf()
        .canonicalize()
        .unwrap_or_else(|_| path.to_path_buf())
}

/// Strip a prefix from a path.
pub(crate) fn strip_prefix(
    root: &std::path::Path,
    path: &std::path::Path,
) -> Result<std::path::PathBuf, errors::GardenError> {
    let stripped_path = if path.starts_with(root) {
        // Is the path a child of the current garden root?
        path.strip_prefix(root)
            .map_err(|err| {
                errors::GardenError::ConfigurationError(format!(
                    "{path:?} is not a child of {root:?}: {err:?}"
                ))
            })?
            .to_path_buf()
    } else {
        path.to_path_buf()
    };

    Ok(stripped_path)
}

/// Strip a prefix from a path. Returns a path as a string.
pub(crate) fn strip_prefix_into_string(
    root: &std::path::Path,
    path: &std::path::Path,
) -> Result<String, errors::GardenError> {
    let path_str = strip_prefix(root, path)?.to_string_lossy().to_string();
    Ok(path_str)
}