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
+34 -39
View File
@@ -8,15 +8,10 @@ use portable_pty::{native_pty_system, CommandBuilder, MasterPty, PtySize};
use serde::Serialize;
use tauri::{AppHandle, Emitter, Manager, State};
use crate::workspace::{current_root_path, WorkspaceError, WorkspaceState};
use crate::error::{Error, Result};
use crate::workspace::{current_root, WorkspaceState};
use crate::terminal_platform::find_executable;
type PtyResult<T> = Result<T, WorkspaceError>;
fn err(msg: impl Into<String>) -> WorkspaceError {
WorkspaceError::from_message(msg)
}
pub struct PtyState {
inner: Mutex<HashMap<String, OpenPty>>,
}
@@ -153,7 +148,7 @@ PROMPT_COMMAND=("${PROMPT_COMMAND[@]}" __rs_prompt)
PS1="${PS1}"'\[\e]133;B\a\]'
"#;
fn spawn_shell(root: &Path, cols: u16, rows: u16, profile: Option<&str>, owner: &str) -> PtyResult<ShellProcess> {
fn spawn_shell(root: &Path, cols: u16, rows: u16, profile: Option<&str>, owner: &str) -> Result<ShellProcess> {
let system = native_pty_system();
let pair = system
.openpty(PtySize {
@@ -162,10 +157,10 @@ fn spawn_shell(root: &Path, cols: u16, rows: u16, profile: Option<&str>, owner:
pixel_width: 0,
pixel_height: 0,
})
.map_err(|e| err(e.to_string()))?;
.map_err(Error::from_display)?;
let profiles = pty_profiles();
let selected = profiles.iter().find(|p| profile.map_or(p.kind != "node", |id| p.id == id))
.ok_or_else(|| err("The selected shell is not installed. Select another terminal profile."))?;
.ok_or_else(|| Error::new("The selected shell is not installed. Select another terminal profile."))?;
let mut cmd = CommandBuilder::new(&selected.executable);
let mut integration_file = None;
match selected.kind.as_str() {
@@ -183,7 +178,7 @@ fn spawn_shell(root: &Path, cols: u16, rows: u16, profile: Option<&str>, owner:
}
"cmd" => { cmd.args(["/D", "/K", "prompt $E]633;Cwd;$P$E\\$E]133;A$E\\$P$G$E]133;B$E\\"]); }
"node" => { cmd.args(["--inspect=127.0.0.1:0", "-i"]); }
_ => return Err(err("Unsupported shell profile")),
_ => return Err(Error::new("Unsupported shell profile")),
}
cmd.cwd(&root);
cmd.env("TERM", "xterm-256color");
@@ -191,12 +186,12 @@ fn spawn_shell(root: &Path, cols: u16, rows: u16, profile: Option<&str>, owner:
let reader = pair
.master
.try_clone_reader()
.map_err(|e| err(e.to_string()))?;
let writer = pair.master.take_writer().map_err(|e| err(e.to_string()))?;
.map_err(Error::from_display)?;
let writer = pair.master.take_writer().map_err(Error::from_display)?;
let child = pair
.slave
.spawn_command(cmd)
.map_err(|e| err(e.to_string()))?;
.map_err(Error::from_display)?;
drop(pair.slave);
let session = OpenPty {
owner: owner.into(),
@@ -220,17 +215,17 @@ pub fn pty_open(
cwd: String,
profile: Option<String>,
directory: Option<String>,
) -> PtyResult<()> {
let root = current_root_path(&workspace, window.label())?;
) -> Result<()> {
let root = current_root(&workspace, window.label())?;
if root.to_string_lossy() != cwd {
return Err(err("workspace changed before the terminal started"));
return Err(Error::new("workspace changed before the terminal started"));
}
let mut guard = pty.inner.lock().map_err(|_| err("pty lock"))?;
let mut guard = pty.inner.lock().map_err(|_| Error::new("pty lock"))?;
if guard.contains_key(&id) {
return Err(err("terminal already exists"));
return Err(Error::new("terminal already exists"));
}
let directory = directory.map(PathBuf::from).unwrap_or(root);
if !directory.is_dir() { return Err(err("Terminal working directory does not exist")); }
if !directory.is_dir() { return Err(Error::new("Terminal working directory does not exist")); }
let (session, mut reader, mut child) = spawn_shell(&directory, cols, rows, profile.as_deref(), window.label())?;
guard.insert(id.clone(), session);
drop(guard);
@@ -282,21 +277,21 @@ pub fn pty_open(
}
#[tauri::command]
pub fn pty_write(pty: State<PtyState>, id: String, data: String) -> PtyResult<()> {
let guard = pty.inner.lock().map_err(|_| err("pty lock"))?;
let session = guard.get(&id).ok_or_else(|| err("terminal is not open"))?;
let mut writer = session.writer.lock().map_err(|_| err("pty writer lock"))?;
pub fn pty_write(pty: State<PtyState>, id: String, data: String) -> Result<()> {
let guard = pty.inner.lock().map_err(|_| Error::new("pty lock"))?;
let session = guard.get(&id).ok_or_else(|| Error::new("terminal is not open"))?;
let mut writer = session.writer.lock().map_err(|_| Error::new("pty writer lock"))?;
writer
.write_all(data.as_bytes())
.map_err(|e| err(e.to_string()))?;
.map_err(Error::from_display)?;
writer.flush().ok();
Ok(())
}
#[tauri::command]
pub fn pty_resize(pty: State<PtyState>, id: String, cols: u16, rows: u16) -> PtyResult<()> {
let guard = pty.inner.lock().map_err(|_| err("pty lock"))?;
let session = guard.get(&id).ok_or_else(|| err("terminal is not open"))?;
pub fn pty_resize(pty: State<PtyState>, id: String, cols: u16, rows: u16) -> Result<()> {
let guard = pty.inner.lock().map_err(|_| Error::new("pty lock"))?;
let session = guard.get(&id).ok_or_else(|| Error::new("terminal is not open"))?;
session
.master
.resize(PtySize {
@@ -305,13 +300,13 @@ pub fn pty_resize(pty: State<PtyState>, id: String, cols: u16, rows: u16) -> Pty
pixel_width: 0,
pixel_height: 0,
})
.map_err(|e| err(e.to_string()))?;
.map_err(Error::from_display)?;
Ok(())
}
#[tauri::command]
pub fn pty_close(pty: State<PtyState>, id: String) -> PtyResult<()> {
let mut guard = pty.inner.lock().map_err(|_| err("pty lock"))?;
pub fn pty_close(pty: State<PtyState>, id: String) -> Result<()> {
let mut guard = pty.inner.lock().map_err(|_| Error::new("pty lock"))?;
let session = guard.remove(&id);
drop(guard);
drop(session);
@@ -319,24 +314,24 @@ pub fn pty_close(pty: State<PtyState>, id: String) -> PtyResult<()> {
}
#[tauri::command]
pub async fn pty_new_window(app: AppHandle, window: tauri::Window, workspace: State<'_, WorkspaceState>, id: String) -> PtyResult<()> {
let root = current_root_path(&workspace, window.label())?;
if !id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') { return Err(err("Invalid window ID")); }
let mut url = tauri::Url::parse("http://localhost/").map_err(|e| err(e.to_string()))?;
pub async fn pty_new_window(app: AppHandle, window: tauri::Window, workspace: State<'_, WorkspaceState>, id: String) -> Result<()> {
let root = current_root(&workspace, window.label())?;
if !id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') { return Err(Error::new("Invalid window ID")); }
let mut url = tauri::Url::parse("http://localhost/").map_err(Error::from_display)?;
url.query_pairs_mut().append_pair("terminalWindow", &root.to_string_lossy());
let label = format!("terminal-{id}");
if app.get_webview_window(&label).is_some() { return Err(err("Terminal window already exists")); }
if app.get_webview_window(&label).is_some() { return Err(Error::new("Terminal window already exists")); }
workspace.bind_terminal(&label, root)?;
let result = tauri::WebviewWindowBuilder::new(&app, &label, tauri::WebviewUrl::App(format!("?{}", url.query().unwrap_or_default()).into()))
.title("RuneStone — Terminal").inner_size(1000.0, 650.0).min_inner_size(500.0, 300.0).build()
.map_err(|e| err(e.to_string()));
.map_err(Error::from_display);
if result.is_err() { workspace.close_window(&label); }
result?;
Ok(())
}
#[tauri::command]
pub fn pty_dictation() -> PtyResult<()> {
pub fn pty_dictation() -> Result<()> {
#[cfg(windows)]
{
use windows_sys::Win32::UI::Input::KeyboardAndMouse::{keybd_event, KEYEVENTF_KEYUP, VK_LWIN};
@@ -350,7 +345,7 @@ pub fn pty_dictation() -> PtyResult<()> {
Ok(())
}
#[cfg(not(windows))]
{ Err(err("Use your operating system's dictation shortcut with the terminal focused.")) }
{ Err(Error::new("Use your operating system's dictation shortcut with the terminal focused.")) }
}
#[cfg(test)]