Continued work on saving files

This commit is contained in:
Iaphetes 2025-05-12 21:19:32 +02:00
parent 0006419b08
commit 3951f91901
2 changed files with 8 additions and 7 deletions

View file

@ -91,7 +91,7 @@ pub fn get_open_file_path(app_handle: tauri::AppHandle) -> Option<String> {
let open_file_path = config.core_cfg.current_file.clone(); let open_file_path = config.core_cfg.current_file.clone();
return open_file_path.map(|p| p.to_string_lossy().into_owned()); return open_file_path.map(|p| p.to_string_lossy().into_owned());
} }
pub fn set_open_file_path(app_handle: tauri::AppHandle, path: &String) { pub fn set_open_file_path(app_handle: tauri::AppHandle, path: &str) {
let mut config = app_handle.state::<Mutex<Config>>(); let mut config = app_handle.state::<Mutex<Config>>();
// Lock the mutex to get mutable access: // Lock the mutex to get mutable access:

View file

@ -4,7 +4,7 @@ use crate::{
get_basepath, get_basepath,
}; };
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput}; use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
use std::fs::read_to_string; use std::{fs::read_to_string, path::PathBuf};
use std::path::Path; use std::path::Path;
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url}; use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
@ -22,17 +22,18 @@ fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error
} }
#[tauri::command] #[tauri::command]
pub async fn save_file(app_handle: tauri::AppHandle, content: String) { pub async fn save_file(app_handle: tauri::AppHandle, content: String) -> Result<(), String> {
if let Some(path) = get_open_file_path(app_handle.clone()) { if let Some(path) = get_open_file_path(app_handle.clone()) {
if let Ok(file_content) = load_file(app_handle, path).await { if let Ok(file_content) = load_file(app_handle.clone(), &path).await {
// store_diff(&app_handle, &path, file_name, diff); store_diff(&app_handle, &path.clone(), &PathBuf::from(path).file_name().unwrap().to_string_lossy(), compare_content(&file_content, &content).map_err(|e| format!("{e:?}"))?);
println!("{:?}", compare_content(&file_content, &content)); println!("{:?}", compare_content(&file_content, &content));
} }
} }
Ok(())
} }
#[tauri::command] #[tauri::command]
pub async fn load_file(app_handle: tauri::AppHandle, path: String) -> Result<String, String> { pub async fn load_file(app_handle: tauri::AppHandle, path: &str) -> Result<String, String> {
set_open_file_path(app_handle, &path); set_open_file_path(app_handle, path);
read_to_string(path).map_err(|e| e.to_string()) read_to_string(path).map_err(|e| e.to_string())
} }