wasmer_deploy_schema/schema/
wasi.rs

1use serde::{Deserialize, Serialize};
2
3use super::Merge;
4
5/// WASI related configuration.
6#[derive(Serialize, Default, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
7pub struct CapabilityWasiV1 {
8    /// CLI arguments.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub cli_args: Option<Vec<String>>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    // FIXME(issue 933): remove the validation hack - see [`JsonValidationEnvVar`] for details.
13    // See https://github.com/wasmerio/edge/issues/933
14    #[schemars(with = "Vec<JsonValidationEnvVar>")]
15    pub env_vars: Option<Vec<EnvVarV1>>,
16}
17
18impl Merge for CapabilityWasiV1 {
19    fn merge_extend(self, other: &Self) -> Self {
20        Self {
21            cli_args: self.cli_args.merge_extend(&other.cli_args),
22            env_vars: match (self.env_vars, &other.env_vars) {
23                (None, None) => None,
24                (Some(e), None) => Some(e),
25                (None, Some(e)) => Some(e.clone()),
26                (Some(a), Some(b)) => Some(merge_env_vars(a, b.clone())),
27            },
28        }
29    }
30}
31
32fn merge_env_vars(mut a: Vec<EnvVarV1>, b: Vec<EnvVarV1>) -> Vec<EnvVarV1> {
33    a.extend(b);
34    a
35}
36
37#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
38pub struct EnvVarV1 {
39    pub name: String,
40    #[serde(flatten)]
41    pub source: EnvVarSourceV1,
42}
43
44impl EnvVarV1 {
45    pub fn new_value(name: impl Into<String>, value: impl Into<String>) -> Self {
46        Self {
47            name: name.into(),
48            source: EnvVarSourceV1::Value(value.into()),
49        }
50    }
51}
52
53// Hack that is currently needed to make json validation pass.
54// Otherwise the validation library can't deal with the #[serde(flatten)] attribute
55// on [`EnvVarV1::source`].
56// FIXME(issue 933): remove the validation hack
57// See https://github.com/wasmerio/edge/issues/933
58#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
59#[serde(untagged)]
60enum JsonValidationEnvVar {
61    Value(JsonValidationEnvVarValue),
62}
63
64// See [`JsonValidationEnvVar`] for why this is needed.
65#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
66struct JsonValidationEnvVarValue {
67    name: String,
68    value: String,
69}
70
71#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
72pub enum EnvVarSourceV1 {
73    #[serde(rename = "value")]
74    Value(String),
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    /// Test that env var (de)serialization uses the expected structure.
82    #[test]
83    fn test_env_var_deser() {
84        let v = EnvVarV1 {
85            name: "FOO".to_string(),
86            source: EnvVarSourceV1::Value("BAR".to_string()),
87        };
88
89        let s = serde_json::to_string(&v).unwrap();
90        assert_eq!(s, r#"{"name":"FOO","value":"BAR"}"#);
91
92        let e2: EnvVarV1 = serde_json::from_str(&s).unwrap();
93        assert_eq!(e2, v);
94    }
95}