devalang_core/config/
driver.rs

1use devalang_types::{
2    PluginEntry as SharedPluginEntry, ProjectConfig as SharedProjectConfig,
3    ProjectConfigBankEntry as SharedProjectConfigBankEntry,
4    ProjectConfigDefaults as SharedProjectConfigDefaults,
5    ProjectConfigPluginEntry as SharedProjectConfigPluginEntry,
6};
7use devalang_utils::path as path_utils;
8use std::path::PathBuf;
9
10pub type ProjectConfig = SharedProjectConfig;
11pub type ProjectConfigDefaults = SharedProjectConfigDefaults;
12pub type ProjectConfigBankEntry = SharedProjectConfigBankEntry;
13pub type ProjectConfigPluginEntry = SharedProjectConfigPluginEntry;
14pub type PluginEntry = SharedPluginEntry;
15
16pub trait ProjectConfigExt {
17    fn new_config() -> Self;
18    fn with_defaults(
19        entry: Option<String>,
20        output: Option<String>,
21        watch: Option<bool>,
22        repeat: Option<bool>,
23        debug: Option<bool>,
24        compress: Option<bool>,
25        sample_rate: Option<u32>,
26        audio_format: Option<String>,
27        output_format: Option<Vec<String>>,
28    ) -> Self;
29    fn get() -> Result<Self, String>
30    where
31        Self: Sized;
32    fn from_string(config_string: &str) -> Result<(Self, String), String>
33    where
34        Self: Sized;
35    fn write_config(&self, new_config: &Self) -> Result<(), String>
36    where
37        Self: Sized;
38}
39
40impl ProjectConfigExt for SharedProjectConfig {
41    fn new_config() -> Self {
42        SharedProjectConfig::default()
43    }
44
45    fn with_defaults(
46        entry: Option<String>,
47        output: Option<String>,
48        watch: Option<bool>,
49        repeat: Option<bool>,
50        debug: Option<bool>,
51        compress: Option<bool>,
52        sample_rate: Option<u32>,
53        audio_format: Option<String>,
54        output_format: Option<Vec<String>>,
55    ) -> Self {
56        SharedProjectConfig {
57            defaults: SharedProjectConfigDefaults {
58                entry,
59                output,
60                watch,
61                repeat,
62                debug,
63                compress,
64                sample_rate,
65                audio_format,
66                output_format,
67            },
68            banks: Some(Vec::new()),
69            plugins: Some(Vec::new()),
70        }
71    }
72
73    fn get() -> Result<Self, String> {
74        let config_path = path_utils::get_devalang_config_path()?;
75
76        let config_content = std::fs::read_to_string(&config_path)
77            .map_err(|e| format!("Failed to read config file: {}", e))?;
78
79        let config: SharedProjectConfig = toml::from_str(&config_content)
80            .map_err(|e| format!("Failed to parse config file: {}", e))?;
81
82        Ok(config)
83    }
84
85    fn from_string(config_string: &str) -> Result<(Self, String), String> {
86        let config: SharedProjectConfig = toml::from_str(config_string)
87            .map_err(|e| format!("Failed to parse config string: {}", e))?;
88        let config_path = ".devalang".to_string();
89
90        Ok((config, config_path))
91    }
92
93    fn write_config(&self, new_config: &Self) -> Result<(), String> {
94        let config_path: PathBuf = match path_utils::get_project_root() {
95            Ok(root) => root.join(path_utils::DEVALANG_CONFIG),
96            Err(_) => PathBuf::from(path_utils::DEVALANG_CONFIG),
97        };
98
99        let content = toml::to_string(new_config)
100            .map_err(|e| format!("Failed to serialize config: {}", e))?;
101
102        std::fs::write(&config_path, content).map_err(|e| {
103            format!(
104                "Failed to write config to file '{}': {}",
105                config_path.display(),
106                e
107            )
108        })?;
109
110        Ok(())
111    }
112}