@devaloop/devalang 0.0.1-alpha.16-hotfix.1 → 0.0.1-alpha.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/.cargo/config.toml +2 -0
  2. package/.devalang +6 -10
  3. package/.github/workflows/ci.yml +19 -8
  4. package/Cargo.toml +18 -2
  5. package/README.md +80 -33
  6. package/docs/CHANGELOG.md +56 -0
  7. package/docs/ROADMAP.md +6 -3
  8. package/examples/index.deva +52 -35
  9. package/out-tsc/bin/index.d.ts +2 -0
  10. package/out-tsc/core/functions/index.d.ts +37 -0
  11. package/out-tsc/core/functions/index.js +76 -0
  12. package/out-tsc/core/index.d.ts +6 -0
  13. package/out-tsc/core/index.js +22 -0
  14. package/out-tsc/core/types/index.d.ts +4 -0
  15. package/out-tsc/core/types/index.js +20 -0
  16. package/out-tsc/core/types/plugin.d.ts +18 -0
  17. package/out-tsc/core/types/plugin.js +2 -0
  18. package/out-tsc/core/types/result.d.ts +27 -0
  19. package/out-tsc/core/types/result.js +2 -0
  20. package/out-tsc/core/types/statement.d.ts +106 -0
  21. package/out-tsc/core/types/statement.js +2 -0
  22. package/out-tsc/core/types/value.d.ts +43 -0
  23. package/out-tsc/core/types/value.js +2 -0
  24. package/out-tsc/index.d.ts +7 -0
  25. package/out-tsc/index.js +41 -2
  26. package/out-tsc/pkg/devalang_core.d.ts +7 -0
  27. package/out-tsc/pkg/devalang_core_bg.wasm.d.ts +33 -0
  28. package/out-tsc/scripts/copy-wasm-dts.d.ts +1 -0
  29. package/out-tsc/scripts/copy-wasm-dts.js +73 -0
  30. package/out-tsc/scripts/postinstall.d.ts +1 -0
  31. package/out-tsc/scripts/postinstall.js +33 -23
  32. package/out-tsc/scripts/version/bump.d.ts +1 -0
  33. package/out-tsc/scripts/version/fetch.d.ts +1 -0
  34. package/out-tsc/scripts/version/index.d.ts +1 -0
  35. package/out-tsc/scripts/version/sync.d.ts +1 -0
  36. package/package.json +16 -4
  37. package/project-version.json +3 -3
  38. package/rust/cli/bank/api.rs +122 -0
  39. package/rust/cli/bank/commands.rs +275 -0
  40. package/rust/cli/bank/mod.rs +29 -0
  41. package/rust/cli/build/commands.rs +97 -0
  42. package/rust/cli/build/mod.rs +2 -0
  43. package/rust/cli/build/process.rs +146 -0
  44. package/rust/cli/{check.rs → check/mod.rs} +18 -31
  45. package/rust/cli/discover/commands.rs +253 -0
  46. package/rust/cli/discover/config.rs +111 -0
  47. package/rust/cli/discover/fs.rs +19 -0
  48. package/rust/cli/discover/install.rs +103 -0
  49. package/rust/cli/discover/metadata.rs +48 -0
  50. package/rust/cli/discover/mod.rs +5 -0
  51. package/rust/cli/{init.rs → init/commands.rs} +88 -87
  52. package/rust/cli/init/mod.rs +1 -0
  53. package/rust/{installer → cli/install}/addon.rs +5 -9
  54. package/rust/cli/install/bank.rs +53 -0
  55. package/rust/cli/{install.rs → install/commands.rs} +9 -9
  56. package/rust/{installer → cli/install}/mod.rs +2 -3
  57. package/rust/cli/install/plugin.rs +61 -0
  58. package/rust/cli/{login.rs → login/commands.rs} +8 -11
  59. package/rust/cli/login/mod.rs +1 -0
  60. package/rust/cli/mod.rs +2 -3
  61. package/rust/cli/{driver.rs → parser.rs} +19 -2
  62. package/rust/cli/play/commands.rs +324 -0
  63. package/rust/cli/play/io.rs +17 -0
  64. package/rust/cli/play/mod.rs +5 -0
  65. package/rust/cli/play/process.rs +150 -0
  66. package/rust/cli/play/realtime.rs +91 -0
  67. package/rust/cli/play/utils.rs +23 -0
  68. package/rust/cli/telemetry/commands.rs +22 -0
  69. package/rust/cli/telemetry/event_creator.rs +80 -0
  70. package/rust/cli/telemetry/mod.rs +3 -0
  71. package/rust/cli/telemetry/send.rs +51 -0
  72. package/rust/cli/{template.rs → template/commands.rs} +1 -1
  73. package/rust/cli/template/mod.rs +1 -0
  74. package/rust/cli/{update.rs → update/commands.rs} +6 -6
  75. package/rust/cli/update/mod.rs +1 -0
  76. package/rust/config/driver.rs +57 -72
  77. package/rust/config/mod.rs +1 -2
  78. package/rust/config/ops.rs +26 -0
  79. package/rust/config/settings.rs +60 -50
  80. package/rust/core/audio/engine/helpers.rs +146 -0
  81. package/rust/core/audio/engine/mod.rs +7 -0
  82. package/rust/core/audio/engine/sample.rs +298 -0
  83. package/rust/core/audio/engine/synth.rs +310 -0
  84. package/rust/core/audio/evaluator.rs +15 -12
  85. package/rust/core/audio/interpreter/arrow_call.rs +99 -24
  86. package/rust/core/audio/interpreter/call.rs +81 -60
  87. package/rust/core/audio/interpreter/condition.rs +3 -2
  88. package/rust/core/audio/interpreter/driver.rs +206 -151
  89. package/rust/core/audio/interpreter/let_.rs +1 -1
  90. package/rust/core/audio/interpreter/load.rs +2 -1
  91. package/rust/core/audio/interpreter/loop_.rs +7 -6
  92. package/rust/core/audio/interpreter/sleep.rs +2 -1
  93. package/rust/core/audio/interpreter/spawn.rs +45 -57
  94. package/rust/core/audio/interpreter/tempo.rs +31 -10
  95. package/rust/core/audio/interpreter/trigger.rs +2 -2
  96. package/rust/core/audio/loader/trigger.rs +4 -7
  97. package/rust/core/audio/player.rs +6 -0
  98. package/rust/core/audio/renderer.rs +5 -7
  99. package/rust/core/audio/special/env.rs +3 -1
  100. package/rust/core/audio/special/math.rs +4 -4
  101. package/rust/core/audio/special/modulator.rs +2 -2
  102. package/rust/core/builder/mod.rs +9 -3
  103. package/rust/core/debugger/lexer.rs +1 -1
  104. package/rust/core/debugger/mod.rs +6 -0
  105. package/rust/core/debugger/module.rs +4 -4
  106. package/rust/core/debugger/preprocessor.rs +1 -1
  107. package/rust/core/debugger/store.rs +2 -2
  108. package/rust/core/error/mod.rs +189 -0
  109. package/rust/core/lexer/handler/arrow.rs +1 -1
  110. package/rust/core/lexer/handler/at.rs +1 -1
  111. package/rust/core/lexer/handler/brace.rs +2 -2
  112. package/rust/core/lexer/handler/colon.rs +1 -1
  113. package/rust/core/lexer/handler/comment.rs +1 -1
  114. package/rust/core/lexer/handler/dot.rs +1 -1
  115. package/rust/core/lexer/handler/driver.rs +1 -1
  116. package/rust/core/lexer/handler/identifier.rs +1 -1
  117. package/rust/core/lexer/handler/mod.rs +1 -2
  118. package/rust/core/lexer/handler/number.rs +1 -1
  119. package/rust/core/lexer/handler/operator.rs +1 -1
  120. package/rust/core/lexer/handler/parenthesis.rs +2 -2
  121. package/rust/core/lexer/handler/slash.rs +1 -1
  122. package/rust/core/lexer/handler/string.rs +1 -1
  123. package/rust/core/lexer/mod.rs +22 -12
  124. package/rust/core/lexer/token.rs +90 -97
  125. package/rust/core/mod.rs +0 -1
  126. package/rust/core/parser/driver.rs +66 -13
  127. package/rust/core/parser/handler/arrow_call.rs +28 -8
  128. package/rust/core/parser/handler/at.rs +55 -21
  129. package/rust/core/parser/handler/bank.rs +14 -4
  130. package/rust/core/parser/handler/condition.rs +6 -3
  131. package/rust/core/parser/handler/dot.rs +2 -1
  132. package/rust/core/parser/handler/identifier/automate.rs +13 -16
  133. package/rust/core/parser/handler/identifier/call.rs +4 -4
  134. package/rust/core/parser/handler/identifier/emit.rs +9 -5
  135. package/rust/core/parser/handler/identifier/function.rs +20 -7
  136. package/rust/core/parser/handler/identifier/group.rs +11 -7
  137. package/rust/core/parser/handler/identifier/let_.rs +24 -9
  138. package/rust/core/parser/handler/identifier/mod.rs +6 -5
  139. package/rust/core/parser/handler/identifier/on.rs +16 -7
  140. package/rust/core/parser/handler/identifier/print.rs +6 -9
  141. package/rust/core/parser/handler/identifier/sleep.rs +12 -5
  142. package/rust/core/parser/handler/identifier/spawn.rs +4 -4
  143. package/rust/core/parser/handler/identifier/synth.rs +79 -9
  144. package/rust/core/parser/handler/loop_.rs +39 -14
  145. package/rust/core/parser/handler/tempo.rs +9 -5
  146. package/rust/core/parser/mod.rs +0 -1
  147. package/rust/core/parser/statement.rs +6 -137
  148. package/rust/core/plugin/loader.rs +41 -27
  149. package/rust/core/plugin/runner.rs +68 -17
  150. package/rust/core/preprocessor/loader.rs +155 -33
  151. package/rust/core/preprocessor/processor.rs +2 -2
  152. package/rust/core/preprocessor/resolver/bank.rs +6 -8
  153. package/rust/core/preprocessor/resolver/call.rs +20 -24
  154. package/rust/core/preprocessor/resolver/condition.rs +6 -8
  155. package/rust/core/preprocessor/resolver/driver.rs +14 -16
  156. package/rust/core/preprocessor/resolver/function.rs +6 -6
  157. package/rust/core/preprocessor/resolver/group.rs +6 -8
  158. package/rust/core/preprocessor/resolver/loop_.rs +8 -10
  159. package/rust/core/preprocessor/resolver/spawn.rs +19 -23
  160. package/rust/core/preprocessor/resolver/synth.rs +6 -8
  161. package/rust/core/preprocessor/resolver/tempo.rs +6 -8
  162. package/rust/core/preprocessor/resolver/trigger.rs +22 -19
  163. package/rust/core/preprocessor/resolver/value.rs +99 -4
  164. package/rust/core/store/export.rs +28 -28
  165. package/rust/core/store/function.rs +6 -0
  166. package/rust/core/store/global.rs +7 -1
  167. package/rust/core/store/import.rs +28 -28
  168. package/rust/core/store/variable.rs +1 -1
  169. package/rust/core/utils/mod.rs +0 -1
  170. package/rust/lib.rs +102 -9
  171. package/rust/main.rs +156 -45
  172. package/rust/types/Cargo.toml +8 -0
  173. package/rust/types/src/addons.rs +55 -0
  174. package/rust/types/src/ast.rs +198 -0
  175. package/rust/types/src/config.rs +74 -0
  176. package/rust/types/src/lib.rs +12 -0
  177. package/rust/types/src/telemetry.rs +85 -0
  178. package/rust/utils/Cargo.toml +23 -0
  179. package/rust/utils/{error.rs → src/error.rs} +186 -200
  180. package/rust/utils/src/file.rs +94 -0
  181. package/rust/utils/src/first_usage.rs +97 -0
  182. package/rust/utils/{mod.rs → src/lib.rs} +1 -1
  183. package/rust/utils/{logger.rs → src/logger.rs} +17 -12
  184. package/rust/utils/src/path.rs +88 -0
  185. package/rust/utils/src/signature.rs +41 -0
  186. package/rust/utils/{spinner.rs → src/spinner.rs} +3 -5
  187. package/rust/utils/src/version.rs +27 -0
  188. package/rust/utils/{watcher.rs → src/watcher.rs} +13 -1
  189. package/rust/web/api.rs +5 -0
  190. package/rust/web/cdn.rs +34 -0
  191. package/templates/minimal/README.md +98 -54
  192. package/templates/welcome/README.md +98 -54
  193. package/templates/welcome/src/index.deva +56 -8
  194. package/templates/welcome/src/variables.deva +2 -4
  195. package/tests/rust/TODO.md +0 -0
  196. package/tests/typescript/index.spec.ts +136 -0
  197. package/tests/typescript/playhead.spec.ts +36 -0
  198. package/tests/typescript/render_e2e.spec.ts +77 -0
  199. package/tsconfig.json +1 -1
  200. package/typescript/core/functions/index.ts +83 -0
  201. package/typescript/core/index.ts +6 -0
  202. package/typescript/core/types/index.ts +4 -0
  203. package/typescript/core/types/plugin.ts +19 -0
  204. package/typescript/core/types/result.ts +29 -0
  205. package/typescript/core/types/statement.ts +47 -0
  206. package/typescript/core/types/value.ts +29 -0
  207. package/typescript/index.ts +7 -2
  208. package/typescript/pkg/devalang_core.d.ts +4 -0
  209. package/typescript/scripts/copy-wasm-dts.ts +41 -0
  210. package/typescript/scripts/postinstall.ts +45 -32
  211. package/rust/cli/bank.rs +0 -462
  212. package/rust/cli/build.rs +0 -252
  213. package/rust/cli/generator.rs +0 -1
  214. package/rust/cli/play.rs +0 -1123
  215. package/rust/cli/telemetry.rs +0 -19
  216. package/rust/common/api.rs +0 -5
  217. package/rust/common/cdn.rs +0 -5
  218. package/rust/config/loader.rs +0 -165
  219. package/rust/config/stats.rs +0 -257
  220. package/rust/core/audio/engine.rs +0 -696
  221. package/rust/core/shared/bank.rs +0 -21
  222. package/rust/core/shared/duration.rs +0 -9
  223. package/rust/core/shared/mod.rs +0 -3
  224. package/rust/core/shared/value.rs +0 -35
  225. package/rust/core/utils/validation.rs +0 -35
  226. package/rust/installer/bank.rs +0 -62
  227. package/rust/installer/plugin.rs +0 -54
  228. package/rust/installer/utils.rs +0 -56
  229. package/rust/utils/file.rs +0 -38
  230. package/rust/utils/first_usage.rs +0 -76
  231. package/rust/utils/signature.rs +0 -19
  232. package/rust/utils/telemetry.rs +0 -292
  233. package/rust/utils/version.rs +0 -15
  234. /package/rust/{common → web}/mod.rs +0 -0
  235. /package/rust/{common → web}/sso.rs +0 -0
