plot/lib/logic/
fs_file.rs1use std::{fs, path::Path};
2
3
4
5pub fn file_ensure(path: &Path, content: &str) {
7 if let Some(parent) = path.parent() {
8 let _ = fs::create_dir_all(parent);
9 }
10 if !path.exists() {
11 let _ = fs::write(path, content);
12 }
13}
14pub fn file_backup(path: &Path) {
16 if path.exists() {
17 let mut new_name = path.file_name().unwrap_or_default().to_os_string();
18 new_name.push(".bak");
19 let backup_path = path.with_file_name(new_name);
20 let _ = fs::copy(path, backup_path);
21 }
22}
23pub fn file_save_force(path: &Path, content: &str) {
24 if let Some(parent) = path.parent() {
25 let _ = fs::create_dir_all(parent);
26 }
27 fs::write(path, content).expect("Błąd krytyczny: Nie udało się zapisać pliku");
28}
29
30pub fn file_save_safe(path: &Path, content: &str) {
32 file_backup(path);
33 file_save_force(path, content);
34}
35
36pub fn file_remove(path: &Path) {
37 if path.exists() {
38 let _ = fs::remove_file(path);
41 }
42}
43
44pub fn file_save_safe_if_changed(path: &Path, content: &str) {
47 if path.exists() {
48 if let Ok(existing_content) = fs::read_to_string(path) {
50 if existing_content == content {
51 return;
53 }
54 }
55 }
56
57 file_save_safe(path, content);
60}