run-kit 0.7.1

Universal multi-language runner and smart REPL
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Configuration Schema

mod parser;
mod schema;

pub use parser::ConfigParser;
pub use schema::*;

use crate::v2::Result;
use std::collections::HashMap;
use std::path::Path;

#[derive(Debug, Clone)]
pub struct RunConfig {
    pub project: ProjectConfig,
    pub dependencies: HashMap<String, DependencyConfig>,
    pub dev_dependencies: HashMap<String, DependencyConfig>,
    pub components: HashMap<String, ComponentConfig>,
    pub tests: HashMap<String, TestCaseConfig>,
    pub plugins: HashMap<String, PluginConfig>,
    pub deploy: HashMap<String, DeployConfig>,
    pub docker: DockerConfig,
    pub dev: DevConfig,
    pub build: BuildConfig,
    pub registry: RegistrySettings,
}

#[derive(Debug, Clone, Default)]
pub struct DockerConfig {
    pub services: HashMap<String, DockerService>,
}

#[derive(Debug, Clone)]
pub struct DockerService {
    pub url: String,
    pub env_var: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ProjectConfig {
    pub name: String,
    pub version: String,
    pub description: Option<String>,
    pub authors: Vec<String>,
    pub license: Option<String>,
    pub repository: Option<String>,
}

#[derive(Debug, Clone)]
pub struct DependencyConfig {
    pub version: String,
    pub optional: bool,
    pub features: Vec<String>,
    pub git: Option<String>,
    pub path: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ComponentConfig {
    pub path: Option<String>,
    pub source: Option<String>,
    pub language: Option<String>,
    pub build: Option<String>,
    pub capabilities: Vec<String>,
    pub env: HashMap<String, String>,
    pub dependencies: Vec<String>,
    pub health_check: Option<HealthCheckConfig>,
    pub restart: Option<String>,
}

#[derive(Debug, Clone)]
pub struct HealthCheckConfig {
    pub function: String,
    pub interval: u64,
    pub timeout: u64,
    pub retries: u32,
}

#[derive(Debug, Clone)]
pub struct DevConfig {
    pub hot_reload: bool,
    pub watch: Vec<String>,
    pub port: u16,
    pub verbose: bool,
    pub services: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct BuildConfig {
    pub output_dir: String,
    pub target: String,
    pub opt_level: String,
    pub debug: bool,
    pub reproducible: bool,
    pub source_date_epoch: Option<u64>,
}

#[derive(Debug, Clone)]
pub struct RegistrySettings {
    pub url: String,
    pub mirrors: Vec<String>,
    pub auth_token: Option<String>,
}

impl Default for RunConfig {
    fn default() -> Self {
        Self {
            project: ProjectConfig {
                name: "unnamed".to_string(),
                version: "0.1.0".to_string(),
                description: None,
                authors: vec![],
                license: None,
                repository: None,
            },
            dependencies: HashMap::new(),
            dev_dependencies: HashMap::new(),
            components: HashMap::new(),
            tests: HashMap::new(),
            plugins: HashMap::new(),
            deploy: HashMap::new(),
            docker: DockerConfig::default(),
            dev: DevConfig::default(),
            build: BuildConfig::default(),
            registry: RegistrySettings::default(),
        }
    }
}

impl Default for DevConfig {
    fn default() -> Self {
        Self {
            hot_reload: true,
            watch: vec!["src/**/*".to_string(), "wit/**/*.wit".to_string()],
            port: 3000,
            verbose: false,
            services: vec![],
        }
    }
}

impl Default for BuildConfig {
    fn default() -> Self {
        Self {
            output_dir: "target/wasm".to_string(),
            target: "wasm32-wasip2".to_string(),
            opt_level: "release".to_string(),
            debug: false,
            reproducible: false,
            source_date_epoch: None,
        }
    }
}

impl Default for RegistrySettings {
    fn default() -> Self {
        Self {
            url: "https://registry.esubalew.dev".to_string(),
            mirrors: vec![],
            auth_token: None,
        }
    }
}

impl RunConfig {
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path)?;
        ConfigParser::parse(&content)
    }

    pub fn save(&self, path: &Path) -> Result<()> {
        let content = self.serialize();
        std::fs::write(path, content)?;
        Ok(())
    }

