Skip to main content

Mountain/IPC/WindServiceHandlers/NativeDialog/
ParseDialogFilters.rs

1#![allow(non_snake_case)]
2//! Parse `options.filters` from a VS Code `showOpenDialog` call into the
3//! `DialogFilter` shape the Tauri dialog plugin accepts. Silently skips
4//! malformed or empty entries - the user still gets the picker, just
5//! without the filter hint.
6
7use serde_json::Value;
8
9use crate::IPC::WindServiceHandlers::NativeDialog::DialogFilter::DialogFilter;
10
11pub fn ParseDialogFilters(Options:&Value) -> Vec<DialogFilter> {
12	Options
13		.get("filters")
14		.and_then(Value::as_array)
15		.map(|Array| {
16			Array
17				.iter()
18				.filter_map(|Entry| {
19					let Name = Entry.get("name").and_then(Value::as_str).unwrap_or("Files").to_string();
20					let Extensions:Vec<String> = Entry
21						.get("extensions")
22						.and_then(Value::as_array)
23						.map(|List| List.iter().filter_map(|V| V.as_str().map(str::to_string)).collect())
24						.unwrap_or_default();
25					if Extensions.is_empty() {
26						None
27					} else {
28						Some(DialogFilter { Name, Extensions })
29					}
30				})
31				.collect()
32		})
33		.unwrap_or_default()
34}