wasmrun 0.19.0

A WebAssembly Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Configuration management for Wasmrun

use crate::error::{Result, WasmrunError};
use crate::plugin::{PluginInfo, PluginSource};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WasmrunConfig {
    pub version: String,
    pub settings: GlobalSettings,
    pub plugin_configs: HashMap<String, toml::Value>,
    pub external_plugins: HashMap<String, ExternalPluginEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalSettings {
    pub auto_update: bool,
    pub registry_url: String,
    pub max_concurrent_ops: usize,
    pub cache_dir: Option<PathBuf>,
    pub install_dir: Option<PathBuf>,
    pub verbose: bool,
    pub default_optimization: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalPluginEntry {
    pub info: PluginInfo,
    pub source: PluginSource,
    pub installed_at: String,
    pub enabled: bool,
    pub install_path: String,
    pub executable_path: Option<String>,
}

impl Default for GlobalSettings {
    fn default() -> Self {
        Self {
            auto_update: false,
            registry_url: "https://crates.io".to_string(),
            max_concurrent_ops: 4,
            cache_dir: None,
            install_dir: None,
            verbose: false,
            default_optimization: "size".to_string(),
        }
    }
}

impl Default for WasmrunConfig {
    fn default() -> Self {
        Self {
            version: env!("CARGO_PKG_VERSION").to_string(),
            settings: GlobalSettings::default(),
            plugin_configs: HashMap::new(),
            external_plugins: HashMap::new(),
        }
    }
}

impl WasmrunConfig {
    pub fn config_path() -> Result<PathBuf> {
        if let Ok(test_path) = std::env::var("WASMRUN_CONFIG_PATH") {
            return Ok(PathBuf::from(test_path).join("config.toml"));
        }

        let home_dir = dirs::home_dir()
            .ok_or_else(|| WasmrunError::from("Could not determine home directory"))?;

        Ok(home_dir.join(".wasmrun").join("config.toml"))
    }

    pub fn config_dir() -> Result<PathBuf> {
        if let Ok(test_path) = std::env::var("WASMRUN_CONFIG_PATH") {
            return Ok(PathBuf::from(test_path));
        }

        let home_dir = dirs::home_dir()
            .ok_or_else(|| WasmrunError::from("Could not determine home directory"))?;

        Ok(home_dir.join(".wasmrun"))
    }

    pub fn plugin_dir() -> Result<PathBuf> {
        let config = Self::load_or_default()?;

        if let Some(install_dir) = &config.settings.install_dir {
            Ok(install_dir.clone())
        } else {
            Ok(Self::config_dir()?.join("plugins"))
        }
    }

    #[allow(dead_code)] // TODO: Future plugin cache directory management
    pub fn cache_dir() -> Result<PathBuf> {
        let config = Self::load_or_default()?;

        if let Some(cache_dir) = &config.settings.cache_dir {
            Ok(cache_dir.clone())
        } else {
            Ok(Self::config_dir()?.join("cache"))
        }
    }

    pub fn load() -> Result<Self> {
        let config_path = Self::config_path()?;

        if !config_path.exists() {
            return Err(WasmrunError::Config(
                crate::error::ConfigError::FileNotFound {
                    path: format!(
                        "{}. Run 'wasmrun init' to create it.",
                        config_path.display()
                    ),
                },
            ));
        }

        if config_path.is_dir() {
            return Err(WasmrunError::Config(
                crate::error::ConfigError::InvalidValue {
                    message: format!(
                        "Config path is a directory, not a file: {}",
                        config_path.display()
                    ),
                },
            ));
        }

        let config_content = fs::read_to_string(&config_path).map_err(|e| {
            WasmrunError::Config(crate::error::ConfigError::ParseError {
                message: format!("Failed to read config file: {e}"),
            })
        })?;

        let config: Self = toml::from_str(&config_content).map_err(|e| {
            WasmrunError::Config(crate::error::ConfigError::ParseError {
                message: format!("Failed to parse TOML config file: {e}"),
            })
        })?;

        if config.version.is_empty() {
            return Err(WasmrunError::Config(
                crate::error::ConfigError::MissingRequired {
                    key: "version".to_string(),
                },
            ));
        }

        config.validate_and_setup()
    }

    pub fn load_or_default() -> Result<Self> {
        match Self::load() {
            Ok(config) => Ok(config),
            Err(_) => {
                let config = Self::default();
                config.save()?;
                Ok(config)
            }
        }
    }

    pub fn save(&self) -> Result<()> {
        let config_path = Self::config_path()?;

        if let Some(parent) = config_path.parent() {
            fs::create_dir_all(parent).map_err(|e| {
                WasmrunError::from(format!("Failed to create config directory: {e}"))
            })?;
        }

        let config_content = toml::to_string_pretty(self).map_err(|e| {
            WasmrunError::Config(crate::error::ConfigError::ParseError {
                message: format!("Failed to serialize config to TOML: {e}"),
            })
        })?;

        fs::write(&config_path, config_content).map_err(|e| {
            WasmrunError::Config(crate::error::ConfigError::ParseError {
                message: format!("Failed to write config file: {e}"),
            })
        })?;

        Ok(())
    }

    #[allow(dead_code)] // TODO: Future initial configuration setup
    pub fn create_initial_config() -> Result<()> {
        let config_path = Self::config_path()?;

        if config_path.exists() {
            println!(
                "Configuration file already exists at: {}",
                config_path.display()
            );
            return Ok(());
        }

        let config = Self::default();
        config.save()?;

        println!(
            "✅ Created configuration file at: {}",
            config_path.display()
        );
        Ok(())
    }

    fn validate_and_setup(mut self) -> Result<Self> {
        let plugin_dir = if let Some(install_dir) = &self.settings.install_dir {
            install_dir.clone()
        } else {
            Self::config_dir()?.join("plugins")
        };

        let cache_dir = if let Some(cache_dir) = &self.settings.cache_dir {
            cache_dir.clone()
        } else {
            Self::config_dir()?.join("cache")
        };

        fs::create_dir_all(&plugin_dir)
            .map_err(|e| WasmrunError::from(format!("Failed to create plugin directory: {e}")))?;

        fs::create_dir_all(&cache_dir)
            .map_err(|e| WasmrunError::from(format!("Failed to create cache directory: {e}")))?;

        self.settings.install_dir = Some(plugin_dir);
        self.settings.cache_dir = Some(cache_dir);

        match self.version.as_str() {
            env!("CARGO_PKG_VERSION") => {}
            _ => {
                return Err(WasmrunError::Config(
                    crate::error::ConfigError::InvalidValue {
                        message: format!(
                            "Unsupported config version: {}. Please update Wasmrun.",
                            self.version
                        ),
                    },
                ));
            }
        }

        Ok(self)
    }

    // External plugin management
    #[allow(dead_code)] // TODO: Future external plugin installation
    pub fn add_external_plugin(
        &mut self,
        name: String,
        info: PluginInfo,
        source: PluginSource,
        install_path: String,
    ) -> Result<()> {
        let entry = ExternalPluginEntry {
            info,
            source,
            installed_at: chrono::Utc::now().to_rfc3339(),
            enabled: true,
            install_path,
            executable_path: None,
        };

        self.external_plugins.insert(name, entry);
        self.save()?;
        Ok(())
    }

    #[allow(dead_code)] // TODO: Future plugin management
    pub fn remove_external_plugin(&mut self, name: &str) -> Result<()> {
        self.external_plugins.remove(name);
        self.save()?;
        Ok(())
    }

    #[allow(dead_code)] // TODO: Future plugin installation checking
    pub fn is_external_plugin_installed(&self, name: &str) -> bool {
        self.external_plugins.contains_key(name)
    }

    #[allow(dead_code)] // TODO: Get external plugin by name
    pub fn get_external_plugin(&self, name: &str) -> Option<&ExternalPluginEntry> {
        self.external_plugins.get(name)
    }

    #[allow(dead_code)] // TODO: Future plugin listing functionality
    pub fn get_external_plugins(&self) -> Vec<&PluginInfo> {
        self.external_plugins
            .values()
            .map(|entry| &entry.info)
            .collect()
    }

    #[allow(dead_code)] // TODO: Future plugin enable/disable functionality
    pub fn set_external_plugin_enabled(&mut self, name: &str, enabled: bool) -> Result<()> {
        if let Some(entry) = self.external_plugins.get_mut(name) {
            entry.enabled = enabled;
            self.save()?;
            Ok(())
        } else {
            Err(WasmrunError::from(format!(
                "External plugin '{name}' not found"
            )))
        }
    }

    #[allow(dead_code)] // TODO: Future plugin metadata update functionality
    pub fn update_external_plugin_metadata(&mut self, name: &str, info: PluginInfo) -> Result<()> {
        if let Some(entry) = self.external_plugins.get_mut(name) {
            entry.info = info;
            self.save()?;
            Ok(())
        } else {
            Err(WasmrunError::from(format!(
                "External plugin '{name}' not found"
            )))
        }
    }

    #[allow(dead_code)] // TODO: Future plugin validation system
    pub fn validate_external_plugins(&mut self) -> Result<Vec<String>> {
        let mut missing_plugins = Vec::new();
        let plugin_dir = Self::plugin_dir()?;

        let mut plugins_to_remove = Vec::new();

        for (name, entry) in &self.external_plugins {
            let install_path = plugin_dir.join(&entry.install_path);
            if !install_path.exists() {
                missing_plugins.push(name.clone());
                plugins_to_remove.push(name.clone());
            }
        }

        for name in plugins_to_remove {
            self.external_plugins.remove(&name);
        }

        if !missing_plugins.is_empty() {
            self.save()?;
        }

        Ok(missing_plugins)
    }

    #[allow(dead_code)] // TODO: Future plugin statistics system
    pub fn get_external_plugin_stats(&self) -> (usize, usize, usize, Vec<String>) {
        let total = self.external_plugins.len();
        let enabled = self.external_plugins.values().filter(|e| e.enabled).count();
        let disabled = total - enabled;

        let mut languages = Vec::new();
        for entry in self.external_plugins.values() {
            for ext in &entry.info.extensions {
                if !languages.contains(ext) {
                    languages.push(ext.clone());
                }
            }
        }
        languages.sort();

        (total, enabled, disabled, languages)
    }

    // Plugin-specific configuration
    #[allow(dead_code)] // TODO: Future plugin-specific configuration
    pub fn get_plugin_config(&self, plugin_name: &str) -> Option<&toml::Value> {
        self.plugin_configs.get(plugin_name)
    }

    #[allow(dead_code)] // TODO: Future plugin-specific configuration
    pub fn set_plugin_config(&mut self, plugin_name: String, config: toml::Value) -> Result<()> {
        self.plugin_configs.insert(plugin_name, config);
        self.save()?;
        Ok(())
    }

    #[allow(dead_code)] // TODO: Future plugin-specific configuration
    pub fn remove_plugin_config(&mut self, plugin_name: &str) -> Result<()> {
        self.plugin_configs.remove(plugin_name);
        self.save()?;
        Ok(())
    }

    #[allow(dead_code)] // TODO: Future configuration display functionality
    pub fn print_config(&self) -> Result<()> {
        let config_toml = toml::to_string_pretty(self)
            .map_err(|e| WasmrunError::from(format!("Failed to serialize config: {e}")))?;

        println!("Current Wasmrun Configuration:");
        println!("============================");
        println!("{config_toml}");

        Ok(())
    }

    #[allow(dead_code)] // TODO: Future configuration reset functionality
    pub fn reset(&mut self) -> Result<()> {
        *self = Self::default();
        self.save()?;
        Ok(())
    }
}