pathkit/
core.rs

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
use anyhow::Result;
use path_absolutize::Absolutize;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::{Path as StdPath, PathBuf};

#[derive(Debug)]
pub struct Path {
    pub(super) path: PathBuf,
}

impl Path {
    pub fn new(path: impl AsRef<StdPath>) -> Self {
        return Path {
            path: path.as_ref().to_path_buf(),
        };
    }

    pub fn absolutize(&self) -> Result<Self> {
        return Ok(Self::new(self.path.absolutize()?));
    }

    pub fn absolutize_from(&self, cwd: impl AsRef<StdPath>) -> Result<Self> {
        return Ok(Self::new(self.path.absolutize_from(cwd)?));
    }

    pub fn absolutize_virtually(&self, virtual_root: impl AsRef<StdPath>) -> Result<Self> {
        return Ok(Self::new(self.path.absolutize_virtually(virtual_root)?));
    }

    pub fn canonicalize(&self) -> Result<Self, std::io::Error> {
        return std::fs::canonicalize(&self.path).map(Self::new);
    }

    pub fn extension(&self) -> Option<&OsStr> {
        return self.path.extension();
    }

    pub fn file_name(&self) -> Option<&OsStr> {
        return self.path.file_name();
    }

    pub fn file_stem(&self) -> Option<&OsStr> {
        return self.path.file_stem();
    }

    pub fn is_absolute(&self) -> bool {
        return self.path.is_absolute();
    }

    pub fn join(&self, path: impl AsRef<StdPath>) -> Self {
        return Self::new(self.path.join(path));
    }

    pub fn parent(&self) -> Option<Self> {
        return self.path.parent().map(Self::new);
    }

    pub fn to_str(&self) -> Option<&str> {
        return self.path.to_str();
    }

    pub fn to_string_lossy(&self) -> Cow<'_, str> {
        return self.path.to_string_lossy();
    }
}