git_clone_canonical/
basedir.rs

1use std::convert::AsRef;
2use std::fmt;
3use std::path::{Path, PathBuf};
4use std::str::FromStr;
5
6#[derive(Debug)]
7pub struct BaseDir(PathBuf);
8
9impl Default for BaseDir {
10    fn default() -> Self {
11        BaseDir(
12            dirs::home_dir()
13                .unwrap_or_else(|| PathBuf::from("/tmp"))
14                .join("src"),
15        )
16    }
17}
18
19impl AsRef<Path> for BaseDir {
20    fn as_ref(&self) -> &Path {
21        self.0.as_ref()
22    }
23}
24
25impl fmt::Display for BaseDir {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        self.0.display().fmt(f)
28    }
29}
30
31impl FromStr for BaseDir {
32    type Err = <PathBuf as FromStr>::Err;
33
34    fn from_str(s: &str) -> Result<Self, Self::Err> {
35        PathBuf::from_str(s).map(BaseDir)
36    }
37}