@@ -0,0 +1,80 @@
1
+ use crate::config::settings::get_user_config;
2
+ use devalang_types::{
3
+ TelemetryErrorLevel as SharedTelemetryErrorLevel, TelemetryEvent as SharedTelemetryEvent,
4
+ };
5
+
6
+ pub type TelemetryEvent = SharedTelemetryEvent;
7
+ pub type TelemetryErrorLevel = SharedTelemetryErrorLevel;
8
+
9
+ pub trait TelemetryEventExt {
10
+ fn set_timestamp(&mut self, timestamp: String);
11
+ fn set_duration(&mut self, duration: u64);
12
+ fn set_success(&mut self, success: bool);
13
+ fn set_error(
14
+ &mut self,
15
+ level: TelemetryErrorLevel,
16
+ message: Option<String>,
17
+ exit_code: Option<i32>,
18
+ );
19
+ }
20
+
21
+ impl TelemetryEventExt for SharedTelemetryEvent {
22
+ fn set_timestamp(&mut self, timestamp: String) {
23
+ self.timestamp = timestamp;
24
+ }
25
+
26
+ fn set_duration(&mut self, duration: u64) {
27
+ self.duration = duration;
28
+ }
29
+
30
+ fn set_success(&mut self, success: bool) {
31
+ self.success = success;
32
+ }
33
+
34
+ fn set_error(
35
+ &mut self,
36
+ level: TelemetryErrorLevel,
37
+ message: Option<String>,
38
+ exit_code: Option<i32>,
39
+ ) {
40
+ self.error_level = level;
41
+ self.error_message = message;
42
+ self.exit_code = exit_code;
43
+ }
44
+ }
45
+
46
+ pub struct TelemetryEventCreator {
47
+ pub events: Vec<TelemetryEvent>,
48
+ }
49
+
50
+ impl TelemetryEventCreator {
51
+ pub fn new() -> Self {
52
+ TelemetryEventCreator { events: Vec::new() }
53
+ }
54
+
55
+ pub fn create_event(&mut self, event: TelemetryEvent) {
56
+ self.events.push(event.clone());
57
+ }
58
+
59
+ pub fn get_base_event(&self) -> TelemetryEvent {
60
+ let mut default_event = TelemetryEvent::default();
61
+
62
+ if let Some(user_cfg) = get_user_config() {
63
+ default_event = TelemetryEvent {
64
+ uuid: user_cfg.telemetry.uuid.clone(),
65
+ cli_version: env!("CARGO_PKG_VERSION").to_string(),
66
+ os: std::env::consts::OS.to_string(),
67
+ command: std::env::args().collect::<Vec<_>>(),
68
+ project_info: None,
69
+ error_level: TelemetryErrorLevel::None,
70
+ error_message: None,
71
+ exit_code: None,
72
+ timestamp: chrono::Utc::now().to_string(),
73
+ duration: 0,
74
+ success: true,
75
+ };
76
+ }
77
+
78
+ default_event
79
+ }
80
+ }
@@ -0,0 +1,3 @@
1
+ pub mod commands;
2
+ pub mod event_creator;
3
+ pub mod send;
@@ -0,0 +1,51 @@
1
+ use std::time::Duration;
2
+
3
+ use crate::{config::settings::get_user_config, web::api::get_api_url};
4
+ use devalang_types::{TelemetryEvent, TelemetrySendError};
5
+
6
+ pub async fn send_telemetry_event(event: &TelemetryEvent) -> Result<(), TelemetrySendError> {
7
+ if let Some(cfg) = get_user_config() {
8
+ if cfg.telemetry.enabled == false {
9
+ return Ok(());
10
+ }
11
+ } else {
12
+ return Ok(());
13
+ }
14
+
15
+ let telemetry_url = format!("{}/v1/telemetry/send", get_api_url());
16
+ let client = reqwest::Client::builder()
17
+ .timeout(Duration::from_secs(5))
18
+ .build()
19
+ .map_err(|e| TelemetrySendError::Http(format!("client build error: {}", e)))?;
20
+
21
+ let mut last_err: Option<String> = None;
22
+ for (i, delay_ms) in [0u64, 250, 500, 1000].iter().enumerate() {
23
+ if *delay_ms > 0 {
24
+ tokio::time::sleep(Duration::from_millis(*delay_ms)).await;
25
+ }
26
+
27
+ let res = client
28
+ .post(telemetry_url.clone())
29
+ .json(event)
30
+ .send()
31
+ .await
32
+ .and_then(|r| r.error_for_status());
33
+
34
+ match res {
35
+ Ok(_) => {
36
+ return Ok(());
37
+ }
38
+ Err(err) => {
39
+ last_err = Some(err.to_string());
40
+
41
+ if i == 3 {
42
+ break;
43
+ }
44
+ }
45
+ }
46
+ }
47
+
48
+ Err(TelemetrySendError::Http(
49
+ last_err.unwrap_or_else(|| "unknown error".to_string()),
50
+ ))
51
+ }
@@ -1,4 +1,4 @@
1
- use crate::utils::file::format_file_size;
1
+ use devalang_utils::file::format_file_size;
2
2
  use include_dir::{Dir, DirEntry, include_dir};