    pub fn serialize(&self) -> String {
        let mut output = String::new();

        output.push_str("[project]\n");
        output.push_str(&format!("name = \"{}\"\n", self.project.name));
        output.push_str(&format!("version = \"{}\"\n", self.project.version));
        if let Some(ref desc) = self.project.description {
            output.push_str(&format!("description = \"{}\"\n", desc));
        }
        if !self.project.authors.is_empty() {
            let authors: Vec<String> = self
                .project
                .authors
                .iter()
                .map(|a| format!("\"{}\"", a))
                .collect();
            output.push_str(&format!("authors = [{}]\n", authors.join(", ")));
        }
        if let Some(ref license) = self.project.license {
            output.push_str(&format!("license = \"{}\"\n", license));
        }
        if let Some(ref repo) = self.project.repository {
            output.push_str(&format!("repository = \"{}\"\n", repo));
        }
        output.push('\n');

        if !self.dependencies.is_empty() {
            let mut simple = Vec::new();
            let mut complex = Vec::new();
            for (name, dep) in &self.dependencies {
                if dep.optional
                    || !dep.features.is_empty()
                    || dep.git.is_some()
                    || dep.path.is_some()
                {
                    complex.push((name, dep));
                } else {
                    simple.push((name, dep));
                }
            }
            if !simple.is_empty() {
                output.push_str("[dependencies]\n");
                for (name, dep) in simple {
                    output.push_str(&format!("\"{}\" = \"{}\"\n", name, dep.version));
                }
                output.push('\n');
            }
            for (name, dep) in complex {
                output.push_str(&format!("[dependencies.{}]\n", name));
                output.push_str(&format!("version = \"{}\"\n", dep.version));
                if dep.optional {
                    output.push_str("optional = true\n");
                }
                if let Some(ref git) = dep.git {
                    output.push_str(&format!("git = \"{}\"\n", git));
                }
                if let Some(ref path) = dep.path {
                    output.push_str(&format!("path = \"{}\"\n", path));
                }
                if !dep.features.is_empty() {
                    let feats: Vec<String> =
                        dep.features.iter().map(|f| format!("\"{}\"", f)).collect();
                    output.push_str(&format!("features = [{}]\n", feats.join(", ")));
                }
                output.push('\n');
            }
        }

        if !self.dev_dependencies.is_empty() {
            let mut simple = Vec::new();
            let mut complex = Vec::new();
            for (name, dep) in &self.dev_dependencies {
                if dep.optional
                    || !dep.features.is_empty()
                    || dep.git.is_some()
                    || dep.path.is_some()
                {
                    complex.push((name, dep));
                } else {
                    simple.push((name, dep));
                }
            }
            if !simple.is_empty() {
                output.push_str("[dev-dependencies]\n");
                for (name, dep) in simple {
                    output.push_str(&format!("\"{}\" = \"{}\"\n", name, dep.version));
                }
                output.push('\n');
            }
            for (name, dep) in complex {
                output.push_str(&format!("[dev-dependencies.{}]\n", name));
                output.push_str(&format!("version = \"{}\"\n", dep.version));
                if dep.optional {
                    output.push_str("optional = true\n");
                }
                if let Some(ref git) = dep.git {
                    output.push_str(&format!("git = \"{}\"\n", git));
                }
                if let Some(ref path) = dep.path {
                    output.push_str(&format!("path = \"{}\"\n", path));
                }
                if !dep.features.is_empty() {
                    let feats: Vec<String> =
                        dep.features.iter().map(|f| format!("\"{}\"", f)).collect();
                    output.push_str(&format!("features = [{}]\n", feats.join(", ")));
                }
                output.push('\n');
            }
        }

        for (name, comp) in &self.components {
            output.push_str(&format!("[components.{}]\n", name));
            if let Some(ref path) = comp.path {
                output.push_str(&format!("path = \"{}\"\n", path));
            }
            if let Some(ref source) = comp.source {
                output.push_str(&format!("source = \"{}\"\n", source));
            }
            if let Some(ref lang) = comp.language {
                output.push_str(&format!("language = \"{}\"\n", lang));
            }
            if let Some(ref build) = comp.build {
                output.push_str(&format!("build = \"{}\"\n", build));
            }
            if !comp.capabilities.is_empty() {
                let caps: Vec<String> = comp
                    .capabilities
                    .iter()
                    .map(|c| format!("\"{}\"", c))
                    .collect();
                output.push_str(&format!("capabilities = [{}]\n", caps.join(", ")));
            }
            if !comp.dependencies.is_empty() {
                let deps: Vec<String> = comp
                    .dependencies
                    .iter()
                    .map(|d| format!("\"{}\"", d))
                    .collect();
                output.push_str(&format!("dependencies = [{}]\n", deps.join(", ")));
            }
            if let Some(ref restart) = comp.restart {
                output.push_str(&format!("restart = \"{}\"\n", restart));
            }
            output.push('\n');

            if !comp.env.is_empty() {
                output.push_str(&format!("[components.{}.env]\n", name));
                for (key, value) in &comp.env {
                    output.push_str(&format!("{} = \"{}\"\n", key, value));
                }
                output.push('\n');
            }

            if let Some(ref health) = comp.health_check {
                output.push_str(&format!("[components.{}.health_check]\n", name));
                output.push_str(&format!("function = \"{}\"\n", health.function));
                output.push_str(&format!("interval = {}\n", health.interval));
                output.push_str(&format!("timeout = {}\n", health.timeout));
                output.push_str(&format!("retries = {}\n", health.retries));
                output.push('\n');
            }
        }

        if !self.tests.is_empty() {
            for (name, test) in &self.tests {
                output.push_str(&format!("[tests.{}]\n", name));
                output.push_str(&format!("component = \"{}\"\n", test.component));
                output.push_str(&format!("function = \"{}\"\n", test.function));
                if !test.args.is_empty() {
                    let args: Vec<String> =
                        test.args.iter().map(|a| format!("\"{}\"", a)).collect();
                    output.push_str(&format!("args = [{}]\n", args.join(", ")));
                }
                if let Some(ref expect) = test.expect {
                    output.push_str(&format!("expect = \"{}\"\n", expect));
                }
                if let Some(exit) = test.expect_exit {
                    output.push_str(&format!("expect_exit = {}\n", exit));
                }
                if let Some(ref err) = test.expect_error {
                    output.push_str(&format!("expect_error = \"{}\"\n", err));
                }
                output.push('\n');
            }
        }

        if !self.plugins.is_empty() {
            for (name, plugin) in &self.plugins {
                output.push_str(&format!("[plugins.{}]\n", name));
                if let Some(ref path) = plugin.path {
                    output.push_str(&format!("path = \"{}\"\n", path));
                }
                if let Some(ref package) = plugin.package {
                    output.push_str(&format!("package = \"{}\"\n", package));
                }
                if let Some(ref version) = plugin.version {
                    output.push_str(&format!("version = \"{}\"\n", version));
                }
                output.push_str(&format!("enabled = {}\n", plugin.enabled));
                if !plugin.hooks.is_empty() {
                    let hooks: Vec<String> =
                        plugin.hooks.iter().map(|h| format!("\"{}\"", h)).collect();
                    output.push_str(&format!("hooks = [{}]\n", hooks.join(", ")));
                }
                output.push('\n');
            }
        }

        if !self.deploy.is_empty() {
            for (name, deploy) in &self.deploy {
                output.push_str(&format!("[deploy.{}]\n", name));
                output.push_str(&format!("target = \"{}\"\n", deploy.target_type));
                for (key, value) in &deploy.options {
                    output.push_str(&format!("{} = \"{}\"\n", key, value));
                }
                output.push('\n');
            }
        }

        output.push_str("[dev]\n");
        output.push_str(&format!("hot_reload = {}\n", self.dev.hot_reload));
        output.push_str(&format!("port = {}\n", self.dev.port));
        output.push_str(&format!("verbose = {}\n", self.dev.verbose));
        if !self.dev.watch.is_empty() {
            let watch: Vec<String> = self
                .dev
                .watch
                .iter()
                .map(|w| format!("\"{}\"", w))
                .collect();
            output.push_str(&format!("watch = [{}]\n", watch.join(", ")));
        }
        if !self.dev.services.is_empty() {
            let services: Vec<String> = self
                .dev
                .services
                .iter()
                .map(|s| format!("\"{}\"", s))
                .collect();
            output.push_str(&format!("services = [{}]\n", services.join(", ")));
        }
        output.push('\n');

        output.push_str("[build]\n");
        output.push_str(&format!("output_dir = \"{}\"\n", self.build.output_dir));
        output.push_str(&format!("target = \"{}\"\n", self.build.target));
        output.push_str(&format!("opt_level = \"{}\"\n", self.build.opt_level));
        output.push_str(&format!("debug = {}\n", self.build.debug));
        output.push_str(&format!("reproducible = {}\n", self.build.reproducible));
        if let Some(epoch) = self.build.source_date_epoch {
            output.push_str(&format!("source_date_epoch = {}\n", epoch));
        }
        output.push('\n');

        output.push_str("[registry]\n");
        output.push_str(&format!("url = \"{}\"\n", self.registry.url));
        if !self.registry.mirrors.is_empty() {
            let mirrors: Vec<String> = self
                .registry
                .mirrors
                .iter()
                .map(|m| format!("\"{}\"", m))
                .collect();
            output.push_str(&format!("mirrors = [{}]\n", mirrors.join(", ")));
        }
        if let Some(ref token) = self.registry.auth_token {
            output.push_str(&format!("auth_token = \"{}\"\n", token));
        }
        output.push('\n');

        if !self.docker.services.is_empty() {
            for (name, service) in &self.docker.services {
                output.push_str(&format!("[docker.{}]\n", name));
                output.push_str(&format!("url = \"{}\"\n", service.url));
                if let Some(ref env_var) = service.env_var {
                    output.push_str(&format!("env_var = \"{}\"\n", env_var));
                }
                output.push('\n');
            }
        }

        output
    }
}