next_web_utils/system/
sys_path.rs1pub struct SystemPathUtil;
3
4impl SystemPathUtil {
5 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 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 pub fn temp_dir() -> Option<String> {
25 std::env::temp_dir().to_str().map(|s| s.to_string())
26 }
27
28 pub fn template_dir() -> Option<String> {
31 dirs::template_dir().and_then(|f| f.to_str().map(|s| s.to_string()))
32 }
33
34 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 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 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 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 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 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 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 #[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()); }
109
110 #[test]
111 fn test_home_dir() {
112 if let Some(home_dir) = SystemPathUtil::home_dir() {
113 assert!(!home_dir.is_empty()); }
115 }
116
117 #[test]
118 fn test_temp_dir() {
119 if let Some(temp_dir) = SystemPathUtil::temp_dir() {
120 assert!(!temp_dir.is_empty()); }
122 }
123
124 #[test]
125 fn test_template_dir() {
126 if let Some(template_dir) = SystemPathUtil::template_dir() {
127 assert!(!template_dir.is_empty()); }
129 }
130
131 #[test]
132 fn test_download_dir() {
133 if let Some(download_dir) = SystemPathUtil::download_dir() {
134 assert!(!download_dir.is_empty()); }
136 }
137
138 #[test]
139 fn test_picture_dir() {
140 if let Some(picture_dir) = SystemPathUtil::picture_dir() {
141 assert!(!picture_dir.is_empty()); }
143 }
144
145 #[test]
146 fn test_config_dir() {
147 if let Some(config_dir) = SystemPathUtil::config_dir() {
148 assert!(!config_dir.is_empty()); }
150 }
151
152 #[test]
153 fn test_data_dir() {
154 if let Some(data_dir) = SystemPathUtil::data_dir() {
155 assert!(!data_dir.is_empty()); }
157 }
158
159 #[test]
160 fn test_executable_dir() {
161 if let Some(executable_dir) = SystemPathUtil::executable_dir() {
162 assert!(!executable_dir.is_empty()); }
164 }
165
166 #[test]
167 fn test_cache_dir() {
168 if let Some(cache_dir) = SystemPathUtil::cache_dir() {
169 assert!(!cache_dir.is_empty()); }
171 }
172
173 #[test]
174 fn test_runtime_dir() {
175 if let Some(runtime_dir) = SystemPathUtil::runtime_dir() {
176 assert!(!runtime_dir.is_empty()); }
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()); }
186 }
187}