devalang_core/config/
driver.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Deserialize, Clone, Serialize)]
5pub struct ProjectConfig {
6    pub defaults: ProjectConfigDefaults,
7    pub banks: Option<Vec<ProjectConfigBankEntry>>,
8    pub plugins: Option<Vec<PluginEntry>>,
9}
10
11#[derive(Debug, Deserialize, Clone, Serialize)]
12pub struct ProjectConfigDefaults {
13    pub entry: Option<String>,
14    pub output: Option<String>,
15    pub watch: Option<bool>,
16    pub repeat: Option<bool>,
17    pub debug: Option<bool>,
18    pub compress: Option<bool>,
19}
20
21#[derive(Debug, Deserialize, Clone, Serialize)]
22pub struct ProjectConfigBankMetadata {
23    pub bank: HashMap<String, String>,
24    pub triggers: Option<Vec<HashMap<String, String>>>,
25}
26
27#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
28pub struct ProjectConfigBankEntry {
29    pub path: String,
30    pub version: Option<String>,
31}
32
33#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
34pub struct PluginEntry {
35    pub path: String,
36    pub version: String,
37    pub author: String,
38    pub access: String,
39}
40
41impl ProjectConfig {
42    pub fn new() -> Self {
43        ProjectConfig {
44            defaults: ProjectConfigDefaults {
45                entry: None,
46                output: None,
47                watch: None,
48                repeat: None,
49                debug: None,
50                compress: None,
51            },
52            banks: Some(Vec::new()),
53            plugins: Some(Vec::new()),
54        }
55    }
56
57    pub fn with_defaults(
58        entry: Option<String>,
59        output: Option<String>,
60        watch: Option<bool>,
61        repeat: Option<bool>,
62        debug: Option<bool>,
63        compress: Option<bool>,
64    ) -> Self {
65        ProjectConfig {
66            defaults: ProjectConfigDefaults {
67                entry,
68                output,
69                watch,
70                repeat,
71                debug,
72                compress,
73            },
74            banks: Some(Vec::new()),
75            plugins: Some(Vec::new()),
76        }
77    }
78
79    pub fn get() -> Result<ProjectConfig, String> {
80        let root = std::env::current_dir().unwrap();
81        let config_path = root.join(".devalang");
82
83        if config_path.try_exists().is_err() {
84            return Err(format!(
85                "Config file not found at path: {}",
86                config_path.display()
87            ));
88        }
89
90        let config_content = std::fs::read_to_string(&config_path)
91            .map_err(|e| format!("Failed to read config file: {}", e))?;
92
93        let config: ProjectConfig = toml::from_str(&config_content)
94            .map_err(|e| format!("Failed to parse config file: {}", e))?;
95
96        Ok(config)
97    }
98
99    pub fn from_string(config_string: &str) -> Result<(Self, String), String> {
100        let config: ProjectConfig = toml::from_str(config_string)
101            .map_err(|e| format!("Failed to parse config string: {}", e))?;
102        let config_path = ".devalang".to_string();
103
104        Ok((config, config_path))
105    }
106
107    pub fn write(&self, new_config: &ProjectConfig) -> Result<(), String> {
108        let config_path = ".devalang";
109
110        let content = toml::to_string(new_config)
111            .map_err(|e| format!("Failed to serialize config: {}", e))?;
112
113        std::fs::write(config_path, content)
114            .map_err(|e| format!("Failed to write config to file '{}': {}", config_path, e))?;
115
116        Ok(())
117    }
118}