plot/lib/logic/
file_toml_config.rs1use std::{fs, path::Path};
2
3use crate::lib::logic::{ConfigManifest, tag_time};
5
6pub struct IoConfig;
13
14impl IoConfig {
15 pub const DEFAULT_PATH: &'static str = "./.x-do.toml";
17
18 fn generate_default_content() -> String {
25 r#"
32# ============================================================================
33# 【ENG】 x-do Batch Configuration File
34# 【POL】 Plik konfiguracji wsadowej x-do
35# ============================================================================
36
37[[job]]
38id = "default_job"
39name = "snapshot-project"
40description = "【ENG】 Standard Rust project snapshot / 【POL】 Standardowy zrzut projektu Rust"
41quiet_work = true
42
43[job.scan]
44work_path = "."
45patterns = [
46 "./{.rustfmt,Cargo,rust-toolchain,Makefile}.toml&/",
47 "./**/*.rs&/",
48 "!./target/**",
49 "!./temp/**",
50 "!./.git/**",
51 "./.{gitattributes,gitignore}",
52 "./.github/workflows/*.yml&/",
53 "./.vscode/settings.json&/",
54 "./{API,ARCHITECTURE,AUTHORS,CHANGELOG,README,ROADMAP,TODO}.md"
55]
56ignore_case = false
57
58[job.spec]
59mode = "m"
60hide_stats = false
61hide_promo = false
62
63[job.spec.trimming]
64# show_page = 1
65# page_size = 20
66
67[job.table_sotc]
68sort_by = "kind"
69sort_order = "asc"
70reverse = false
71structure = "tree"
72columns = ["date", "time", "size", "tree", "icon", "number", "path"]
73trim_page = 1
74more_icons = false
75
76[job.export]
77save_sotc_at = "./target/.cargo-plot/File"
78title_sotc = "Rust Project Snapshot [Structure of the content]"
79save_cots_at = "./target/.cargo-plot/Docs"
80title_cots = "Rust Project Snapshot [Content of the structure]"
81"#
82 .to_string()
83 }
84
85 pub fn default_config_init_if_missing(path: &str) -> Result<(), String> {
91 if !Path::new(path).exists() {
92 Self::default_config_reset_force(path)?;
93 println!("🌱 Utworzono domyślny plik konfiguracyjny: {}", path);
94 }
95 Ok(())
96 }
97
98 pub fn default_config_reset_force(path: &str) -> Result<(), String> {
101 let content = Self::generate_default_content();
102 fs::write(path, content).map_err(|e| format!("❌ Błąd zapisu pliku konfiguracyjnego {}: {}", path, e))?;
103 Ok(())
104 }
105
106 pub fn default_config_reset_safe(path: &str) -> Result<(), String> {
109 if Path::new(path).exists() {
110 let tag = tag_time().0; let backup_path = format!("{}.{}.bak", path, tag);
112
113 fs::rename(path, &backup_path).map_err(|e| format!("❌ Błąd podczas tworzenia kopii zapasowej: {}", e))?;
114
115 println!("📦 Utworzono kopię zapasową starej konfiguracji: {}", backup_path);
116 }
117
118 Self::default_config_reset_force(path)?;
119 println!("🔄 Zresetowano plik konfiguracyjny (utworzono nowy: {})", path);
120
121 Ok(())
122 }
123
124 pub fn default_config_loader() -> Result<ConfigManifest, String> { Self::config_loader_from(Self::DEFAULT_PATH) }
130
131 pub fn config_loader_from(path: &str) -> Result<ConfigManifest, String> {
133 let content = fs::read_to_string(path).map_err(|e| format!("❌ Nie można odczytać pliku {}: {}", path, e))?;
134
135 toml::from_str(&content).map_err(|e| format!("❌ Błąd parsowania struktury TOML w pliku {}: {}", path, e))
137 }
138}