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
use serde::{Deserialize, Serialize};

use super::{CapabilityMapV1, StringWebcPackageIdent, WebcPackageIdentifierV1};

#[doc(inline)]
pub use wcgi_host::CgiDialect;

/// Spawn a new workload on demand.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadV2 {
    pub source: StringWebcPackageIdent,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum RunnerV2 {
    #[serde(rename = "wasi")]
    Wasi(RunnerWasiV1),
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct RunnerWasiV1 {
    #[serde(default)]
    pub capabilities: CapabilityMapV1,
}

/// Specifies customizations to various kinds of sources.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum SourceSpecV1 {
    #[serde(rename = "webc")]
    Webc(SourceWebcV1),
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct SourceWebcV1 {
    /// Name of the command to use.
    ///
    /// Will default to the declared entrypoiint, or if not found to to the
    /// first declared command.
    pub command: Option<String>,
}

/// Configures a workload to be deployed.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadV1 {
    /// Name for this workload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// How to execute the package.
    pub runner: WorkloadRunnerV1,

    #[serde(default)]
    pub capabilities: CapabilityMapV1,
}

impl WorkloadV1 {
    pub fn webc(&self) -> Option<&WebcPackageIdentifierV1> {
        self.runner.webc()
    }

    pub fn webc_mut(&mut self) -> Option<&mut WebcPackageIdentifierV1> {
        self.runner.webc_mut()
    }
}

/// How run to execute the workload.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum WorkloadRunnerV1 {
    #[serde(rename = "wasm")]
    Wasm(WorkloadRunnerWasmV1),
    #[serde(rename = "webc_command")]
    WebcCommand(WorkloadRunnerWebcCommandV1),
    #[serde(rename = "wcgi")]
    WCgi(RunnerWCgiV1),
    #[serde(rename = "web_proxy")]
    WebProxy(RunnerWebProxyV1),
    #[serde(rename = "tcp_proxy")]
    TcpProxy(RunnerTcpProxyV1),
}

impl WorkloadRunnerV1 {
    /// Get the webc package, if available in the config.
    pub fn webc(&self) -> Option<&WebcPackageIdentifierV1> {
        match self {
            WorkloadRunnerV1::Wasm(w) => w.source.webc(),
            WorkloadRunnerV1::WebcCommand(w) => Some(&w.package),
            WorkloadRunnerV1::WCgi(w) => w.source.webc(),
            WorkloadRunnerV1::WebProxy(p) => p.source.webc(),
            WorkloadRunnerV1::TcpProxy(p) => p.source.webc(),
        }
    }

    /// Get the webc package, if available in the config.
    pub fn webc_mut(&mut self) -> Option<&mut WebcPackageIdentifierV1> {
        match self {
            WorkloadRunnerV1::Wasm(w) => w.source.webc_mut(),
            WorkloadRunnerV1::WebcCommand(w) => Some(&mut w.package),
            WorkloadRunnerV1::WCgi(w) => w.source.webc_mut(),
            WorkloadRunnerV1::WebProxy(p) => p.source.webc_mut(),
            WorkloadRunnerV1::TcpProxy(p) => p.source.webc_mut(),
        }
    }

    pub fn as_web_proxy(&self) -> Option<&RunnerWebProxyV1> {
        if let Self::WebProxy(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_tcp_proxy(&self) -> Option<&RunnerTcpProxyV1> {
        if let Self::TcpProxy(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_wcgi(&self) -> Option<&RunnerWCgiV1> {
        if let Self::WCgi(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_webc_command(&self) -> Option<&WorkloadRunnerWebcCommandV1> {
        if let Self::WebcCommand(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_wasm(&self) -> Option<&WorkloadRunnerWasmV1> {
        if let Self::Wasm(v) = self {
            Some(v)
        } else {
            None
        }
    }
}

/// Run a Webassembly file.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct RunnerWCgiV1 {
    pub source: WorkloadRunnerWasmSourceV1,
    /// The CGI variant to use.
    ///
    /// Will be auto-detected if not specified, and defaults to WCGI if auto
    /// detection fails.
    pub dialect: Option<CgiDialect>,
}

/// Run a Webassembly file via a HTTP connection.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct RunnerWebProxyV1 {
    pub source: WorkloadRunnerWasmSourceV1,
}

/// Run a Webassembly file via a TCP connection.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct RunnerTcpProxyV1 {
    pub source: WorkloadRunnerWasmSourceV1,
}

/// Run a Webassembly file.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadRunnerWasmV1 {
    pub source: WorkloadRunnerWasmSourceV1,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum WorkloadRunnerWasmSourceV1 {
    #[serde(rename = "webc")]
    Webc(WebcSourceV1),
    #[serde(rename = "package")]
    Package(StringWebcPackageIdent),
    #[serde(rename = "fetch")]
    Fetch(WorkloadRunnerWasmSourceFetchV1),
    #[serde(rename = "local_path")]
    LocalPath(WorkloadRunnerWasmSourceLocalPathV1),
}

impl WorkloadRunnerWasmSourceV1 {
    pub fn webc(&self) -> Option<&WebcPackageIdentifierV1> {
        match self {
            Self::Webc(w) => Some(&w.package),
            Self::Fetch(_) => None,
            Self::LocalPath(_) => None,
            Self::Package(p) => Some(&p.0),
        }
    }

    pub fn webc_mut(&mut self) -> Option<&mut WebcPackageIdentifierV1> {
        match self {
            Self::Webc(w) => Some(&mut w.package),
            Self::Fetch(_) => None,
            Self::LocalPath(_) => None,
            Self::Package(p) => Some(&mut p.0),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WebcSourceV1 {
    // TODO: this should also accept a "webc url" once it is standardized/specced.
    #[serde(flatten)]
    pub package: WebcPackageIdentifierV1,

    /// Optional authentication token.
    // TODO: it's awkward to place this in here...
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auth_token: Option<String>,
}

/// Fetch a wasm file from a URL.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadRunnerWasmSourceFetchV1 {
    pub url: url::Url,
}

/// Load a wasm file from the server file system.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadRunnerWasmSourceLocalPathV1 {
    pub path: String,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum WorkloadRunnerWasmFileSourceV1 {
    Webc(WebcSourceV1),
}

// #[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
// pub struct WebcSourceV1 {
//     /// Source identifier or URL for the package.
//     ///
//     /// Eg:
//     /// * Plain name+namespace: namespace/name
//     /// * With version: namespace/name@0.5.1
//     /// * With package hash: namespace/name@x23f2342349b0342342342342342b4
//     /// * With custom registry: https://my-registry.com/namespace/name@1.0.0
//     pub package: WebcPackageIdentifierV1,

//     /// Authentication token to use for package retrieval.
//     pub auth_token: Option<String>,
// }

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct WorkloadRunnerWebcCommandV1 {
    // TODO: this should also accept a "webc url" once it is standardized/specced.
    pub package: WebcPackageIdentifierV1,
    /// The name of the command to run.
    pub command: String,
}