Skip to main content

geometric_langlands_cli/
config.rs

1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::path::{Path, PathBuf};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Config {
7    pub database_path: PathBuf,
8    pub default_precision: u32,
9    pub max_iterations: u32,
10    pub convergence_threshold: f64,
11    pub computation: ComputationConfig,
12    pub visualization: VisualizationConfig,
13    pub neural: NeuralConfig,
14    pub repl: ReplConfig,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct ComputationConfig {
19    pub enable_parallel: bool,
20    pub enable_gpu: bool,
21    pub cache_results: bool,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct VisualizationConfig {
26    pub default_resolution: [u32; 2],
27    pub color_scheme: String,
28    pub enable_latex: bool,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct NeuralConfig {
33    pub default_architecture: String,
34    pub learning_rate: f64,
35    pub batch_size: usize,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct ReplConfig {
40    pub history_size: usize,
41    pub auto_save: bool,
42    pub prompt: String,
43}
44
45impl Default for Config {
46    fn default() -> Self {
47        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
48        let config_dir = home.join(".config").join("langlands-cli");
49        
50        Self {
51            database_path: config_dir.join("database.db"),
52            default_precision: 64,
53            max_iterations: 10000,
54            convergence_threshold: 1e-10,
55            computation: ComputationConfig {
56                enable_parallel: true,
57                enable_gpu: false,
58                cache_results: true,
59            },
60            visualization: VisualizationConfig {
61                default_resolution: [800, 600],
62                color_scheme: "viridis".to_string(),
63                enable_latex: true,
64            },
65            neural: NeuralConfig {
66                default_architecture: "langlands_v1".to_string(),
67                learning_rate: 0.001,
68                batch_size: 32,
69            },
70            repl: ReplConfig {
71                history_size: 1000,
72                auto_save: true,
73                prompt: "langlands> ".to_string(),
74            },
75        }
76    }
77}
78
79impl Config {
80    pub fn load(config_path: Option<&Path>) -> Result<Self> {
81        if let Some(path) = config_path {
82            if path.exists() {
83                let content = std::fs::read_to_string(path)?;
84                let config: Config = toml::from_str(&content)?;
85                return Ok(config);
86            }
87        }
88        
89        // Try default config location
90        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
91        let default_path = home.join(".config").join("langlands-cli").join("config.toml");
92        
93        if default_path.exists() {
94            let content = std::fs::read_to_string(default_path)?;
95            let config: Config = toml::from_str(&content)?;
96            Ok(config)
97        } else {
98            // Create default config
99            let config = Config::default();
100            
101            // Create config directory
102            if let Some(parent) = default_path.parent() {
103                std::fs::create_dir_all(parent)?;
104            }
105            
106            // Save default config
107            let content = toml::to_string_pretty(&config)?;
108            std::fs::write(default_path, content)?;
109            
110            Ok(config)
111        }
112    }
113    
114    pub fn save(&self, path: &Path) -> Result<()> {
115        if let Some(parent) = path.parent() {
116            std::fs::create_dir_all(parent)?;
117        }
118        
119        let content = toml::to_string_pretty(self)?;
120        std::fs::write(path, content)?;
121        Ok(())
122    }
123}