added file diffing for file saving

This commit is contained in:
Iaphetes 2025-03-13 21:26:12 +01:00
parent 5b31b76e69
commit df53857426
2 changed files with 21 additions and 2 deletions

View file

@ -2,12 +2,31 @@ use crate::{
config::{get_open_file_path, set_open_file_path},
get_basepath,
};
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
use std::fs::read_to_string;
use std::path::Path;
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error> {
// initializing the module
let dmp = DiffMatchPatch::new();
// create a list of diffs
let diffs = dmp.diff_main::<Compat>(old_content, new_content)?;
// Now, we are going to create a list of `patches` to be applied to the old text to get the new text
let patches = dmp.patch_make(PatchInput::new_diffs(&diffs))?;
// in the real world you are going to transmit or store this diff serialized to undiff format to be consumed or used somewhere elese
let patch_txt = dmp.patch_to_text(&patches);
Ok(patch_txt)
}
#[tauri::command]
pub async fn save_file(app_handle: tauri::AppHandle, content: String) {
println!("{:?}", get_open_file_path(app_handle));
if let Some(path) = get_open_file_path(app_handle.clone()) {
if let Ok(file_content) = load_file(app_handle, path).await {
println!("{:?}", compare_content(&file_content, &content));
}
}
}
#[tauri::command]