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
use derive_more::Into;
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;

#[derive(Debug, Into)]
pub struct BaseDir(PathBuf);

impl Default for BaseDir {
    fn default() -> Self {
        BaseDir(
            dirs::home_dir()
                .unwrap_or_else(|| PathBuf::from("/tmp"))
                .join("src"),
        )
    }
}

impl fmt::Display for BaseDir {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.display().fmt(f)
    }
}

impl FromStr for BaseDir {
    type Err = <PathBuf as FromStr>::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        PathBuf::from_str(s).map(BaseDir)
    }
}