tauri_api/
path.rs

1use std::path::{Path, PathBuf};
2
3use serde_repr::{Deserialize_repr, Serialize_repr};
4
5/// A Base Directory to use.
6/// The base directory is the optional root of a FS operation.
7/// If informed by the API call, all paths will be relative to the path of the given directory.
8///
9/// For more information, check the [dirs_next documentation](https://docs.rs/dirs_next/).
10#[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
11#[repr(u16)]
12pub enum BaseDirectory {
13  /// The Audio directory.
14  Audio = 1,
15  /// The Cache directory.
16  Cache,
17  /// The Config directory.
18  Config,
19  /// The Data directory.
20  Data,
21  /// The LocalData directory.
22  LocalData,
23  /// The Desktop directory.
24  Desktop,
25  /// The Document directory.
26  Document,
27  /// The Download directory.
28  Download,
29  /// The Executable directory.
30  Executable,
31  /// The Font directory.
32  Font,
33  /// The Home directory.
34  Home,
35  /// The Picture directory.
36  Picture,
37  /// The Public directory.
38  Public,
39  /// The Runtime directory.
40  Runtime,
41  /// The Template directory.
42  Template,
43  /// The Video directory.
44  Video,
45  /// The Resource directory.
46  Resource,
47  /// The default App config directory.
48  /// Resolves to ${CONFIG_DIR}/${APP_NAME}
49  App,
50}
51
52/// Resolves the path with the optional base directory.
53///
54/// # Example
55/// ```
56/// use tauri_api::path::{resolve_path, BaseDirectory};
57/// let path = resolve_path("path/to/something", Some(BaseDirectory::Config))
58///   .expect("failed to resolve path");
59/// // path is equal to "/home/${whoami}/.config/path/to/something" on Linux
60/// ```
61pub fn resolve_path<P: AsRef<Path>>(path: P, dir: Option<BaseDirectory>) -> crate::Result<PathBuf> {
62  if let Some(base_dir) = dir {
63    let base_dir_path = match base_dir {
64      BaseDirectory::Audio => audio_dir(),
65      BaseDirectory::Cache => cache_dir(),
66      BaseDirectory::Config => config_dir(),
67      BaseDirectory::Data => data_dir(),
68      BaseDirectory::LocalData => local_data_dir(),
69      BaseDirectory::Desktop => desktop_dir(),
70      BaseDirectory::Document => document_dir(),
71      BaseDirectory::Download => download_dir(),
72      BaseDirectory::Executable => executable_dir(),
73      BaseDirectory::Font => font_dir(),
74      BaseDirectory::Home => home_dir(),
75      BaseDirectory::Picture => picture_dir(),
76      BaseDirectory::Public => public_dir(),
77      BaseDirectory::Runtime => runtime_dir(),
78      BaseDirectory::Template => template_dir(),
79      BaseDirectory::Video => video_dir(),
80      BaseDirectory::Resource => resource_dir(),
81      BaseDirectory::App => app_dir(),
82    };
83    if let Some(mut base_dir_path_value) = base_dir_path {
84      base_dir_path_value.push(path);
85      Ok(base_dir_path_value)
86    } else {
87      Err(crate::Error::Path("unable to determine base dir path".to_string()).into())
88    }
89  } else {
90    let mut dir_path = PathBuf::new();
91    dir_path.push(path);
92    Ok(dir_path)
93  }
94}
95
96/// Returns the path to the user's audio directory.
97pub fn audio_dir() -> Option<PathBuf> {
98  dirs_next::audio_dir()
99}
100
101/// Returns the path to the user's cache directory.
102pub fn cache_dir() -> Option<PathBuf> {
103  dirs_next::cache_dir()
104}
105
106/// Returns the path to the user's config directory.
107pub fn config_dir() -> Option<PathBuf> {
108  dirs_next::config_dir()
109}
110
111/// Returns the path to the user's data directory.
112pub fn data_dir() -> Option<PathBuf> {
113  dirs_next::data_dir()
114}
115
116/// Returns the path to the user's local data directory.
117pub fn local_data_dir() -> Option<PathBuf> {
118  dirs_next::data_local_dir()
119}
120
121/// Returns the path to the user's desktop directory.
122pub fn desktop_dir() -> Option<PathBuf> {
123  dirs_next::desktop_dir()
124}
125
126/// Returns the path to the user's document directory.
127pub fn document_dir() -> Option<PathBuf> {
128  dirs_next::document_dir()
129}
130
131/// Returns the path to the user's download directory.
132pub fn download_dir() -> Option<PathBuf> {
133  dirs_next::download_dir()
134}
135
136/// Returns the path to the user's executable directory.
137pub fn executable_dir() -> Option<PathBuf> {
138  dirs_next::executable_dir()
139}
140
141/// Returns the path to the user's font directory.
142pub fn font_dir() -> Option<PathBuf> {
143  dirs_next::font_dir()
144}
145
146/// Returns the path to the user's home directory.
147pub fn home_dir() -> Option<PathBuf> {
148  dirs_next::home_dir()
149}
150
151/// Returns the path to the user's picture directory.
152pub fn picture_dir() -> Option<PathBuf> {
153  dirs_next::picture_dir()
154}
155
156/// Returns the path to the user's public directory.
157pub fn public_dir() -> Option<PathBuf> {
158  dirs_next::public_dir()
159}
160
161/// Returns the path to the user's runtime directory.
162pub fn runtime_dir() -> Option<PathBuf> {
163  dirs_next::runtime_dir()
164}
165
166/// Returns the path to the user's template directory.
167pub fn template_dir() -> Option<PathBuf> {
168  dirs_next::template_dir()
169}
170
171/// Returns the path to the user's video dir
172pub fn video_dir() -> Option<PathBuf> {
173  dirs_next::video_dir()
174}
175
176/// Returns the path to the resource directory of this app.
177pub fn resource_dir() -> Option<PathBuf> {
178  crate::platform::resource_dir().ok()
179}
180
181fn app_name() -> crate::Result<String> {
182  let exe = std::env::current_exe()?;
183  let app_name = exe
184    .file_name()
185    .expect("failed to get exe filename")
186    .to_string_lossy();
187
188  Ok(app_name.to_string())
189}
190
191/// Returns the path to the suggested directory for your app config files.
192pub fn app_dir() -> Option<PathBuf> {
193  dirs_next::config_dir().and_then(|mut dir| {
194    if let Ok(app_name) = app_name() {
195      dir.push(app_name);
196      Some(dir)
197    } else {
198      None
199    }
200  })
201}