rush_sync_server/setup/
setup_toml.rs1use crate::core::prelude::*;
3use std::path::PathBuf;
4use tokio::fs;
5
6const DEFAULT_CONFIG: &str = r#"[general]
7max_messages = 100
8typewriter_delay = 30
9input_max_length = 100
10max_history = 30
11poll_rate = 16
12
13[theme]
14input_text = "Black"
15input_bg = "White"
16cursor = "Black"
17output_text = "DarkGray"
18output_bg = "Black"
19
20[prompt]
21text = "/// "
22color = "Black"
23
24[language]
25current = "en"
26"#;
27
28pub async fn ensure_config_exists() -> Result<PathBuf> {
29 let exe_path = std::env::current_exe().map_err(AppError::Io)?;
30 let base_dir = exe_path.parent().ok_or_else(|| {
31 AppError::Validation(get_translation("system.config.dir_error", &[]))
33 })?;
34
35 let config_dir = base_dir.join(".rss");
36 if !config_dir.exists() {
37 fs::create_dir_all(&config_dir)
38 .await
39 .map_err(AppError::Io)?;
40 log::debug!(
42 "{}",
43 get_translation(
44 "system.config.dir_created",
45 &[&config_dir.display().to_string()]
46 )
47 );
48 }
49
50 let config_path = config_dir.join("rush.toml");
51 if !config_path.exists() {
52 fs::write(&config_path, DEFAULT_CONFIG)
53 .await
54 .map_err(AppError::Io)?;
55 log::info!(
57 "{}",
58 get_translation(
59 "system.config.file_created",
60 &[&config_path.display().to_string()]
61 )
62 );
63 }
64
65 Ok(config_path)
66}
67
68pub fn get_config_paths() -> Vec<PathBuf> {
69 let mut paths = Vec::new();
70 if let Ok(exe_path) = std::env::current_exe() {
71 if let Some(base_dir) = exe_path.parent() {
72 paths.push(base_dir.join(".rss/rush.toml"));
73 paths.push(base_dir.join("rush.toml"));
74 paths.push(base_dir.join("config/rush.toml"));
75 }
76 }
77 #[cfg(debug_assertions)]
78 {
79 paths.push(PathBuf::from("rush.toml"));
80 paths.push(PathBuf::from("src/rush.toml"));
81 }
82 paths
83}