Skip to main content

Mountain/IPC/WindServiceHandlers/NativeDialog/
ParseDialogFilters.rs

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