path_disp/
lib.rs

1use std::{fmt::Display, path::Path};
2
3pub fn to_list<T: Display>(path: T) -> Vec<String> {
4    format!("{}", path)
5        .replace("\\\\", "/")
6        .replace("\\", "/")
7        .split("/")
8        .map(|s| s.to_owned())
9        .collect()
10}
11
12use path_absolutize::*;
13
14pub trait PathDisp {
15    fn to_list(&self) -> Vec<String>;
16    fn disp(&self) -> String;
17    fn disp_by(&self, sep: &str) -> String;
18    fn abs(&self) -> String;
19    fn resolve(&self, path: &str) -> String;
20}
21
22impl PathDisp for Path {
23    fn resolve(&self, path: &str) -> String {
24        std::path::Path::new(&format!("{}/{}", self.disp(), Self::new(path).disp()))
25            .absolutize()
26            .unwrap()
27            .to_str()
28            .unwrap()
29            .to_string()
30    }
31
32    fn to_list(&self) -> Vec<String> {
33        to_list(self.to_str().unwrap().to_string())
34    }
35
36    fn disp_by(&self, sep: &str) -> String {
37        self.to_list().join(sep)
38    }
39
40    fn disp(&self) -> String {
41        self.disp_by("/")
42    }
43
44    fn abs(&self) -> String {
45        self.resolve("")
46    }
47}
48
49#[cfg(test)]
50mod test {
51    use crate::*;
52    use std::path::Path;
53
54    #[test]
55    fn test() {
56        let path = Path::new("./src");
57        println!("{:?}", path.resolve("../a/b/../c"));
58    }
59}