3
3
 
4
4
  static TEMPLATES_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/templates");
@@ -0,0 +1 @@
1
+ pub mod commands;
@@ -1,6 +1,6 @@
1
- pub async fn handle_update_command(
2
- _only: Option<String>,
3
- ) -> Result<(), Box<dyn std::error::Error>> {
4
- println!("Updates are not yet implemented. This is a placeholder function.");
5
- Ok(())
6
- }
1
+ pub async fn handle_update_command(
2
+ _only: Option<String>,
3
+ ) -> Result<(), Box<dyn std::error::Error>> {
4
+ println!("Updates are not yet implemented.");
5
+ Ok(())
6
+ }
@@ -0,0 +1 @@
1
+ pub mod commands;
@@ -1,60 +1,45 @@
1
- use serde::{Deserialize, Serialize};
2
- use std::collections::HashMap;
3
-
4
- #[derive(Debug, Deserialize, Clone, Serialize)]
5
- pub 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)]
12
- pub 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)]
22
- pub 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)]
28
- pub struct ProjectConfigBankEntry {
29
- pub path: String,
30
- pub version: Option<String>,
31
- }
32
-
33
- #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
34
- pub struct PluginEntry {
35
- pub path: String,
36
- pub version: String,
37
- pub author: String,
38
- pub access: String,
1
+ use devalang_types::{
2
+ PluginEntry as SharedPluginEntry, ProjectConfig as SharedProjectConfig,
3
+ ProjectConfigBankEntry as SharedProjectConfigBankEntry,
4
+ ProjectConfigDefaults as SharedProjectConfigDefaults,
5
+ ProjectConfigPluginEntry as SharedProjectConfigPluginEntry,
6
+ };
7
+ use devalang_utils::path as path_utils;
8
+ use std::path::PathBuf;
9
+
10
+ pub type ProjectConfig = SharedProjectConfig;
11
+ pub type ProjectConfigDefaults = SharedProjectConfigDefaults;
12
+ pub type ProjectConfigBankEntry = SharedProjectConfigBankEntry;
13
+ pub type ProjectConfigPluginEntry = SharedProjectConfigPluginEntry;
14
+ pub type PluginEntry = SharedPluginEntry;
15
+
16
+ pub 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
+ ) -> Self;
26
+ fn get() -> Result<Self, String>
27
+ where
28
+ Self: Sized;
29
+ fn from_string(config_string: &str) -> Result<(Self, String), String>
30
+ where
31
+ Self: Sized;
32
+ fn write_config(&self, new_config: &Self) -> Result<(), String>
33
+ where
34
+ Self: Sized;
39
35
  }
