serum_dev_tools/
path.rs

1#[macro_export]
2macro_rules! home_path {
3    ($my_struct:ident, $path:literal) => {
4        #[derive(Clone, Debug)]
5        pub struct $my_struct(String);
6
7        impl Default for $my_struct {
8            fn default() -> Self {
9                match dirs::home_dir() {
10                    None => {
11                        println!("$HOME doesn't exist. This probably won't do what you want.");
12                        $my_struct(".".to_string())
13                    }
14                    Some(mut path) => {
15                        path.push($path);
16                        $my_struct(path.as_path().display().to_string())
17                    }
18                }
19            }
20        }
21
22        impl ToString for $my_struct {
23            fn to_string(&self) -> String {
24                self.0.clone()
25            }
26        }
27
28        impl FromStr for $my_struct {
29            type Err = anyhow::Error;
30
31            fn from_str(s: &str) -> Result<Self, Self::Err> {
32                Ok(Self(s.to_string()))
33            }
34        }
35    };
36}