From 5a15c931d0604068ea39711a443b0d55e77edc8a Mon Sep 17 00:00:00 2001 From: Jake Date: Fri, 24 Apr 2026 23:47:41 +0100 Subject: [PATCH] feat(phase9): write_text_file_cmd Thin UTF-8 writer used by the new save-dialog path. Caller owns path safety; the source path is always OS-dialog-provided. Two unit tests: roundtrips a small UTF-8 string with non-ASCII chars and asserts a nonexistent parent path returns an actionable error. Co-Authored-By: Claude Opus 4.7 (1M context) --- src-tauri/Cargo.toml | 5 ++++ src-tauri/src/commands/fs.rs | 44 +++++++++++++++++++++++++++++++++++ src-tauri/src/commands/mod.rs | 1 + 3 files changed, 50 insertions(+) create mode 100644 src-tauri/src/commands/fs.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 6844b01..61b985d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -74,6 +74,11 @@ libloading = "0.8" sqlx = { version = "0.8", default-features = false, features = ["runtime-tokio", "sqlite"] } uuid = { version = "1", features = ["v4"] } +[dev-dependencies] +# Phase 9 fs::write_text_file_cmd tests use a temp directory so we don't +# pollute the workspace. +tempfile = "3" + [target.'cfg(target_os = "linux")'.dependencies] webkit2gtk = "2.0" # Needed for setting the preview overlay's WindowTypeHint to Utility via diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs new file mode 100644 index 0000000..17a5b75 --- /dev/null +++ b/src-tauri/src/commands/fs.rs @@ -0,0 +1,44 @@ +// Phase 9. Thin filesystem write command for the save-dialog path. +// +// Path safety: the caller is expected to obtain `path` via the OS save +// dialog (tauri-plugin-dialog). We do not validate traversal, extension, +// or parent-dir existence beyond what the OS filesystem reports — the +// dialog already constrains the user's choice to what they can write to. + +/// Write UTF-8 text to a user-chosen path. Errors propagate the OS +/// message verbatim wrapped with the path so the toast on the frontend +/// is actionable ("Failed to write …: Permission denied" rather than a +/// bare error code). +#[tauri::command] +pub async fn write_text_file_cmd(path: String, contents: String) -> Result<(), String> { + tokio::fs::write(&path, contents) + .await + .map_err(|e| format!("Failed to write {path}: {e}")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn write_text_file_roundtrips_utf8() { + let dir = tempfile::tempdir().expect("tempdir"); + let path = dir.path().join("out.md").display().to_string(); + write_text_file_cmd(path.clone(), "hello\nwørld\n".into()) + .await + .expect("write"); + + let round = tokio::fs::read_to_string(&path).await.expect("read"); + assert_eq!(round, "hello\nwørld\n"); + } + + #[tokio::test] + async fn write_text_file_errors_on_bad_parent() { + let result = write_text_file_cmd( + "/definitely-not-a-real-path-kon-phase9/out.md".into(), + "x".into(), + ) + .await; + assert!(result.is_err(), "expected error for nonexistent parent"); + } +} diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 17e719b..504134c 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -2,6 +2,7 @@ pub mod audio; pub mod clipboard; pub mod diagnostics; pub mod feedback; +pub mod fs; pub mod hardware; pub mod hotkey; pub mod intentions;