40
36
 
41
- impl 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
- }
37
+ impl ProjectConfigExt for SharedProjectConfig {
38
+ fn new_config() -> Self {
39
+ SharedProjectConfig::default()
55
40
  }
56
41
 
57
- pub fn with_defaults(
42
+ fn with_defaults(
58
43
  entry: Option<String>,
59
44
  output: Option<String>,
60
45
  watch: Option<bool>,
@@ -62,8 +47,8 @@ impl ProjectConfig {
62
47
  debug: Option<bool>,
63
48
  compress: Option<bool>,
64
49
  ) -> Self {
65
- ProjectConfig {
66
- defaults: ProjectConfigDefaults {
50
+ SharedProjectConfig {
51
+ defaults: SharedProjectConfigDefaults {
67
52
  entry,
68
53
  output,
69
54
  watch,
@@ -76,42 +61,42 @@ impl ProjectConfig {
76
61
  }
77
62
  }
78
63
 
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
- }
64
+ fn get() -> Result<Self, String> {
65
+ let config_path = path_utils::get_devalang_config_path()?;
89
66
 
90
67
  let config_content = std::fs::read_to_string(&config_path)
91
68
  .map_err(|e| format!("Failed to read config file: {}", e))?;
92
69
 
93
- let config: ProjectConfig = toml::from_str(&config_content)
70
+ let config: SharedProjectConfig = toml::from_str(&config_content)
94
71
  .map_err(|e| format!("Failed to parse config file: {}", e))?;
95
72
 
96
73
  Ok(config)
97
74
  }
98
75
 
99
- pub fn from_string(config_string: &str) -> Result<(Self, String), String> {
100
- let config: ProjectConfig = toml::from_str(config_string)
76
+ fn from_string(config_string: &str) -> Result<(Self, String), String> {
77
+ let config: SharedProjectConfig = toml::from_str(config_string)
101
78
  .map_err(|e| format!("Failed to parse config string: {}", e))?;
102
79
  let config_path = ".devalang".to_string();
103
80
 
104
81
  Ok((config, config_path))
105
82
  }
106
83
 
107
- pub fn write(&self, new_config: &ProjectConfig) -> Result<(), String> {
108
- let config_path = ".devalang";
84
+ fn write_config(&self, new_config: &Self) -> Result<(), String> {
85
+ let config_path: PathBuf = match path_utils::get_project_root() {
86
+ Ok(root) => root.join(path_utils::DEVALANG_CONFIG),
87
+ Err(_) => PathBuf::from(path_utils::DEVALANG_CONFIG),
88
+ };
109
89
 
110
90
  let content = toml::to_string(new_config)
111
91
  .map_err(|e| format!("Failed to serialize config: {}", e))?;
112
92
 
113
- std::fs::write(config_path, content)
114
- .map_err(|e| format!("Failed to write config to file '{}': {}", config_path, e))?;
93
+ std::fs::write(&config_path, content).map_err(|e| {
94
+ format!(
95
+ "Failed to write config to file '{}': {}",
96
+ config_path.display(),
97
+ e
98
+ )
99
+ })?;
115
100
 
116
101
  Ok(())
117
102
  }
@@ -1,4 +1,3 @@
1
1
  pub mod driver;
2
- pub mod loader;
2
+ pub mod ops;
3
3
  pub mod settings;
4
- pub mod stats;
@@ -0,0 +1,26 @@
1
+ use crate::config::driver::ProjectConfig;
2
+ use std::fs;
3
+ use std::path::Path;
4
+
5
+ pub fn load_config(path: Option<&Path>) -> Option<ProjectConfig> {
6
+ let config_path_buf;
7
+ let config_path = match path {
8
+ Some(p) => p,
9
+ None => {
10
+ config_path_buf = match devalang_utils::path::get_devalang_config_path() {
11
+ Ok(p) => p,
12
+ Err(_) => {
13
+ return None;
14
+ }
15
+ };
16
+ &config_path_buf
17
+ }
18
+ };
19
+
20
+ if config_path.exists() {
21
+ let content = fs::read_to_string(config_path).ok()?;
22
+ toml::from_str(&content).ok()
23
+ } else {
24
+ None
25
+ }
26
+ }
@@ -1,26 +1,17 @@
1
- use serde::{Deserialize, Serialize};
2
- use std::{any::Any, io::Write};
1
+ use devalang_types::{TelemetrySettings, UserSettings};
2
+ use serde_json::Value as JsonValue;
3
+ use std::io::Write;
3
4
 
4
- #[derive(Serialize, Deserialize, Default, Debug)]
5
- pub struct UserSettings {
6
- pub session: String,
7
- pub telemetry: TelemetrySettings,
8
- }
9
-
10
- #[derive(Serialize, Deserialize, Default, Debug)]
11
- pub struct TelemetrySettings {
12
- pub uuid: String,
13
- pub stats: bool,
14
- pub level: String,
15
- pub enabled: bool,
16
- }
17
-
18
- pub fn get_home_dir() -> std::path::PathBuf {
19
- dirs::home_dir().unwrap_or_default()
5
+ pub fn get_home_dir() -> Option<std::path::PathBuf> {
6
+ dirs::home_dir()
20
7
  }
21
8
 
22
9
  pub fn get_devalang_homedir() -> std::path::PathBuf {
23
- get_home_dir().join(".devalang")
10
+ if let Some(home_dir) = get_home_dir() {
11
+ home_dir.join(".devalang")
12
+ } else {
13
+ std::path::PathBuf::from("~/.devalang")
14
+ }
24
15
  }
25
16
 
26
17
  pub fn get_default_user_config() -> UserSettings {
@@ -28,16 +19,15 @@ pub fn get_default_user_config() -> UserSettings {
28
19
  session: "".into(),
29
20
  telemetry: TelemetrySettings {
30
21
  uuid: uuid::Uuid::new_v4().to_string(),
31
- enabled: true,
22
+ enabled: false,
32
23
  level: "basic".into(),
33
- stats: true,
24
+ stats: false,
34
25
  },
35
26
  }
36
27
  }
37
28
 
38
29
  pub fn get_user_config() -> Option<UserSettings> {
39
- let config_path = get_devalang_homedir().join("config.json");
40
- if config_path.try_exists().is_ok() {
30
+ if let Some(config_path) = get_devalang_homedir().join("config.json").into() {
41
31
  let file = std::fs::File::open(config_path).ok()?;
42
32
  let settings = serde_json::from_reader(file).ok()?;
43
33
  Some(settings)
@@ -47,45 +37,65 @@ pub fn get_user_config() -> Option<UserSettings> {
47
37
  }
48
38
 
49
39
  pub fn write_user_config_file() {
50
- let settings = get_user_config().unwrap_or(get_default_user_config());
40
+ if let Some(config_path) = get_devalang_homedir().join("config.json").into() {
41
+ let settings = get_user_config().unwrap_or_else(get_default_user_config);
51
42
 
52
- let config_path = get_devalang_homedir().join("config.json");
53
- let mut file = std::fs::File::create(config_path).unwrap();
54
- let config_json = serde_json::to_string(&settings).unwrap();
43
+ let config_json = serde_json::to_string(&settings).unwrap();
55
44
 
56
- file.write_all(config_json.as_bytes()).unwrap();
45
+ if let Err(e) = write_config_atomic(&config_path, &config_json) {
46
+ println!("Could not write config file: {}", e);
47
+ }
48
+ } else {
49
+ println!("Could not create config file");
50
+ }
57
51
  }
58
52
 
59
- pub fn set_user_config_bool(key: &str, value: bool) {
60
- let mut settings = get_user_config().unwrap_or_default();
61
-
62
- match key {
63
- "telemetry" => {
64
- settings.telemetry.enabled = value;
53
+ pub fn ensure_user_config_file_exists() {
54
+ if let Some(config_path) = get_devalang_homedir().join("config.json").into() {
55
+ if !config_path.exists() {
56
+ write_user_config_file();
65
57
  }
66
- _ => {}
67
58
  }
68
-
69
- let config_path = get_devalang_homedir().join("config.json");
70
- let config_json = serde_json::to_string(&settings).unwrap();
71
- let mut file = std::fs::File::create(config_path).unwrap();
72
-
73
- file.write_all(config_json.as_bytes()).unwrap();
74
59
  }
75
60
 
76
- pub fn set_user_config_string(key: &str, value: String) {
61
+ pub fn set_user_config_value(key: &str, value: JsonValue) {
77
62
  let mut settings = get_user_config().unwrap_or_default();
78
63
 
79
- match key {
80
- "session" => {
81
- settings.session = value;
64
+ match (key, &value) {
65
+ ("telemetry", JsonValue::Bool(b)) => {
66
+ settings.telemetry.enabled = *b;
67
+ }
68
+ ("session", JsonValue::String(s)) => {
69
+ settings.session = s.clone();
82
70
  }
83
- _ => {}
71
+ _ => {
72
+ println!("Unsupported key or value type for '{}': {:?}", key, value);
73
+ }
74
+ }
75
+
76
+ if let Some(config_path) = get_devalang_homedir().join("config.json").into() {
77
+ let config_json = serde_json::to_string(&settings).unwrap();
78
+ if let Err(e) = write_config_atomic(&config_path, &config_json) {
79
+ println!("Could not write config file: {}", e);
80
+ }
81
+ } else {
82
+ println!("Could not create config file");
83
+ }
84
+ }
85
+
86
+ pub fn write_config_atomic(
87
+ config_path: &std::path::PathBuf,
88
+ contents: &str,
89
+ ) -> std::io::Result<()> {
90
+ if let Some(parent) = config_path.parent() {
91
+ std::fs::create_dir_all(parent)?;
84
92
  }
85
93
 
86
- let config_path = get_devalang_homedir().join("config.json");
87
- let config_json = serde_json::to_string(&settings).unwrap();
88
- let mut file = std::fs::File::create(config_path).unwrap();
94
+ let tmp_path = config_path.with_extension("json.tmp");
95
+ let mut tmp_file = std::fs::File::create(&tmp_path)?;
96
+ tmp_file.write_all(contents.as_bytes())?;
97
+ tmp_file.sync_all()?;
98
+ std::fs::rename(&tmp_path, config_path)?;
89
99
 
90
- file.write_all(config_json.as_bytes()).unwrap();
100
+ Ok(())
91
101
  }
@@ -0,0 +1,146 @@
1
+ use devalang_types::Value;
2
+ use std::collections::HashMap;
3
+
4
+ type OptMap = Option<HashMap<String, Value>>;
5
+
6
+ pub fn env_maps_from_automation(
7
+ automation: &Option<HashMap<String, Value>>,
8
+ ) -> (OptMap, OptMap, OptMap) {
9
+ if let Some(auto) = automation {
10
+ let vol = match auto.get("volume") {
11
+ Some(Value::Map(m)) => Some(m.clone()),
12
+ _ => None,
13
+ };
14
+ let pan = match auto.get("pan") {
15
+ Some(Value::Map(m)) => Some(m.clone()),
16
+ _ => None,
17
+ };
18
+ let pit = match auto.get("pitch") {
19
+ Some(Value::Map(m)) => Some(m.clone()),
20
+ _ => None,
21
+ };
22
+ (vol, pan, pit)
23
+ } else {
24
+ (None, None, None)
25
+ }
26
+ }
27
+
28
+ pub fn eval_env_map(
29
+ env_opt: &Option<HashMap<String, Value>>,
30
+ progress: f32,
31
+ default_val: f32,
32
+ ) -> f32 {
33
+ let env = match env_opt {
34
+ Some(m) => m,
35
+ None => {
36
+ return default_val;
37
+ }
38
+ };
39
+ let mut points: Vec<(f32, f32)> = Vec::with_capacity(env.len());
40
+ for (k, v) in env.iter() {
41
+ let key = if k.ends_with('%') {
42
+ &k[..k.len() - 1]
43
+ } else {
44
+ &k[..]
45
+ };
46
+ if let Ok(mut p) = key.parse::<f32>() {
47
+ p = (p / 100.0).clamp(0.0, 1.0);
48
+ let val = match v {
49
+ Value::Number(n) => *n,
50
+ Value::String(s) => s.parse::<f32>().unwrap_or(default_val),
51
+ Value::Identifier(s) => s.parse::<f32>().unwrap_or(default_val),
52
+ _ => default_val,
53
+ };
54
+ points.push((p, val));
55
+ }
56
+ }
57
+ if points.is_empty() {
58
+ return default_val;
59
+ }
60
+ points.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
61
+ let t = progress.clamp(0.0, 1.0);
62
+ if t <= points[0].0 {
63
+ return points[0].1;
64
+ }
65
+ if t >= points[points.len() - 1].0 {
66
+ return points[points.len() - 1].1;
67
+ }
68
+ for w in points.windows(2) {
69
+ let (p0, v0) = w[0];
70
+ let (p1, v1) = w[1];
71
+ if t >= p0 && t <= p1 {
72
+ let ratio = if (p1 - p0).abs() < f32::EPSILON {
73
+ 0.0
74
+ } else {
75
+ (t - p0) / (p1 - p0)
76
+ };
77
+ return v0 + (v1 - v0) * ratio;
78
+ }
79
+ }
80
+ default_val
81
+ }
82
+
83
+ pub fn oscillator_sample(waveform: &str, current_freq: f32, t: f32) -> f32 {
84
+ let phase = 2.0 * std::f32::consts::PI * current_freq * t;
85
+ match waveform {
86
+ "sine" => phase.sin(),
87
+ "square" => {
88
+ if phase.sin() >= 0.0 {
89
+ 1.0
90
+ } else {
91
+ -1.0
92
+ }
93
+ }
94
+ "saw" => 2.0 * (current_freq * t - (current_freq * t + 0.5).floor()),
95
+ "triangle" => (2.0 * (2.0 * (current_freq * t).fract() - 1.0)).abs() * 2.0 - 1.0,
96
+ _ => 0.0,
97
+ }
98
+ }
99
+
100
+ pub fn adsr_envelope_value(
101
+ i: usize,
102
+ attack_samples: usize,
103
+ decay_samples: usize,
104
+ sustain_samples: usize,
105
+ release_samples: usize,
106
+ sustain_level: f32,
107
+ ) -> f32 {
108
+ if i < attack_samples {
109
+ (i as f32) / (attack_samples as f32)
110
+ } else if i < attack_samples + decay_samples {
111
+ 1.0 - (1.0 - sustain_level) * (((i - attack_samples) as f32) / (decay_samples as f32))
112
+ } else if i < attack_samples + decay_samples + sustain_samples {
113
+ sustain_level
114
+ } else if release_samples > 0 {
115
+ sustain_level
116
+ * (1.0
117
+ - ((i - attack_samples - decay_samples - sustain_samples) as f32)
118
+ / (release_samples as f32))
119
+ } else {
120
+ 0.0
121
+ }
122
+ }
123
+
124
+ pub fn pan_gains(pan_val: f32) -> (f32, f32) {
125
+ let left_gain = 1.0 - pan_val.max(0.0);
126
+ let right_gain = 1.0 + pan_val.min(0.0);
127
+ (left_gain, right_gain)
128
+ }
129
+
130
+ pub fn mix_stereo_samples_into_buffer(
131
+ engine: &mut super::synth::AudioEngine,
132
+ start_sample: usize,
133
+ channels: usize,
134
+ stereo_samples: &[i16],
135
+ ) {
136
+ let offset = start_sample * channels;
137
+ let required_len = offset + stereo_samples.len();
138
+
139
+ if engine.buffer.len() < required_len {
140
+ engine.buffer.resize(required_len, 0);
141
+ }
142
+
143
+ for (i, sample) in stereo_samples.iter().enumerate() {
144
+ engine.buffer[offset + i] = engine.buffer[offset + i].saturating_add(*sample);
145
+ }
146
+ }
@@ -0,0 +1,7 @@
1
+ pub mod helpers;
2
+ pub mod sample;
3
+ pub mod synth;
4
+
5
+ pub use synth::AudioEngine;
6
+
7
+ pub use helpers::*;