From 072ad61eb95719a9f7bc8f4718ba80e970cf5f24 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sat, 24 Jan 2026 20:26:30 -0600 Subject: [PATCH] feat: implement async methods for saving and opening file --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/io.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 751dd12..f8c50af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3656,6 +3656,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tokio" +version = "1.49.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" +dependencies = [ + "pin-project-lite", +] + [[package]] name = "toml_datetime" version = "0.7.5+spec-1.1.0" @@ -4849,6 +4858,7 @@ version = "0.1.0" dependencies = [ "iced", "rfd", + "tokio", "uuid", ] diff --git a/Cargo.toml b/Cargo.toml index 8daeef9..6750e27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2024" [dependencies] iced = { version = "0.14.0", features = ["image", "markdown", "highlighter"] } rfd = "0.17.1" +tokio = { version ="1.49.0", features = ["fs"] } uuid = { version = "1.19.0", features = ["v4"] } diff --git a/src/io.rs b/src/io.rs index 605df36..b97fc03 100644 --- a/src/io.rs +++ b/src/io.rs @@ -3,11 +3,55 @@ use std::{ path::PathBuf, }; +use rfd::AsyncFileDialog; +use tokio::fs; + +pub async fn open_file() -> Result<(PathBuf, String), String> { + let handle_result = AsyncFileDialog::new() + .set_directory("/") + .pick_file() + .await + .ok_or(String::from("Dialog cancelled")); + + match handle_result { + Ok(handle) => { + let path = handle.path().to_owned(); + let content_result = fs::read_to_string(&path).await.map_err(|e| e.to_string()); + + match content_result { + Ok(content) => Ok((path, content)), + Err(e) => Err(e), + } + } + Err(e) => Err(e), + } +} + +pub async fn save_file(path: Option, text: String) -> Result { + let save_path = match path { + Some(p) => p, + None => AsyncFileDialog::new() + .set_directory("/") + .save_file() + .await + .ok_or(String::from("Dialog cancelled"))? + .path() + .to_owned(), + }; + + let save_result = fs::write(&save_path, text).await.map_err(|e| e.to_string()); + + match save_result { + Ok(_) => Ok(save_path), + Err(err) => Err(err), + } +} + pub fn save_file_to_disk(path: PathBuf, text: String) { let save_result = write(path, text); match save_result { - Ok(_) => {}, + Ok(_) => {} Err(err) => eprintln!("Error: {}", err), } }