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
use std::fmt::Display;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

pub fn canonicalize(p: &Path) -> Result<CanonicalizedPathBuf> {
    fs::canonicalize(p)
        .with_context(|| format!("error opening file {}", p.to_str().unwrap()))
        .map(CanonicalizedPathBuf)
}

#[derive(Debug, Eq, PartialEq, Hash, Clone, Default)]
pub struct CanonicalizedPathBuf(PathBuf);

impl CanonicalizedPathBuf {
    pub fn to_str(&self) -> &str {
        self.0
            .to_str()
            .expect("encountered a non-utf8 character in file path!")
    }

    /// This function may panic, it's intended for test purposes!
    pub fn join<P: AsRef<Path>>(&self, path: P) -> Self {
        canonicalize(&self.0.join(path)).unwrap()
    }

    #[cfg(test)]
    pub fn path_buf(self) -> PathBuf {
        self.0
    }

    pub fn file_name(&self) -> &str {
        self.0
            .file_name()
            .unwrap()
            .to_str()
            .expect("encountered a non-utf8 character in file path!")
    }
}

impl Display for CanonicalizedPathBuf {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.display().fmt(f)
    }
}

impl AsRef<Path> for CanonicalizedPathBuf {
    fn as_ref(&self) -> &Path {
        &self.0
    }
}

impl From<CanonicalizedPathBuf> for PathBuf {
    fn from(cpb: CanonicalizedPathBuf) -> Self {
        cpb.0
    }
}

fn get_dependency_path<O: AsRef<Path>>(origin_path: O, path: &str) -> Result<CanonicalizedPathBuf> {
    let origin_path = origin_path.as_ref();
    let path = Path::new(path);
    let ret = if path.is_absolute() {
        path.to_path_buf()
    } else if origin_path.is_dir() {
        origin_path.join(path)
    } else {
        origin_path.parent().unwrap().join(path)
    };

    canonicalize(&ret)
}

pub trait RelativePath {
    fn get_dependency_path(&self, path: &str) -> Result<CanonicalizedPathBuf>;
}

impl<T: AsRef<Path>> RelativePath for T {
    fn get_dependency_path(&self, path: &str) -> Result<CanonicalizedPathBuf> {
        get_dependency_path(self.as_ref(), path)
    }
}