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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#[cfg(feature = "home_dir")]
mod home_dir {
    #[cfg(target_family = "windows")]
    mod home_dir_windows {
        use {
            core::slice::from_raw_parts,
            std::{
                ffi::{c_void, OsString},
                os::windows::prelude::OsStringExt,
                path::PathBuf,
                ptr::null_mut,
            },
            windows_sys::Win32::{
                Globalization::lstrlenW,
                System::Com::CoTaskMemFree,
                UI::Shell::{FOLDERID_Profile, SHGetKnownFolderPath},
            },
        };

        /// Return the path of the user's home directory.
        pub fn home_dir() -> Option<PathBuf> {
            let mut path_ptr = null_mut();
            (unsafe { SHGetKnownFolderPath(&FOLDERID_Profile, 0, 0, &mut path_ptr) } == 0)
                .then_some({
                    let wide = unsafe { from_raw_parts(path_ptr, lstrlenW(path_ptr) as usize) };
                    let ostr = OsString::from_wide(wide);
                    unsafe { CoTaskMemFree(path_ptr as *const c_void) }
                    ostr.into()
                })
        }
    }
    #[cfg(target_family = "windows")]
    pub use home_dir_windows::*;

    #[cfg(not(target_family = "windows"))]
    mod home_dir_ne_windows {
        use std::{env::var_os, path::PathBuf};
        const HOME: &'static str = "HOME";

        /// Return the path of the user's home directory.
        pub fn home_dir() -> Option<PathBuf> {
            var_os(HOME).map(Into::into)
        }
    }
    #[cfg(not(target_family = "windows"))]
    pub use home_dir_ne_windows::*;
}
#[cfg(feature = "home_dir")]
pub use home_dir::*;

#[cfg(feature = "expand_tilde")]
mod expand_tilde {
    use {
        super::home_dir,
        std::path::{Path, PathBuf},
    };
    const TILDE: &'static str = "~";

    /// Expand the tilde (`~`) from within the provided path.
    pub fn expand_tilde(path: impl AsRef<Path>) -> Option<PathBuf> {
        let p = path.as_ref();

        Some(if p.starts_with(TILDE) {
            let mut home = home_dir()?;

            if !p.ends_with(TILDE) {
                let mut cmpts = p.components();
                cmpts.next()?;
                home.extend(cmpts);
            }
            home
        } else {
            p.to_path_buf()
        })
    }
}
#[cfg(feature = "expand_tilde")]
pub use expand_tilde::*;

#[cfg(feature = "test")]
mod test {
    mod expand_tilde {
        #[test]
        fn expand_tilde_test() {
            let expected = dirs::home_dir().unwrap().join("foo");
            let expanded = crate::expand_tilde("~/foo").unwrap();
            assert_eq!(expanded, expected)
        }
    }

    mod home_dir {
        #[test]
        fn home_dir_test() {
            let expected = dirs::home_dir().unwrap();
            let home_dir = crate::home_dir().unwrap();
            assert_eq!(home_dir, expected)
        }
    }
}