15 lines
1.0 KiB
Rust
15 lines
1.0 KiB
Rust
use tauri_plugin_dialog::DialogExt;
|
|
|
|
#[tauri::command]
|
|
pub async fn theme_export(app: tauri::AppHandle, contents: String, default_name: String) -> Result<Option<String>, String> {
|
|
if contents.len() > 1_048_576 { return Err("The theme exceeds the 1 MB limit.".into()); }
|
|
serde_json::from_str::<serde_json::Value>(&contents).map_err(|_| "The theme contains invalid JSON.".to_string())?;
|
|
let name = std::path::Path::new(&default_name).file_name().and_then(|name| name.to_str()).unwrap_or("theme.json").to_string();
|
|
tauri::async_runtime::spawn_blocking(move || {
|
|
let Some(file) = app.dialog().file().set_title("Export theme").add_filter("Theme JSON", &["json"]).set_file_name(name).blocking_save_file() else { return Ok(None); };
|
|
let path = file.into_path().map_err(|error| error.to_string())?;
|
|
std::fs::write(&path, contents).map_err(|error| format!("Could not export the theme: {error}"))?;
|
|
Ok(Some(path.to_string_lossy().into_owned()))
|
|
}).await.map_err(|error| error.to_string())?
|
|
}
|