next_web_utils/system/
sys_path.rs

1/// `SystemPathUtil` 是一个工具结构体,用于获取系统路径相关的信息。
2pub struct SystemPathUtil;
3
4impl SystemPathUtil {
5    /// 获取当前工作目录的路径。
6    /// 如果无法获取当前工作目录,则返回 `"./"` 作为默认值
7    pub fn current_dir() -> String {
8        std::env::current_dir()
9            .map(|s| s.to_str().map(|s| s.to_string()))
10            .map(|s| s.unwrap_or("./".into()))
11            .unwrap_or("./".into())
12    }
13
14    /// 获取用户的主目录路径。
15    /// 如果无法获取主目录路径,则返回 `None`
16    pub fn home_dir() -> Option<String> {
17        dirs::home_dir()
18            .map(|s| s.to_str().map(|s| s.to_string()))
19            .unwrap_or(None)
20    }
21
22    /// 获取系统的临时目录路径。
23    /// 如果无法获取临时目录路径,则返回 `None`
24    pub fn temp_dir() -> Option<String> {
25        std::env::temp_dir().to_str().map(|s| s.to_string())
26    }
27
28    /// 获取模板目录路径(如果存在)。
29    /// 如果无法获取模板目录路径,则返回 `None`
30    pub fn template_dir() -> Option<String> {
31        dirs::template_dir().and_then(|f| f.to_str().map(|s| s.to_string()))
32    }
33
34    /// 获取下载目录路径(如果存在)。
35    /// 如果无法获取下载目录路径,则返回 `None`
36    pub fn download_dir() -> Option<String> {
37        dirs::download_dir()
38            .map(|s| s.to_str().map(|s| s.to_string()))
39            .unwrap_or(None)
40    }
41
42    /// 获取图片目录路径(如果存在)。
43    /// 如果无法获取图片目录路径,则返回 `None`
44    pub fn picture_dir() -> Option<String> {
45        dirs::picture_dir()
46            .map(|s| s.to_str().map(|s| s.to_string()))
47            .unwrap_or(None)
48    }
49
50    /// 获取配置目录路径(如果存在)。
51    /// 如果无法获取配置目录路径,则返回 `None`
52    pub fn config_dir() -> Option<String> {
53        dirs::config_dir()
54            .map(|s| s.to_str().map(|s| s.to_string()))
55            .unwrap_or(None)
56    }
57
58    /// 获取数据目录路径(如果存在)。
59    /// 如果无法获取数据目录路径,则返回 `None`
60    pub fn data_dir() -> Option<String> {
61        dirs::data_dir()
62            .map(|s| s.to_str().map(|s| s.to_string()))
63            .unwrap_or(None)
64    }
65
66    /// 获取可执行文件所在的目录路径(如果存在)。
67    /// 如果无法获取可执行文件目录路径,则返回 `None`
68    pub fn executable_dir() -> Option<String> {
69        dirs::executable_dir()
70            .map(|s| s.to_str().map(|s| s.to_string()))
71            .unwrap_or(None)
72    }
73
74    /// 获取缓存目录路径(如果存在)。
75    /// 如果无法获取缓存目录路径,则返回 `None`
76    pub fn cache_dir() -> Option<String> {
77        dirs::cache_dir()
78            .map(|s| s.to_str().map(|s| s.to_string()))
79            .unwrap_or(None)
80    }
81
82    /// 获取运行时目录路径(如果存在)。
83    /// 如果无法获取运行时目录路径,则返回 `None`
84    pub fn runtime_dir() -> Option<String> {
85        dirs::runtime_dir()
86            .map(|s| s.to_str().map(|s| s.to_string()))
87            .unwrap_or(None)
88    }
89
90    /// 获取当前可执行文件的目录路径(仅限 Windows 平台)。
91    /// 如果无法获取可执行文件路径,则返回 `None`
92    #[cfg(target_os = "windows")]
93    pub fn current_exe_dir() -> Option<String> {
94        std::env::current_exe()
95            .map(|s| s.to_str().map(|s| s.to_string()))
96            .unwrap_or(None)
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn test_current_dir() {
106        let current_dir = SystemPathUtil::current_dir();
107        assert!(!current_dir.is_empty()); // 当前工作目录不应为空
108    }
109
110    #[test]
111    fn test_home_dir() {
112        if let Some(home_dir) = SystemPathUtil::home_dir() {
113            assert!(!home_dir.is_empty()); // 主目录路径不应为空
114        }
115    }
116
117    #[test]
118    fn test_temp_dir() {
119        if let Some(temp_dir) = SystemPathUtil::temp_dir() {
120            assert!(!temp_dir.is_empty()); // 临时目录路径不应为空
121        }
122    }
123
124    #[test]
125    fn test_template_dir() {
126        if let Some(template_dir) = SystemPathUtil::template_dir() {
127            assert!(!template_dir.is_empty()); // 模板目录路径不应为空
128        }
129    }
130
131    #[test]
132    fn test_download_dir() {
133        if let Some(download_dir) = SystemPathUtil::download_dir() {
134            assert!(!download_dir.is_empty()); // 下载目录路径不应为空
135        }
136    }
137
138    #[test]
139    fn test_picture_dir() {
140        if let Some(picture_dir) = SystemPathUtil::picture_dir() {
141            assert!(!picture_dir.is_empty()); // 图片目录路径不应为空
142        }
143    }
144
145    #[test]
146    fn test_config_dir() {
147        if let Some(config_dir) = SystemPathUtil::config_dir() {
148            assert!(!config_dir.is_empty()); // 配置目录路径不应为空
149        }
150    }
151
152    #[test]
153    fn test_data_dir() {
154        if let Some(data_dir) = SystemPathUtil::data_dir() {
155            assert!(!data_dir.is_empty()); // 数据目录路径不应为空
156        }
157    }
158
159    #[test]
160    fn test_executable_dir() {
161        if let Some(executable_dir) = SystemPathUtil::executable_dir() {
162            assert!(!executable_dir.is_empty()); // 可执行文件目录路径不应为空
163        }
164    }
165
166    #[test]
167    fn test_cache_dir() {
168        if let Some(cache_dir) = SystemPathUtil::cache_dir() {
169            assert!(!cache_dir.is_empty()); // 缓存目录路径不应为空
170        }
171    }
172
173    #[test]
174    fn test_runtime_dir() {
175        if let Some(runtime_dir) = SystemPathUtil::runtime_dir() {
176            assert!(!runtime_dir.is_empty()); // 运行时目录路径不应为空
177        }
178    }
179
180    #[cfg(target_os = "windows")]
181    #[test]
182    fn test_current_exe_dir() {
183        if let Some(current_exe_dir) = SystemPathUtil::current_exe_dir() {
184            assert!(!current_exe_dir.is_empty()); // 当前可执行文件目录路径不应为空
185        }
186    }
187}