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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::path::PathBuf;
use serde_repr::{Deserialize_repr, Serialize_repr};
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
#[repr(u16)]
pub enum BaseDirectory {
Audio = 1,
Cache,
Config,
Data,
LocalData,
Desktop,
Document,
Download,
Executable,
Font,
Home,
Picture,
Public,
Runtime,
Template,
Video,
Resource,
App,
}
pub fn resolve_path(path: String, dir: Option<BaseDirectory>) -> crate::Result<String> {
if let Some(base_dir) = dir {
let base_dir_path = match base_dir {
BaseDirectory::Audio => audio_dir(),
BaseDirectory::Cache => cache_dir(),
BaseDirectory::Config => config_dir(),
BaseDirectory::Data => data_dir(),
BaseDirectory::LocalData => local_data_dir(),
BaseDirectory::Desktop => desktop_dir(),
BaseDirectory::Document => document_dir(),
BaseDirectory::Download => download_dir(),
BaseDirectory::Executable => executable_dir(),
BaseDirectory::Font => font_dir(),
BaseDirectory::Home => home_dir(),
BaseDirectory::Picture => picture_dir(),
BaseDirectory::Public => public_dir(),
BaseDirectory::Runtime => runtime_dir(),
BaseDirectory::Template => template_dir(),
BaseDirectory::Video => video_dir(),
BaseDirectory::Resource => resource_dir(),
BaseDirectory::App => app_dir(),
};
if let Some(mut base_dir_path_value) = base_dir_path {
base_dir_path_value.push(path);
Ok(base_dir_path_value.to_string_lossy().to_string())
} else {
Err(crate::Error::Path("unable to determine base dir path".to_string()).into())
}
} else {
Ok(path)
}
}
pub fn audio_dir() -> Option<PathBuf> {
dirs::audio_dir()
}
pub fn cache_dir() -> Option<PathBuf> {
dirs::cache_dir()
}
pub fn config_dir() -> Option<PathBuf> {
dirs::config_dir()
}
pub fn data_dir() -> Option<PathBuf> {
dirs::data_dir()
}
pub fn local_data_dir() -> Option<PathBuf> {
dirs::data_local_dir()
}
pub fn desktop_dir() -> Option<PathBuf> {
dirs::desktop_dir()
}
pub fn document_dir() -> Option<PathBuf> {
dirs::document_dir()
}
pub fn download_dir() -> Option<PathBuf> {
dirs::download_dir()
}
pub fn executable_dir() -> Option<PathBuf> {
dirs::executable_dir()
}
pub fn font_dir() -> Option<PathBuf> {
dirs::font_dir()
}
pub fn home_dir() -> Option<PathBuf> {
dirs::home_dir()
}
pub fn picture_dir() -> Option<PathBuf> {
dirs::picture_dir()
}
pub fn public_dir() -> Option<PathBuf> {
dirs::public_dir()
}
pub fn runtime_dir() -> Option<PathBuf> {
dirs::runtime_dir()
}
pub fn template_dir() -> Option<PathBuf> {
dirs::template_dir()
}
pub fn video_dir() -> Option<PathBuf> {
dirs::video_dir()
}
pub fn resource_dir() -> Option<PathBuf> {
crate::platform::resource_dir().ok()
}
fn app_name() -> crate::Result<String> {
let exe = std::env::current_exe()?;
let app_name = exe
.file_name()
.expect("failed to get exe filename")
.to_string_lossy();
Ok(app_name.to_string())
}
pub fn app_dir() -> Option<PathBuf> {
dirs::config_dir().and_then(|mut dir| {
if let Ok(app_name) = app_name() {
dir.push(app_name);
Some(dir)
} else {
None
}
})
}