Add a shared error type and shared file helpers to the Rust host

Replace the three error styles with one Error type and one Result alias in
error.rs. The error crosses IPC as the same plain string as before. The
message text does not change.

Move the shared file mechanics to fsutil.rs. These are the reserved-name
check, the link and junction check, the limited reads, and the atomic
writes. Each module keeps its own path rules, limits, and messages. The
plugin settings writer now flushes its file to disk before the rename.

Remove the current_root_path and list_entries wrappers and an unused mime
value. Use named windows-sys constants for the process, file, drive, and
error codes.

Add a test that compares the command lists in lib.rs, build.rs, and the
capability files. The settings capability must be a subset of the full list.

In Cargo.toml, set the author, keep only the rlib crate type, and remove
the unused thiserror dependency. Remove panic = "abort" from the release
profile. A panic on a worker thread then stops only that thread. It does
not stop the editor with unsaved buffers.
This commit is contained in:
Knotty Wyvern
2026-10-27 22:41:11 -07:00
parent 69f90df571
commit bd46f3a189
18 changed files with 1103 additions and 882 deletions
+22 -21
View File
@@ -2,7 +2,8 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}};
use tauri::{AppHandle, Manager, State};
use crate::workspace::{current_root_path, WorkspaceState};
use crate::error::Result;
use crate::workspace::{current_root, WorkspaceState};
#[derive(Default)]
pub struct SettingsState(Mutex<HashMap<String, Arc<AtomicBool>>>);
@@ -14,7 +15,7 @@ struct PendingOpen<'a> {
}
impl SettingsState {
fn reserve(&self, label: &str) -> Result<Option<PendingOpen<'_>>, String> {
fn reserve(&self, label: &str) -> Result<Option<PendingOpen<'_>>> {
let mut pending = self.0.lock().map_err(|_| "Settings window lock failed.")?;
if let Some(cancelled) = pending.get(label) {
return if cancelled.load(Ordering::SeqCst) {
@@ -45,7 +46,7 @@ impl Drop for PendingOpen<'_> {
}
}
fn settings_label(owner: &str) -> Result<String, String> {
fn settings_label(owner: &str) -> Result<String> {
if owner == "main" || owner.starts_with("workspace-") {
Ok(format!("settings-{owner}"))
} else {
@@ -53,9 +54,9 @@ fn settings_label(owner: &str) -> Result<String, String> {
}
}
fn bind_workspace(workspace: &WorkspaceState, owner: &str, label: &str) -> Result<(), String> {
if let Ok(root) = current_root_path(workspace, owner) {
workspace.bind_terminal(label, root).map_err(|error| error.to_string())?;
fn bind_workspace(workspace: &WorkspaceState, owner: &str, label: &str) -> Result<()> {
if let Ok(root) = current_root(workspace, owner) {
workspace.bind_terminal(label, root)?;
} else {
workspace.close_window(label);
}
@@ -68,14 +69,14 @@ pub async fn settings_open_window(
window: tauri::Window,
state: State<'_, SettingsState>,
workspace: State<'_, WorkspaceState>,
) -> Result<(), String> {
) -> Result<()> {
let label = settings_label(window.label())?;
// The reservation survives native calls, but its mutex guard never does.
let Some(pending) = state.reserve(&label)? else { return Ok(()); };
let root = current_root_path(&workspace, window.label()).ok();
let root = current_root(&workspace, window.label()).ok();
let valid = || pending.can_publish(
app.get_webview_window(window.label()).is_some(),
&root, &current_root_path(&workspace, window.label()).ok(),
&root, &current_root(&workspace, window.label()).ok(),
);
if !valid() { return Err("The workspace closed while settings opened.".into()); }
bind_workspace(&workspace, window.label(), &label)?;
@@ -97,14 +98,14 @@ pub async fn settings_open_window(
.disable_drag_drop_handler()
.build() {
Ok(settings) => settings,
Err(error) => { workspace.close_window(&label); return Err(error.to_string()); }
Err(error) => { workspace.close_window(&label); return Err(error.into()); }
}
};
let result = (|| {
let result = (|| -> Result<()> {
if !valid() { return Err("The workspace changed while settings opened.".into()); }
settings.unminimize().map_err(|error| error.to_string())?;
settings.show().map_err(|error| error.to_string())?;
settings.set_focus().map_err(|error| error.to_string())?;
settings.unminimize()?;
settings.show()?;
settings.set_focus()?;
if !valid() { return Err("The workspace changed while settings opened.".into()); }
Ok(())
})();
@@ -174,7 +175,7 @@ mod tests {
#[test]
fn failed_build_releases_its_reservation_for_retry() {
let state = SettingsState::default();
let failed = || -> Result<(), String> {
let failed = || -> Result<()> {
let _pending = state.reserve("settings-main")?.unwrap();
Err("Native build failed".into())
};
@@ -203,20 +204,20 @@ mod tests {
workspace.bind_terminal("workspace-2", "/two".into()).unwrap();
bind_workspace(&workspace, "workspace-1", "settings-workspace-1").unwrap();
bind_workspace(&workspace, "workspace-2", "settings-workspace-2").unwrap();
assert_eq!(current_root_path(&workspace, "settings-workspace-1").unwrap(), std::path::PathBuf::from("/one"));
assert_eq!(current_root_path(&workspace, "settings-workspace-2").unwrap(), std::path::PathBuf::from("/two"));
assert_eq!(current_root(&workspace, "settings-workspace-1").unwrap(), std::path::PathBuf::from("/one"));
assert_eq!(current_root(&workspace, "settings-workspace-2").unwrap(), std::path::PathBuf::from("/two"));
workspace.close_window("settings-workspace-1");
assert!(current_root_path(&workspace, "settings-workspace-1").is_err());
assert!(current_root_path(&workspace, "workspace-1").is_ok());
assert!(current_root(&workspace, "settings-workspace-1").is_err());
assert!(current_root(&workspace, "workspace-1").is_ok());
}
#[test]
fn settings_without_a_workspace_never_use_the_recent_workspace() {
let workspace = WorkspaceState::new();
bind_workspace(&workspace, "main", "settings-main").unwrap();
assert!(current_root_path(&workspace, "settings-main").is_err());
assert!(current_root(&workspace, "settings-main").is_err());
workspace.bind_terminal("main", "/opened-later".into()).unwrap();
bind_workspace(&workspace, "main", "settings-main").unwrap();
assert_eq!(current_root_path(&workspace, "settings-main").unwrap(), std::path::PathBuf::from("/opened-later"));
assert_eq!(current_root(&workspace, "settings-main").unwrap(), std::path::PathBuf::from("/opened-later"));
}
}