devalang_core/config/
driver.rs1use std::collections::HashMap;
2
3use serde::{ Deserialize, Serialize };
4
5#[derive(Debug, Deserialize, Clone, Serialize)]
6pub struct Config {
7 pub defaults: ConfigDefaults,
8 pub banks: Option<Vec<BankEntry>>,
9 pub plugins: Option<Vec<PluginEntry>>,
10}
11
12#[derive(Debug, Deserialize, Clone, Serialize)]
13pub struct ConfigDefaults {
14 pub entry: Option<String>,
15 pub output: Option<String>,
16 pub watch: Option<bool>,
17 pub repeat: Option<bool>,
18}
19
20#[derive(Debug, Deserialize, Clone, Serialize)]
21pub struct BankMetadata {
22 pub bank: HashMap<String, String>,
23 pub triggers: Option<Vec<HashMap<String, String>>>,
24}
25
26#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
27pub struct BankEntry {
28 pub path: String,
29 pub version: Option<String>,
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
33pub struct PluginEntry {
34 pub path: String,
35 pub version: String,
36 pub author: String,
37 pub access: String,
38}
39
40impl Config {
41 pub fn new() -> Self {
42 Config {
43 defaults: ConfigDefaults {
44 entry: None,
45 output: None,
46 watch: None,
47 repeat: None,
48 },
49 banks: Some(Vec::new()),
50 plugins: Some(Vec::new()),
51 }
52 }
53
54 pub fn with_defaults(
55 entry: Option<String>,
56 output: Option<String>,
57 watch: Option<bool>,
58 repeat: Option<bool>
59 ) -> Self {
60 Config {
61 defaults: ConfigDefaults {
62 entry,
63 output,
64 watch,
65 repeat,
66 },
67 banks: Some(Vec::new()),
68 plugins: Some(Vec::new()),
69 }
70 }
71
72 pub fn from_string(config_string: &str) -> Result<(Self, String), String> {
73 let config: Config = toml
74 ::from_str(config_string)
75 .map_err(|e| format!("Failed to parse config string: {}", e))?;
76 let config_path = ".devalang".to_string();
77
78 Ok((config, config_path))
79 }
80
81 pub fn write(&self, new_config: &Config) -> Result<(), String> {
82 let config_path = ".devalang";
83
84 let content = toml
85 ::to_string(new_config)
86 .map_err(|e| format!("Failed to serialize config: {}", e))?;
87
88 std::fs
89 ::write(config_path, content)
90 .map_err(|e| format!("Failed to write config to file '{}': {}", config_path, e))?;
91
92 Ok(())
93 }
94}