rainbeam_shared/
path.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
use std::path::{Path, PathBuf};
use std::env::current_dir as std_current_dir;
use std::io::Result;
use std::fmt::Display;

use serde::{Deserialize, Serialize};

/// [`PathBuf`] wrapper
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PathBufD(pub PathBuf);

impl PathBufD {
    pub fn push<P>(&mut self, path: P) -> ()
    where
        P: AsRef<Path>,
    {
        self.0.push(path)
    }

    pub fn join<P>(self, path: P) -> Self
    where
        P: AsRef<Path>,
    {
        Self(self.0.join(path))
    }
}

impl Default for PathBufD {
    fn default() -> Self {
        Self(PathBuf::default())
    }
}

impl Display for PathBufD {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0.to_str().unwrap_or(""))
    }
}

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

impl Into<PathBufD> for PathBuf {
    fn into(self) -> PathBufD {
        PathBufD(self)
    }
}

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

/// Get the current directory from env
pub fn current_dir() -> Result<PathBufD> {
    Ok(PathBufD(match std_current_dir() {
        Ok(p) => p,
        Err(e) => return Err(e),
    }))
}