The directory of the users home - Linux: /home/Alice - MacOS: /Users/Alice - Windows: C:\Users\Alice
(#[cfg_attr(windows, allow(unused_variables))] os: &Os)
| 73 | /// - MacOS: /Users/Alice |
| 74 | /// - Windows: C:\Users\Alice |
| 75 | pub fn home_dir(#[cfg_attr(windows, allow(unused_variables))] os: &Os) -> Result<PathBuf> { |
| 76 | #[cfg(unix)] |
| 77 | match cfg!(test) { |
| 78 | true => os |
| 79 | .env |
| 80 | .get("HOME") |
| 81 | .map_err(|_err| DirectoryError::NoHomeDirectory) |
| 82 | .and_then(|h| { |
| 83 | if h.is_empty() { |
| 84 | Err(DirectoryError::NoHomeDirectory) |
| 85 | } else { |
| 86 | Ok(h) |
| 87 | } |
| 88 | }) |
| 89 | .map(PathBuf::from) |
| 90 | .map(|p| os.fs.chroot_path(p)), |
| 91 | false => dirs::home_dir().ok_or(DirectoryError::NoHomeDirectory), |
| 92 | } |
| 93 | |
| 94 | #[cfg(windows)] |
| 95 | match cfg!(test) { |
| 96 | true => os |
| 97 | .env |
| 98 | .get("USERPROFILE") |
| 99 | .map_err(|_err| DirectoryError::NoHomeDirectory) |
| 100 | .and_then(|h| { |
| 101 | if h.is_empty() { |
| 102 | Err(DirectoryError::NoHomeDirectory) |
| 103 | } else { |
| 104 | Ok(h) |
| 105 | } |
| 106 | }) |
| 107 | .map(PathBuf::from) |
| 108 | .map(|p| os.fs.chroot_path(p)), |
| 109 | false => dirs::home_dir().ok_or(DirectoryError::NoHomeDirectory), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Get the macos tempdir from the `confstr` function |
| 114 | #[cfg(target_os = "macos")] |
no test coverage detected