path.rs 1.0 KB

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