summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: a78a767d20c425693b4ea2be917193e10b1996bc (plain) (blame)
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
94
95
96
use std::path::{Component, Path, PathBuf};

#[derive(Debug)]
pub enum NormalizeError {
    EmptyInput,
}

pub fn normalize_path(root: &Path, origin: &Path, input: &Path) -> Result<PathBuf, NormalizeError> {
    if input.as_os_str().is_empty() {
        return Err(NormalizeError::EmptyInput);
    }
    let mut stack = Vec::new();
    let origin_joint_input = origin.join(input);
    for component in origin_joint_input.components() {
        match component {
            Component::CurDir => (),
            Component::ParentDir => {
                stack.pop();
            }
            Component::Prefix(_) => stack.push(component),
            Component::Normal(_) => stack.push(component),
            Component::RootDir => stack.push(component),
        }
    }
    let normalized_origin_join_input = PathBuf::from_iter(stack);
    Ok(normalize_to_root(&normalized_origin_join_input, root))
}

fn normalize_to_root(target: &Path, mut root: &Path) -> PathBuf {
    let mut prefix = PathBuf::new();
    loop {
        if let Ok(suffix) = target.strip_prefix(root) {
            return prefix.join(suffix);
        }
        if let Some(new_root) = root.parent() {
            prefix.push("..");
            root = new_root;
        } else {
            return target.to_path_buf();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::{NormalizeError, normalize_path};

    #[test]
    fn empty_path_returns_error() {
        let root = Path::new("");
        let origin_dir = Path::new("");
        let input = Path::new("");
        let result = normalize_path(root, origin_dir, input);
        assert!(matches!(result, Err(NormalizeError::EmptyInput)));
    }

    #[test]
    fn plain_filename_with_root_at_cwd_returns_filename() {
        let root = Path::new("");
        let origin_dir = Path::new("");
        let input = Path::new("main.rs");
        let result = normalize_path(root, origin_dir, input);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Path::new("main.rs"));
    }

    #[test]
    fn relative_path_from_origin_is_resolved() {
        let root = Path::new("");
        let origin_dir = Path::new("src");
        let input = Path::new("../main.rs");
        let result = normalize_path(root, origin_dir, input);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Path::new("main.rs"));
    }

    #[test]
    fn path_is_made_relative_to_root() {
        let root = Path::new("/project");
        let origin_dir = Path::new("/project/src");
        let input = Path::new("main.rs");
        let result = normalize_path(root, origin_dir, input);
        assert_eq!(result.unwrap(), Path::new("src/main.rs"));
    }

    #[test]
    fn path_is_made_relative_to_root_from_outside() {
        let root = Path::new("/project");
        let origin_dir = Path::new("/outside");
        let input = Path::new("main.rs");
        let result = normalize_path(root, origin_dir, input);
        assert_eq!(result.unwrap(), Path::new("../outside/main.rs"));
    }
}