Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Opaque Return Type Conflict in pick_folders and pick_files Method #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct DialogFilter<'a> {
name: &'a str,
}

// Define a trait to represent the iterator of PathBuf items.
pub trait PathBufIterator: Iterator<Item = PathBuf> {}

// Implement the trait for the actual iterator type.
impl<I: Iterator<Item = PathBuf>> PathBufIterator for I {}

/// The file dialog builder.
///
/// Constructs file picker dialogs that can select single/multiple files or directories.
Expand Down Expand Up @@ -167,16 +173,17 @@ impl<'a> FileDialogBuilder<'a> {
/// ```
///
/// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled.
pub async fn pick_files(&mut self) -> crate::Result<Option<impl Iterator<Item = PathBuf>>> {
pub async fn pick_files(&mut self) -> crate::Result<Option<Box<dyn PathBufIterator>>> {
self.multiple = true;

let raw = inner::open(serde_wasm_bindgen::to_value(&self)?).await?;

if let Ok(files) = Array::try_from(raw) {
let files =
ArrayIterator::new(files).map(|raw| serde_wasm_bindgen::from_value(raw).unwrap());

Ok(Some(files))
let files = ArrayIterator::new(files)
.map(|raw| serde_wasm_bindgen::from_value(raw).unwrap())
.collect::<Vec<PathBuf>>();

Ok(Some(Box::new(files.into_iter()) as Box<dyn PathBufIterator>))
} else {
Ok(None)
}
Expand Down Expand Up @@ -218,17 +225,18 @@ impl<'a> FileDialogBuilder<'a> {
/// ```
///
/// Requires [`allowlist > dialog > open`](https://tauri.app/v1/api/config#dialogallowlistconfig.open) to be enabled.
pub async fn pick_folders(&mut self) -> crate::Result<Option<impl Iterator<Item = PathBuf>>> {
pub async fn pick_folders(&mut self) -> crate::Result<Option<impl PathBufIterator>> {
self.directory = true;
self.multiple = true;

let raw = inner::open(serde_wasm_bindgen::to_value(&self)?).await?;

if let Ok(files) = Array::try_from(raw) {
let files =
ArrayIterator::new(files).map(|raw| serde_wasm_bindgen::from_value(raw).unwrap());

Ok(Some(files))
let files = ArrayIterator::new(files)
.map(|raw| serde_wasm_bindgen::from_value(raw).unwrap())
.collect::<Vec<PathBuf>>();

Ok(Some(Box::new(files.into_iter()) as Box<dyn PathBufIterator>))
} else {
Ok(None)
}
Expand Down