Skip to content

Commit 255a4f3

Browse files
amrbashirSir-Thom
authored andcommitted
refactor: add methods and implement traits for FilePath and SafeFilePath (tauri-apps#1727)
* refactor: add methods and implement traits for `FilePath` and `SafeFilePath` closes tauri-apps#1726 * clippy * path -> as_path * fix prettierignore * Discard changes to Cargo.lock * Discard changes to Cargo.toml * update tauri deps
1 parent 5577938 commit 255a4f3

17 files changed

+370
-233
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"fs": patch
3+
"dialog": patch
4+
---
5+
6+
Add utility methods on `FilePath` and `SafeFilePath` enums which are:
7+
8+
- `path`
9+
- `simplified`
10+
- `into_path`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": patch
3+
"dialog": patch
4+
---
5+
6+
Implement `Serialize`, `Deserialize`, `From`, `TryFrom` and `FromStr` traits for `FilePath` and `SafeFilePath` enums.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": patch
3+
"dialog": patch
4+
---
5+
6+
Mark `Error` enum as `#[non_exhuastive]`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": patch
3+
"dialog": patch
4+
---
5+
6+
Add `SafeFilePath` enum.

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pnpm-lock.yaml
1212

1313
# examples gen directory
1414
examples/*/src-tauri/gen/
15-
plugins/examples/*/src-tauri/gen/
15+
plugins/*/examples/*/src-tauri/gen/
1616

1717
# autogenerated files
1818
**/autogenerated/**/*.md

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/dialog/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ serde_json = { workspace = true }
2626
tauri = { workspace = true }
2727
log = { workspace = true }
2828
thiserror = { workspace = true }
29-
dunce = { workspace = true }
3029
url = { workspace = true }
3130
tauri-plugin-fs = { path = "../fs", version = "2.0.0-rc.2" }
3231

plugins/dialog/src/commands.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub(crate) async fn open<R: Runtime>(
136136
let folders = dialog_builder.blocking_pick_folders();
137137
if let Some(folders) = &folders {
138138
for folder in folders {
139-
if let Ok(path) = folder.path() {
139+
if let Ok(path) = folder.clone().into_path() {
140140
if let Some(s) = window.try_fs_scope() {
141141
s.allow_directory(path, options.recursive);
142142
}
@@ -149,7 +149,7 @@ pub(crate) async fn open<R: Runtime>(
149149
} else {
150150
let folder = dialog_builder.blocking_pick_folder();
151151
if let Some(folder) = &folder {
152-
if let Ok(path) = folder.path() {
152+
if let Ok(path) = folder.clone().into_path() {
153153
if let Some(s) = window.try_fs_scope() {
154154
s.allow_directory(path, options.recursive);
155155
}
@@ -164,7 +164,7 @@ pub(crate) async fn open<R: Runtime>(
164164
let files = dialog_builder.blocking_pick_files();
165165
if let Some(files) = &files {
166166
for file in files {
167-
if let Ok(path) = file.path() {
167+
if let Ok(path) = file.clone().into_path() {
168168
if let Some(s) = window.try_fs_scope() {
169169
s.allow_file(&path);
170170
}
@@ -178,7 +178,7 @@ pub(crate) async fn open<R: Runtime>(
178178
let file = dialog_builder.blocking_pick_file();
179179

180180
if let Some(file) = &file {
181-
if let Ok(path) = file.path() {
181+
if let Ok(path) = file.clone().into_path() {
182182
if let Some(s) = window.try_fs_scope() {
183183
s.allow_file(&path);
184184
}
@@ -218,7 +218,7 @@ pub(crate) async fn save<R: Runtime>(
218218

219219
let path = dialog_builder.blocking_save_file();
220220
if let Some(p) = &path {
221-
if let Ok(path) = p.path() {
221+
if let Ok(path) = p.clone().into_path() {
222222
if let Some(s) = window.try_fs_scope() {
223223
s.allow_file(&path);
224224
}

plugins/dialog/src/error.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::{ser::Serializer, Serialize};
77
pub type Result<T> = std::result::Result<T, Error>;
88

99
#[derive(Debug, thiserror::Error)]
10+
#[non_exhaustive]
1011
pub enum Error {
1112
#[error(transparent)]
1213
Tauri(#[from] tauri::Error),
@@ -20,8 +21,6 @@ pub enum Error {
2021
FolderPickerNotImplemented,
2122
#[error(transparent)]
2223
Fs(#[from] tauri_plugin_fs::Error),
23-
#[error("URL is not a valid path")]
24-
InvalidPathUrl,
2524
}
2625

2726
impl Serialize for Error {

plugins/dialog/src/lib.rs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
1212
)]
1313

14-
use serde::{Deserialize, Serialize};
14+
use serde::Serialize;
1515
use tauri::{
1616
plugin::{Builder, TauriPlugin},
1717
Manager, Runtime,
@@ -24,6 +24,7 @@ use std::{
2424

2525
pub use models::*;
2626

27+
pub use tauri_plugin_fs::FilePath;
2728
#[cfg(desktop)]
2829
mod desktop;
2930
#[cfg(mobile)]
@@ -294,57 +295,6 @@ impl<R: Runtime> MessageDialogBuilder<R> {
294295
blocking_fn!(self, show)
295296
}
296297
}
297-
298-
/// Represents either a filesystem path or a URI pointing to a file
299-
/// such as `file://` URIs or Android `content://` URIs.
300-
#[derive(Debug, Deserialize, Serialize)]
301-
#[serde(untagged)]
302-
pub enum FilePath {
303-
Url(url::Url),
304-
Path(PathBuf),
305-
}
306-
307-
impl From<PathBuf> for FilePath {
308-
fn from(value: PathBuf) -> Self {
309-
Self::Path(value)
310-
}
311-
}
312-
313-
impl From<url::Url> for FilePath {
314-
fn from(value: url::Url) -> Self {
315-
Self::Url(value)
316-
}
317-
}
318-
319-
impl From<FilePath> for tauri_plugin_fs::FilePath {
320-
fn from(value: FilePath) -> Self {
321-
match value {
322-
FilePath::Path(p) => tauri_plugin_fs::FilePath::Path(p),
323-
FilePath::Url(url) => tauri_plugin_fs::FilePath::Url(url),
324-
}
325-
}
326-
}
327-
328-
impl FilePath {
329-
fn simplified(self) -> Self {
330-
match self {
331-
Self::Url(url) => Self::Url(url),
332-
Self::Path(p) => Self::Path(dunce::simplified(&p).to_path_buf()),
333-
}
334-
}
335-
336-
#[inline]
337-
fn path(&self) -> Result<PathBuf> {
338-
match self {
339-
Self::Url(url) => url
340-
.to_file_path()
341-
.map(PathBuf::from)
342-
.map_err(|_| Error::InvalidPathUrl),
343-
Self::Path(p) => Ok(p.to_owned()),
344-
}
345-
}
346-
}
347-
348298
#[derive(Debug, Serialize)]
349299
pub(crate) struct Filter {
350300
pub name: String,

0 commit comments

Comments
 (0)