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

use super::Merge;

/// WASI related configuration.
#[derive(Serialize, Default, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct CapabilityWasiV1 {
    /// CLI arguments.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cli_args: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    // FIXME(issue 933): remove the validation hack - see [`JsonValidationEnvVar`] for details.
    // See https://github.com/wasmerio/edge/issues/933
    #[schemars(with = "Option<Vec<JsonValidationEnvVar>>")]
    pub env_vars: Option<Vec<EnvVarV1>>,
}

impl Merge for CapabilityWasiV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            cli_args: self.cli_args.merge_extend(&other.cli_args),
            env_vars: match (self.env_vars, &other.env_vars) {
                (None, None) => None,
                (Some(e), None) => Some(e),
                (None, Some(e)) => Some(e.clone()),
                (Some(a), Some(b)) => Some(merge_env_vars(a, b.clone())),
            },
        }
    }
}

fn merge_env_vars(mut a: Vec<EnvVarV1>, b: Vec<EnvVarV1>) -> Vec<EnvVarV1> {
    a.extend(b);
    a
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct EnvVarV1 {
    pub name: String,
    #[serde(flatten)]
    pub source: EnvVarSourceV1,
}

impl EnvVarV1 {
    pub fn new_value(name: impl Into<String>, value: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            source: EnvVarSourceV1::Value(value.into()),
        }
    }
}

// Hack that is currently needed to make json validation pass.
// Otherwise the validation library can't deal with the #[serde(flatten)] attribute
// on [`EnvVarV1::source`].
// FIXME(issue 933): remove the validation hack
// See https://github.com/wasmerio/edge/issues/933
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
#[serde(untagged)]
enum JsonValidationEnvVar {
    Value(JsonValidationEnvVarValue),
}

// See [`JsonValidationEnvVar`] for why this is needed.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
struct JsonValidationEnvVarValue {
    name: String,
    value: String,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub enum EnvVarSourceV1 {
    #[serde(rename = "value")]
    Value(String),
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Test that env var (de)serialization uses the expected structure.
    #[test]
    fn test_env_var_deser() {
        let v = EnvVarV1 {
            name: "FOO".to_string(),
            source: EnvVarSourceV1::Value("BAR".to_string()),
        };

        let s = serde_json::to_string(&v).unwrap();
        assert_eq!(s, r#"{"name":"FOO","value":"BAR"}"#);

        let e2: EnvVarV1 = serde_json::from_str(&s).unwrap();
        assert_eq!(e2, v);
    }
}