Fichiers
validateur/src-tauri/src/lib.rs
T
2026-10-11 01:56:32 +02:00

81 lignes
3.2 KiB
Rust

use std::fs;
use std::path::{Path, PathBuf};
/// Chemin par défaut, résolu à l'exécution depuis le profil utilisateur
/// (aucun chemin en dur). Renvoie None si la variable n'existe pas.
#[tauri::command]
fn chemin_par_defaut() -> Option<String> {
let home = std::env::var_os("USERPROFILE").or_else(|| std::env::var_os("HOME"))?;
let p = PathBuf::from(home)
.join("Documents").join("_CODE").join("_VercorsInventaire").join("vercors_points.json");
Some(p.to_string_lossy().into_owned())
}
/// Lecture seule du fichier source.
#[tauri::command]
fn lire_points(chemin: String) -> Result<serde_json::Value, String> {
let txt = fs::read_to_string(&chemin).map_err(|e| format!("Lecture {chemin} : {e}"))?;
serde_json::from_str(&txt).map_err(|e| format!("JSON invalide : {e}"))
}
fn chemin_validation(source: &str) -> PathBuf {
Path::new(source).with_file_name("vercors_validation.json")
}
#[tauri::command]
fn lire_validation(source: String) -> Result<Option<serde_json::Value>, String> {
let p = chemin_validation(&source);
if !p.exists() { return Ok(None); }
let txt = fs::read_to_string(&p).map_err(|e| e.to_string())?;
serde_json::from_str(&txt).map(Some).map_err(|e| format!("{} : JSON invalide : {e}", p.display()))
}
/// Écriture atomique : .tmp puis renommage ; l'ancienne version devient .bak.
fn ecrire_atomique(cible: &Path, contenu: &str) -> Result<(), String> {
let tmp = cible.with_extension("json.tmp");
fs::write(&tmp, contenu).map_err(|e| format!("Écriture {} : {e}", tmp.display()))?;
if cible.exists() {
let bak = cible.with_extension("json.bak");
fs::copy(cible, &bak).map_err(|e| format!("Sauvegarde .bak : {e}"))?;
}
fs::rename(&tmp, cible).map_err(|e| format!("Renommage : {e}"))
}
#[tauri::command]
fn sauver_validation(source: String, donnees: serde_json::Value) -> Result<String, String> {
if Path::new(&source).file_name().map(|n| n == "vercors_validation.json").unwrap_or(false) {
return Err("chemin source incohérent".into());
}
let p = chemin_validation(&source);
let txt = serde_json::to_string_pretty(&donnees).map_err(|e| e.to_string())?;
ecrire_atomique(&p, &txt)?;
Ok(p.to_string_lossy().into_owned())
}
#[tauri::command]
fn exporter(chemin: String, donnees: serde_json::Value) -> Result<(), String> {
let txt = serde_json::to_string_pretty(&donnees).map_err(|e| e.to_string())?;
ecrire_atomique(Path::new(&chemin), &txt)
}
/// Ouvre un lien http(s) dans le navigateur par défaut.
#[tauri::command]
fn ouvrir_lien(app: tauri::AppHandle, url: String) -> Result<(), String> {
use tauri_plugin_opener::OpenerExt;
if !(url.starts_with("https://") || url.starts_with("http://")) {
return Err("seuls les liens http(s) sont ouverts".into());
}
app.opener().open_url(url, None::<&str>).map_err(|e| e.to_string())
}
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![
chemin_par_defaut, lire_points, lire_validation, sauver_validation, exporter, ouvrir_lien
])
.run(tauri::generate_context!())
.expect("erreur au démarrage de Tauri");
}