feat: implement async methods for saving and opening file

This commit is contained in:
Stevan Freeborn
2026-01-24 20:51:26 -06:00
parent f5d98197ac
commit 072ad61eb9
3 changed files with 56 additions and 1 deletions
Generated
+10
View File
@@ -3656,6 +3656,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" 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]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.7.5+spec-1.1.0" version = "0.7.5+spec-1.1.0"
@@ -4849,6 +4858,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"iced", "iced",
"rfd", "rfd",
"tokio",
"uuid", "uuid",
] ]
+1
View File
@@ -6,4 +6,5 @@ edition = "2024"
[dependencies] [dependencies]
iced = { version = "0.14.0", features = ["image", "markdown", "highlighter"] } iced = { version = "0.14.0", features = ["image", "markdown", "highlighter"] }
rfd = "0.17.1" rfd = "0.17.1"
tokio = { version ="1.49.0", features = ["fs"] }
uuid = { version = "1.19.0", features = ["v4"] } uuid = { version = "1.19.0", features = ["v4"] }
+45 -1
View File
@@ -3,11 +3,55 @@ use std::{
path::PathBuf, 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<PathBuf>, text: String) -> Result<PathBuf, String> {
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) { pub fn save_file_to_disk(path: PathBuf, text: String) {
let save_result = write(path, text); let save_result = write(path, text);
match save_result { match save_result {
Ok(_) => {}, Ok(_) => {}
Err(err) => eprintln!("Error: {}", err), Err(err) => eprintln!("Error: {}", err),
} }
} }