-
Notifications
You must be signed in to change notification settings - Fork 242
/
lib.rs
387 lines (344 loc) · 12.4 KB
/
lib.rs
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
//! Configuration of an application for the Spin runtime.
#![deny(missing_docs)]
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};
/// Application configuration.
#[derive(Clone, Debug)]
pub struct Configuration<T> {
/// General application information.
pub info: ApplicationInformation,
/// Configuration for the application components.
pub components: Vec<T>,
}
impl<T> Configuration<T> {
/// Derives a Configuration from the serialisation format.
pub fn from_raw(raw: RawConfiguration<T>, origin: ApplicationOrigin) -> Self {
Self {
info: ApplicationInformation::from_raw(raw.info, origin),
components: raw.components,
}
}
}
/// Application configuration file format.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct RawConfiguration<T> {
/// General application information.
#[serde(flatten)]
pub info: RawApplicationInformation,
/// Configuration for the application components.
#[serde(rename = "component")]
pub components: Vec<T>,
}
/// A local component, as defined in `spin.toml`, potentially
/// mutable to be distributed.
/// TODO
///
/// Find a better name.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LinkableComponent {
/// Common component configuration.
#[serde(flatten)]
pub core: CoreComponent,
// TODO
//
// There should be subsections for various environments
// (i.e. dependencies.local, dependencies.prod)
/// Optional list of dependencies to be resolved at runtime by the host.
pub dependencies: Option<HashMap<String, Dependency>>,
/// Optional build information or configuration that could be used
/// by a plugin to build the Wasm module.
#[serde(rename = "build")]
pub build: Option<BuildConfig>,
}
/// Core component configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CoreComponent {
/// The module source.
pub source: ModuleSource,
/// ID of the component. Used at runtime to select between
/// multiple components of the same application.
pub id: String,
/// Per-component WebAssembly configuration.
/// This takes precedence over the application-level
/// WebAssembly configuration.
#[serde(flatten)]
pub wasm: WasmConfig,
/// Trigger configuration.
pub trigger: TriggerConfig,
}
/// General application information.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct RawApplicationInformation {
/// Name of the application.
pub name: String,
/// Version of the application.
pub version: String,
/// Description of the application.
pub description: Option<String>,
/// Authors of the application.
pub authors: Option<Vec<String>>,
/// Trigger for the application.
/// Currently, all components of a given application must be
/// invoked as a result of the same trigger "type".
/// In the future, applications with mixed triggers might be allowed,
/// but for now, a component with a different trigger must be part of
/// a separate application.
pub trigger: ApplicationTrigger,
/// TODO
pub namespace: Option<String>,
}
/// General application information.
#[derive(Clone, Debug)]
pub struct ApplicationInformation {
/// Name of the application.
pub name: String,
/// Version of the application.
pub version: String,
/// Description of the application.
pub description: Option<String>,
/// Authors of the application.
pub authors: Vec<String>,
/// Trigger for the application.
///
/// Currently, all components of a given application must be
/// invoked as a result of the same trigger "type".
/// In the future, applications with mixed triggers might be allowed,
/// but for now, a component with a different trigger must be part of
/// a separate application.
pub trigger: ApplicationTrigger,
/// TODO
pub namespace: Option<String>,
/// The location from which the application is loaded.
pub origin: ApplicationOrigin,
}
/// The location from which an application was loaded.
#[derive(Clone, Debug)]
pub enum ApplicationOrigin {
/// The application was loaded from the specified file.
File(PathBuf),
}
impl ApplicationInformation {
/// Derives an ApplicationInformation from the serialisation format.
pub fn from_raw(raw: RawApplicationInformation, origin: ApplicationOrigin) -> Self {
Self {
name: raw.name,
version: raw.version,
description: raw.description,
authors: raw.authors.unwrap_or_default(),
trigger: raw.trigger,
namespace: raw.namespace,
origin,
}
}
}
/// The trigger type.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", tag = "type")]
pub enum ApplicationTrigger {
/// HTTP trigger type.
Http(HttpTriggerConfiguration),
}
impl Default for ApplicationTrigger {
fn default() -> Self {
Self::Http(HttpTriggerConfiguration::default())
}
}
/// HTTP trigger configuration.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct HttpTriggerConfiguration {
/// Base path for the HTTP application.
pub base: String,
}
impl Default for HttpTriggerConfiguration {
fn default() -> Self {
Self { base: "/".into() }
}
}
/// WebAssembly configuration.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct WasmConfig {
/// Environment variables to be mapped inside the Wasm module at runtime.
pub environment: Option<HashMap<String, String>>,
/// Files to be mapped inside the Wasm module at runtime.
pub files: Option<Vec<String>>,
/// Optional list of HTTP hosts the component is allowed to connect.
pub allowed_http_hosts: Option<Vec<String>>,
}
/// Source for the module.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", untagged)]
pub enum ModuleSource {
/// Local path or parcel reference to a module that needs to be linked.
FileReference(PathBuf),
/// Reference to a remote bindle
Bindle(BindleComponentSource),
/// Local path to a linked module.
/// This variant is manually created by the linker component,
/// and cannot be directly used in configuration outside the linker.
Linked(PathBuf),
}
/// A component source from Bindle.
/// This assumes access to the Bindle server.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct BindleComponentSource {
/// Reference to the bindle (name/version)
pub reference: String,
/// Parcel to use from the bindle.
pub parcel: String,
}
impl Default for ModuleSource {
fn default() -> Self {
// TODO
//
// What does Default mean for a module source?
Self::FileReference(PathBuf::new())
}
}
/// Configuration for the HTTP trigger.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct HttpConfig {
/// HTTP route the component will be invoked for.
pub route: String,
/// The HTTP executor the component requires.
pub executor: Option<HttpExecutor>,
}
impl Default for HttpConfig {
fn default() -> Self {
Self {
route: "/".to_string(),
executor: Default::default(),
}
}
}
/// The type of interface the component implements.
/// TODO
///
/// These should be versioned.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub enum HttpExecutor {
/// The component implements the Spin HTTP interface.
Spin,
/// The component implements the Wagi interface.
Wagi,
}
impl Default for HttpExecutor {
fn default() -> Self {
Self::Spin
}
}
/// Information about building the component.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct BuildConfig {}
/// Trigger configuration.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", untagged)]
pub enum TriggerConfig {
/// HTTP trigger configuration
Http(HttpConfig),
}
impl Default for TriggerConfig {
fn default() -> Self {
Self::Http(Default::default())
}
}
/// Dependency for a component.
/// Each entry this map should correspond to exactly one
/// import module from the Wasm module.
///
/// Currently, this map should either contain an interface that
/// should be satisfied by the host (through a host implementation),
/// or an exact reference (*not* a version range) to a component from the registry.
/// The dependency type.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", tag = "type")]
pub enum Dependency {
/// A host dependency.
Host,
/// A component dependency.
Component(BindleComponentSource),
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
const CFG_TEST: &str = r#"
name = "chain-of-command"
version = "6.11.2"
description = "A simple application that returns the number of lights"
authors = [ "Gul Madred", "Edward Jellico", "JL" ]
trigger = { type = "http", base = "/" }
[[component]]
source = "path/to/wasm/file.wasm"
id = "four-lights"
files = ["file.txt", "subdir/another.txt"]
[component.trigger]
route = "/lights"
executor = "spin"
[component.dependencies]
cache = { type = "host" }
markdown = { type = "component", reference = "github/octo-markdown/1.0.0", parcel = "md.wasm" }
[component.environment]
env1 = "first"
env2 = "second"
[[component]]
id = "abc"
[component.source]
reference = "bindle reference"
parcel = "parcel"
[component.trigger]
route = "/test"
"#;
#[test]
fn test_local_config() -> Result<()> {
let cfg: RawConfiguration<LinkableComponent> = toml::from_str(CFG_TEST)?;
assert_eq!(cfg.info.name, "chain-of-command");
assert_eq!(cfg.info.version, "6.11.2");
assert_eq!(
cfg.info.description,
Some("A simple application that returns the number of lights".to_string())
);
let ApplicationTrigger::Http(http) = cfg.info.trigger;
assert_eq!(http.base, "/".to_string());
assert_eq!(cfg.info.authors.unwrap().len(), 3);
assert_eq!(cfg.components[0].core.id, "four-lights".to_string());
let TriggerConfig::Http(http) = cfg.components[0].core.trigger.clone();
assert_eq!(http.executor.unwrap(), HttpExecutor::Spin);
assert_eq!(http.route, "/lights".to_string());
let test_component = &cfg.components[0];
let test_deps = test_component.dependencies.as_ref().unwrap();
let test_env = test_component.core.wasm.environment.as_ref().unwrap();
let test_files = test_component.core.wasm.files.as_ref().unwrap();
assert_eq!(test_deps.get("cache").unwrap(), &Dependency::Host);
assert_eq!(
test_deps.get("markdown").unwrap(),
&Dependency::Component(BindleComponentSource {
reference: "github/octo-markdown/1.0.0".to_string(),
parcel: "md.wasm".to_string()
})
);
let b = match cfg.components[1].core.source.clone() {
ModuleSource::Bindle(b) => b,
ModuleSource::FileReference(_) => panic!("expected bindle source"),
ModuleSource::Linked(_) => panic!("expected bindle source"),
};
assert_eq!(b.reference, "bindle reference".to_string());
assert_eq!(b.parcel, "parcel".to_string());
assert_eq!(2, test_env.len());
assert_eq!("first", test_env.get("env1").unwrap());
assert_eq!("second", test_env.get("env2").unwrap());
assert_eq!(2, test_files.len());
assert_eq!("file.txt", test_files[0]);
assert_eq!("subdir/another.txt", test_files[1]);
Ok(())
}
}