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
+45 -1
View File
@@ -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<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) {
let save_result = write(path, text);
match save_result {
Ok(_) => {},
Ok(_) => {}
Err(err) => eprintln!("Error: {}", err),
}
}